Override env values defined in container spec

前端 未结 1 1882
耶瑟儿~
耶瑟儿~ 2021-01-04 06:04

I have a configmap where I have defined the following key-value mapping in the data section:

apiVersion: v1
kind: ConfigMap
metadata:
  namespac         


        
相关标签:
1条回答
  • 2021-01-04 06:20

    From Kubernetes api refernece : https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.13/#container-v1-core

    envFrom : List of sources to populate environment variables in the container. The keys defined within a source must be a C_IDENTIFIER. All invalid keys will be reported as an event when the container is starting. When a key exists in multiple sources, the value associated with the last source will take precedence. Values defined by an Env with a duplicate key will take precedence. Cannot be updated.

    So above clearly states the env will take precedence than envFrom.

    When a key exists in multiple sources, the value associated with the last source will take precedence.

    So, for overriding see below:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      namespace: default
      name: test-config
    data:
      TEST: "CONFIGMAP_VALUE"
    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: busy
      namespace: default
    spec:
      containers:
      - name: busybox
        image: busybox
        env:
        - name: TEST
          value: "DEFAULT_VAULT"
        - name: TEST
          valueFrom:
            configMapKeyRef:
              name: test-config
              key: TEST
        command:
        - "sh"
        - "-c"
        - >
          while true; do
            echo "$(TEST)";
            sleep 3600;
          done
    

    Check:

    kubectl logs busy -n default
    CONFIGMAP_VALUE
    
    0 讨论(0)
提交回复
热议问题