Home > Software design >  If value doesn't exist OR has true value
If value doesn't exist OR has true value

Time:08-02

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!

CodePudding user response:

Try creating the function

{{- define "isFlagEnabled" -}}
{{- if .Values.global -}} {{/* <-- check parent exists to avoid nil pointer evaluating interface {}.myflag */}}
{{- if .Values.global.myflag -}}
{{- .Values.global.myflag -}}
{{- end -}}
{{- else if .Values.myflag -}} {{/* <-- make sure its else if so you wont override if both defined */}}
{{- .Values.myflag -}}
{{- else -}}
{{- printf "false" }}
{{- end -}}
{{- end -}}

use in template

 {{- define "flagUsage" -}}
    {{- if eq (include "isFlagEnabled" .) "true" -}}
    {{- printf "%s" (include "isFlagEnabled" .) -}}
    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 -}}
    {{- end -}}

Ref read more at : https://itecnote.com/tecnote/if-condition-checking-value-returned-by-helm-template/

CodePudding user response:

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 }}

CodePudding user response:

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.

You can test it here:

template.yaml

{{- if .Values.livenessReadinessProbe }}
{{- if eq (.Values.livenessReadinessProbe.enabled) true }}
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 }}
{{- end }}

values.yaml

livenessReadinessProbe:
  enabled: true

See the output when you adjust the values.yaml:

  • Delete livenessReadinessProbe.enabled, or set it to false.
  • Delete the entire value block.
  • Related