<jupyter_start><jupyter_text>How to create a ConfigMap and use its data in Pods===========================[ConfigMaps](https://kubernetes.io/docs/tasks/configure-pod-container/configmap/) allow you to decouple configuration artifacts from image content to keep containerized applications portable. In this notebook we would learn how to create a ConfigMap and also how to use its data in Pods as seen in https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/<jupyter_code>from kubernetes import client, config
from kubernetes.client.rest import ApiException<jupyter_output><empty_output><jupyter_text>Load config from default location<jupyter_code>config.load_kube_config()<jupyter_output><empty_output><jupyter_text>Create API endpoint instance and API resource instances<jupyter_code>api_instance = client.CoreV1Api()
cmap = client.V1ConfigMap()<jupyter_output><empty_output><jupyter_text>Create key value pair data for the ConfigMap<jupyter_code>cmap.metadata = client.V1ObjectMeta(name="special-config")
cmap.data = {}
cmap.data["special.how"] = "very"
cmap.data["special.type"] = "charm"<jupyter_output><empty_output><jupyter_text>Create ConfigMap<jupyter_code>api_instance.create_namespaced_config_map(namespace="default", body=cmap)<jupyter_output><empty_output><jupyter_text>Create API endpoint instance and API resource instances for test Pod<jupyter_code>pod = client.V1Pod()
spec = client.V1PodSpec()
pod.metadata = client.V1ObjectMeta(name="dapi-test-pod")<jupyter_output><empty_output><jupyter_text>Initialize test Pod container<jupyter_code>container = client.V1Container()
container.name = "test-container"
container.image = "gcr.io/google_containers/busybox"
container.command = ["/bin/sh", "-c", "env"]<jupyter_output><empty_output><jupyter_text>Define Pod environment variables with data from ConfigMaps<jupyter_code>container.env = [client.V1EnvVar(name="SPECIAL_LEVEL_KEY"), client.V1EnvVar(name="SPECIAL_TYPE_KEY")]
container.env[0].value_from = client.V1EnvVarSource()
container.env[0].value_from.config_map_key_ref = client.V1ConfigMapKeySelector(name="special-config", key="special.how")

container.env[1].value_from = client.V1EnvVarSource()
container.env[1].value_from.config_map_key_ref = client.V1ConfigMapKeySelector(name="special-config", key="special.type")

spec.restart_policy = "Never"
spec.containers = [container]
pod.spec = spec<jupyter_output><empty_output><jupyter_text>Create Pod<jupyter_code>api_instance.create_namespaced_pod(namespace="default",body=pod)<jupyter_output><empty_output><jupyter_text>View ConfigMap data from Pod log<jupyter_code>log = ""
try: 
    log = api_instance.read_namespaced_pod_log(name="dapi-test-pod", namespace="default")
except ApiException as e:
    if str(e).find("ContainerCreating") != -1:
        print("Creating Pod container.\nRe-run current cell.")
    else:
        print("Exception when calling CoreV1Api->read_namespaced_pod_log: %s\n" % e)

for line in log.split("\n"):
    if line.startswith("SPECIAL"):
        print(line)<jupyter_output><empty_output><jupyter_text>Delete ConfigMap<jupyter_code>api_instance.delete_namespaced_config_map(name="special-config", namespace="default", body=cmap)<jupyter_output><empty_output><jupyter_text>Delete Pod<jupyter_code>api_instance.delete_namespaced_pod(name="dapi-test-pod", namespace="default", body=client.V1DeleteOptions())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1278592375366569, 'fraction_numerical': 0.005865102639296188, 'mean_word_length': 8.745714285714286, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 50, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '446463', 'license': 'Apache-2.0', 'n_tokens_mistral': 1134, 'n_tokens_neox': 1118, 'n_words': 180, 'path': 'examples/notebooks/create_configmap.ipynb', 'repo_name': 'sivansyuee/python', 'size': '3410'}