I’m switching from ansible to kustomize for deployments into k8s. I’ve created some basic deployments into different environments using overlays and it works pretty well but I’m having difficulties when it comes to keeping my code DRY.
For example I have a lot of repeated code in my overlays mostly around the configuration of container environment variables using envFrom
and env
. The same values are used in 3 different deployments, the main web app, a background job work and an ancillary app along with 1 job used for DB migrations. The envFrom:
config isn’t a big issue as it’s just one config. But the number of variables configure under env:
, both hard coded and referencing secrets already in the cluster is 30+.
For example
...
envFrom:
- configMapRef:
name: dev-app-cm
optional: false
env:
- name: POSTGRESQL_PASSWORD
valueFrom:
secretKeyRef:
name: dev-app-scrt
key: db-password
- name: POSTGRESQL_DATABASE
valueFrom:
secretKeyRef:
name: dev-app-scrt
key: db-name
- name: REDIS_URL
valueFrom:
secretKeyRef:
name: dev-app-scrt
key: redis-url
- name: NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: COMPONENT
valueFrom:
fieldRef:
fieldPath: metadata.labels['component']
- name: APPLICATION_NAME
value: app1
- name: APP_ENV
value: dev
- name: APP_ENVIRONMENT
value: development
- name: BACKEND_SERVICE_HOSTNAME
value: backend-k8s-svc.default.svc.cluster.local
- name: BACKEND_SERVICE_PORT
value: '8080'
...
What’s the best way to reuse these across 4 different resources? I’ve tried replacements, components and vars. Replacements and components didn’t quote work the way I expected it too and vars is due to be deprecated.