Search code examples
kuberneteskubernetes-helm

How can I check if a k8s secret exists in a Helm chart/k8s template, or use a default value?


I have a template part like:

    spec:
      containers:
        - name: webinspect-runner-{{ .Values.pipeline.sequence }}
          ...
          env:
            - name: wi_base_url
              valueFrom:
                secretKeyRef:
                  name: webinspect
                  key: wi-base-url
            - name: wi_type
              valueFrom:
                secretKeyRef:
                  name: webinspect
                  key: wi-type

The webinspect/wi_type secret may be missing. I want the container also don't have the wi_type envvar or get a default value (better) when the secret is missing, but k8s just reports CreateContainerConfigError: couldn't find key wi-type in Secret namespace/webinspect and the pod fails.

Is there a way to use a default value, or skip the block if the secret does not exist?


Solution

  • Two options, the first is add optional: true to the secretKeyRef block(s) which makes it skip. The second is a much more complex approach using the lookup template function in Helm. Probably go with the first :)