Helm chart value based on namespace annotation

9/22/2020

In a helm chart is it possible to set a value at deployment time that is based on an annotation from the namespace it is being deployed to?

-- Raymond Self
kubernetes
kubernetes-helm

2 Answers

9/22/2020

Yes, you can set different values based on namespace and kube-context as well. Check this out for reference - https://stackoverflow.com/a/63982817/6673707

-- Shashank Sinha
Source: StackOverflow

9/22/2020

Current versions of Helm 3 have a lookup template function that can retrieve object data from the cluster. You could use this in this context as something like:

{{- $ns := lookup "v1" "Namespace" "" .Release.Namespace }}
{{- $istio := index $ns.metadata.annotations "istio-injection" }}
{{- if eq $istio "enabled" }}
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
...
{{ end }}

The documentation has the important caveat that lookup doesn't work in helm template or helm install --dry-run since these modes don't contact the cluster at all; lookup will return nil.

Also note that this will only take effect when you re-run a helm command. A custom Kubernetes operator might be a little more effort to build, but it will get re-triggered whenever an object you're watching changes, which will behave a little more consistently with other Kubernetes objects. (That is to say, you could write an operator to automatically create or delete or modify resources whenever the annotation value changes.)

-- David Maze
Source: StackOverflow