1
votes

I am using helm version 2.14.1. I have created helm charts for an application that will be deployed by users to test their code on kubernetes cluster. I want to add labels for username values, so I can retrieve deployments by users (deployments by user labels). Is there a way to include system username in helm charts just like we do in Java with System.getProperty("user.name"). My helm template is like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "common.fullname" . }}--{{ Release.name }}
  labels:
    application: {{ include "common.name" . }}
    branch: "{{ Release.name }}"
    username: "{{ System.user.name }}"   # need to fetch the logged in user from system here
spec:
...

Is there a standard way to achieve this or is there anyway I can allow users to input there usernames from command line while using helm install or helm template commands?

EDIT: Although, the --set works for me in setting the values for my chart, I also need to set the same value in the dependencies. Something like this:

values.yaml

username: "" 

dependency1:
 username: {{ .Values.username }} 

dependency2:
 username: {{ .Values.username }}

...

Of course the above implementation doesn't work. I need to reference the set value in the dependencies as well

2
You can always use the --set optionMichael Albers
@MichaelAlbers as in helm template --set username=<username>?Avdhut Mankavale
Assuming username is in your values.yaml, then yes. In Bash you could then do helm template ... --set username=$USERMichael Albers
In my day job, every user has their own Kubernetes namespace, and we use RBAC controls to limit users to their own namespace. That should achieve the higher-level goal (a cluster admin can kubectl get deployments -n dmaze) and is automatic and enforceable.David Maze
@AvdhutMankavale Does it solve your issue?Wytrzymały Wiktor

2 Answers

1
votes

This is a community wiki answer based on the comments and posted for better visibility. Feel free to expand it.

You can use the helm template command with a --set option:

--set stringArray              set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
--set-file stringArray         set values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2)
--set-string stringArray       set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)

The --set parameters have the highest precedence among other methods of passing values into the charts. It means that by default values come from the values.yaml which can be overridden by a parent chart's values.yaml, which can in turn be overridden by a user-supplied values file, which can in turn be overridden by --set parameters.

You can check more details and examples in the official docs.

0
votes

I have resolved this. Thanks for help @MichaelAlbers and @WytrzymałyWiktor. So the solution is as below.

helm template path/to/chart --set global.username=username

And then in all the templates refer to this value as {{ .Values.global.username }}. This works for any dependency chart as well.