1) equal with string
Khi bạn muốn if else so sanh bằng
{{- if eq .Values.pluton.enabled "xxx"}}
{{- else if eq .Values.environment "prod-vdc" }}
{{- else }}
{{- end }}
Còn so sánh true false:
{{- if .Values.pluton.enabled }}
{{- end }}
Kiếm tra có tồn tại key đó ko?
It is quite impressive how hard it is to check a map key in Go templates to do some simple if conditions in your Helm charts or other Kubernetes templates.
At least for Helm, there is a nice solution. For this, you have to know that Helm uses the Sprig template library which has support for dict types. And the dict
type provides a hasKey
method:
{{- if hasKey .Values.mymap "mykey" }}
# do something conditional here...
{{- end }}
https://stackoverflow.com/questions/59795596/helm-optional-nested-variables
Most charts will default the parent object to a empty map in values.yaml
so it always exists.
foo: {}
Then {{ if .Values.foo.bar }}
works.
If that’s not possible, test both keys:
{{ if .Values.foo }}
{{ if .Values.foo.bar }}
{{ .Values.foo.bar }}
{{ end }}
{{ end }}
Using the and
function doesn’t work in this case due to and
evaluating all parameters, even if the first is falsey.
There is also the hasKey
function included from sprig if you ever need to check the existence of a falsey or empty value:
{{ if hasKey .Values.foo "bar" }}