Home > Net >  how to use multiple variables with same name in helm chart?
how to use multiple variables with same name in helm chart?

Time:10-10

i have helm chart which need to define multiple same variables name like this

      containers:
      - name: APP_NAME
        image: CONTAINER_IMAGE
        envFrom:
        - secretRef:
            name: secret1
        - secretRef:
            name: secret2
        - configMapRef:
            name: configmap1
        - secretRef:
            name: secret3

i added the nonproduction.yaml chart like this

envFrom:
  secretRef:
    name1: secret1
  secretRef:
    name2: secret2
  configmapRef:
    name3: configmap1
  secretRef:
    name4: secret3

and define the deployment.yaml

      containers:
      - name: {{ .Values.appName }}
        image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
        envFrom:
        - secretRef:
            name: {{ .Values.envFrom.secretRef.name1 }}
        - secretRef:
            name: {{ .Values.envFrom.secretRef.name2 }}
        - configMapRef:
            name: {{ .Values.envFrom.configmapRef.name3 }}
        - secretRef:
            name: {{ .Values.envFrom.secretRef.name4 }}

but it went to duplicate key on the terminal, how can i use same name variable on helm chart without changing the name?

edit: add number so we dont get dizzy by the 'name'

CodePudding user response:

Instead of calling individual secrets and config map, You can call all of them at a time.

  env:
    {{- toYaml .Values.envs | nindent 12 }}
  envFrom:
    {{- toYaml .Values.envFrom | nindent 12 }}

CodePudding user response:

YAML supports a set of basic data structures, like JSON (and in fact a valid JSON file will generally be valid YAML). In YAML's terminology, there are scalars (strings, numbers, booleans), sequences (lists, arrays), and mappings (dictionaries).

In the values.yaml file you've shown, envFrom: is a mapping, but three of the keys are the same; you've said that the property secretRef has the mapping value {name1: value1}, and also that the property secretRef has the mapping value {name2: value2}, and that's where Helm gets confused.

You can change the values.yaml file to a YAML sequence instead by putting a hyphen - before each item:

envFrom:
  - secretRef:
      name1: secret1
  - secretRef:
      name2: secret2
  - et: cetera
# ^ add "-" before each item

At this point you can use the approach in @karthik's answer to copy this structure as-is into the YAML output, since the sequence form matches what Kubernetes expects here.

envFrom: {{- toYaml .Values.envFrom | nindent 12 }}

If you did need to iterate through individual values here, since it is now a sequence, you can't reference an individual item by name (and it's not easy in Helm to find an item in a sequence of mappings with some property) but only by index or via the range iteration keyword.

  • Related