Search code examples
kubernetesgoogle-cloud-platformnamespaceskubernetes-helm

How to create a namespace if it doesn't exists from HELM templates?


I have a kind: Namespace template YAML, as per below:

apiVersion: v1
kind: Namespace
metadata:
  name: {{ .Values.namespace }}
  namespace: ""

How do I make helm install create the above-given namespace ({{ .Values.namespace }}) if and only if above namespace ({{ .Values.namespace }}) doesn't exits in the pointed Kubernetes cluster?


Solution

  • For helm2 it's best to avoiding creating the namespace as part of your chart content if at all possible and letting helm manage it. helm install with the --namespace=<namespace_name> option should create a namespace for you automatically. You can reference that namespace in your chart with {{ .Release.Namespace }}. There's currently only one example of creating a namespace in the public helm/charts repo and it uses a manual flag for checking whether to create it

    For helm3 functionality has changed and there's a github issue on this