adding yaml array as env to kubernetes/kustomize

9/2/2019

So, I have a yaml file that looks like this:

service:
        users:
            - username: some-user
              password: some-pass (would be placed in Secret)
            - username: some-user
              password: some-pass (would be placed in Secret)

I need to add it in env (In spring boot's priority list, config from env is higher than config from yaml. And this would be env(dev,stage,live) based). I already tried this with Kustomize

configMapGenerator:
    - name: my-java-server-props
      files:
      - config.yaml

And then using this in Deployment

envFrom:
  - configMapRef:
      name: my-java-server-props

Other configs in config.yml like this:

spring:
  datasource:
    url: some-url-here

are then add in .properties file for configMapGenerator like this:

spring_datasource_url=some-url-here

but the config with array doesn't seem to work.

any hints on where am I missing the trick? Also, the password comes from credstash; so, I have a different script which gets the value from credstash and creates a manifest for Secret.

The end goal to add passwords would be something like this in Deployment:

name: service_users.0.password
      valueFrom:
        secretKeyRef:
          key: value
          name: service-user-store-password
-- nirvair
kubernetes
kustomize

1 Answer

9/2/2019

This was an issue in spring boot from some time ago, support for binding environment variables to array elements. There is a good document to describe the solution on the spring boot wiki: https://github.com/spring-projects/spring-boot/wiki/Relaxed-Binding-2.0.

To summarize, array elements can be indexed using i, so in your case:

env:
- name: SERVICE_USERS_0_PASSWORD
  valueFrom:
    secretKeyRef:
      key: value
      name: service-user-store-password
- name: SERVICE_USERS_1_PASSWORD
  valueFrom:
    secretKeyRef:
      key: value
      name: service-another-user-store-password
-- James McShane
Source: StackOverflow