I want to use pulumi transformations to update an environment variable value in a k8s manifest:
containers:
- name: pgbouncer
image: edoburu/pgbouncer:1.15.0
env:
# order of env vars should not change because pulumi code depends on it
- name: DATABASE_URL
value: 'psql://${username}:${password}@${endpoint}/${name}'
- name: POOL_MODE
value: 'transaction'
- name: MAX_CLIENT_CONN
value: '50'
- name: ADMIN_USERS
value: '${username}'
I did it by using the order or the environment variables See the following code, but I don't think it's good to keep it that way for its non-readability and error-prone in the future.
const pg_bouncer_reader = new k8s.yaml.ConfigFile("pg-bouncer-reader", {
file: "../../pulumi-common/pg-bouncer.deployment.yaml",
transformations: [
(obj: any) => {
obj.spec.template.spec.containers[0].env[0].value = readerConnectionString;
obj.spec.template.spec.containers[0].env[3].value = defaultCluster.masterUsername;
},
],
});
is there a way to target YAML list element using syntax like obj.spec.template.spec.containers[0].env[name = 'DATABASE_URL']
?
You can simply use any JavaScript method to search through lists. A colleague of mine (shoutout to Levi!) managed this using JavaScripts .find
method:
const deployment = new k8s.yaml.ConfigFile("deployment", {
file: "deployment.yaml",
transformations: [
(obj: any) => {
obj.spec.template.spec.containers
.find((container: any) => container.name === "pgbouncer")
.env
.find((env: any) => env.name === "DATABASE_URL")
.value = readerConnectionString;
obj.spec.template.spec.containers
.find((container: any) => container.name === "pgbouncer")
.env
.find((env: any) => env.name === "ADMIN_USERS")
.value = defaultCluster.masterUsername;
},
],
});