MockKubernetesApi

class safir.testing.kubernetes.MockKubernetesApi

Bases: object

Mock Kubernetes API for testing.

This object simulates (with almost everything left out) the BatchV1Api, CoreV1Api, CustomObjectApi, and NetworkingV1Api client objects while keeping simple internal state. It is intended to be used as a mock inside tests.

Methods ending with _for_test are outside of the API and are intended for use by the test suite.

This mock does not enforce namespace creation before creating objects in a namespace. Creating an object in a namespace will implicitly create that namespace if it doesn’t exist. However, it will not store a V1Namespace object or emit events for list_namespace watches. To verify that a namespace was properly created (although not the order of creation), check for the namespace object explicitly with read_namespace or list_namespace.

Objects stored with create_* or replace_* methods are NOT copied. The object provided will be stored, so changing that object will change the object returned by subsequent API calls. Likewise, the object returned by read_* calls will be the same object stored in the mock, and changing it will change the mock’s data. (Sometimes this is the desired behavior, sometimes it isn’t; we had to pick one and this is the approach we picked.)

Not all methods are implemented for each kind, and not all list_* APIs support watches. In general, APIs are only implemented when some other package needs them.

initial_pod_phase

String value to set the status of pods to when created. If this is set to Running (the default), a pod start event will also be generated when the pod is created.

error_callback

If set, called with the method name and any arguments whenever any Kubernetes API method is called and before it takes any acttion. This can be used for fault injection for testing purposes.

Notes

This class is normally not instantiated directly. Instead, call the patch_kubernetes function from a fixture to set up the mock. This is also why it is configurable by setting attributes rather than constructor arguments; the individual test usually doesn’t have control of the constructor.

Methods Summary

create_namespace(body, *[, _request_timeout])

Create a namespace.

create_namespaced_config_map(namespace, body, *)

Create a ConfigMap object.

create_namespaced_custom_object(group, ...)

Create a new custom namespaced object.

create_namespaced_event(namespace, body, *)

Store a new namespaced event.

create_namespaced_ingress(namespace, body, *)

Create an ingress object.

create_namespaced_job(namespace, body, *[, ...])

Create a job object.

create_namespaced_network_policy(namespace, ...)

Create a network policy object.

create_namespaced_persistent_volume_claim(...)

Create a persistent volume claim.

create_namespaced_pod(namespace, body, *[, ...])

Create a pod object.

create_namespaced_resource_quota(namespace, ...)

Create a resource quota object.

create_namespaced_secret(namespace, body, *)

Create a secret object.

create_namespaced_service(namespace, body, *)

Create a service object.

delete_namespace(name, *[, ...])

Delete a namespace.

delete_namespaced_config_map(name, namespace, *)

Delete a ConfigMap object.

delete_namespaced_custom_object(group, ...)

Delete a custom namespaced object.

delete_namespaced_ingress(name, namespace, *)

Delete an ingress object.

delete_namespaced_job(name, namespace, *[, ...])

Delete a job object.

delete_namespaced_persistent_volume_claim(...)

Delete a persistent volume claim object.

delete_namespaced_pod(name, namespace, *[, ...])

Delete a pod object.

delete_namespaced_service(name, namespace, *)

Delete a service object.

get_all_objects_for_test(kind)

Return all objects of a given kind sorted by namespace and name.

get_namespace_objects_for_test(namespace)

Return all objects in the given namespace.

get_namespaced_custom_object(group, version, ...)

Retrieve a namespaced custom object.

list_cluster_custom_object(group, version, ...)

List all custom objects in the cluster.

list_namespace(*[, field_selector, ...])

List namespaces.

list_namespaced_custom_object(group, ...[, ...])

List custom objects in a namespace.

list_namespaced_event(namespace, *[, ...])

List namespaced events.

list_namespaced_ingress(namespace, *[, ...])

List ingress objects in a namespace.

list_namespaced_job(namespace, *[, ...])

List job objects in a namespace.

list_namespaced_persistent_volume_claim(...)

List persistent volume claim objects in a namespace.

list_namespaced_pod(namespace, *[, ...])

List pod objects in a namespace.

list_namespaced_service(namespace, *[, ...])

List service objects in a namespace.

list_node(*[, label_selector, _request_timeout])

List node information.

patch_namespaced_custom_object_status(group, ...)

Patch the status of a namespaced custom object.

patch_namespaced_ingress_status(name, ...[, ...])

Patch the status of an ingress object.

patch_namespaced_pod_status(name, namespace, ...)

Patch the status of a pod object.

patch_namespaced_secret(name, namespace, body, *)

Patch a secret object.

read_namespace(name, *[, _request_timeout])

Return the namespace object for a namespace.

read_namespaced_config_map(name, namespace, *)

Read a config map object.

read_namespaced_ingress(name, namespace, *)

Read a ingress object.

read_namespaced_job(name, namespace, *[, ...])

Read a job object.

read_namespaced_network_policy(name, ...[, ...])

Read a network policy object.

read_namespaced_persistent_volume_claim(...)

Read a persistent volume claim.

read_namespaced_pod(name, namespace, *[, ...])

Read a pod object.

read_namespaced_pod_status(name, namespace, *)

Read the status of a pod.

read_namespaced_resource_quota(name, ...[, ...])

Read a resource quota object.

read_namespaced_secret(name, namespace, *[, ...])

Read a secret.

read_namespaced_service(name, namespace, *)

Read a service.

replace_namespaced_custom_object(group, ...)

Replace a custom namespaced object.

replace_namespaced_secret(name, namespace, ...)

Replace a secret.

set_nodes_for_test(nodes)

Set the node structures that will be returned by list_node.

Methods Documentation

async create_namespace(body, *, _request_timeout=None)

Create a namespace.

The mock doesn’t truly track namespaces since it autocreates them when an object is created in that namespace (maybe that behavior should be optional). However, this method detects conflicts and stores the V1Namespace object so that it can be verified.

Parameters:
  • body (V1Namespace) – Namespace to create.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Raises:

kubernetes_asyncio.client.ApiException – Raised with 409 status if the namespace already exists.

Return type:

None

async create_namespaced_config_map(namespace, body, *, _request_timeout=None)

Create a ConfigMap object.

Parameters:
  • namespace (str) – Namespace in which to store the object.

  • body (V1ConfigMap) – Object to store.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Raises:

kubernetes_asyncio.client.ApiException – Raised with 409 status if the object already exists.

Return type:

None

async create_namespaced_custom_object(group, version, namespace, plural, body, *, _request_timeout=None)

Create a new custom namespaced object.

Parameters:
  • group (str) – API group for this custom object.

  • version (str) – API version for this custom object.

  • namespace (str) – Namespace in which to create the object.

  • plural (str) – API plural for this custom object.

  • body (dict[str, Any]) – Custom object to create.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Raises:

kubernetes_asyncio.client.ApiException – Raised with 409 status if the object already exists.

Return type:

None

async create_namespaced_event(namespace, body, *, _request_timeout=None)

Store a new namespaced event.

This uses the old core event API, not the new Events API.

Parameters:
  • namespace (str) – Namespace of the event.

  • body (CoreV1Event) – New event to store.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Return type:

None

async create_namespaced_ingress(namespace, body, *, _request_timeout=None)

Create an ingress object.

In real life, it usually takes some time for the Ingress controller to set the status.load_balancer.ingress field. It appears that status.load_balancer is created when the Ingress is but is empty, so that’s what we do too.

Use patch_namespaced_ingress_status() to update the field to indicate that the ingress is ready.

Parameters:
  • namespace (str) – Namespace in which to create the object.

  • body (V1Ingress) – Object to create.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Raises:

kubernetes_asyncio.client.ApiException – Raised with 409 status if the object already exists.

Return type:

None

async create_namespaced_job(namespace, body, *, _request_timeout=None)

Create a job object.

A pod corresponding to this job will also be created. The pod will have a label job-name set to the name of the Job object, and will honor the name or generateName metadata from the pod spec in the Job. If neither is set, it will use the job name followed by - as the base for a generated name.

If initial_pod_phase on the mock is set to Running, the status.active field of the job will be set to 1.

Parameters:
  • namespace (str) – Namespace in which to create the object.

  • body (V1Job) – Object to create.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Raises:

kubernetes_asyncio.client.ApiException – Raised with 409 status if the object already exists.

Return type:

None

async create_namespaced_network_policy(namespace, body, *, _request_timeout=None)

Create a network policy object.

Parameters:
  • namespace (str) – Namespace in which to create the object.

  • body (V1NetworkPolicy) – Object to create.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Raises:

kubernetes_asyncio.client.ApiException – Raised with 409 status if the object already exists.

Return type:

None

async create_namespaced_persistent_volume_claim(namespace, body, *, _request_timeout=None)

Create a persistent volume claim.

Parameters:
  • namespace (str) – Namespace in which to create the persistent volume claim.

  • body (V1PersistentVolumeClaim) – Persistent volume claim to create.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Raises:

kubernetes_asyncio.client.ApiException – Raised with 409 status if the persistent volume claim already exists.

Return type:

None

async create_namespaced_pod(namespace, body, *, _request_timeout=None)

Create a pod object.

If initial_pod_phase on the mock Kubernetes object is set to Running, set the state to Running and generate a startup event. Otherwise, the status is set to whatever initial_pod_phase is set to, and no event is generated.

Parameters:
  • namespace (str) – Namespace in which to create the pod.

  • body (V1Pod) – Pod specification.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Raises:

kubernetes_asyncio.client.ApiException – Raised with 409 status if the pod already exists.

Return type:

None

async create_namespaced_resource_quota(namespace, body, *, _request_timeout=None)

Create a resource quota object.

Parameters:
  • namespace (str) – Namespace in which to create the object.

  • body (V1ResourceQuota) – Object to create.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Raises:

kubernetes_asyncio.client.ApiException – Raised with 409 status if the object already exists.

Return type:

None

async create_namespaced_secret(namespace, body, *, _request_timeout=None)

Create a secret object.

Parameters:
  • namespace (str) – Namespace in which to create the object.

  • body (V1Secret) – Object to create.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Raises:

kubernetes_asyncio.client.ApiException – Raised with 409 status if the object already exists.

Return type:

None

async create_namespaced_service(namespace, body, *, _request_timeout=None)

Create a service object.

Parameters:
  • namespace (str) – Namespace in which to create the object.

  • body (V1Service) – Object to create.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Raises:

kubernetes_asyncio.client.ApiException – Raised with 409 status if the object already exists.

Return type:

None

async delete_namespace(name, *, grace_period_seconds=None, propagation_policy='Foreground', body=None, _request_timeout=None)

Delete a namespace.

This also immediately removes all objects in the namespace.

Parameters:
  • name (str) – Namespace to delete.

  • grace_period_seconds (int | None, default: None) – Grace period for object deletion (currently ignored).

  • propagation_policy (str, default: 'Foreground') – Propagation policy for deletion. Has no effect on the mock.

  • body (V1DeleteOptions | None, default: None) – Delete options (currently ignored).

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Raises:

kubernetes_asyncio.client.ApiException – Raised with 404 status if the namespace does not exist.

Return type:

None

async delete_namespaced_config_map(name, namespace, *, grace_period_seconds=None, propagation_policy='Foreground', body=None, _request_timeout=None)

Delete a ConfigMap object.

Parameters:
  • name (str) – Name of object.

  • namespace (str) – Namespace of object.

  • grace_period_seconds (int | None, default: None) – Grace period for object deletion (currently ignored).

  • propagation_policy (str, default: 'Foreground') – Propagation policy for deletion. Has no effect on the mock.

  • body (V1DeleteOptions | None, default: None) – Delete options (currently ignored).

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Success status if object was deleted.

Return type:

kubernetes_asyncio.client.V1Status

Raises:

kubernetes_asyncio.client.ApiException – Raised with 404 status if the object does not exist.

async delete_namespaced_custom_object(group, version, namespace, plural, name, *, grace_period_seconds=None, propagation_policy='Foreground', body=None, _request_timeout=None)

Delete a custom namespaced object.

Parameters:
  • group (str) – API group for this custom object.

  • version (str) – API version for this custom object.

  • namespace (str) – Namespace in which to delete the object.

  • plural (str) – API plural for this custom object.

  • name (str) – Custom object to delete.

  • grace_period_seconds (int | None, default: None) – Grace period for object deletion (currently ignored).

  • propagation_policy (str, default: 'Foreground') – Propagation policy for deletion. Has no effect on the mock.

  • body (V1DeleteOptions | None, default: None) – Delete options (currently ignored).

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Success status if object was deleted.

Return type:

kubernetes_asyncio.client.V1Status

Raises:

kubernetes_asyncio.client.ApiException – Raised with 409 status if the object already exists.

async delete_namespaced_ingress(name, namespace, *, grace_period_seconds=None, propagation_policy='Foreground', body=None, _request_timeout=None)

Delete an ingress object.

Parameters:
  • name (str) – Name of ingress to delete.

  • namespace (str) – Namespace of ingress to delete.

  • grace_period_seconds (int | None, default: None) – Grace period for object deletion (currently ignored).

  • propagation_policy (str, default: 'Foreground') – Propagation policy for deletion. Has no effect on the mock.

  • body (V1DeleteOptions | None, default: None) – Delete options (currently ignored).

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Success status.

Return type:

kubernetes_asyncio.client.V1Status

Raises:

kubernetes_asyncio.client.ApiException – Raised with 404 status if the ingress was not found.

async delete_namespaced_job(name, namespace, *, grace_period_seconds=None, propagation_policy='Foreground', body=None, _request_timeout=None)

Delete a job object.

Will also propagate to pods.

Parameters:
  • name (str) – Name of job to delete.

  • namespace (str) – Namespace of job to delete.

  • grace_period_seconds (int | None, default: None) – Grace period for object deletion (currently ignored).

  • propagation_policy (str, default: 'Foreground') – Propagation policy for deletion. Has no effect on the mock.

  • body (V1DeleteOptions | None, default: None) – Delete options (currently ignored).

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Success status.

Return type:

kubernetes_asyncio.client.V1Status

Raises:

kubernetes_asyncio.client.ApiException – Raised with 404 status if the job was not found.

async delete_namespaced_persistent_volume_claim(name, namespace, *, grace_period_seconds=None, propagation_policy='Foreground', body=None, _request_timeout=None)

Delete a persistent volume claim object.

Parameters:
  • name (str) – Name of persistent volume claim to delete.

  • namespace (str) – Namespace of persistent volume claim to delete.

  • grace_period_seconds (int | None, default: None) – Grace period for object deletion (currently ignored).

  • propagation_policy (str, default: 'Foreground') – Propagation policy for deletion. Has no effect on the mock.

  • body (V1DeleteOptions | None, default: None) – Delete options (currently ignored).

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Success status.

Return type:

kubernetes_asyncio.client.V1Status

Raises:

kubernetes_asyncio.client.ApiException – Raised with 404 status if the ingress was not found.

async delete_namespaced_pod(name, namespace, *, grace_period_seconds=None, propagation_policy='Foreground', body=None, _request_timeout=None)

Delete a pod object.

Parameters:
  • name (str) – Name of pod to delete.

  • namespace (str) – Namespace of pod to delete.

  • grace_period_seconds (int | None, default: None) – Grace period for object deletion (currently ignored).

  • propagation_policy (str, default: 'Foreground') – Propagation policy for deletion. Has no effect on the mock.

  • body (V1DeleteOptions | None, default: None) – Delete options (currently ignored).

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Success status.

Return type:

kubernetes_asyncio.client.V1Status

Raises:

kubernetes_asyncio.client.ApiException – Raised with 404 status if the pod was not found.

async delete_namespaced_service(name, namespace, *, grace_period_seconds=None, propagation_policy='Foreground', body=None, _request_timeout=None)

Delete a service object.

Parameters:
  • name (str) – Name of service to delete.

  • namespace (str) – Namespace of service to delete.

  • grace_period_seconds (int | None, default: None) – Grace period for object deletion (currently ignored).

  • propagation_policy (str, default: 'Foreground') – Propagation policy for deletion. Has no effect on the mock.

  • body (V1DeleteOptions | None, default: None) – Delete options (currently ignored).

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Success status.

Return type:

kubernetes_asyncio.client.V1Status

Raises:

kubernetes_asyncio.client.ApiException – Raised with 404 status if the service was not found.

get_all_objects_for_test(kind)

Return all objects of a given kind sorted by namespace and name.

Parameters:

kind (str) – The Kubernetes kind, such as Secret or Pod. This is case-sensitive.

Returns:

All objects of that kind found in the mock, sorted by namespace and then name.

Return type:

list of Any

get_namespace_objects_for_test(namespace)

Return all objects in the given namespace.

Parameters:

namespace (str) – Name of the namespace.

Returns:

All objects found in that namespace, sorted by kind and then name. Due to how objects are stored in the mock, we can’t distinguish between a missing namespace and a namespace with no objects. In both cases, the empty list is returned.

Return type:

list of Any

async get_namespaced_custom_object(group, version, namespace, plural, name, *, _request_timeout=None)

Retrieve a namespaced custom object.

Parameters:
  • group (str) – API group for this custom object.

  • version (str) – API version for this custom object.

  • namespace (str) – Namespace in which to create the object.

  • plural (str) – API plural for this custom object.

  • name (str) – Name of the object to retrieve.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Body of the custom object.

Return type:

dict of Any

Raises:

kubernetes_asyncio.client.ApiException – Raised with 404 status if the object does not exist.

async list_cluster_custom_object(group, version, plural, *, _request_timeout=None)

List all custom objects in the cluster.

Parameters:
  • group (str) – API group for this custom object.

  • version (str) – API version for this custom object.

  • plural (str) – API plural for this custom object.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Dictionary with one key, items, whose value is a list of all the specified custom objects stored in the cluster.

Return type:

dict

async list_namespace(*, field_selector=None, label_selector=None, resource_version=None, timeout_seconds=None, watch=False, _preload_content=True, _request_timeout=None)

List namespaces.

This does support watches. Only namespaces that are explicitly created with create_namespace will be shown, not ones that were implicitly created by creating some other object.

Parameters:
  • namespace – Namespace of jobs to list.

  • field_selector (str | None, default: None) – Only metadata.name=... is supported. It is parsed to find the namespace name and only the namespace matching that name will be returned.

  • label_selector (str | None, default: None) – Which objects to retrieve. All labels must match.

  • resource_version (str | None, default: None) – Where to start in the event stream when performing a watch. If None, starts with the next change.

  • timeout_seconds (int | None, default: None) – How long to return events for before exiting when performing a watch.

  • watch (bool, default: False) – Whether to act as a watch.

  • _preload_content (bool, default: True) – Verified to be False when performing a watch.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

List of namespaces, when not called as a watch. If called as a watch, returns a mock aiohttp.Response with a readline metehod that yields the events.

Return type:

kubernetes_asyncio.client.V1JobList or unittest.mock.Mock

Raises:
  • AssertionError – Some other field_selector was provided.

  • kubernetes_asyncio.client.ApiException – Raised with 404 status if the namespace does not exist.

async list_namespaced_custom_object(group, version, namespace, plural, *, field_selector=None, label_selector=None, resource_version=None, timeout_seconds=None, watch=False, _preload_content=True, _request_timeout=None)

List custom objects in a namespace.

This does support watches.

Parameters:
  • group (str) – API group for this custom object.

  • version (str) – API version for this custom object.

  • namespace (str) – Namespace of custom object to list.

  • plural (str) – API plural for this custom object.

  • field_selector (str | None, default: None) – Only metadata.name=... is supported. It is parsed to find the custom object name and only custom objects matching that name will be returned.

  • label_selector (str | None, default: None) – Which objects to retrieve. All labels must match.

  • resource_version (str | None, default: None) – Where to start in the event stream when performing a watch. If None, starts with the next change.

  • timeout_seconds (int | None, default: None) – How long to return events for before exiting when performing a watch.

  • watch (bool, default: False) – Whether to act as a watch.

  • _preload_content (bool, default: True) – Verified to be False when performing a watch.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

List of custom objects in that namespace as a dict with one key, items, when not called as a watch. If called as a watch, returns a mock aiohttp.Response with a readline metehod that yields the events.

Return type:

dict or unittest.mock.Mock

Raises:
  • AssertionError – Some other field_selector was provided.

  • kubernetes_asyncio.client.ApiException – Raised with 404 status if the namespace does not exist.

async list_namespaced_event(namespace, *, field_selector=None, resource_version=None, timeout_seconds=None, watch=False, _preload_content=True, _request_timeout=None)

List namespaced events.

This uses the old core event API, not the new Events API. It does support watches.

Parameters:
  • namespace (str) – Namespace to watch for events.

  • field_selector (str | None, default: None) – Which events to retrieve when performing a watch. Currently, this is ignored.

  • resource_version (str | None, default: None) – Where to start in the event stream when performing a watch.

  • timeout_seconds (int | None, default: None) – How long to return events for before exiting when performing a watch.

  • watch (bool, default: False) – Whether to act as a watch.

  • _preload_content (bool, default: True) – Verified to be False when performing a watch.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

List of events, when not called as a watch. If called as a watch, returns a mock aiohttp.Response with a readline method that yields the events.

Return type:

kubernetes_asyncio.client.CoreV1EventList or unittest.mock.Mock

async list_namespaced_ingress(namespace, *, field_selector=None, label_selector=None, resource_version=None, timeout_seconds=None, watch=False, _preload_content=True, _request_timeout=None)

List ingress objects in a namespace.

This does support watches.

Parameters:
  • namespace (str) – Namespace of ingresses to list.

  • field_selector (str | None, default: None) – Only metadata.name=... is supported. It is parsed to find the ingress name and only ingresses matching that name will be returned.

  • label_selector (str | None, default: None) – Which objects to retrieve. All labels must match.

  • resource_version (str | None, default: None) – Where to start in the event stream when performing a watch. If None, starts with the next change.

  • timeout_seconds (int | None, default: None) – How long to return events for before exiting when performing a watch.

  • watch (bool, default: False) – Whether to act as a watch.

  • _preload_content (bool, default: True) – Verified to be False when performing a watch.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

List of ingresses in that namespace, when not called as a watch. If called as a watch, returns a mock aiohttp.Response with a readline metehod that yields the events.

Return type:

kubernetes_asyncio.client.V1IngressList or unittest.mock.Mock

Raises:
  • AssertionError – Some other field_selector was provided.

  • kubernetes_asyncio.client.ApiException – Raised with 404 status if the namespace does not exist.

async list_namespaced_job(namespace, *, field_selector=None, label_selector=None, resource_version=None, timeout_seconds=None, watch=False, _preload_content=True, _request_timeout=None)

List job objects in a namespace.

This does support watches.

Parameters:
  • namespace (str) – Namespace of jobs to list.

  • field_selector (str | None, default: None) – Only metadata.name=... is supported. It is parsed to find the job name and only jobs matching that name will be returned.

  • label_selector (str | None, default: None) – Which objects to retrieve. All labels must match.

  • resource_version (str | None, default: None) – Where to start in the event stream when performing a watch. If None, starts with the next change.

  • timeout_seconds (int | None, default: None) – How long to return events for before exiting when performing a watch.

  • watch (bool, default: False) – Whether to act as a watch.

  • _preload_content (bool, default: True) – Verified to be False when performing a watch.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

List of jobs in that namespace, when not called as a watch. If called as a watch, returns a mock aiohttp.Response with a readline metehod that yields the events.

Return type:

kubernetes_asyncio.client.V1JobList or unittest.mock.Mock

Raises:
  • AssertionError – Some other field_selector was provided.

  • kubernetes_asyncio.client.ApiException – Raised with 404 status if the namespace does not exist.

async list_namespaced_persistent_volume_claim(namespace, *, field_selector=None, label_selector=None, resource_version=None, timeout_seconds=None, watch=False, _preload_content=True, _request_timeout=None)

List persistent volume claim objects in a namespace.

This does support watches.

Parameters:
  • namespace (str) – Namespace of persistent volume claim to list.

  • field_selector (str | None, default: None) – Only metadata.name=... is supported. It is parsed to find the persistent volume claim name and only persistent volume claims matching that name will be returned.

  • label_selector (str | None, default: None) – Which objects to retrieve. All labels must match.

  • resource_version (str | None, default: None) – Where to start in the event stream when performing a watch. If None, starts with the next change.

  • timeout_seconds (int | None, default: None) – How long to return events for before exiting when performing a watch.

  • watch (bool, default: False) – Whether to act as a watch.

  • _preload_content (bool, default: True) – Verified to be False when performing a watch.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

List of persistent volume claims in that namespace, when not called as a watch. If called as a watch, returns a mock aiohttp.Response with a readline metehod that yields the events.

Return type:

kubernetes_asyncio.client.V1PersistentVolumeClaimList

Raises:
  • AssertionError – Some other field_selector was provided.

  • kubernetes_asyncio.client.ApiException – Raised with 404 status if the namespace does not exist.

async list_namespaced_pod(namespace, *, field_selector=None, label_selector=None, resource_version=None, timeout_seconds=None, watch=False, _preload_content=True, _request_timeout=None)

List pod objects in a namespace.

This does support watches.

Parameters:
  • namespace (str) – Namespace of pods to list.

  • field_selector (str | None, default: None) – Only metadata.name=... is supported. It is parsed to find the pod name and only pods matching that name will be returned.

  • label_selector (str | None, default: None) – Which objects to retrieve. All labels must match.

  • resource_version (str | None, default: None) – Where to start in the event stream when performing a watch. If None, starts with the next change.

  • timeout_seconds (int | None, default: None) – How long to return events for before exiting when performing a watch.

  • watch (bool, default: False) – Whether to act as a watch.

  • _preload_content (bool, default: True) – Verified to be False when performing a watch.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

List of pods in that namespace, when not called as a watch. If called as a watch, returns a mock aiohttp.Response with a readline metehod that yields the events.

Return type:

kubernetes_asyncio.client.V1PodList or unittest.mock.Mock

Raises:
  • AssertionError – Some other field_selector was provided.

  • kubernetes_asyncio.client.ApiException – Raised with 404 status if the namespace does not exist.

async list_namespaced_service(namespace, *, field_selector=None, label_selector=None, resource_version=None, timeout_seconds=None, watch=False, _preload_content=True, _request_timeout=None)

List service objects in a namespace.

This does support watches.

Parameters:
  • namespace (str) – Namespace of services to list.

  • field_selector (str | None, default: None) – Only metadata.name=... is supported. It is parsed to find the service name and only services matching that name will be returned.

  • label_selector (str | None, default: None) – Which objects to retrieve. All labels must match.

  • resource_version (str | None, default: None) – Where to start in the event stream when performing a watch. If None, starts with the next change.

  • timeout_seconds (int | None, default: None) – How long to return events for before exiting when performing a watch.

  • watch (bool, default: False) – Whether to act as a watch.

  • _preload_content (bool, default: True) – Verified to be False when performing a watch.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

List of services in that namespace, when not called as a watch. If called as a watch, returns a mock aiohttp.Response with a readline metehod that yields the events.

Return type:

kubernetes_asyncio.client.V1ServiceList or unittest.mock.Mock

Raises:
  • AssertionError – Some other field_selector was provided.

  • kubernetes_asyncio.client.ApiException – Raised with 404 status if the namespace does not exist.

async list_node(*, label_selector=None, _request_timeout=None)

List node information.

Parameters:
  • label_selector (str | None, default: None) – Which objects to retrieve. All labels must match.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

The node information previouslyl stored with set_nodes_for_test, if any.

Return type:

kubernetes_asyncio.client.V1NodeList

async patch_namespaced_custom_object_status(group, version, namespace, plural, name, body, *, _request_timeout=None)

Patch the status of a namespaced custom object.

Parameters:
  • group (str) – API group for this custom object.

  • version (str) – API version for this custom object.

  • namespace (str) – Namespace in which to create the object.

  • plural (str) – API plural for this custom object.

  • name (str) – Name of the object to retrieve.

  • body (list[dict[str, Any]]) – Body of the patch. The only patch supported is one with op of replace and path of /status.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Modified body of the custom object.

Return type:

dict of Any

Raises:
  • kubernetes_asyncio.client.ApiException – Raised with 404 status if the object does not exist.

  • AssertionError – Raised if any other type of patch is provided.

async patch_namespaced_ingress_status(name, namespace, body, *, _request_timeout=None)

Patch the status of an ingress object.

Parameters:
  • name (str) – Name of ingress object.

  • namespace (str) – Namespace of ingress object.

  • body (list[dict[str, Any]]) – Patches to apply. Only patches with op of replace are supported, and only with path of /status/loadBalancer/ingress.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Corresponding object.

Return type:

kubernetes_asyncio.client.V1Ingress

Raises:
  • kubernetes_asyncio.client.ApiException – Raised with 404 status if the ingress does not exist.

  • AssertionError – Raised if any other type of patch was provided.

async patch_namespaced_pod_status(name, namespace, body, *, _request_timeout=None)

Patch the status of a pod object.

Parameters:
  • name (str) – Name of pod object.

  • namespace (str) – Namespace of secret object.

  • body (list[dict[str, Any]]) – Patches to apply. Only patches with op of replace are supported, and only with path of /status/phase.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Patched pod object.

Return type:

kubernetes_asyncio.client.V1Pod

Raises:
  • kubernetes_asyncio.client.ApiException – Raised with 404 status if the secret does not exist.

  • AssertionError – Raised if any other type of patch was provided.

async patch_namespaced_secret(name, namespace, body, *, _request_timeout=None)

Patch a secret object.

Parameters:
  • name (str) – Name of secret object.

  • namespace (str) – Namespace of secret object.

  • body (list[dict[str, Any]]) – Patches to apply. Only patches with op of replace are supported, and only with path of either /metadata/annotations or /metadata/labels.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Patched secret object.

Return type:

kubernetes_asyncio.client.V1Secret

Raises:
  • kubernetes_asyncio.client.ApiException – Raised with 404 status if the secret does not exist.

  • AssertionError – Raised if any other type of patch was provided.

async read_namespace(name, *, _request_timeout=None)

Return the namespace object for a namespace.

This will only return a namespace object if one was created via create_namespace, not if one was implicitly added by creating some other object.

Parameters:
  • name (str) – Name of namespace to retrieve.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Corresponding namespace object. If create_namespace has not been called, but the namespace was added implicitly, an exception will be raised.

Return type:

kubernetes_asyncio.client.V1Namespace

Raises:

kubernetes_asyncio.client.ApiException – Raised with 404 status if the namespace does not exist.

async read_namespaced_config_map(name, namespace, *, _request_timeout=None)

Read a config map object.

Parameters:
  • name (str) – Name of object.

  • namespace (str) – Namespace of object.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Corresponding object.

Return type:

kubernetes_asyncio.client.V1ConfigMap

Raises:

kubernetes_asyncio.client.ApiException – Raised with 404 status if the config map was not found.

async read_namespaced_ingress(name, namespace, *, _request_timeout=None)

Read a ingress object.

Parameters:
  • name (str) – Name of the ingress.

  • namespace (str) – Namespace of the ingress.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Ingress object.

Return type:

kubernetes_asyncio.client.V1Ingress

Raises:

kubernetes_asyncio.client.ApiException – Raised with 404 status if the ingress was not found.

async read_namespaced_job(name, namespace, *, _request_timeout=None)

Read a job object.

Parameters:
  • name (str) – Name of the job.

  • namespace (str) – Namespace of the job.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Job object.

Return type:

kubernetes_asyncio.client.V1Job

Raises:

kubernetes_asyncio.client.ApiException – Raised with 404 status if the job was not found.

async read_namespaced_network_policy(name, namespace, *, _request_timeout=None)

Read a network policy object.

Parameters:
  • name (str) – Name of the network policy.

  • namespace (str) – Namespace of the network policy.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Network policy object.

Return type:

kubernetes_asyncio.client.V1NetworkPolicy

Raises:

kubernetes_asyncio.client.ApiException – Raised with 404 status if the network policy was not found.

async read_namespaced_persistent_volume_claim(name, namespace, *, _request_timeout=None)

Read a persistent volume claim.

Parameters:
  • name (str) – Name of the persistent volume claim.

  • namespace (str) – Namespace of the persistent volume claim.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Persistent volume claim object.

Return type:

kubernetes_asyncio.client.V1PersistentVolumeClaim

Raises:

kubernetes_asyncio.client.ApiException – Raised with 404 status if the persistent volume claim was not found.

async read_namespaced_pod(name, namespace, *, _request_timeout=None)

Read a pod object.

Parameters:
  • name (str) – Name of the pod.

  • namespace (str) – Namespace of the pod.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Pod object.

Return type:

kubernetes_asyncio.client.V1Pod

Raises:

kubernetes_asyncio.client.ApiException – Raised with 404 status if the pod was not found.

async read_namespaced_pod_status(name, namespace, *, _request_timeout=None)

Read the status of a pod.

Parameters:
  • name (str) – Name of the pod.

  • namespace (str) – Namespace of the pod.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Pod object. The Kubernetes API returns a V1Pod rather than, as expected, a V1PodStatus. Presumably the acutal API populates only the status portion, but we return the whole pod for testing purposes since it shouldn’t matter.

Return type:

kubernetes_asyncio.client.V1Pod

Raises:

kubernetes_asyncio.client.ApiException – Raised with 404 status if the pod was not found.

async read_namespaced_resource_quota(name, namespace, *, _request_timeout=None)

Read a resource quota object.

Parameters:
  • name (str) – Name of object.

  • namespace (str) – Namespace of object.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Corresponding object.

Return type:

kubernetes_asyncio.client.V1ResourceQuota

Raises:

kubernetes_asyncio.client.ApiException – Raised with 404 status if the resource quota was not found.

async read_namespaced_secret(name, namespace, *, _request_timeout=None)

Read a secret.

Parameters:
  • name (str) – Name of secret.

  • namespace (str) – Namespace of secret.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Requested secret.

Return type:

kubernetes_asyncio.client.V1Secret

Raises:

kubernetes_asyncio.client.ApiException – Raised with 404 status if the secret does not exist.

async read_namespaced_service(name, namespace, *, _request_timeout=None)

Read a service.

Parameters:
  • name (str) – Name of service.

  • namespace (str) – Namespace of service.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Returns:

Requested service.

Return type:

kubernetes_asyncio.client.V1Service

Raises:

kubernetes_asyncio.client.ApiException – Raised with 404 status if the service does not exist.

async replace_namespaced_custom_object(group, version, namespace, plural, name, body, *, _request_timeout=None)

Replace a custom namespaced object.

Parameters:
  • group (str) – API group for this custom object.

  • version (str) – API version for this custom object.

  • namespace (str) – Namespace in which to create the object.

  • plural (str) – API plural for this custom object.

  • body (dict[str, Any]) – New contents of custom object.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

  • name (str)

Raises:

kubernetes_asyncio.client.ApiException – Raised with 404 if the object does not exist.

Return type:

None

async replace_namespaced_secret(name, namespace, body, *, _request_timeout=None)

Replace a secret.

Parameters:
  • name (str) – Name of secret.

  • namespace (str) – Namespace of secret.

  • body (V1Secret) – New body of secret.

  • _request_timeout (float | None, default: None) – Ignored, accepted for compatibility with the Kubernetes API.

Raises:

kubernetes_asyncio.client.ApiException – Raised with 404 status if the secret does not exist.

Return type:

None

set_nodes_for_test(nodes)

Set the node structures that will be returned by list_node.

Parameters:

nodes (list[V1Node]) – New node list to return.

Return type:

None