Skip to content

Instantly share code, notes, and snippets.

@daveneeley
Created December 30, 2023 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daveneeley/5f88d46d4c1f0491a6abdc9af0162a1e to your computer and use it in GitHub Desktop.
Save daveneeley/5f88d46d4c1f0491a6abdc9af0162a1e to your computer and use it in GitHub Desktop.
render template against merged release template before rendering final values
# controls which charts are installed
environments:
default:
values:
- fake:
install: false
ci_cd:
values:
- fake:
install: true
# controls values in the release
# but elastic-exporter.values.gotmpl is evaluated too early
# want a method to apply the template after all other values are merged
templates:
release_template:
values:
- chart:
key: template-default-value
- elastic-exporter.values.gotmpl
installedTemplate: |
{{`{{ eq .Values.fake.install true }}`}}
# passes because installedTemplate will be set to false
helmfile diff --environment=default && echo "environment=default: PASS"
# fails because elastic-exporter.values.gotmpl will be rendered
# but it tries to set a value whose key is loaded in the same render
helmfile diff --environment=ci_cd && echo "environment=ci_cd: PASS" || echo "environment=ci_cd: FAIL"
{{/* won't render because chart.key is created in the same base and doesn't "exist" yet */}}
{{- $chartVals := .Values }}
{{- $chartVals = $chartVals | setValueAtPath "chart.key" "chart-specific-val" }}
{{ toYaml $chartVals }}
bases:
- base-1.yaml.gotmpl
---
bases:
- base-2.yaml.gotmpl
---
repositories:
- name: prometheus-community
url: https://prometheus-community.github.io/helm-charts
releases:
- name: elastic-exporter
namespace: monitoring
chart: prometheus-community/prometheus-elasticsearch-exporter
inherit:
- template: release_template
@daveneeley
Copy link
Author

output of bash demo.sh

Adding repo prometheus-community https://prometheus-community.github.io/helm-charts
"prometheus-community" has been added to your repositories

Listing releases matching ^elastic-exporter$
environment=default: PASS
Adding repo prometheus-community https://prometheus-community.github.io/helm-charts
"prometheus-community" has been added to your repositories

in ./helmfile.yaml: failed processing release elastic-exporter: failed to render values files "elastic-exporter.values.gotmpl": failed to render [elastic-exporter.values.gotmpl], because of template: stringTemplate:3:30: executing "stringTemplate" at <setValueAtPath "chart.key" "chart-specific-val">: error calling setValueAtPath: failed to set value at path "chart.key": value for key "chart" does not exist
environment=ci_cd: FAIL

@daveneeley
Copy link
Author

This gist was created to showcase a dilemma in helmfile/helmfile#1261

Note that if you changed elastic-exporter.values.gotmpl to the following that it renders successfully.

chart:
  key: chart-specific-val

The point is that programmatic merging with custom template functions like setValueAtPath can't be used because it seems that .Values is not kept up to date as the values/files are loaded in the current phase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment