如何通过 set 命令将默认模板中定义的 imagePullSecrets 传递给 helm

red*_*888 3 kubernetes-helm helm3

当您运行时helm create mychart,它的 imagePullSecrets 定义如下:

spec:
  {{- with .Values.imagePullSecrets }}
  imagePullSecrets:
    {{- toYaml . | nindent 8 }}
  {{- end }
Run Code Online (Sandbox Code Playgroud)

在默认值文件中,它看起来像是向其传递了一个空白数组:

imagePullSecrets: []
Run Code Online (Sandbox Code Playgroud)

我已经有一堆根据此默认模板构建的具有此设置的图表。以前我不需要使用 imagePullSecrets,所以我只是将其保留原样,但现在我在某些情况下想通过 cli 在部署时设置它。

Helm 现在支持数组,但这似乎不起作用:

--set "mychart.imagePullSecrets[0].name={reg-creds}"
Run Code Online (Sandbox Code Playgroud)

返回:

Error: UPGRADE FAILED: error validating "": error validating data: ValidationError(Deployment.spec.template.spec.imagePullSecrets[0].name): invalid type for io.k8s.api.core.v1.LocalObjectReference.name: got "array", expected "string"
Run Code Online (Sandbox Code Playgroud)

然后我尝试传递一个字符串:

--set "mychart.imagePullSecrets='- name: reg-creds'"

Error: unable to build kubernetes objects from release manifest: error validating "": error validating data: ValidationError(Deployment.spec.template.spec.imagePullSecrets): invalid type for io.k8s.api.core.v1.PodSpec.imagePullSecrets: got "string", expected "array"
Run Code Online (Sandbox Code Playgroud)

这些错误消息令人愤怒。是否可以设置这个值,--set这样我就可以避免重构我的所有图表?

Dav*_*aze 5

语法独特且复杂helm install --set一个不寻常的语法是花{foo,bar}括号中的值将值设置为数组。然后,在您的示例中,--set object.path={value}将值设置为单元素数组;您看到的错误是它需要是一个字符串。

这意味着这里的一个简单解决方法是删除 右侧的花括号--set。还有一个--set-string选项强制将值解释为字符串,即使它包含大括号或逗号。

helm install ... --set "mychart.imagePullSecrets[0].name=reg-creds"
#                       no curly braces around the value ^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

使用 YAML 文件来提供此值可能会更清晰,并且具有更标准的语法。

# image-pull-secrets.yaml
imagePullSecrets:
  - name: reg-creds
Run Code Online (Sandbox Code Playgroud)

您可以将其包含在每个环境值文件中,或将其作为独立值文件传递。无论哪种情况,您都可以使用该helm install -f选项来提供文件。helm install -f拥有多个值文件很好。

helm install ... -f image-pull-secrets.yaml
Run Code Online (Sandbox Code Playgroud)