How to fix helm namespaces when specified namespace in templates before, but setting the namespace afterwards via the helm -n namepace flag

Viewed 3085

Some time ago we deployed many different releases where we specified the namespaces in the templates itself, like f.e.:

apiVersion: v1
kind: ConfigMap
metadata:
  name: secret-database-config
  namespace: {{ .Release.Name }}
  labels:
    app: secret-database-config
data:
  POSTGRES_HOST: 123
  ...

But we realized now that this is not the correct approach, but you should use the -n namespace flag (see here).

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 -)

So if we fix our files

apiVersion: v1
kind: ConfigMap
metadata:
  name: secret-database-config
  labels:
    app: secret-database-config
data:
  POSTGRES_HOST: 123
  ...

and run now:

helm upgrade --install --debug -n myproject123 -f helm/configs/myproject123.yaml myproject123 helm

We get following errors:

history.go:56: [debug] getting history for release myproject123
Release "myproject123" does not exist. Installing it now.
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /Users/myuser/coding/myrepo/helm

Error: rendered manifests contain a resource that already exists. Unable to continue with install: Namespace "myproject123" in namespace "" exists and cannot be imported into the current release: invalid ownership metadata; annotation validation error: key "meta.helm.sh/release-namespace" must equal "myproject123": current value is "default"
helm.go:81: [debug] Namespace "myproject123" in namespace "" exists and cannot be imported into the current release: invalid ownership metadata; annotation validation error: key "meta.helm.sh/release-namespace" must equal "myproject123": current value is "default"
rendered manifests contain a resource that already exists. Unable to continue with install
helm.sh/helm/v3/pkg/action.(*Install).Run
  /private/tmp/helm-20210310-44407-1006esy/pkg/action/install.go:276
main.runInstall
  /private/tmp/helm-20210310-44407-1006esy/cmd/helm/install.go:242
main.newUpgradeCmd.func2
  /private/tmp/helm-20210310-44407-1006esy/cmd/helm/upgrade.go:115
github.com/spf13/cobra.(*Command).execute
  /Users/brew/Library/Caches/Homebrew/go_mod_cache/pkg/mod/github.com/spf13/cobra@v1.1.1/command.go:850
github.com/spf13/cobra.(*Command).ExecuteC
  /Users/brew/Library/Caches/Homebrew/go_mod_cache/pkg/mod/github.com/spf13/cobra@v1.1.1/command.go:958
github.com/spf13/cobra.(*Command).Execute
  /Users/brew/Library/Caches/Homebrew/go_mod_cache/pkg/mod/github.com/spf13/cobra@v1.1.1/command.go:895
main.main
  /private/tmp/helm-20210310-44407-1006esy/cmd/helm/helm.go:80
runtime.main
  /usr/local/Cellar/go/1.16/libexec/src/runtime/proc.go:225
runtime.goexit
  /usr/local/Cellar/go/1.16/libexec/src/runtime/asm_amd64.s:1371
make: *** [ns_upgrade] Error 1

Any ideas how this can be fixed? It is not possible for us to delete everything and then install it again due to downtimes (and the amount of projects we have already deployed).

1 Answers

Use {{ .Release.Namespace }} instead of {{ .Release.Name }}. Then, you will be able to overwrite the namespace during installation via cli.

apiVersion: v1
kind: ConfigMap
metadata:
  name: secret-database-config
  namespace: {{ .Release.Namespace }}
  labels:
    app: secret-database-config
data:
  POSTGRES_HOST: 123
  ...
Related