From d803d8d516303df78b98c725d0529afffe184122 Mon Sep 17 00:00:00 2001 From: Yan Bondarenko <202671653+yankawai@users.noreply.github.com> Date: Thu, 5 Nov 2026 15:49:42 +0100 Subject: [PATCH] fix(kubernetes): include the namespace in the kubevirt-csi ClusterRole name The ClusterRole that lets the kubevirt-csi controller get PersistentVolumes, and its ClusterRoleBinding, were named after the release only. They are cluster-scoped, and a release is named after the cluster, so two tenants with a cluster of the same name render the same pair. Installing the second one does not fail: the existing objects are patched and adopted. Deleting either cluster then deletes the pair, and the other cluster's CSI controller fails every ControllerExpandVolume with "persistentvolumes ... is forbidden" until its own release is upgraded again, so a PVC resize stays in Resizing. Name both objects after the namespace and the release, the way the kccm ClusterRole already is. On upgrade Helm creates the new pair before it deletes the old one, so the driver keeps its access. Signed-off-by: Yan Bondarenko <202671653+yankawai@users.noreply.github.com> Assisted-by: LLM (cherry picked from commit aaedcef7d96aecb9db6f010767261446099de0ae) Signed-off-by: cozystack-ci[bot] <274107086+cozystack-ci[bot]@users.noreply.github.com> --- .../csi/infra-cluster-service-account.yaml | 11 +++- .../apps/kubernetes/tests/csi_rbac_test.yaml | 66 +++++++++++++++++++ 2 files changed, 74 insertions(+), 3 deletions(-) diff --git a/packages/apps/kubernetes/templates/csi/infra-cluster-service-account.yaml b/packages/apps/kubernetes/templates/csi/infra-cluster-service-account.yaml index cdcbe61c4..abf3a1a80 100644 --- a/packages/apps/kubernetes/templates/csi/infra-cluster-service-account.yaml +++ b/packages/apps/kubernetes/templates/csi/infra-cluster-service-account.yaml @@ -51,10 +51,15 @@ subjects: - kind: ServiceAccount name: {{ .Release.Name }}-kcsi --- +# Cluster-scoped, so the name carries the namespace, as kccm's does. The +# release name alone repeats across tenants: two clusters named "test" in two +# tenants would share one ClusterRole and binding, and uninstalling either +# cluster deletes them from under the other, whose CSI driver can then no +# longer get its PersistentVolumes and volume expansion fails. apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: - name: {{ .Release.Name }}-kcsi + name: {{ .Release.Namespace }}-{{ .Release.Name }}-kcsi rules: - apiGroups: [""] resources: ["persistentvolumes"] @@ -63,10 +68,10 @@ rules: kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: - name: {{ .Release.Name }}-kcsi-binding + name: {{ .Release.Namespace }}-{{ .Release.Name }}-kcsi-binding roleRef: kind: ClusterRole - name: {{ .Release.Name }}-kcsi + name: {{ .Release.Namespace }}-{{ .Release.Name }}-kcsi apiGroup: rbac.authorization.k8s.io subjects: - kind: ServiceAccount diff --git a/packages/apps/kubernetes/tests/csi_rbac_test.yaml b/packages/apps/kubernetes/tests/csi_rbac_test.yaml index f533ec4a8..08ae52ada 100644 --- a/packages/apps/kubernetes/tests/csi_rbac_test.yaml +++ b/packages/apps/kubernetes/tests/csi_rbac_test.yaml @@ -22,3 +22,69 @@ tests: apiGroups: ["subresources.kubevirt.io"] resources: ["virtualmachines/addvolume", "virtualmachines/removevolume", "virtualmachineinstances/removevolume"] verbs: ["update"] + + - it: names the PersistentVolume ClusterRole after the namespace and the release + documentSelector: + path: kind + value: ClusterRole + asserts: + - equal: + path: metadata.name + value: tenant-test-kubernetes-test-kcsi + - equal: + path: rules + value: + - apiGroups: [""] + resources: ["persistentvolumes"] + verbs: ["get"] + + - it: binds that ClusterRole to the release's own ServiceAccount + documentSelector: + path: kind + value: ClusterRoleBinding + asserts: + - equal: + path: metadata.name + value: tenant-test-kubernetes-test-kcsi-binding + - equal: + path: roleRef.name + value: tenant-test-kubernetes-test-kcsi + - equal: + path: subjects + value: + - kind: ServiceAccount + name: kubernetes-test-kcsi + namespace: tenant-test + + - it: gives a same-named cluster in another tenant its own ClusterRole and binding + release: + name: kubernetes-test + namespace: tenant-other + asserts: + - equal: + path: metadata.name + value: tenant-other-kubernetes-test-kcsi + documentSelector: + path: kind + value: ClusterRole + - equal: + path: metadata.name + value: tenant-other-kubernetes-test-kcsi-binding + documentSelector: + path: kind + value: ClusterRoleBinding + - equal: + path: roleRef.name + value: tenant-other-kubernetes-test-kcsi + documentSelector: + path: kind + value: ClusterRoleBinding + - equal: + path: subjects + value: + - kind: ServiceAccount + name: kubernetes-test-kcsi + namespace: tenant-other + documentSelector: + path: kind + value: ClusterRoleBinding