how to read values from parent's values yaml while running a k8s job from sub-chart

56 Views Asked by At

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/

1

There are 1 best solutions below

1
David Maze On BEST ANSWER

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:

...lower level charts cannot access things in parent charts...

Values are namespaced, but namespaces are pruned. ...[for a child] chart, the scope of the values has been reduced and the namespace prefix removed

If your parent chart's values.yaml has

tags:
  mqtt: true
global:
  a: true
subchart:
  b: true

Then when a template file in the subchart is evaluated, it gets the value of subchart (matching the dependency name in Chart.yaml or requirements.yaml) plus global merged as-is, effectively getting

global:
  a: true
b: true

In template code, this is visible at the point .Values is used. It's possible to define a function in a parent chart and call it in a child (but please don't); if you do, .Values is 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 dependencies

# values_dev.yaml
global:
  tags:
    mqtt: true
annotations:
{{- if eq .Values.global.tags.mqtt false }}
  "helm.sh/hook": pre-upgrade, pre-delete  
{{- else }}
  "helm.sh/hook": pre-delete
{{- end }}

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

{{/* subchart/templates/_job.tpl */}}
{{- define "cleanup-job" -}}
apiVersion: batch/v1
kind: Job
...
{{ end -}}
{{/* parent/templates/cleanup-job.yaml */}}
{{- include "cleanup-job" . -}}