模板中的 Helm 值

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

我希望我的 Helm 图表根据云提供商(AWS 与阿里云)使用不同的图像存储库,不幸的是,我在尝试运行

helm package
命令时收到以下错误:

Error: cannot load values.yaml: error converting YAML to JSON: yaml: invalid map key: map[interface {}]interface {}{"tpl (.Files.Get \"config/repository.config\") . | quote":interface {}(nil)}

在我的

values.yaml
我有:

configuration:
  system:
    mode:
      cloud_provider: aws

image:
repository: {{ tpl (.Files.Get "config/repository.config") . | quote }}

在我的

config/repository.config
文件中我有:

{{- if eq ( include "cloud_provider" . | trim ) "aliyun" -}}
    registry.cn-qingdao.aliyuncs.com/x/kube-state-metrics
{{- end -}}
{{- if eq ( include "cloud_provider" . | trim ) "aws" -}}
    k8s.gcr.io/kube-state-metrics/kube-state-metrics
{{- end -}}

templates/_helpers.tpl
文件中我添加了:

{{- define "cloud_provider" -}}
    {{ required "Cloud provider required at .Values.configuration.system.mode.cloud_provider due to Google to unreachable in China" .Values.configuration.system.mode.cloud_provider }}
    {{ $valid_cloud_provider := list "aws" "aliyun" }}
    {{- if not (has .Values.configuration.system.mode.cloud_provider $valid_cloud_provider ) -}}
        {{ fail "Invalid cloud provider set. Should be aws or aliyun" }}
    {{end}}
    {{- else -}}
        {{ .Values.configuration.system.mode.cloud_provider | default "disabled" }}
    {{- end -}}
{{- end -}}
yaml kubernetes-helm
1个回答
0
投票

这不是错误的直接解决方案,而是一种替代方法。我希望这也能有所帮助:

我不会将此类表达式放入值文件中

values.yaml

repository: {{ tpl (.Files.Get "config/repository.config") . | quote }}

我会为每个环境使用不同的 yaml 文件:

添加

aliyun.yaml
值文件

registry: registry.cn-qingdao.aliyuncs.com/x/kube-state-metrics

并使用此值文件调用 helm

helm ... -f aliyun.yaml
© www.soinside.com 2019 - 2024. All rights reserved.