Dynamic Client
The dynamic client allows you to interact with any API resource, even those that are not explicitly defined in the static client. This is accomplished by dynamically discovering the API resources at runtime and using the Kubernetes API to interact with them.
Usage
To use the dynamic client, first create an instance of DynamicClient
. This can be done by passing an instance of ApiClient
to the constructor.
from kubernetes import client
from kubernetes.client import configuration
from kubernetes.client.api import core_v1
# Creating a dynamic client
client = dynamic.DynamicClient(
api_client.ApiClient(configuration=config.load_kube_config())
)
Once you have an instance of DynamicClient
, you can use the resources
attribute to access the API resources.
# fetching the service api
api = client.resources.get(api_version="v1", kind="Service")
The get()
method of the resources
attribute takes the API version and kind as arguments. It returns an instance of Resource
, which represents the API resource. You can use the Resource
instance to interact with the API resource.
Examples
Here are some examples of how to use the dynamic client.
Options
The dynamic client provides several options for interacting with API resources.
- Create - You can create new resources using the
create()
method of theResource
instance.
# Creating service `frontend-service` in the `default` namespace
service = api.create(body=service_manifest, namespace="default")
- Get - You can get an existing resource using the
get()
method of theResource
instance.
# Listing service `frontend-service` in the `default` namespace
service_created = api.get(name=name, namespace="default")
- Patch - You can patch an existing resource using the
patch()
method of theResource
instance.
# Patching the `spec` section of the `frontend-service`
service_patched = api.patch(body=service_manifest, name=name, namespace="default")
- Delete - You can delete an existing resource using the
delete()
method of theResource
instance.
# Deleting service `frontend-service` from the `default` namespace
service_deleted = api.delete(name=name, body={}, namespace="default")
Resources
Top-Level Directory Explanations
doc/ - This directory contains documentation files for the project.
doc/source/ - This directory contains the source files for the documentation.
examples/ - This directory contains example usage of the Kubernetes client library.
examples/dynamic-client/ - This directory contains examples of using the dynamic client to interact with Kubernetes.
kubernetes/ - This directory contains the main Kubernetes client library.
kubernetes/base/ - This directory contains the base Kubernetes client library.
kubernetes/base/config/ - This directory contains configuration files for the base library.
kubernetes/base/dynamic/ - This directory contains the dynamic client implementation for the base library.
kubernetes/base/stream/ - This directory contains the stream implementation for the base library.
kubernetes/base/watch/ - This directory contains the watch implementation for the base library.
kubernetes/client/ - This directory contains the top-level client for the Kubernetes client library.
kubernetes/client/api/ - This directory contains the API definitions for the client library.
kubernetes/client/models/ - This directory contains the data models used by the client library.
kubernetes/utils/ - This directory contains utility functions for the Kubernetes client library.
scripts/ - This directory contains scripts used in the development and build process.