From 2732519df767fa468d9b8ebb4a53acec2d8a6356 Mon Sep 17 00:00:00 2001 From: Ray Low Date: Mon, 23 Nov 2026 15:51:40 +0800 Subject: [PATCH] fix(kubernetes-nodes): label worker disks with the WorkloadMonitor selector The worker WorkloadMonitor selects PVCs by its selector labels, but a worker VM's disk PVC carried none of them, so it was never recorded as a Workload. CAPK stamps cluster-name and role on the VirtualMachine and its VMI template only and copies dataVolumeTemplates verbatim apart from a name prefix; CDI then builds the PVC from the DataVolume's labels. Putting the selector on the disk-system template is the only place the labels reach the PVC without teaching the shared controller to walk PVC -> DataVolume -> VirtualMachine owner chains. The disk must not carry cluster.x-k8s.io/cluster-name: the Kubernetes backup strategy selects DataVolumes and PVCs by it, so every worker root disk would be copied into each backup and deleted and restored by an in-place restore as cluster data. The WorkloadMonitor selector therefore drops cluster-name and keeps deployment-name and role; deployment-name already names a single MachineDeployment in the namespace, and the virt-launcher Pods the monitor counts still carry both labels. The KubevirtMachineTemplate is named by a hash of its content, so this renames it and every KubeVirt worker pool rolls once on upgrade. That is safe: the chart keeps any template a MachineSet still references, and the adoption migration matches templates by the 6-hex suffix pattern rather than by a fixed hash. Existing PVCs are not relabelled in place (CDI sets labels only at creation); the roll replaces them with labelled ones. Proxmox pools do not render this template and do not roll. Tested with helm unittest: cases assert the disk template carries exactly deployment-name and role and no cluster-name, and that the WorkloadMonitor selector is those two labels; snapshot and pinned template-name literals are updated for the new hash. Assisted-by: LLM Signed-off-by: Ray Low --- .../kubernetes-nodes/templates/nodegroup.yaml | 16 +++++++++- .../render_snapshot_test.yaml.snap | 24 ++++++++------ .../tests/nodegroup_test.yaml | 31 +++++++++++++++++-- .../tests/resources_cpu_test.yaml | 4 +-- 4 files changed, 61 insertions(+), 14 deletions(-) diff --git a/packages/apps/kubernetes-nodes/templates/nodegroup.yaml b/packages/apps/kubernetes-nodes/templates/nodegroup.yaml index 5bf3eaa51..85c8633bc 100644 --- a/packages/apps/kubernetes-nodes/templates/nodegroup.yaml +++ b/packages/apps/kubernetes-nodes/templates/nodegroup.yaml @@ -473,6 +473,17 @@ spec: {{- end }} - metadata: name: disk-system + {{- /* The worker WorkloadMonitor selects PVCs by these labels. CAPK + stamps cluster-name and role on the VM and its VMI template + only, copying dataVolumeTemplates verbatim, and CDI copies a + DataVolume's labels onto the PVC it creates -- so without them + here the disk is never tracked as a Workload. cluster-name must + NOT be added: the Kubernetes backup strategy selects DataVolumes + and PVCs by it, and would then back up and restore-in-place + every worker root disk as cluster data. */}} + labels: + cluster.x-k8s.io/deployment-name: {{ .clusterName }}-{{ .groupName }} + cluster.x-k8s.io/role: worker {{- /* usePopulator=false stays ONLY on the HTTP/import path: it preserves the legacy import behaviour and keeps an image-omitted DV — hence the KMT hash — byte-identical to the pre-feature render, so those workers @@ -1138,8 +1149,11 @@ spec: {{- end }} kind: kubernetes type: worker + {{- /* No cluster-name here: deployment-name already names one + MachineDeployment in the namespace, and requiring cluster-name would + force it onto the worker disk, which the Kubernetes backup strategy + would then select (see the disk-system labels above). */}} selector: - cluster.x-k8s.io/cluster-name: {{ $clusterName }} cluster.x-k8s.io/deployment-name: {{ $clusterName }}-{{ $groupName }} cluster.x-k8s.io/role: worker version: {{ $.Chart.Version }} diff --git a/packages/apps/kubernetes-nodes/tests/__snapshot__/render_snapshot_test.yaml.snap b/packages/apps/kubernetes-nodes/tests/__snapshot__/render_snapshot_test.yaml.snap index c8e990a9c..232cffa02 100644 --- a/packages/apps/kubernetes-nodes/tests/__snapshot__/render_snapshot_test.yaml.snap +++ b/packages/apps/kubernetes-nodes/tests/__snapshot__/render_snapshot_test.yaml.snap @@ -3,7 +3,7 @@ matches the golden render of a GPU pool: apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1 kind: KubevirtMachineTemplate metadata: - name: kubernetes-myk8s-md0-8ed0ab + name: kubernetes-myk8s-md0-26667b namespace: tenant-test spec: template: @@ -19,6 +19,9 @@ matches the golden render of a GPU pool: - metadata: annotations: cdi.kubevirt.io/storage.usePopulator: "false" + labels: + cluster.x-k8s.io/deployment-name: kubernetes-myk8s-md0 + cluster.x-k8s.io/role: worker name: disk-system spec: source: @@ -115,7 +118,7 @@ matches the golden render of a GPU pool: infrastructureRef: apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1 kind: KubevirtMachineTemplate - name: kubernetes-myk8s-md0-8ed0ab + name: kubernetes-myk8s-md0-26667b namespace: tenant-test version: v1.35.6 3: | @@ -149,7 +152,6 @@ matches the golden render of a GPU pool: kind: kubernetes minReplicas: 0 selector: - cluster.x-k8s.io/cluster-name: kubernetes-myk8s cluster.x-k8s.io/deployment-name: kubernetes-myk8s-md0 cluster.x-k8s.io/role: worker type: worker @@ -159,7 +161,7 @@ matches the golden render of a kubelet-override pool: apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1 kind: KubevirtMachineTemplate metadata: - name: kubernetes-myk8s-md0-2d362e + name: kubernetes-myk8s-md0-04d5d9 namespace: tenant-test spec: template: @@ -175,6 +177,9 @@ matches the golden render of a kubelet-override pool: - metadata: annotations: cdi.kubevirt.io/storage.usePopulator: "false" + labels: + cluster.x-k8s.io/deployment-name: kubernetes-myk8s-md0 + cluster.x-k8s.io/role: worker name: disk-system spec: source: @@ -268,7 +273,7 @@ matches the golden render of a kubelet-override pool: infrastructureRef: apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1 kind: KubevirtMachineTemplate - name: kubernetes-myk8s-md0-2d362e + name: kubernetes-myk8s-md0-04d5d9 namespace: tenant-test version: v1.35.6 3: | @@ -302,7 +307,6 @@ matches the golden render of a kubelet-override pool: kind: kubernetes minReplicas: 0 selector: - cluster.x-k8s.io/cluster-name: kubernetes-myk8s cluster.x-k8s.io/deployment-name: kubernetes-myk8s-md0 cluster.x-k8s.io/role: worker type: worker @@ -312,7 +316,7 @@ matches the golden render of the pool objects: apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1 kind: KubevirtMachineTemplate metadata: - name: kubernetes-myk8s-md0-a6ac3e + name: kubernetes-myk8s-md0-57ef01 namespace: tenant-test spec: template: @@ -329,6 +333,9 @@ matches the golden render of the pool objects: - metadata: annotations: cdi.kubevirt.io/storage.usePopulator: "false" + labels: + cluster.x-k8s.io/deployment-name: kubernetes-myk8s-md0 + cluster.x-k8s.io/role: worker name: disk-system spec: source: @@ -424,7 +431,7 @@ matches the golden render of the pool objects: infrastructureRef: apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1 kind: KubevirtMachineTemplate - name: kubernetes-myk8s-md0-a6ac3e + name: kubernetes-myk8s-md0-57ef01 namespace: tenant-test version: v1.35.6 3: | @@ -458,7 +465,6 @@ matches the golden render of the pool objects: kind: kubernetes minReplicas: 0 selector: - cluster.x-k8s.io/cluster-name: kubernetes-myk8s cluster.x-k8s.io/deployment-name: kubernetes-myk8s-md0 cluster.x-k8s.io/role: worker type: worker diff --git a/packages/apps/kubernetes-nodes/tests/nodegroup_test.yaml b/packages/apps/kubernetes-nodes/tests/nodegroup_test.yaml index e14bb480d..071f916f5 100644 --- a/packages/apps/kubernetes-nodes/tests/nodegroup_test.yaml +++ b/packages/apps/kubernetes-nodes/tests/nodegroup_test.yaml @@ -115,8 +115,35 @@ tests: path: spec.type value: worker - equal: - path: spec.selector["cluster.x-k8s.io/cluster-name"] - value: kubernetes-myk8s + path: spec.selector + value: + cluster.x-k8s.io/deployment-name: kubernetes-myk8s-md0 + cluster.x-k8s.io/role: worker + + # CDI copies the DataVolume's labels onto the PVC it creates, and CAPK does + # not label dataVolumeTemplates, so the worker disk only joins the + # WorkloadMonitor above if the template carries its whole selector. + - it: labels the worker disk with the WorkloadMonitor selector + documentSelector: + path: kind + value: KubevirtMachineTemplate + asserts: + - equal: + path: spec.template.spec.virtualMachineTemplate.spec.dataVolumeTemplates[0].metadata.labels + value: + cluster.x-k8s.io/deployment-name: kubernetes-myk8s-md0 + cluster.x-k8s.io/role: worker + + # The Kubernetes backup strategy selects DataVolumes and PVCs by + # cluster-name, so carrying it would back up every worker root disk and make + # an in-place restore delete and restore it as cluster data. + - it: keeps cluster-name off the worker disk + documentSelector: + path: kind + value: KubevirtMachineTemplate + asserts: + - notExists: + path: spec.template.spec.virtualMachineTemplate.spec.dataVolumeTemplates[0].metadata.labels["cluster.x-k8s.io/cluster-name"] --- suite: worker node pool validation templates: diff --git a/packages/apps/kubernetes-nodes/tests/resources_cpu_test.yaml b/packages/apps/kubernetes-nodes/tests/resources_cpu_test.yaml index 6bc9e31d7..b31ddd11d 100644 --- a/packages/apps/kubernetes-nodes/tests/resources_cpu_test.yaml +++ b/packages/apps/kubernetes-nodes/tests/resources_cpu_test.yaml @@ -246,7 +246,7 @@ tests: value: KubevirtMachineTemplate - equal: path: metadata.name - value: kubernetes-myk8s-md0-ce2335 + value: kubernetes-myk8s-md0-d5921f documentSelector: path: kind value: KubevirtMachineTemplate @@ -314,7 +314,7 @@ tests: asserts: - equal: path: metadata.name - value: kubernetes-myk8s-md0-ce2335 + value: kubernetes-myk8s-md0-d5921f # KubeVirt declares cores as uint32, so a count past its ceiling cannot size # the guest even though it is a whole number.