Having duplicate environment variable names for a container can lead to unexpected behavior, as the last declared variable will overwrite any previous ones with the same name. This rule aims to detect and prevent such situations, thereby enhancing the clarity and predictability of Kubernetes configurations.
If an environment variable of a container is declared multiple times with different values, it can become unclear which value will be used by the application and can lead to unpredictable behavior. Also, if an issue arises related to the environment variable, debugging can be challenging. The problem could be due to the overwritten variable, but this might take time to be noticeable.
Identify the duplicate environment variables for a container and remove the duplicates manually. If the same variable is needed with different values, consider using a different name for each variable.
apiVersion: v1
kind: Pod
metadata:
name: env-example
spec:
containers:
- name: nginx
image: nginx:1.7.9
env:
- name: VAR1 # Noncompliant
value: "value1"
- name: VAR1 # Noncompliant
value: "value2"
apiVersion: v1
kind: Pod
metadata:
name: env-example
spec:
containers:
- name: nginx
image: nginx:1.7.9
env:
- name: VAR1
value: "value1"
- name: VAR2
value: "value2"