0

I have a configmap yaml that needs to be executed if a particular value is set to true. This is the config map yaml def

{{ if (.Values.telemetry.create) }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: telemetry-allowlist
data:
  metrics.json: |
    {
...

Now in some cases users forget to add this value .Values.telemetry.create

This causes the entire setup to fail with the error

at <.Values.telemetry.create>: nil pointer evaluating interface {}.create

Wondering how to add default values so that it defaults to false if the value is not present

CSUNNY
  • 414
  • 1
  • 7
  • 23
  • Does this answer your question? [Helm chart fails with "nil pointer evaluating interface {}" when trying to evaluate a missing nested key](https://stackoverflow.com/questions/61154736/helm-chart-fails-with-nil-pointer-evaluating-interface-when-trying-to-evalu) – David Maze Dec 13 '21 at 18:47
  • You can assign `{{ $telemetry := .Values.telemetry | default dict }}` and then test on `{{ $telemetry.create }}`. That will substitute an empty dictionary for the nil value and avoid the error. – David Maze Dec 13 '21 at 18:48

2 Answers2

1

In order to avoid telemetry being empty, you need to add additional judgments.

According to the helm document, when the object is empty, the if statement judges to return false.

A pipeline is evaluated as false if the value is:

  • a boolean false
  • a numeric zero
  • an empty string
  • a nil (empty or null)
  • an empty collection (map, slice, tuple, dict, array)
{{ if .Values.telemetry }}
{{ if .Values.telemetry.create }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: telemetry-allowlist
data:
  metrics.json: |
    {
...
z.x
  • 1,905
  • 7
  • 11
0

You can set telemetry.create to false in your default values.yaml file.

...
telemetry:
  create: false
...

So even if users miss setting the telemetry.create value, the helm will render from the default values.yaml file and you won't get the nil pointer error.