ClusterRole 存在且无法导入到当前版本中?

Kil*_*Ors 7 kubernetes kubernetes-helm

我试图在同一个集群的两个不同命名空间中安装同一个图表两次。但是我收到此错误:

Error: rendered manifests contain a resource that already exists. Unable to continue with install: ClusterRole "nfs-provisioner" 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 "namespace2": current value is "namespace1"
Run Code Online (Sandbox Code Playgroud)

据我了解,集群角色应该独立于命名空间,所以我发现这是矛盾的。我们使用的是helm3

mat*_*t_j 8

我决定提供一个社区 Wiki 答案,以帮助其他面临类似问题的人。
\n我假设您想多次安装相同的图表,但出现以下错误:

\n
Error: rendered manifests contain a resource that already exists. Unable to continue with install: ClusterRole "<CLUSTERROLE_NAME>" in namespace "" exists and cannot be imported into the current release: ...\n
Run Code Online (Sandbox Code Playgroud)\n


首先,确定我们是否真的需要ClusterRole而不是很重要Role。\n正如我们可以在Role 和 ClusterRole 文档中找到的:

\n
\n

如果要在命名空间内定义角色,请使用 Role;如果要在集群范围内定义角色,请使用 ClusterRole。

\n
\n

其次,我们可以使用变量名称来ClusterRole代替在模板中硬编码名称:

\n

例如,代替:

\n
apiVersion: rbac.authorization.k8s.io/v1\nkind: ClusterRole\nmetadata:\n  name: clusterrole-1\n...\n
Run Code Online (Sandbox Code Playgroud)\n

尝试使用类似的东西:

\n
apiVersion: rbac.authorization.k8s.io/v1\nkind: ClusterRole\nmetadata:\n  name: {{ .Values.clusterrole.name }}\n...\n
Run Code Online (Sandbox Code Playgroud)\n

第三,如果资源已经存在,我们可以使用该lookup函数和控制结构来跳过创建资源。if

\n

看一个简单的例子:

\n
$ cat clusterrole-demo/values.yaml\nclusterrole:\n  name: clusterrole-1\n\n$ cat clusterrole-demo/templates/clusterrole.yaml\n{{- if not (lookup "rbac.authorization.k8s.io/v1" "ClusterRole" "" .Values.clusterrole.name) }}\n\napiVersion: rbac.authorization.k8s.io/v1\nkind: ClusterRole\nmetadata:\n  name: {{ .Values.clusterrole.name }}\nrules:\n- apiGroups:\n  - ""\n  resources:\n  - pods\n  verbs:\n  - get\n  - list\n  - watch\n{{- end }}\n
Run Code Online (Sandbox Code Playgroud)\n

在上面的示例中,如果ClusterRole clusterrole-1已经退出,则不会创建\xe2\x80\x99。

\n