Search code examples
kuberneteskubernetes-podconfigmap

Precedence rule in container env list with duplicate name key in kubernetes


Related to Duplicated env variable names in pod definition, what is the precedence rule to determine the final value?

I have a deployment spec with a repeated env name, see below:

containers:
  - name: c1
  ...
    env:
      - name: DUP1
        value: hello1
      - name: DUP1
        value: hello2

Can I expect the second DUP1 key to always be the one set on the pod? e.g. echo $DUP1 == "hello2". It seems to be the case but I can't find validation about it


Solution

  • If the order maintains like this, it will always be set as hello2

    Note: Environment variables may reference each other, however ordering is important. Variables making use of others defined in the same context must come later in the list. Similarly, avoid circular references.

    so if the order changes for the value like this

                - name: DUP1
                  value: hello2
                - name: DUP1
                  value: hello1
    

    it will be set as hello1

    inject-environment-variable-container