Interview Prep/Helm/Templating: Go Templates & Sprig

When would you use `fail`?

IntermediateConceptTemplating: Go Templates & Sprig
Answer

To reject invalid combinations that a single required cannot express:

{{- if and .Values.autoscaling.enabled .Values.api.replicaCount }}
{{- if gt (int .Values.api.replicaCount) (int .Values.autoscaling.maxReplicas) }}
{{- fail "api.replicaCount cannot exceed autoscaling.maxReplicas" }}
{{- end }}
{{- end }}
{{- if and (eq .Values.env "prod") (not .Values.postgres.persistence.enabled) }}
{{- fail "persistence must be enabled in prod" }}
{{- end }}

Put such validations in a _validate.tpl helper included from one template so they run once.

All Helm questions Roadmap: Helm

More on Templating: Go Templates & Sprig