Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 282
mittelmania Avatar asked Oct 16 '22 04:10

mittelmania


1 Answers

I don't think, you can set default for that case. You need to pre-check if scale field exists. For that, you can use hasKey function from sprig:

{{- if hasKey .Values.deployment "scale" }}
{{- if eq (.Values.deployment.scale.type | default "static") "static" }}
replicas: {{ default "3" .Values.deployment.scale.replicas }}
{{- end }}
{{- else }}
replicas: 3
{{- end }}
like image 102
Grigoriy Mikhalkin Avatar answered Nov 15 '22 05:11

Grigoriy Mikhalkin