Helm chart fails with "nil pointer evaluating interface {}" when trying to evaluate a missing nested key

mittelmania :

I am writing a Helm 3 library chart and would like to create a YAML with default values. However, when trying to set a default value for a nested key that does not exist, Helm fails with the following error message:

nil pointer evaluating interface {}

Say I have this snippet in my Kubernetes object:

{{- if eq (.Values.deployment.scale.type | default "static") "static" }}
  replicas: {{ default "3" .Values.deployment.scale.replicas }}
{{- end }}

If .Values.deployment.scale is defined, the template will render fine and the value of replicas will be 3 even if .Values.deployment.scale.replicas is not defined.

However, if one of the parent keys is not defined, Helm will fail with the error message above. For example if values.yaml is the following:

# values.yaml
deployment:
  not_scale: {}

The render will fail with: nil pointer evaluating interface {}.scale

How is it possible to set a default value for a nested key, even if its parent keys are undefined?

mittelmania :

I've decided to solve this using step-by-step evaluation of the hierarchy and using default dict to assign an empty map if the key does not exist. It both works and looks better.

Example:

{{ $deployment := default dict .Values.deployment }}
{{ $scale      := default dict $deployment.scale }}
{{- if eq ($scale.type | default "static") "static" }}
  replicas: {{ default "3" $scale.replicas }}
{{- end }}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Exception when trying to save state of nested fragment [Fragment no longer exists for key android:target_state]

cmd.ExtraFiles fails when trying to pipe to it

fields_for missing when trying to add accepts_nested_attributes_for

Lerna fails with 404 when trying to link dependency

Helm chart restart pods when configmap changes

Helm chart passing multiple environment values for single key

Variable value as yaml key in helm chart

Datatables fails when trying to load 70000 records

Missing key when writing to topic

helm chart error can't evaluate field Values in type interface {}

ingress.yaml template returns error in renderring --> nil pointer evaluating interface {}.service

Nested objects' key is missing after it is formatted and rendered

Error when running Helm Chart with environment variables

Pandas groupby: Nested loop fails with key error

Kubernetes, Helm - helm upgrade fails when config is specified - JupyterHub

Update nested dict with missing key value pairs

chef solo private key missing when trying to use database cookbook

wget throws a 'missing url' when trying to install a repository signature key

Selenium test fails with Firefox when trying to log in

Makefile fails when trying to run a C program

Missing Keyword when trying to Select into

regex fails when trying to match whole word

r.js build fails when trying to evaluate a plugin that shouldn't be evaluated

Add a missing key in a nested dictionary?

Django template fails to evaluate only when looking up a certain field

Helm: when to push a chart

Trying to install Kong using the Helm chart

Nested default value in a helm chart

Nil pointer evaluating interface with custom values file