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)
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=foowill 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 fooprior tohelm install. When Helm 3.2.0 is released, usehelm install ... --create-namespace.That worked for me with helm 3.2.4
Like a charm!
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
--namespaceflag. 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 fooshould be able install all the roles and rolebindings in thefoonamespace. 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 installany 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
RoleandRoleBindingthrough centralized yaml config files. So the chart needs to create differentRoleandRoleBindingin different namespaces.I have problems when the namespace does not exist as it makes the
helm installfail. 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.