- .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
This code demonstrates how to use the Kubernetes Python client to make API calls with a custom “Accept” header.
Breakdown:
- Import Libraries:
kubernetes.config
: Used to load Kubernetes configuration from a kube-config file.kubernetes.dynamic
: Provides a dynamic client that allows interaction with the Kubernetes API based on resource types.kubernetes.client.api_client
: Provides a low-level API client to interact with the Kubernetes API.
- Load Kubernetes Configuration:
config.load_kube_config()
: Reads authentication and cluster details from the kube-config file.
- Create Dynamic Client:
dynamic.DynamicClient()
: Instantiates a dynamic client using the loaded configuration.
- Fetch API Resource:
client.resources.get(api_version="v1", kind="Node")
: Retrieves the API resource representing Nodes in the cluster.
- Set Custom Header:
params = {'header_params': {'Accept': 'application/json;as=PartialObjectMetadataList;v=v1;g=meta.k8s.io'}}
: Creates a dictionaryparams
containing the “Accept” header with the desired value.Accept
: Defines the desired data format and metadata for the response. In this case, the application wants “PartialObjectMetadataList” (a subset of Node details) in JSON format with API version ‘v1’ from themeta.k8s.io
group.
- Make API Call:
api.get(**params)
: Executes a GET request to the Node API, passing the custom header parameters.
- Print Response Data:
print("%s\t\t\t%s" %("VERSION", "KIND"))
: Prints the column headers for API version and kind.print("%s\t\t%s" %(resp.apiVersion, resp.kind))
: Prints the API version and kind of the response received.
Key Points:
- Dynamic Client: The dynamic client provides flexibility by allowing interactions with various API resources without hardcoding the resource details.
- Custom Headers: Custom headers allow specifying specific preferences for the response format, content, and metadata.
- Kubernetes API Structure: Kubernetes resources are organized by API version, kind (type), and group (namespace).
Example Use Case:
This code demonstrates how to retrieve a subset of Node data (PartialObjectMetadataList) with specific versioning information. This could be useful when only certain node details are needed, improving efficiency and reducing data transfer.
Graph
The graph shows the usage of functions within the codebase.
Select a code symbol to view it's graph