Helm inheriting variable value

3/7/2020

consider this values.yaml file and secrets.yaml fle. Is there any way to read prometheus.promethesSpec.thanos.objectStorageConfig.name and
pass the data of the variable thanosObjectStoreConfig (which is the value of above dict) to the secrets.yaml ?

values.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

secrets.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 }}
-- Rajesh Rajendran
go-templates
kubernetes
kubernetes-helm

1 Answer

3/7/2020

The easiest approach is to avoid the problem entirely. When you helm install or helm upgrade a chart, you can provide any number of -f options to specify additional YAML values files. You can put the specific storage configuration (what you have under the thanosObjectStoreConfig top-level key) in a separate file, with a fixed top-level key, and helm install -f with different files in different environments.

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

If you really want to switch this based on a key, the core Go text/template language includes an index function that can do a dynamic lookup:

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