Search code examples
kuberneteskubernetes-helm

Conditional excluding of blocks


I have a long list of cronjobs that are common across all environments and short list which is specific to some markets.

As per the documentation of if/else, I can conditionally include blocks of text in the template. As the list to include is too large as most markets need it, I would prefer to exclude specific blocks.

As I am looping over a list of markets on a CronJob template, I am looking for an if/else condition to skip a particular iteration when the condition matches.

Something like:

{{- range $job := $.Values.cronjobs }}
{{- range $markets := $.Values.markets }}
{{- if and (eq $job "special") (eq $markets "ordinary") }}
<<---skip templating the below block of code--->>
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  namespace: {{ $.Release.Namespace }}
[...]
{{- end }}

Solution

  • You can negate the condition so that the block is only generated when it's false:

    {{- if not ( and (eq $job "special") (eq $markets "ordinary") ) }}