How do I access the current user in a helm chart template

12/1/2017

I have a helm chart template, and I would like to use the result of whoami as a template variable. How do I do this?

So if my values.yaml file has:

env:
  uniqueId: {{ whoami? }}

how might I do this?

note: I am on os x, so whoami I believe assumes a linux environment, however, in the spirit of this being deployment agnostic I presume there is a non-unix way of doing this.

-- Nathan Feger
kubernetes
kubernetes-helm

1 Answer

12/1/2017

The Helm Chart's "values.yaml" file is typically for default values. Anything that you'd like to override should be done at time of install/upgrade of the chart.

The Helm docs show a lot of different ways in which values can be used: https://github.com/kubernetes/helm/blob/master/docs/charts.md

In this case, one option is to set the value on the command line:

helm install -set env.whoami=$(id -un) ./your-chart.tgz

You could then have a value.yaml file like:

env:
    whoami: "default"

Finally, you can use it in a template like:

  containers:
  - name: {{ .Chart.Name }}
    image: "{{ .Values.image.repository }}:{{ .Chart.Version }}"
    imagePullPolicy: {{ .Values.image.pullPolicy }}
    env:
    - name: WHOAMI
      value: {{ .Values.env.whoami }}

Obviously your template will vary, the above is just a snippet.

-- David Newman
Source: StackOverflow