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

问题描述 投票:0回答:1

当您运行

helm create mychart
时,它的 imagePullSecrets 定义如下:

spec:
  {{- with .Values.imagePullSecrets }}
  imagePullSecrets:
    {{- toYaml . | nindent 8 }}
  {{- end }

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

imagePullSecrets: []

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

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

--set "mychart.imagePullSecrets[0].name={reg-creds}"

退货:

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"

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

--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"

这些错误消息令人恼火。是否可以使用

--set
设置此值,这样我就可以避免重构所有图表?

kubernetes-helm
1个回答
6
投票

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 ^^^^^^^^^

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

# image-pull-secrets.yaml
imagePullSecrets:
  - name: reg-creds

您可以将其包含在每个环境值文件中,或将其作为独立值文件传递。 在任何一种情况下,您都可以使用

helm install -f
选项来提供文件。 拥有多个
helm install -f
值文件就可以了。

helm install ... -f image-pull-secrets.yaml
© www.soinside.com 2019 - 2024. All rights reserved.