reference current kube context in helm chart template

5/23/2019

Bit of a helm novice here, in short I want to reference the current Kubernetes context within my helm chart template. Is this possible? Example:

if the upgrade was run via:

/var/jenkins_home/helm291 upgrade -i --kube-context Dev

And the template contained a deployment.yaml with the following:

...
spec:
  containers:
    - name: {{ .Chart.Name }}
      image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
      env:
      {{- if contains "Dev" .Release.Kube_Context}}

Is there some variable I am missing that can check the Kube Context (aka replace Release.Kube_Context)? Am I approaching this the wrong way (specifying environment configuration)?

-- pasquers
kubernetes
kubernetes-helm

2 Answers

5/23/2019

There is no way of doing it and it's also a bad practice to do it in the templates. Templates should be generic - you modify environment specific things in values.yaml files. You can use the --kube-context flag instead if you are running it from Jenkins

-- Adilet Maratov
Source: StackOverflow

5/23/2019

You can export the context values you need and then execute helm, so they will be available as environmental vars.

For example, if you need the token access:

export TOKEN=$(kubectl config view -o jsonpath='{.users[?(@.name == "dev-user")].user.auth-provider.config.id-token}'); /var/jenkins_home/helm291 upgrade -i --kube-context Dev

-- Chus
Source: StackOverflow