'How do I access a specific item from my values.yaml File

My values.yaml file looks like this:

myApp:
  jenkinsCreds:
    usernamePassword:
    - credentialsId: 'github-jenkins'
      passwordVariable: 'pass'
      usernameVariable: 'USERNAME'
      helm: 
        foo: " "
    - credentialsId: 'rabbitmq-dev'
      passwordVariable: 'rabbitmq_username'
      usernamevariable: 'rabbitmq_password'
      helm:
        rabbitmqUser: ""
        rabbitMqPass: ""

I want to access the rabitmqUser and rabbitmqPass items in a secret. How could I do this in Helm? I can't figure out how to grab it.

This is what my secret used to look like before I added all the new data to values.yaml:

apiVersion: v1
metadata:
  name: SuperSecret
kind: Secret
type: Opaque
stringData:
  rabbitmq_pass: {{ .Values.rabbitmqPass | quote }}
  rabbitmq_user: {{ .Values.rabbitmqUser | quote }}

I am not sure how I would get the values out of my values.yaml file.

I thought if I changed it to: {{.Values.myApp.jenkinsCreds.usernamePassword.helm.rabbitmqUser}} and {{.Values.myApp.jenkinsCreds.usernamePassword.helm.rabbitMqPass}}I would be able to access the values but this is not the case.

What's the best way to access the rabbitmqUser and the rabbitMqPass from my secret. I'd prefer not to change the shape of my values.yaml file but if I need to that's OK.



Solution 1:[1]

Just add some necessary loops and judgments where values is used.

eg.

values.yaml

myApp:
  jenkinsCreds:
    usernamePassword:
    - credentialsId: 'github-jenkins'
      passwordVariable: 'pass'
      usernameVariable: 'USERNAME'
      helm: 
        foo: " "
    - credentialsId: 'rabbitmq-dev'
      passwordVariable: 'rabbitmq_username'
      usernamevariable: 'rabbitmq_password'
      helm:
        rabbitmqUser: "root"
        rabbitmqPass: "123456"

secret.yaml

apiVersion: v1
metadata:
  name: SuperSecret
kind: Secret
type: Opaque
stringData:
  {{- range $i, $v := .Values.myApp.jenkinsCreds.usernamePassword }}
  {{- if eq $v.credentialsId "rabbitmq-dev" }}
  rabbitmq_pass: {{ $v.helm.rabbitmqPass | quote }}
  rabbitmq_user: {{ $v.helm.rabbitmqUser | quote }}
  {{- end }}
  {{- end }}

output:

# Source: test/templates/secret.yaml
apiVersion: v1
metadata:
  name: SuperSecret
kind: Secret
type: Opaque
stringData:
  rabbitmq_pass: "123456"
  rabbitmq_user: "root"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 z.x