helm: How to create a namespace if it doesn't exists

I have a kind: Namespace template yaml like follows,

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 kubernets cluster

Output of helm version: 2.9.1

Output of kubectl version: 2.9.1

Cloud Provider/Platform (AKS, GKE, Minikube etc.): Google Cloud

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 2
  • Comments: 15 (8 by maintainers)

Most upvoted comments

This is a known limitation of Helm. Helm will not do cross-namespace management, and we do not recommend setting namespace: directly in a template.

If you want to install resources in a namespace other than the default namespace, helm install --namespace=foo will install the resources in that namespace. If the namespace does not exist, it will create it.

Does that help?

Right now, use kubectl create namespace foo prior to helm install. When Helm 3.2.0 is released, use helm install ... --create-namespace.

Right now, use kubectl create namespace foo prior to helm install. When Helm 3.2.0 is released, use helm install ... --create-namespace.

That worked for me with helm 3.2.4

Like a charm!

This is a known limitation of Helm. Helm will not do cross-namespace management, and we do not recommend setting namespace: directly in a template.

If you want to install resources in a namespace other than the default namespace, helm install --namespace=foo will install the resources in that namespace. If the namespace does not exist, it will create it.

Does that help?

What about a situation where you have 2 resources that should go in different namespaces??

@marckhouzam yes, you are correct helm only creates one namespace you specify with --namespace flag. But going forward with Helm v3 you will need to create the namespace for the chart release as well, so better have one step in advance which does create all required namespaces.

helm install --namespace foo should be able install all the roles and rolebindings in the foo namespace. The --namespace flag will create the namespace if it didn’t exist. You don’t need to provide a namespace in the template metadata.

For Helm 3, the namespace creation doesn’t happen during helm install any more, so you’ll have to create that namespace ahead of time as you do today.

We use a helm chart to manage custom RBAC permissions. That chart creates different Role and RoleBinding through centralized yaml config files. So the chart needs to create different Role and RoleBinding in different namespaces.

I have problems when the namespace does not exist as it makes the helm install fail. Right now I have to parse the yaml config files and figure out if a namespace does not exist already then create it with kubectl, only then can I run helm.

Any better suggestion? Thanks.

For reference, here are more details about the chart: https://github.com/helm/charts/pull/11261#issuecomment-464493406

Are the functions deployed as part of the same chart? I agree with @ragarcia26’s advice of breaking up these components into individual charts. I imagine the operator and the functions serve separate functions of work and should be managed separately.

Alternatively, you can use tools like helmfile that can manage multiple charts across separate namespaces.