如何检查 Helm chart/k8s 模板中是否存在 k8s secret,或者使用默认值?

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

我有一个模板部分:

    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

webinspect/wi_type 秘密可能丢失了。我希望容器也没有 wi_type envvar 或在密钥丢失时获得默认值(更好),但 k8s 只报告 CreateContainerConfigError: couldn't find key wi-type in Secret namespace/webinspect 并且 pod 失败。

有没有办法使用默认值,或者如果秘密不存在则跳过该块?

两个选项,第一个是将 optional: true 添加到 secretKeyRef 块,使其跳过。第二种是使用 lookup template function in Helm 的更复杂的方法。可能是第一个:)