Helm:如何避免在升级时重新创建秘密?

rab*_*ens 1 kubernetes kubernetes-helm

我在这样的秘密模板中有一些东西:

apiVersion: v1
kind: Secret
metadata:
  # not relevant
type: Opaque
data:
  password: {{ randAlphaNum 32 | b64enc | quote }}
Run Code Online (Sandbox Code Playgroud)

现在,当执行 时helm upgrade,秘密被重新创建,但使用它的 pod 不是(它们也不应该,这没关系)。

这会导致 pod 在重新启动或升级时失败,因为新密码现在与旧密码不匹配。

当秘密存在时,是否可以跳过重新创建秘密,例如 a{{- if not(exists theSecret) }}以及如何做?

Har*_*var 5

您可以使用HELM 中的查找 功能来检查秘密是否存在

https://helm.sh/docs/chart_template_guide/functions_and_pipelines/#using-the-lookup-function

掌舵图中的功能如下: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 -}}
Run Code Online (Sandbox Code Playgroud)

秘密创作将类似于

示例文件: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 -}}
Run Code Online (Sandbox Code Playgroud)

图表示例: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 }}
Run Code Online (Sandbox Code Playgroud)

  • 后续:我将Helm(仍然是3.2.4)升级到3.5.4,现在可以使用了。 (2认同)