I have a deployment with a pod that has his configuration defined through a lot of environment variables. Now I want to add a sidecar container that requires exactly the same environment variables as the already defined container. Instead of just copy/pasting all the variables I'd like to stick to the DRY principle.
The current definition looks something like this:
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
containers:
- name: container1
env:
- name: MYSQL_HOST
value: {{ template "mariadb.primary.fullname" .Subcharts.mariadb }}
- name: MYSQL_DATABASE
value: {{ .Values.mariadb.auth.database | quote }}
- name: MYSQL_USER
valueFrom:
secretKeyRef:
name: {{ .Values.secretName | default (printf "%s-%s" .Release.Name "db") }}
key: {{ .Values.usernameKey | default "db-username" }}
- name: MYSQL_PASSWORD
valueFrom:
secretKeyRef:
name: {{ .Values.secretName | default (printf "%s-%s" .Release.Name "db") }}
key: {{ .Values.passwordKey | default "db-password" }}
The recommended way to reuse environment variables is through a ConfigMap. Now I can create this:
apiVersion: v1
kind: ConfigMap
metadata:
name: config
data:
MYSQL_HOST: {{ template "mariadb.primary.fullname" .Subcharts.mariadb }}
MYSQL_DATABASE: {{ .Values.mariadb.auth.database | quote }}
---
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
containers:
- name: container1
envFrom:
- configMapRef:
name: config
- name: container2
envFrom:
- configMapRef:
name: config
But now I would still need to repeat myself for MYSQL_USER and MYSQL_PASSWORD (and for the 20 other env variables that are like this).
Is it possible to reuse the existing secrets MYSQL_USER and MYSQL_PASSWORD as environment variables without creating duplicates on container1 and container2?