Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate env variables in helm deployment?

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?

like image 893
dsnfldsnglknalndqoidnefgiosngi Avatar asked Oct 29 '25 17:10

dsnfldsnglknalndqoidnefgiosngi


1 Answers

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.)

like image 59
David Maze Avatar answered Oct 31 '25 05:10

David Maze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!