I want to implement some k8s services only if there is a service with suffix "mysuffix" in the namespace. I tried the following but doesn't work
{{- $previousReleaseInstalled := false -}}
{{- range $index, $service := (lookup "v1" "Service" .Release.Namespace "").items }}
{{- if hasSuffix "mysuffix" "$service.name" }}
{{- $previousReleaseInstalled := true -}}
{{- end }}
{{- end }}
{{- if $previousReleaseInstalled -}}
implement some services
{{- end }}
CodePudding user response:
Your lookup
function is almost correct. The issue with the assignment of the $previousReleaseInstalled
variable. You need to use =
instead of :=
inside the range
. Check this answer for the details Why doesn't this change the value of the variable in the range loop in Helm?
Another issue is the wrong key name. You should use $service.metadata.name
instead of $service.name
, and remove the quotes around the $service.metadata.name
{{- $previousReleaseInstalled := false -}}
{{- range $index, $service := (lookup "v1" "Service" .Release.Namespace
"").items }}
{{- if hasSuffix "mysuffix" $service.metadata.name }}
{{- $previousReleaseInstalled = true -}}
{{- end }}
{{- end }}
{{- if $previousReleaseInstalled -}}
implement some services
{{- end }}
Another important thing to note, the lookup
function always returns empty response when used with the helm template
command. https://helm.sh/docs/chart_template_guide/function_list/#lookup