- .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 provides a set of functions for creating Kubernetes resources from YAML files or dictionaries. It leverages the kubernetes
Python client library.
Key Functions:
create_from_directory(k8s_client, yaml_dir, verbose=False, namespace="default", apply=False, **kwargs)
:
- Takes a directory path (
yaml_dir
) containing YAML files defining Kubernetes resources. - Iterates over all YAML files in the directory, attempting to create each resource.
- Optionally applies server-side apply (
apply=True
) to create resources. - Handles errors by collecting
ApiException
instances for failed resources and raising aFailToCreateError
exception.
create_from_yaml(k8s_client, yaml_file=None, yaml_objects=None, verbose=False, namespace="default", apply=False, **kwargs)
:
- Takes either a YAML file path (
yaml_file
) or a list of YAML objects (yaml_objects
). - Loads the YAML content and creates resources from it.
- Similar to
create_from_directory
, it supports server-side apply and error handling.
create_from_dict(k8s_client, data, verbose=False, namespace="default", apply=False, **kwargs)
:
- Takes a dictionary (
data
) representing a Kubernetes resource. - Detects if the dictionary represents a single object or a list of objects.
- Iterates over objects in the list, creating each one.
- Calls
create_from_yaml_single_item
to handle individual resource creation.
create_from_yaml_single_item(k8s_client, yml_object, verbose=False, apply=False, **kwargs)
:
- Takes a single YAML object (
yml_object
) representing a Kubernetes resource. - Determines the resource type (
kind
) and its API version. - Uses the appropriate API client method (e.g.,
create_namespaced_pod
,create_service
) to create the resource. - Supports server-side apply for resource creation.
- Prints a confirmation message if
verbose
is set toTrue
.
Error Handling:
- The code uses a custom exception class,
FailToCreateError
, to encapsulate errors encountered during resource creation. - This exception stores a list of
client.rest.ApiException
instances, providing detailed information about each failed resource.
Key Points:
- The code provides a convenient way to create Kubernetes resources from YAML files or dictionaries.
- It supports both the traditional
create
operation and server-side apply, enabling safer and more robust resource updates. - The code handles errors gracefully by collecting and reporting
ApiException
instances.
Usage:
from kubernetes.client import ApiClient
from kubernetes.utils import create_from_yaml
k8s_client = ApiClient()
# Create resources from a YAML file
create_from_yaml(k8s_client, yaml_file="deployment.yaml", verbose=True)
# Create resources from a list of YAML objects
yaml_objects = [
{"apiVersion": "apps/v1", "kind": "Deployment", ...},
{"apiVersion": "v1", "kind": "Service", ...},
]
create_from_yaml(k8s_client, yaml_objects=yaml_objects)
Graph
The graph shows the usage of functions within the codebase.
Select a code symbol to view it's graph