【问题标题】:How to fix helm namespaces when specified namespace in templates before, but setting the namespace afterwards via the helm -n namepace flag之前在模板中指定命名空间时如何修复 helm 命名空间,但之后通过 helm -n namepace 标志设置命名空间
【发布时间】:2021-07-07 20:35:08
【问题描述】:

前段时间,我们部署了许多不同的版本,我们在模板本身中指定了命名空间,例如 f.e.:

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

但我们现在意识到这不是正确的方法,但您应该使用-n 命名空间标志(请参阅here)。

一般来说,模板不应定义命名空间。这是因为 Helm 将对象安装到由 --namespace 标志提供的命名空间中。通过省略这些信息,它还为模板提供了一些用于渲染后操作的灵活性(如 helm template | kubectl create --namespace foo -f -)

所以如果我们修复我们的文件

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

现在运行:

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

我们收到以下错误:

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

任何想法如何解决这个问题? 由于停机时间(以及我们已经部署的项目数量),我们无法删除所有内容然后重新安装。

【问题讨论】:

  • 抱歉劫持了你的问题,但是,你介意告诉我为什么不推荐使用 namespace: {{ .Release.Name }} 吗?
  • 用提到的最佳实践问题的链接更新了我的帖子。
  • 在使用-n 选项重新安装之前,您是否可以接受helm delete myproject123?这将删除现有资源并解决此冲突,但代价是一些应用程序停机。 (helm -n 选项还告诉 Helm 在哪里保存有关已安装内容的状态,这是此处缺少的链接,但我不知道如何修复它。)
  • 不,我们不可能删除所有内容,因为我们不能接受任何停机时间。

标签: kubernetes kubernetes-helm


【解决方案1】:

使用{{ .Release.Namespace }} 而不是{{ .Release.Name }}。然后,您将能够在安装过程中通过 cli 覆盖命名空间。

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

【讨论】:

  • 这不是我要问的。我不想更改命名空间,我想完全删除键命名空间以符合最佳实践,即不在模板本身中包含命名空间:github.com/helm/helm/issues/5465
猜你喜欢
  • 2019-04-18
  • 1970-01-01
  • 2019-12-17
  • 2021-12-24
  • 2021-05-22
  • 1970-01-01
  • 2020-08-21
  • 1970-01-01
  • 2014-11-14
相关资源
最近更新 更多