title: How to fix helm namespaces when specified namespace in templates before, but setting the namespace afterwards via the helm -n namepace flag
body: <p>Some time ago we deployed many different releases where we specified the namespaces in the templates itself, like f.e.:</p>
  <pre><code>apiVersion: v1
  kind: ConfigMap
  metadata:
    name: secret-database-config
    namespace: {{ .Release.Name }}
    labels:
      app: secret-database-config
  data:
    POSTGRES_HOST: 123
    ...
  </code></pre>
  <p>But we realized now that this is not the correct approach, but you should use the <code>-n</code> namespace flag (see <a href="https://github.com/helm/helm/issues/5465" rel="nofollow noreferrer">here</a>).</p>
  <blockquote>
  <p>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 -)</p>
  </blockquote>
  <p>So if we fix our files</p>
  <pre><code>apiVersion: v1
  kind: ConfigMap
  metadata:
    name: secret-database-config
    labels:
      app: secret-database-config
  data:
    POSTGRES_HOST: 123
    ...
  </code></pre>
  <p>and run now:</p>
  <pre><code>helm upgrade --install --debug -n myproject123 -f helm/configs/myproject123.yaml myproject123 helm
  </code></pre>
  <p>We get following errors:</p>
  <pre><code>history.go:56: [debug] getting history for release myproject123
  Release &quot;myproject123&quot; does not exist. Installing it now.
  install.go:173: [debug] Original chart version: &quot;&quot;
  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 &quot;myproject123&quot; in namespace &quot;&quot; exists and cannot be imported into the current release: invalid ownership metadata; annotation validation error: key &quot;meta.helm.sh/release-namespace&quot; must equal &quot;myproject123&quot;: current value is &quot;default&quot;
  helm.go:81: [debug] Namespace &quot;myproject123&quot; in namespace &quot;&quot; exists and cannot be imported into the current release: invalid ownership metadata; annotation validation error: key &quot;meta.helm.sh/release-namespace&quot; must equal &quot;myproject123&quot;: current value is &quot;default&quot;
  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
  </code></pre>
  <p>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).</p>