kubernetes - helm upgrade ignores namespace configuration

10/23/2019

I use k8s and deploy my application with gitlab. My cluster has for example the namespace production. If I install initially the application I run:

$ helm install --name super-app -f values.yml ./Path/To/Project/helm

This command will install successfully install the application in the namespace production since its specified in the helm values:

replicaCount: 3
imagePullSecret: regcred
namespace: production

In the project helm charts the I se the default namespace:

replicaCount: 3
imagePullSecret: regcred
namespace: default

When I run the following command from my gitlab ci runner:

helm upgrade -f ./values.yaml --set image.tag=master-$DOCKER_IMAGE_TAG super-app ./helm

In the values.yaml is again the namespace production specified, I get the following result:

Release "super-app has been upgraded.
LAST DEPLOYED: Wed Oct 23 12:15:36 2019
NAMESPACE: production
STATUS: DEPLOYED

RESOURCES:
==> v1/ConfigMap
NAME        DATA  AGE
super-app  1     0s

==> v1/Deployment
NAME        READY  UP-TO-DATE  AVAILABLE  AGE
super-app  0/3    3           0          0s

==> v1/Pod(related)
NAME                        READY  STATUS             RESTARTS  AGE
super-app-5d6dc6c9d-25q9g  0/1    ContainerCreating  0         0s
super-app-5d6dc6c9d-tdfhh  0/1    ContainerCreating  0         0s
super-app-5d6dc6c9d-z7h96  0/1    ContainerCreating  0         0s

==> v1/Secret
NAME        TYPE    DATA  AGE
super-app  Opaque  0     0s

==> v1/Service
NAME        TYPE          CLUSTER-IP      EXTERNAL-IP  PORT(S)         AGE
super-app  LoadBalancer  10.100.115.194  <pending>    8080:32645/TCP  0s

The application is now deployed in the namespace default and not in production. Even though the existing application (before the helm upgrade command) is running in production namespace. Helm just creates a new Service and application in default namespace. The same logic works for other applications, why does k8s ignore my namespace config?

Thanks

-- ghovat
continuous-integration
gitlab-ci
kubernetes
kubernetes-helm

1 Answer

10/23/2019

Although it's not documented in Chart Best Practices yet, this issue (#5465) addresses namespaces considerations:

In general, templates should not define a namespace. This is because Helm installs objects into the namespace provided with the --namespace flag. By omitting this information, it also provides templates with some flexibility for post-render operations (like helm template | kubectl create --namespace foo -f -)

As quoted, your best option is to add the --namespace to your install/upgrade commands instead of defining it on your templates.

-- Eduardo Baitello
Source: StackOverflow