Concatenate env variables in helm deployment?

12/11/2019

Say I want the following secrets to be used in a db connection string inside a helm template:

- name: DBUSER
  valueFrom:
   secretKeyRef:
     name: dbadmin
     key: username
- name: DBPASS
   valueFrom:
    secretKeyRef:
      name: dbadmin
      key: password

Later I want to create this:

- name: Database__ConnectionString
  value: "server=something.com;port=3306;user=$DBUSER;password=$DBPASS;database=dbname"

How do I access the env values of DBUSER and DBNAME inside of that connection string value?

-- dsnfldsnglknalndqoidnefgiosngi
kubernetes
kubernetes-helm

2 Answers

12/11/2019

You would have to handle this in your container, usually via a command wrapper like

command:
- bash
- -c
- export Database_ConnectionString=server=...;user=$DBUSER && exec mycommand 

Or something like that

-- coderanger
Source: StackOverflow

12/12/2019

Many places in a pod spec support $(VARIABLE_NAME) syntax. In particular, the API documentation for an EnvVar object specifies

value (string): Variable references $(VAR_NAME) are expanded using the previous defined environment variables in the container and any service environment variables.

so you should be able to say something like

- name: Database__ConnectionString
  value: "server=something.com;port=3306;user=$(DBUSER);password=$(DBPASS);database=dbname"

(Note the parentheses around $(DBUSER) and $(DBPASS).)

(In this specific case some database libraries will let you specify all of the individual parts of a connection string as separate environment variables, so instead of doing this you might be able to set environment variables PGUSER and PGPASSWORD if you're using PostgreSQL for example.)

-- David Maze
Source: StackOverflow