Skip to content

Instantly share code, notes, and snippets.

@kvudata
Created June 27, 2018 20:20
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save kvudata/12ba57ae1e7f01799aaa7f36350a9b2e to your computer and use it in GitHub Desktop.
Save kvudata/12ba57ae1e7f01799aaa7f36350a9b2e to your computer and use it in GitHub Desktop.
Use Helm hook to add labels to a namespace before chart installation
# This hook depends on helm creating the target namespace if it doesn't exist
# before the hook is called. This is the case on Helm v2.9.1
apiVersion: batch/v1
kind: Job
metadata:
name: label-ns
namespace: kube-system
labels:
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
annotations:
helm.sh/hook: pre-install
helm.sh/hook-delete-policy: hook-succeeded
spec:
template:
spec:
containers:
- name: labeler
image: gcr.io/google_containers/hyperkube:v1.9.7
command:
- kubectl
- label
- ns
- {{ .Release.Namespace }}
- istio-injection=enabled
restartPolicy: Never
# use tiller service account since it should have permissions to do namespace labeling
serviceAccountName: tiller
@jordan-da
Copy link

i can't believe we have to do this! that there is no way to mutate the namespace via helm.

@jnsvd
Copy link

jnsvd commented Sep 19, 2019

Thank you for sharing this!

@theAkito
Copy link

theAkito commented Mar 6, 2020

Great workaround, thanks.

@catherye
Copy link

if exist, how to handle in yaml?

@mconigliaro
Copy link

That tiller account doesn't exist in helm 3. I posted a complete example here: https://stackoverflow.com/a/76276158/115635

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment