Configuration Options for Development Environment

Environment Variables

Environment variables can be defined in your Kubernetes Pod configuration using the V1EnvVar class. This class allows you to set environment variables for your pods in an inclusive manner.

from kubernetes.client.models import V1EnvVar, V1Container, V1PodSpec, V1Pod

# Example environment variable
env_var = V1EnvVar(name="MY_ENV_VAR", value="my_value")

# Add to a container
container = V1Container(
    name="my_container",
    image="my_image",
    env=[env_var]
)

# Define pod spec
pod_spec = V1PodSpec(containers=[container])

# Create pod
pod = V1Pod(api_version="v1", kind="Pod", metadata={"name": "my-pod"}, spec=pod_spec)

The name must be a C_IDENTIFIER, while the value can reference other environment variables in the format $(VAR_NAME).

Using ConfigMaps and Secrets

For better security and management, environment variables can also be populated from ConfigMaps and Secrets through the V1ConfigMapEnvSource and V1SecretEnvSource classes.

from kubernetes.client.models import V1ConfigMapEnvSource, V1SecretEnvSource, V1EnvFromSource

# Using ConfigMap
config_map_source = V1ConfigMapEnvSource(name="my-config-map")
env_from_config_map = V1EnvFromSource(config_map_ref=config_map_source)

# Using Secret
secret_source = V1SecretEnvSource(name="my-secret")
env_from_secret = V1EnvFromSource(secret_ref=secret_source)

# Add env_from to the container
container_with_env_from = V1Container(
    name="my_container",
    image="my_image",
    env_from=[env_from_config_map, env_from_secret]
)

In this example, env_from allows the container to read environment variables from the ConfigMap or Secret, ensuring a clean separation from hardcoded values.

Device Allocation Configuration

For scenarios requiring specific device allocation, the DeviceClassSpec structure can be utilized. You can define several properties including config, selectors, and the deviceClassName.

from kubernetes.client.models import DeviceClassSpec

device_class_spec = DeviceClassSpec(
    config=[
        {
            'driver': "vendor_driver",
            'parameters': {
                'key': 'value'
            }
        }
    ],
    selectors=[{'key': 'value'}],
    device_class_name="my_device_class"
)

This code defines how devices are allocated, which can be referenced by the devices requested during API calls.

Pod Configuration with Environment Variables

Combining all the previous concepts, a complete pod configuration can be set up as follows:

from kubernetes.client.models import V1Pod, V1PodSpec

pod_spec = V1PodSpec(
    containers=[
        V1Container(
            name="my_container",
            image="my_image",
            env=[env_var],
            env_from=[env_from_config_map, env_from_secret]
        )
    ]
)

pod = V1Pod(api_version="v1", kind="Pod", metadata={"name": "my-pod"}, spec=pod_spec)

This creates a pod that utilizes environment variables fetched from both a ConfigMap and a Secret, enhancing security and flexibility.

Sources:

  • kubernetes/swagger.json.unprocessed
  • kubernetes/docs/V1EnvVar.md
  • kubernetes/client/models/v1_container.py