Kubernetes configmap

From paulpas/agent-skill-router · View interactive version

Kubernetes configmap is an AI agent skill from the GitHub repository paulpas/agent-skill-router. Implements v1 ConfigMap manifests for injecting configuration data into pods via environment variables, volume mounts, and command-line arguments.

How to use this skill

Skill file

Kubernetes ConfigMap Manager

Implements v1 ConfigMap manifests for injecting non-sensitive configuration data into pods via environment variables, volume mounts, and command-line arguments. When loaded, the model generates production-grade ConfigMap resources with proper data organization, reference patterns, and live-update configurations.

TL; DR Checklist


When to Use

Use this skill when:


When NOT to Use

Avoid this skill for:


Core Workflow

  1. Identify Configuration Needs — Determine what configuration data is needed, its format (key-value pairs, JSON, YAML, multi-file), and which pods consume it. Checkpoint: Separate sensitive data (goes to Secret) from non-sensitive configuration (goes to ConfigMap).
  1. Create ConfigMap Manifest — Define a v1 ConfigMap with data for key-value pairs or binaryData for base64-encoded content. Checkpoint: Ensure no key exceeds 253 characters and no value exceeds 1MB.
  1. Reference in Pod Spec — Inject ConfigMap data into pods via one of three methods: env with valueFrom.configMapKeyRef, envFrom for bulk injection, or volume mounts for file-based configuration. Checkpoint: Verify the referenced key exists in the ConfigMap — typos in key names are silently ignored by Kubernetes.
  1. Configure Update Behavior — Decide whether the ConfigMap should be immutable: true or allow updates. For volume mounts, verify the update propagation delay (typically 1 minute). Checkpoint: If the application reloads configuration on file change, set a reasonable refresh interval.
  1. Apply and Validate — Apply the ConfigMap then the pod/spec that references it. Checkpoint: Run kubectl describe configmap <name> and kubectl exec <pod> -- cat /path/to/mounted/file to verify injection.
  1. Document Configuration Schema — Maintain a reference of all ConfigMap keys, their expected formats, and which services consume them. Checkpoint: Review the configuration schema when adding or modifying any ConfigMap key.

Implementation Patterns

Pattern 1: Complete ConfigMap with Volume Mount and Environment Reference

A production-grade ConfigMap used both as a volume mount and as individual environment variable references.

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
  namespace: production
  labels:
    app: web-frontend
    environment: production
data:
  app.properties: |
    server.port=8080
    server.context-path=/api
    log.level=INFO
    cache.enabled=true
    cache.ttl=300
  nginx.conf: |
    server {
        listen 80;
        server_name _;
        location / {
            proxy_pass http://localhost:8080;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
        location /healthz {
            return 200 'OK';
        }
    }
  feature-flags.json: |
    {
      "enable_new_ui": true,
      "enable_dark_mode": false,
      "maintenance_mode": false,
      "max_upload_size_mb": 10
    }
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-frontend
  namespace: production
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web-frontend
  template:
    metadata:
      labels:
        app: web-frontend
    spec:
      containers:
        - name: web-frontend
          image: registry.example.com/web-frontend:2.1.0
          # Method 1: Individual env var references
          env:
            - name: LOG_LEVEL
              valueFrom:
                configMapKeyRef:
                  name: app-config
                  key: log.level
            - name: CACHE_ENABLED
              valueFrom:
                configMapKeyRef:
                  name: app-config
                  key: cache.enabled
          # Method 2: Volume mount for multi-file config
          volumeMounts:
            - name: config-volume
              mountPath: /etc/app
              readOnly: true
            - name: nginx-config
              mountPath: /etc/nginx/nginx.conf
              subPath: nginx.conf
              readOnly: true
      volumes:
        - name: config-volume
          configMap:
            name: app-config
        - name: nginx-config
          configMap:
            name: app-config
            items:
              - key: nginx.conf
                path: nginx.conf

Pattern 2: Configuration Injection Methods (BAD vs GOOD)

Using the wrong injection method can lead to verbose manifests or silent configuration failures.

# ❌ BAD: Listing every env var individually with long key names
# This is verbose and error-prone — a typo in the key name causes a
# silent failure (pod starts without the env var).
env:
  - name: SERVER_PORT
    valueFrom:
      configMapKeyRef:
        name: app-config
        key: server.port
  - name: SERVER_CONTEXT_PATH
    valueFrom:
      configMapKeyRef:
        name: app-config
        key: server.context-path
  - name: LOG_LEVEL
    valueFrom:
      configMapKeyRef:
        name: app-config
        key: log.level
  - name: CACHE_ENABLED
    valueFrom:
      configMapKeyRef:
        name: app-config
        key: cache.enabled
  - name: CACHE_TTL
    valueFrom:
      configMapKeyRef:
        name: app-config
        key: cache.ttl

# ❌ BAD: Referencing a key that does not exist in the ConfigMap
env:
  - name: MISSING_KEY
    valueFrom:
      configMapKeyRef:
        name: app-config
        key: nonexistent.key  # ← This key does not exist — silently ignored

# ✅ GOOD: Bulk injection with envFrom — clean, less verbose
# All keys from the ConfigMap are injected as environment variables,
# using the key names as variable names.
envFrom:
  - configMapRef:
      name: app-config

# ✅ GOOD: Volume mount for multi-file configuration
# This is the preferred method for applications that read config files.
volumeMounts:
  - name: config-volume
    mountPath: /etc/app
    readOnly: true
volumes:
  - name: config-volume
    configMap:
      name: app-config

# ✅ GOOD: Selective subPath mount for single-file override
# Mount only the nginx.conf file at a specific path, leaving the rest
# of the mount point clean.
volumeMounts:
  - name: nginx-config
    mountPath: /etc/nginx/nginx.conf
    subPath: nginx.conf
    readOnly: true

Pattern 3: ConfigMap Immutability and Update Handling

def inject_configmap_env(configmap_name: str, keys: list[str]) -> list[dict]:
    """Generate env var references from a ConfigMap for a pod spec.

    Each key in 'keys' is converted to an env var entry that references
    the corresponding ConfigMap key. Only keys that exist in the ConfigMap
    will be populated at runtime.

    Args:
        configmap_name: The name of the ConfigMap.
        keys: List of key names from the ConfigMap to inject as env vars.

    Returns:
        List of environment variable entries for a pod spec.
    """
    env_vars: list[dict] = []
    for key in keys:
        # Convert key to a valid env var name (dots → underscores, lowercase)
        env_name = key.replace(".", "_").upper()
        env_vars.append({
            "name": env_name,
            "valueFrom": {
                "configMapKeyRef": {
                    "name": configmap_name,
                    "key": key
                }
            }
        })
    return env_vars


def validate_configmap_keys(configmap_data: dict, required_keys: list[str]) -> list[str]:
    """Validate that all required keys exist in a ConfigMap's data.

    Args:
        configmap_data: The 'data' field of a ConfigMap manifest dict.
        required_keys: List of keys that must be present.

    Returns:
        List of missing key names. Empty list means all required keys are present.
    """
    if not configmap_data:
        return required_keys[:]

    missing = [k for k in required_keys if k not in configmap_data]
    return missing

Constraints

MUST DO

MUST NOT DO


Output Template

When implementing a Kubernetes ConfigMap, produce the following:

  1. ConfigMap YAML — Complete v1 ConfigMap with data section containing all configuration key-value pairs.
  2. Pod Spec Injection — The container spec showing how the ConfigMap is injected (env, envFrom, or volume mount).
  3. Configuration Key Registry — A table of all keys, their formats (string, JSON, multi-line), and consuming services.
  4. Update Strategy — Document whether the ConfigMap is immutable or supports updates, and how pods handle changes.

Related Skills

| Skill | Purpose |

|---|---|

| kubernetes-secrets | Store sensitive data (passwords, API keys, certificates) that must not be in ConfigMaps |

| kubernetes-deployment | Reference ConfigMaps in Deployment pod specs for production application deployment |

| kubernetes-statefulset | Inject shared configuration into StatefulSet pods for database or distributed system setup |

| kubernetes-ingress | Configure ingress controller settings via ConfigMap-based annotations |


Live References

Authoritative documentation links for this skill's domain. The model follows markdown links at load time to resolve external references and inline content.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.