Search code examples
spring-bootkuberneteskubernetes-secrets

How to set an SpringBoot array property as a kubernetes secret?


I want to use the direct translation from k8s secret-keys to SpringBoot properties. Therefore I have a helm chart (but similar with plain k8s):

apiVersion: v1
data:
  app.entry[0].name: {{.Values.firstEntry.name | b64enc }}
kind: Secret
metadata:
  name: my-secret
type: Opaque

With that my intention is that this behaves as if I'd set the spring property file:

app.entry[0].name: "someName"

But when I do this I get an error:

 Invalid value: "[app.entry[0].name]": a valid config key must consist of alphanumeric characters, '-', '_' or '.' (e.g. 'key.name',  or 'KEY_NAME',  or 'key-name', regex used for validation is '[-._a-zA-Z0-9]+'),

So, [0] seems not to be allowed as a key name for the secrets.

Any idea how I can inject an array entry into spring directly from a k8s secret name?

Shooting around wildly I tried these that all failed:

  • app.entry[0].name: ... -- k8s rejects '['
  • app.entry__0.name: ... -- k8s ok, but Spring does not recognize this as array (I think)
  • "app.entry[0].name": ... -- k8s rejects '['
  • 'app.entry[0].name': ... -- k8s rejects '['

Solution

  • You should be able to use environnment variables like described in sprint-boot-env.

    app.entry[0].name property will be set using APP_ENTRY_0_NAME environment variable. This could be set in your deployment.

    Using secret like:

    apiVersion: v1
    data:
      value: {{.Values.firstEntry.name | b64enc }}
    kind: Secret
    metadata:
      name: my-secret
    type: Opaque
    

    and then use it with

           env:
           - name: APP_ENTRY_0_NAME
             valueFrom:
               secretKeyRef:
                 name: my-secret
                 key: value