Issue: when I run helm upgrade/install, it also calls a sub chart edge-broker and it can't read values from parent chart for a subchart's job:
helm upgrade -i offline-edge line-edge --version 1.0.0 -n line-edge -f values_dev.yaml i get:
Error: UPGRADE FAILED: template: offline-edge/charts/edge-broker/templates/cleanup-job.yaml:6:20: executing "offline-edge/charts/edge-broker/templates/cleanup-job.yaml" at <$.Values.tags.mqtt>: nil pointer evaluating interface {}.mqtt
values_dev.yaml from parent helm chart has:
tags:
mqtt: true
cleanup-job has:
apiVersion: batch/v1
kind: Job
metadata:
name: cleanup-job
annotations:
{{- if and (eq $.Values.tags.mqtt false) $.Release.IsUpgrade }}
"helm.sh/hook": pre-upgrade, pre-delete
{{- else if and (not $.Values.tags) $.Release.IsUpgrade }}
"helm.sh/hook": pre-delete
{{- else }}
"helm.sh/hook": pre-delete
{{- end }}
tried with helper.tpl in sub-chart, no go:
helper.tpl
{{/*
Helper to get the value of tags.mqtt from the parent chart, defaulting to true if not set
*/}}
{{- define "values.tags.mqtt" -}}
{{- with index .Values "tags" }}
{{- default true .mqtt | quote }}
{{- end -}}
{{- end }}
cleanup-job with tpl change:
apiVersion: batch/v1
kind: Job
metadata:
name: cleanup-job
annotations:
{{- if and (eq (include "values.tags.mqtt" .) "false") .Release.IsUpgrade }}
"helm.sh/hook": pre-upgrade, pre-delete
{{- else }}
"helm.sh/hook": pre-delete
{{- end }}
$.Values.tags.mqtt read from parent chart's values yaml. helm command reads only the sub-charts values yaml but can't get if conditions working.
how can i read the values from parent charts's values yaml?
for example: {{- if and (eq $.Parent.Values.tags.mqtt false) .Release.IsUpgrade }}
helm version: v3.14.2
Ref: https://helm.sh/docs/chart_template_guide/subcharts_and_globals/
You can't see or access your parent chart's values. You may not have a parent chart at all.
The Scope, Dependencies, and Values section of the Helm documentation has a couple of relevant comments:
If your parent chart's
values.yamlhasThen when a template file in the subchart is evaluated, it gets the value of
subchart(matching the dependency name inChart.yamlorrequirements.yaml) plusglobalmerged as-is, effectively gettingIn template code, this is visible at the point
.Valuesis used. It's possible todefinea function in a parent chart and call it in a child (but please don't); if you do,.Valuesis taken from the function's parameter, not from context where the function is defined.This setup sort of seems like your child chart is depending on context from its parent. I'd avoid a setup like this; it makes the child chart hard to test and the tight coupling means it's not especially reusable.
Two things you could consider changing are to move the value into
global, which will get passed in as-is into dependenciesAnother is to wrap the Job creation in the subchart in a helper function, and explicitly call it from the parent chart. Since values come from the function parameter defined at the point of call, this path would use the parent values, and because of the function wrapper, you're not directly implicitly depending on being run in a subchart context.