68 lines
2.0 KiB
Plaintext

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.
== Why is this an issue?
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.
== How to fix it
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.
=== Code examples
==== Noncompliant code example
[source,text,diff-id=1,diff-type=noncompliant]
----
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"
----
==== Compliant solution
[source,text,diff-id=1,diff-type=compliant]
----
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"
----
== Resources
=== Documentation
* https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/[Kubernetes - Environment Variables for a Container]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Resolve the duplication of this environment variable.
=== Highlighting
* Highlight all the locations where duplicated environment variables are being used.
endif::env-github,rspecator-view[]