我在这样的 secret 模板中有一些东西:
apiVersion: v1
kind: Secret
metadata:
# not relevant
type: Opaque
data:
password: {{ randAlphaNum 32 | b64enc | quote }}
现在,在执行 helm upgrade
时,会重新创建 secret ,但使用它的 pod 不会(它们也不应该,这没关系)。
这会导致 pod 在重新启动或升级时失败,因为新密码现在与旧密码不匹配。
是否可以在 secret 存在时跳过重新创建 secret ,例如 {{- if not(exists theSecret) }}
以及如何做到这一点?
最佳答案
您可以使用 HELM 中的查找功能来检查 secret 是否存在
https://helm.sh/docs/chart_template_guide/functions_and_pipelines/#using-the-lookup-function
helm 图表中的函数如下:https://github.com/sankalp-r/helm-charts-examples/blob/1081ab5a5af3a1c7924c826c5a2bed4c19889daf/sample_chart/templates/_helpers.tpl#L67
{{/*
Example for function
*/}}
{{- define "gen.secret" -}}
{{- $secret := lookup "v1" "Secret" .Release.Namespace "test-secret" -}}
{{- if $secret -}}
{{/*
Reusing value of secret if exist
*/}}
password: {{ $secret.data.password }}
{{- else -}}
{{/*
add new data
*/}}
password: {{ randAlphaNum 32 | b64enc | quote }}
{{- end -}}
{{- end -}}
secret 创作会是这样的
示例文件:https://github.com/sankalp-r/helm-charts-examples/blob/main/sample_chart/templates/secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: "test-secret"
type: Opaque
data:
{{- ( include "gen.secret" . ) | indent 2 -}}
图表示例:https://github.com/sankalp-r/helm-charts-examples
{{- $secret := (lookup "v1" "Secret" .Release.Namespace "test-secret" -}}
apiVersion: v1
kind: Secret
metadata:
name: test-secret
type: Opaque
# 2. If the secret exists, write it back
{{ if $secret -}}
data:
password: {{ $secret.data.password }}
# 3. If it doesn't exist ... create new
{{ else -}}
stringData:
password: {{ randAlphaNum 32 | b64enc | quote }}
{{ end }}
关于kubernetes - Helm : How to avoid recreating secrets on upgrade?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67617808/