首页 > 解决方案 > Helm 继承变量值

问题描述

考虑这个 values.yaml 文件和 secrets.yaml 文件。有没有办法读取 变量的数据(即上面dict的值)并将其prometheus.promethesSpec.thanos.objectStorageConfig.name传递给secrets.yaml?
thanosObjectStoreConfig

值.yaml

prometheus:
  prometheusSpec:
    thanos:
      image: thanosio/thanos:v0.11.0
      objectStorageConfig:
        name: thanosObjectStoreConfig
        key: storage

# Defining storage configs for thanos
thanosObjectStoreConfig:
  type: AZURE
  config:
    storage_account: "xxxxxxx"
    storage_account_key: "xxxxxxxxx"
    container: "prometheus"
    endpoint: "blob.core.windows.net"
    max_retries: 0

秘密.yaml

{{- if .Values.prometheus.prometheusSpec.thanos }}
---
apiVersion: v1
kind: Secret
metadata:
  name: thanos-object-store-config
type: Opaque
data:
  storage: < should contain values of `thanosObjectStorageConfig` | b64enc >
  # Tried not working as expected
  # storage: {{ tpl .Values.prometheus.prometheusSpec.thanos.objectStorageConfig.name $ }}
{{ end }}

标签: kuberneteskubernetes-helmgo-templates

解决方案


最简单的方法是完全避免这个问题。当您helm installhelm upgrade图表时,您可以提供任意数量的-f选项来指定额外的 YAML 值文件。您可以将特定的存储配置(thanosObjectStoreConfig顶级密钥下的内容)放在单独的文件中,使用固定的顶级密钥,并helm install -f在不同的环境中使用不同的文件。

helm install -f values-production.yaml ...

如果你真的想基于一个键来切换它,核心 Go文本/模板语言包含一个index可以进行动态查找的函数:

{{- $key := .Values.prometheus.prometheusSpec.thanos.objectStorageConfig.name }}
data:
  storage: {{ index .Values $key | toYaml | b64enc }}

推荐阅读