Hereโs a cool little interpolation trick I found on the internet while trying to figure out how to build a new environment variable from existing ones with Kubernetes:
Letโs say you have a ConfigMap
:
configmap.yamlyaml
apiVersion: v1kind: ConfigMapmetadata:name: some-configmapdata:DB_NAME: "app-db"DB_PORT: "5432"DB_HOST: "app-db-svc"
And a secret because we want to keep things safe:
secret.yamlyaml
apiVersion: v1kind: Secretmetadata:name: some-secretdata:DB_PASSWORD: aHVudGVyMg== # hunter2DB_USER: YXBwLXVzZXI= # app-usertype: Opaque
Now you can render them
deployment.yamlyaml
apiVersion: apps/v1kind: Deploymentmetadata:labels:app: sick-appname: sick-appspec:replicas: 3selector:matchLabels:app: sick-apptemplate:metadata:labels:app: sick-appspec:containers:- image: your-registry.io/your-repo/sick-appname: appports:- containerPort: 80envFrom:- configMapRef:name: some-configmap- secretRef:name: some-secretenv:# Let's say you're using Prisma# You want a DATABASE_URL like this:# postgresql://app-user:hunter2@app-db-svc:5432/app-db- name: DATABASE_URLvalue: postgresql://$(DB_USER):$(DB_PASSWORD)@$(DB_HOST):$(DB_PORT)/$(DB_NAME)resources: {}
Now you donโt have to define a new secret!