Search code examples
kuberneteskubernetes-helm

If value doesn't exist OR has true value


In my final deployment.yaml created from helm template I would like to have liveness and readiness blocks only if in values.yaml the block .Values.livenessReadinessProbe doesn't exist or if .Values.livenessReadinessProbe.enabled is true.

I tried to do it so:

  {{- if or (not .Values.livenessReadinessProbe) (.Values.livenessReadinessProbe.enabled) }}
  livenessProbe:
    httpGet:
      path: /actuator/health/liveness
      port: 8080
    initialDelaySeconds: 300
    failureThreshold: 5
    periodSeconds: 10
  readinessProbe:
    httpGet:
      path: /actuator/health/readiness
      port: 8080
    initialDelaySeconds: 200
    failureThreshold: 5
    periodSeconds: 10
  {{- end }}

But I'm getting nil pointer evaluating interface {}.enabled, if livenessReadinessProbe is absent in values.yaml, so it seems like the second OR condition is being executed, even though the first condition is true (i.e. .Values.livenessReadinessProbe is absent).

How can I achieve it?

My values.yaml with existing livenessReadinessProbe value:

livenessReadinessProbe:
  enabled: true

Thank you in advance!


Solution

  • In the Go text/template language, and and or are functions, not built-in operators, so they don't have the usual "short-circuiting" semantics: first they evaluate all of their arguments, and then or returns the first that is true.

    What I'd do here is use default to provide an empty dictionary as a default value. That is still "false" so you can use it in conditionals, but now since it's a dictionary, you can do an index lookup in it.

    {{- $lrp := .Values.livenessReadinessProbe | default dict }}
    {{- if or (not $lrp) $lrp.enabled }}
    livenessProbe: { ... }
    readinessProbe: { ... }
    {{- end }}