how to build DSN env from several ConfigMap resources?

4/21/2020

In order to a service work, it needs an environment variable called DSN which prints to something like postgres://user:password@postgres.svc.cluster.local:5432/database. This value I built with a ConfigMap resource:

apiVersion: v1
kind: ConfigMap
metadata:
  name: idp-config
  namespace: diary
data:
  DSN: postgres://user:password@postgres.svc.cluster.local:5432/database

This ConfigMap mounts as environment variable in my service Pod. Since the values are different from user and password and these PostgreSQL credentials are in another k8s resource (a Secret and a ConfigMap), how can I properly build this DSN environment in a k8s resource yaml so my service can connect to the database?

-- Rodrigo Souza
kubernetes

1 Answer

4/21/2020

Digging into Kubernetes Docs I was able to find. According to Define Environment Variables for a Container :

Environment variables that you define in a Pod’s configuration can be used elsewhere in the configuration, for example in commands and arguments that you set for the Pod’s containers. In the example configuration below, the GREETING, HONORIFIC, and NAME environment variables are set to Warm greetings to, The Most Honorable, and Kubernetes, respectively. Those environment variables are then used in the CLI arguments passed to the env-print-demo container.

apiVersion: v1
kind: Pod
metadata:
  name: print-greeting
spec:
  containers:
  - name: env-print-demo
    image: bash
    env:
    - name: GREETING
      value: "Warm greetings to"
    - name: HONORIFIC
      value: "The Most Honorable"
    - name: NAME
      value: "Kubernetes"
    command: ["echo"]
    args: ["$(GREETING) $(HONORIFIC) $(NAME)"]
-- Rodrigo Souza
Source: StackOverflow