kubernetes - Helm : How to avoid recreating secret

我在这样的 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/

相关文章:

python - 我是否需要清理/删除在部署我的 Cloud Run 实例时创建的图像?

c# - 如何将普通字符串转换为十六进制的等效字节数组?

typescript - 没有泛型的类型推断?

android - Jetpack 撰写 : Textfield and FAB not using

c# - 将 MagickImage 转换为位图

pandas - 如何通过每行的第一个单词将 pandas 中的行汇总为该第一个单词的聚合?

java - 如何断言数组中某个值的存在

python - 如何从 yfinance 数据中删除时区?

r - 使用 melt 将数据合并到一个长列中

c# - 在不使用 lambda 表达式的情况下从该方法传递额外参数时在方法内部订阅事件