Check if a values.yaml property has any entries in it when you don't know the names?

Viewed 724

I have a helm chart template that looks like this:

  volumes:
    - name: secrets
      projected:
        sources:
        {{- range $secretKey := .Values.secrets }}            
          - secret: 
              name: {{ $secretKey | kebabcase }}-secret
        {{- end }}

This works perfectly, except for when .Values.secrets has no entries in it. Then it gives this error:

error validating data: ValidationError(Deployment.spec.template.spec.volumes[0].projected): missing required field "sources" in io.k8s.api.core.v1.ProjectedVolumeSource

Basically, it is complaining that sources does not have any values.

But I can't find a way to check to only do this section when .Values.secrets has entries. My values.yaml file is filled automatically and sometimes does not have any values for the secrets.

But because it is filled automatically, I don't know the names of the values in it. As such I cannot just do a test for one of the entries (like most examples do).

How can I check if .Values.secrets has any values?

1 Answers

You only need to add a conditional judgment above, and no object is generated when there is no value.

According to the helm document, when the object is empty, the if statement judges to return false.

A pipeline is evaluated as false if the value is:

  • a boolean false
  • a numeric zero
  • an empty string
  • a nil (empty or null)
  • an empty collection (map, slice, tuple, dict, array)
volumes:
{{- if .Values.secrets }}
- name: secrets
  projected:
    sources:
    {{- range $secretKey := .Values.secrets }}
    - secret:
        name: {{ $secretKey | kebabcase }}-secret
    {{- end }}
{{- end }}

case 1:

values.yaml

secrets:

output:

volumes:

case 2:

values.yaml

secrets:
  - "aaa"
  - "bbb"

output:

volumes:
  - name: secrets
    projected:
      sources:
      - secret:
          name: aaa-secret
      - secret:
          name: bbb-secret
Related