fix(kubernetes-nodes): label worker disks with the WorkloadMonitor selector (#5393)

## What this PR does

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.

### Downstream repositories

<!--
Cozystack is upstream for repositories that are NOT kept in sync with it
automatically, and nothing in CI compares the two sides, so a change
here can break them silently.

Which change forces what, and which file to touch, is the trigger map in
docs/agents/contributing.md under "Downstream Repositories". Walk it
before you tick anything.

IF YOU ARE AN AI AGENT OR AN LLM, THIS IS PART OF THE TASK, NOT A FORM
TO FILL IN:

1. Walk the trigger map against the actual diff of this PR, file by
file. Do not decide from the PR title or from your memory of what you
changed.
2. For every repository you tick, open the follow-up PR there and link
it on the same line. A ticked box with no link claims work that does not
exist, which is worse than an empty box. Search that repository for an
open PR or issue covering it first, and link that instead of filing a
duplicate.
3. When the follow-up is out of scope, or needs a decision that is not
yours to make, open an issue in that repository instead, link that, and
say so in the PR body. Do not push a speculative PR into another
repository just to fill a line here.
4. Do not tick "No downstream repository is affected" to make the
checklist go away. If you are unsure, leave every box empty and say so
in the PR body, so a human decides.
-->

- [x] No downstream repository is affected by this change
- [ ] [cozystack/website](https://github.com/cozystack/website) -
follow-up:
- [ ]
[cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack)
- follow-up:
- [ ]
[cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack)
- follow-up:
- [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up:
- [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up:
- [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) -
follow-up:
- [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) -
follow-up:
- [ ]
[cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server)
- follow-up:
- [ ]
[cozystack/external-apps-example](https://github.com/cozystack/external-apps-example)
- follow-up:
- [ ] [cozystack/examples](https://github.com/cozystack/examples) -
follow-up:
- [ ] [cozystack/community](https://github.com/cozystack/community) -
follow-up:

### Release note

```release-note
fix(kubernetes-nodes): Fixed worker node disks not showing up as workloads of their Kubernetes cluster. 
Worker disk PVCs now carry the `cluster.x-k8s.io/deployment-name` and `cluster.x-k8s.io/role` labels that the worker pool's WorkloadMonitor selects on.
Upgrading replaces every worker VM in every KubeVirt-backed node pool once. 
A pool can bring up as many replacement VMs at the same time as its `maxReplicas`, 
so the management cluster needs room for the extra VMs during the rollout. 
An old node is drained only once its replacement has joined, except in pools with passed-through GPUs, 
where one old node at a time can be drained while its replacement starts. 
Pods on the workers are evicted and rescheduled, and anything stored only on a worker's local disk is lost.
Proxmox-backed pools do not restart.
```


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

## Summary

* **Bug Fixes**
* Workload monitoring can now match worker-node storage volumes using
deployment and worker-role labels, without requiring a cluster-name
label.
* Corrected the expected machine-template name for configurations that
specify CPU cores with leading zeros.
* **Tests**
* Updated coverage to verify storage-volume labels and leading-zero CPU
rendering.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Aleksei Sviridkin authored and GitHub committed 2026-11-23 17:32:04 +03:00
commit ff7c2afdbd
4 files changed
+61 -14

No files matched your search

@@ -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 }}
@@ -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
@@ -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:
@@ -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.