Kubernetes

Provides a wrapper for querying Kubernetes cluster resources.

kubernetes(namespace='default')

If namespace is None then all namespaces will be queried. This however will increase the number of calls to Kubernetes API server.

Note

  • Kubernetes wrapper will authenticate using service account, which assumes the worker is running in a Kubernetes cluster.
  • All Kubernetes wrapper calls are scoped to the Kubernetes cluster hosting the worker. It is not intended to be used in querying multiple clusters.

Label Selectors

Kubernetes API provides a way to filter resources using labelSelector. Kubernetes wrapper provides a friendly syntax for filtering.

The following examples show different usage of the Kubernetes wrapper utilizing label filtering:

# Get all pods with label ``application`` equal to ``zmon-worker``
kubernetes().pods(application='zmon-worker')
kubernetes().pods(application__eq='zmon-worker')


# Get all pods with label ``application`` **not equal to** ``zmon-worker``
kubernetes().pods(application__neq='zmon-worker')


# Get all pods with label ``application`` **any of** ``zmon-worker`` or ``zmon-agent``
kubernetes().pods(application__in=['zmon-worker', 'zmon-agent'])

# Get all pods with label ``application`` **not any of** ``zmon-worker`` or ``zmon-agent``
kubernetes().pods(application__notin=['zmon-worker', 'zmon-agent'])

Methods of Kubernetes

pods(name=None, phase=None, ready=None, **kwargs)

Return list of Pods.

Parameters:
  • name (str) – Pod name.
  • phase (str) – Pod status phase. Valid values are: Pending, Running, Failed, Succeeded or Unknown.
  • ready (bool) – Pod readiness status. If None then all pods are returned.
  • kwargs (dict) – Pod Label Selectors filters.
Returns:

List of pods. Typical pod has “metadata”, “status” and “spec” fields.

Return type:

list

nodes(name=None, **kwargs)

Return list of Nodes. Namespace does not apply.

Parameters:
Returns:

List of nodes. Typical pod has “metadata”, “status” and “spec” fields.

Return type:

list

services(name=None, **kwargs)

Return list of Services.

Parameters:
Returns:

List of services. Typical service has “metadata”, “status” and “spec” fields.

Return type:

list

endpoints(name=None, **kwargs)

Return list of Endpoints.

Parameters:
Returns:

List of Endpoints. Typical Endpoint has “metadata”, and “subsets” fields.

Return type:

list

ingresses(name=None, **kwargs)

Return list of Ingresses.

Parameters:
Returns:

List of Ingresses. Typical Ingress has “metadata”, “spec” and “status” fields.

Return type:

list

statefulsets(name=None, replicas=None, **kwargs)

Return list of Statefulsets.

Parameters:
  • name (str) – Statefulset name.
  • replicas (int) – Statefulset replicas.
  • kwargs (dict) – Statefulset Label Selectors filters.
Returns:

List of Statefulsets. Typical Statefulset has “metadata”, “status” and “spec” fields.

Return type:

list

daemonsets(name=None, **kwargs)

Return list of Daemonsets.

Parameters:
Returns:

List of Daemonsets. Typical Daemonset has “metadata”, “status” and “spec” fields.

Return type:

list

replicasets(name=None, replicas=None, **kwargs)

Return list of ReplicaSets.

Parameters:
  • name (str) – ReplicaSet name.
  • replicas (int) – ReplicaSet replicas.
  • kwargs (dict) – ReplicaSet Label Selectors filters.
Returns:

List of ReplicaSets. Typical ReplicaSet has “metadata”, “status” and “spec” fields.

Return type:

list

deployments(name=None, replicas=None, ready=None, **kwargs)

Return list of Deployments.

Parameters:
  • name (str) – Deployment name.
  • replicas (int) – Deployment replicas.
  • ready (bool) – Deployment readiness status.
  • kwargs (dict) – Deployment Label Selectors filters.
Returns:

List of Deployments. Typical Deployment has “metadata”, “status” and “spec” fields.

Return type:

list

configmaps(name=None, **kwargs)

Return list of ConfigMaps.

Parameters:
Returns:

List of ConfigMaps. Typical ConfigMap has “metadata” and “data”.

Return type:

list

persistentvolumeclaims(name=None, phase=None, **kwargs)

Return list of PersistentVolumeClaims.

Parameters:
  • name (str) – PersistentVolumeClaim name.
  • phase (str) – Volume phase.
  • kwargs (dict) – PersistentVolumeClaim Label Selectors filters.
Returns:

List of PersistentVolumeClaims. Typical PersistentVolumeClaim has “metadata”, “status” and “spec” fields.

Return type:

list

persistentvolumes(name=None, phase=None, **kwargs)

Return list of PersistentVolumes.

Parameters:
  • name (str) – PersistentVolume name.
  • phase (str) – Volume phase.
  • kwargs (dict) – PersistentVolume Label Selectors filters.
Returns:

List of PersistentVolumes. Typical PersistentVolume has “metadata”, “status” and “spec” fields.

Return type:

list

jobs(name=None, **kwargs)

Return list of Jobs.

Parameters:
  • name (str) – Job name.
  • **kwargs

    Job labelSelector filters.

Returns:

List of Jobs. Typical Job has “metadata”, “status” and “spec”.

Return type:

list

cronjobs(name=None, **kwargs)

Return list of CronJobs.

Parameters:
  • name (str) – CronJob name.
  • **kwargs

    CronJob labelSelector filters.

Returns:

List of CronJobs. Typical CronJob has “metadata”, “status” and “spec”.

Return type:

list

metrics()

Return API server metrics in prometheus format.

Returns:Cluster metrics.
Return type:dict