- .github
- devel
- doc
-
examples
-
dynamic-client
-
notebooks
-
watch
-
yaml_dir
- README.md
- __init__.py
- annotate_deployment.py
- api_discovery.py
- apply_from_dict.py
- apply_from_directory.py
- apply_from_single_file.py
- cluster_scoped_custom_object.py
- cronjob_crud.py
- deployment_create.py
- deployment_crud.py
- duration-gep2257.py
- in_cluster_config.py
- ingress_create.py
- job_crud.py
- multiple_clusters.py
- namespaced_custom_object.py
- node_labels.py
- out_of_cluster_config.py
- pick_kube_config_context.py
- pod_config_list.py
- pod_exec.py
- pod_portforward.py
- remote_cluster.py
- rollout-daemonset.py
- rollout-statefulset.py
-
dynamic-client
- kubernetes
- scripts
- .gitignore
- CHANGELOG.md
- CONTRIBUTING.md
- LICENSE
- MANIFEST.in
- OWNERS
- README.md
- SECURITY_CONTACTS
- code-of-conduct.md
- codecov.yml
- requirements.txt
- setup.cfg
- setup.py
- test-requirements.txt
- tox.ini
Explanation
The code demonstrates how to interact with Kubernetes resources using the Python Kubernetes client’s dynamic client. It covers creating, listing, patching, and deleting a service resource.
Initialization
- The code starts by creating a dynamic client using the
dynamic.DynamicClient
class, passing in a configuredApiClient
object loaded from the kube-config file. This allows for flexible interaction with different Kubernetes API versions and resource types.
Service Creation
It fetches the service API using
client.resources.get(api_version="v1", kind="Service")
, which provides access to service-related methods.A service manifest (
service_manifest
) is defined, containing the necessary metadata and spec fields to describe the service.The service is then created using
api.create(body=service_manifest, namespace="default")
, placing it in the default namespace.
Service Listing
- The created service is retrieved using
api.get(name=name, namespace="default")
, which retrieves the service object based on its name and namespace.
Service Patching (Updating)
The
service_manifest
is modified to change the ports defined in the spec.The updated service is patched using
api.patch(body=service_manifest, name=name, namespace="default")
. This applies the changes to the existing service.
Service Deletion
- The service is deleted using
api.delete(name=name, body={}, namespace="default")
. This removes the service from the cluster.
Summary
This code provides a basic example of utilizing the dynamic client to manage Kubernetes services. It demonstrates CRUD operations, allowing developers to interact with the Kubernetes API programmatically. The dynamic client’s flexibility enables interaction with various Kubernetes resources, making it a powerful tool for automating Kubernetes tasks.
Graph
The graph shows the usage of functions within the codebase.
Select a code symbol to view it's graph