Files
cozystack/packages/apps/vm-instance/tests/volume_group_hook_test.yaml
T
Mattia Eleuteri 3a5ae23797 feat(virtual-machine): group a VM's claims for Velero volume group snapshots
Velero snapshots the PVCs of an application-consistent VM backup one
after another between the guest freeze and thaw. Windows holds a VSS
freeze for 10 seconds at most, and on LINSTOR a VM with three volumes
(two disks and the persistent TPM/EFI state) outlasts it, so the thaw
fails and the backup is only crash-consistent. Velero 1.18 snapshots
the PVCs sharing a velero.io/volume-group value as one
VolumeGroupSnapshot.

Neither chart can carry that label onto the claims. CDI copies
DataVolume labels to a PVC only when it creates it, and a VMDisk
exists, PVC included, before any VM names it; KubeVirt creates the
persistent-state PVC with a fixed label set. A post-install and
post-upgrade hook of vm-instance labels them instead, and removes the
label from claims of this VM that no longer belong to the group.

Velero has no fallback for a labelled claim it cannot group: an unbound
member, a second CSI driver, or no single default
VolumeGroupSnapshotClass for the driver fails it. The group is formed
only when all of that holds and groupsnapshot v1beta2 is served, so
clusters without group snapshot support render nothing new.

The instancetype suite rendered every template, and the hook's PVC list
lookup needs a scheme it does not mock, so it is scoped to the two
templates it covers.

A vm-disk claim outlives the VM that grouped it, so a pre-delete hook
releases this VM's group on uninstall. Helm runs uninstall hooks from
the stored manifest, so that hook is rendered unconditionally and
selects the claims by this VM's label value when it runs.

Assisted-by: LLM
Signed-off-by: Mattia Eleuteri <mattia@hidora.io>
2026-11-23 11:55:31 +03:00

441 lines
17 KiB
YAML

suite: vm-instance labels its claims into one Velero volume group
# The hook is lookup-gated: the claims, their volumes and the
# VolumeGroupSnapshotClasses are mocked through the kubernetes provider, and
# groupsnapshot v1beta2 is advertised through capabilities. Velero fails a
# labelled claim it cannot group, so most cases pin what must stay unlabelled.
release:
name: vm-instance-win
namespace: tenant-test
templates:
- templates/volume-group-hook.yaml
set:
disks:
- name: win-system
- name: win-data
x-capabilities: &groupSnapshotServed
apiVersions:
- groupsnapshot.storage.k8s.io/v1beta2/VolumeGroupSnapshotClass
- groupsnapshot.storage.k8s.io/v1beta2/VolumeGroupSnapshot
- groupsnapshot.storage.k8s.io/v1beta2/VolumeGroupSnapshotContent
x-scheme: &scheme
"v1/PersistentVolumeClaim":
gvr: {group: "", version: v1, resource: persistentvolumeclaims}
namespaced: true
"v1/PersistentVolume":
gvr: {group: "", version: v1, resource: persistentvolumes}
namespaced: false
"groupsnapshot.storage.k8s.io/v1beta2/VolumeGroupSnapshotClass":
gvr: {group: groupsnapshot.storage.k8s.io, version: v1beta2, resource: volumegroupsnapshotclasses}
namespaced: false
x-system: &system
kind: PersistentVolumeClaim
apiVersion: v1
metadata: {name: vm-disk-win-system, namespace: tenant-test}
spec: {volumeName: pv-system}
status: {phase: Bound}
x-data: &data
kind: PersistentVolumeClaim
apiVersion: v1
metadata: {name: vm-disk-win-data, namespace: tenant-test}
spec: {volumeName: pv-data}
status: {phase: Bound}
x-state: &state
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: persistent-state-for-vm-instance-win-abcde
namespace: tenant-test
labels: {persistent-state-for: vm-instance-win}
spec: {volumeName: pv-state}
status: {phase: Bound}
x-pv-system: &pvSystem {kind: PersistentVolume, apiVersion: v1, metadata: {name: pv-system}, spec: {csi: {driver: linstor.csi.linbit.com}}}
x-pv-data: &pvData {kind: PersistentVolume, apiVersion: v1, metadata: {name: pv-data}, spec: {csi: {driver: linstor.csi.linbit.com}}}
x-pv-state: &pvState {kind: PersistentVolume, apiVersion: v1, metadata: {name: pv-state}, spec: {csi: {driver: linstor.csi.linbit.com}}}
x-default-class: &defaultClass
kind: VolumeGroupSnapshotClass
apiVersion: groupsnapshot.storage.k8s.io/v1beta2
metadata:
name: linstor-group-snapshots
labels: {velero.io/csi-volumegroupsnapshot-class: "true"}
driver: linstor.csi.linbit.com
tests:
- it: labels every disk and the persistent-state claim with the VM name
capabilities: *groupSnapshotServed
kubernetesProvider:
scheme: *scheme
objects: [*system, *data, *state, *pvSystem, *pvData, *pvState, *defaultClass]
asserts:
- hasDocuments:
count: 4
- matchRegex:
path: spec.template.spec.containers[0].args[0]
pattern: 'group vm-disk-win-system\n'
documentSelector: {path: kind, value: Job}
- matchRegex:
path: spec.template.spec.containers[0].args[0]
pattern: 'group vm-disk-win-data\n'
documentSelector: {path: kind, value: Job}
- matchRegex:
path: spec.template.spec.containers[0].args[0]
pattern: 'group persistent-state-for-vm-instance-win-abcde\n'
documentSelector: {path: kind, value: Job}
- equal:
path: rules[0].resourceNames
value: [vm-disk-win-system, vm-disk-win-data, persistent-state-for-vm-instance-win-abcde]
documentSelector: {path: kind, value: Role}
- it: renders nothing once every claim already carries the group
capabilities: *groupSnapshotServed
kubernetesProvider:
scheme: *scheme
objects:
- <<: *system
metadata: {name: vm-disk-win-system, namespace: tenant-test, labels: {velero.io/volume-group: vm-instance-win}}
- <<: *data
metadata: {name: vm-disk-win-data, namespace: tenant-test, labels: {velero.io/volume-group: vm-instance-win}}
- <<: *state
metadata:
name: persistent-state-for-vm-instance-win-abcde
namespace: tenant-test
labels: {persistent-state-for: vm-instance-win, velero.io/volume-group: vm-instance-win}
- *pvSystem
- *pvData
- *pvState
- *defaultClass
asserts:
- hasDocuments:
count: 0
- it: labels nothing while groupsnapshot v1beta2 is not served
kubernetesProvider:
scheme: *scheme
objects: [*system, *data, *state, *pvSystem, *pvData, *pvState, *defaultClass]
asserts:
- hasDocuments:
count: 0
- it: labels nothing while only the class kind of groupsnapshot v1beta2 is served
# linstor-csi keeps its group gate off unless the snapshot kinds are
# served too, and Velero 1.18 has no per-volume fallback.
capabilities:
apiVersions:
- groupsnapshot.storage.k8s.io/v1beta2/VolumeGroupSnapshotClass
kubernetesProvider:
scheme: *scheme
objects: [*system, *data, *state, *pvSystem, *pvData, *pvState, *defaultClass]
asserts:
- hasDocuments:
count: 0
- it: labels nothing while VolumeGroupSnapshot is not served
capabilities:
apiVersions:
- groupsnapshot.storage.k8s.io/v1beta2/VolumeGroupSnapshotClass
- groupsnapshot.storage.k8s.io/v1beta2/VolumeGroupSnapshotContent
kubernetesProvider:
scheme: *scheme
objects: [*system, *data, *state, *pvSystem, *pvData, *pvState, *defaultClass]
asserts:
- hasDocuments:
count: 0
- it: labels nothing while VolumeGroupSnapshotContent is not served
capabilities:
apiVersions:
- groupsnapshot.storage.k8s.io/v1beta2/VolumeGroupSnapshotClass
- groupsnapshot.storage.k8s.io/v1beta2/VolumeGroupSnapshot
kubernetesProvider:
scheme: *scheme
objects: [*system, *data, *state, *pvSystem, *pvData, *pvState, *defaultClass]
asserts:
- hasDocuments:
count: 0
- it: labels nothing without a default VolumeGroupSnapshotClass for the driver
capabilities: *groupSnapshotServed
kubernetesProvider:
scheme: *scheme
objects:
- *system
- *data
- *pvSystem
- *pvData
- *pvState
- <<: *defaultClass
metadata: {name: linstor-group-snapshots}
- <<: *defaultClass
metadata:
name: other-driver
labels: {velero.io/csi-volumegroupsnapshot-class: "true"}
driver: other.csi.example.com
asserts:
- hasDocuments:
count: 0
- it: labels nothing when two default classes serve the driver
capabilities: *groupSnapshotServed
kubernetesProvider:
scheme: *scheme
objects:
- *system
- *data
- *pvSystem
- *pvData
- *pvState
- *defaultClass
- <<: *defaultClass
metadata:
name: linstor-group-snapshots-2
labels: {velero.io/csi-volumegroupsnapshot-class: "true"}
asserts:
- hasDocuments:
count: 0
- it: labels nothing when the claims span two CSI drivers
capabilities: *groupSnapshotServed
kubernetesProvider:
scheme: *scheme
objects:
- *system
- *data
- {kind: PersistentVolume, apiVersion: v1, metadata: {name: pv-system}, spec: {csi: {driver: linstor.csi.linbit.com}}}
- {kind: PersistentVolume, apiVersion: v1, metadata: {name: pv-data}, spec: {csi: {driver: other.csi.example.com}}}
- *defaultClass
asserts:
- hasDocuments:
count: 0
- it: leaves an unbound claim out of the group
capabilities: *groupSnapshotServed
kubernetesProvider:
scheme: *scheme
objects:
- *system
- *data
- <<: *state
spec: {}
- *pvSystem
- *pvData
- *pvState
- *defaultClass
asserts:
- notMatchRegex:
path: spec.template.spec.containers[0].args[0]
pattern: 'persistent-state-for-vm-instance-win-abcde'
documentSelector: {path: kind, value: Job}
- equal:
path: rules[0].resourceNames
value: [vm-disk-win-system, vm-disk-win-data]
documentSelector: {path: kind, value: Role}
- it: leaves out a claim that names its volume but is not Bound yet
capabilities: *groupSnapshotServed
kubernetesProvider:
scheme: *scheme
objects:
- *system
- *data
- <<: *state
status: {phase: Pending}
- *pvSystem
- *pvData
- *pvState
- *defaultClass
asserts:
- equal:
path: rules[0].resourceNames
value: [vm-disk-win-system, vm-disk-win-data]
documentSelector: {path: kind, value: Role}
- it: forms no group from a single bound claim
set:
disks:
- name: win-system
capabilities: *groupSnapshotServed
kubernetesProvider:
scheme: *scheme
objects: [*system, *pvSystem, *pvData, *pvState, *defaultClass]
asserts:
- hasDocuments:
count: 0
- it: removes the label from a detached disk and the group that can no longer form
# win-data is no longer attached, so only win-system remains: a group of
# one is not formed, and both claims that carry this VM's group lose it.
# A claim grouped under another VM is not this release's to touch.
set:
disks:
- name: win-system
capabilities: *groupSnapshotServed
kubernetesProvider:
scheme: *scheme
objects:
- <<: *system
metadata: {name: vm-disk-win-system, namespace: tenant-test, labels: {velero.io/volume-group: vm-instance-win}}
- <<: *data
metadata: {name: vm-disk-win-data, namespace: tenant-test, labels: {velero.io/volume-group: vm-instance-win}}
- kind: PersistentVolumeClaim
apiVersion: v1
metadata: {name: vm-disk-other, namespace: tenant-test, labels: {velero.io/volume-group: vm-instance-other}}
spec: {volumeName: pv-data}
status: {phase: Bound}
- *pvSystem
- *pvData
- *pvState
- *defaultClass
asserts:
- matchRegex:
path: spec.template.spec.containers[0].args[0]
pattern: 'claim "" patch vm-disk-win-data --type=json -p ''\[\{"op":"test","path":"/metadata/labels/velero.io~1volume-group","value":"vm-instance-win"\},\{"op":"remove","path":"/metadata/labels/velero.io~1volume-group"\}\]''\n'
documentSelector: {path: kind, value: Job}
- matchRegex:
path: spec.template.spec.containers[0].args[0]
pattern: 'claim "" patch vm-disk-win-system --type=json -p ''\[\{"op":"test","path":"/metadata/labels/velero.io~1volume-group","value":"vm-instance-win"\},\{"op":"remove","path":"/metadata/labels/velero.io~1volume-group"\}\]''\n'
documentSelector: {path: kind, value: Job}
- notMatchRegex:
path: spec.template.spec.containers[0].args[0]
pattern: 'vm-disk-other'
documentSelector: {path: kind, value: Job}
- notMatchRegex:
path: spec.template.spec.containers[0].args[0]
pattern: '\ngroup [a-z]'
documentSelector: {path: kind, value: Job}
- it: leaves a claim grouped under another VM out of this VM's group
# Taking it over would split the other VM's group across two snapshots.
capabilities: *groupSnapshotServed
kubernetesProvider:
scheme: *scheme
objects:
- <<: *system
metadata: {name: vm-disk-win-system, namespace: tenant-test, labels: {velero.io/volume-group: vm-instance-other}}
- *data
- *state
- *pvSystem
- *pvData
- *pvState
- *defaultClass
asserts:
- equal:
path: rules[0].resourceNames
value: [vm-disk-win-data, persistent-state-for-vm-instance-win-abcde]
documentSelector: {path: kind, value: Role}
- it: groups the disks of a deleted VM once its uninstall released them
# A replacement VM under another name reattaches both disks of
# vm-instance-win, whose pre-delete hook removed their group label.
release:
name: vm-instance-replacement
capabilities: *groupSnapshotServed
kubernetesProvider:
scheme: *scheme
objects: [*system, *data, *pvSystem, *pvData, *defaultClass]
documentSelector: {path: kind, value: Job}
asserts:
- matchRegex:
path: spec.template.spec.containers[0].args[0]
pattern: 'group vm-disk-win-system\n'
- matchRegex:
path: spec.template.spec.containers[0].args[0]
pattern: 'group vm-disk-win-data\n'
- matchRegex:
path: spec.template.spec.containers[0].args[0]
pattern: 'velero\.io/volume-group=vm-instance-replacement '
- it: groups a claim whose group label is present but empty
capabilities: *groupSnapshotServed
kubernetesProvider:
scheme: *scheme
objects:
- <<: *system
metadata: {name: vm-disk-win-system, namespace: tenant-test, labels: {velero.io/volume-group: ""}}
- *data
- *pvSystem
- *pvData
- *defaultClass
documentSelector: {path: kind, value: Job}
asserts:
- matchRegex:
path: spec.template.spec.containers[0].args[0]
pattern: 'group vm-disk-win-system\n'
- matchRegex:
path: spec.template.spec.containers[0].args[0]
pattern: 'group vm-disk-win-data\n'
- it: skips a claim deleted between the render and the Job
capabilities: *groupSnapshotServed
kubernetesProvider:
scheme: *scheme
objects: [*system, *data, *pvSystem, *pvData, *defaultClass]
documentSelector: {path: kind, value: Job}
asserts:
- matchRegex:
path: spec.template.spec.containers[0].args[0]
pattern: 'if ! out=\$\(kubectl "\$verb" persistentvolumeclaims "\$name" "\$@" -n tenant-test 2>&1\); then\n\s+case "\$out" in\n\s+\*"\(NotFound\)"\*\) echo "\$out"; return 0 ;;'
- matchRegex:
path: spec.template.spec.containers[0].args[0]
pattern: 'state=\$\(kubectl get persistentvolumeclaims "\$name" -n tenant-test -o jsonpath=''\{\.metadata\.resourceVersion\}\{" "\}\{\.metadata\.labels\.velero\\\.io/volume-group\}'' 2>&1\)'
- matchRegex:
path: spec.template.spec.containers[0].args[0]
pattern: 'kubectl label persistentvolumeclaims "\$name" -n tenant-test --overwrite --resource-version="\$version" velero\.io/volume-group=vm-instance-win 2>&1'
- matchRegex:
path: spec.template.spec.containers[0].args[0]
pattern: '\*"\(Conflict\)"\*\) ;;'
- matchRegex:
path: spec.template.spec.containers[0].args[0]
pattern: 'have=\$\(kubectl get persistentvolumeclaims "\$name" -n tenant-test -o jsonpath=''\{\.metadata\.labels\.velero\\\.io/volume-group\}''\)\n\s+if \[ "\$have" != "\$want" \]; then'
- it: keeps the hook name and its label within 63 characters for a 53-character release
release:
name: vm-instance-a-virtual-machine-name-of-41-characters-x
capabilities: *groupSnapshotServed
kubernetesProvider:
scheme: *scheme
objects: [*system, *data, *pvSystem, *pvData, *defaultClass]
asserts:
- matchRegex:
path: metadata.name
pattern: '^vm-instance-a-virtual-machine-name-of-41-[0-9a-f]{8}-volume-group$'
- matchRegex:
path: spec.template.metadata.labels.app
pattern: '^.{1,63}$'
documentSelector: {path: kind, value: Job}
- equal:
path: spec.template.spec.serviceAccountName
value: vm-instance-a-virtual-machine-name-of-41-1bec036b-volume-group
documentSelector: {path: kind, value: Job}
- it: runs the Job under a restrictive non-root securityContext
capabilities: *groupSnapshotServed
kubernetesProvider:
scheme: *scheme
objects: [*system, *data, *pvSystem, *pvData, *pvState, *defaultClass]
documentSelector: {path: kind, value: Job}
asserts:
- equal:
path: spec.template.spec.securityContext.runAsNonRoot
value: true
- equal:
path: spec.template.spec.securityContext.runAsUser
value: 65534
- equal:
path: spec.template.spec.containers[0].securityContext.allowPrivilegeEscalation
value: false
- contains:
path: spec.template.spec.containers[0].securityContext.capabilities.drop
content: ALL
- equal:
path: spec.template.metadata.labels["policy.cozystack.io/allow-to-apiserver"]
value: "true"
- it: deletes the hook RBAC once the hooks succeed
capabilities: *groupSnapshotServed
kubernetesProvider:
scheme: *scheme
objects: [*system, *data, *pvSystem, *pvData, *defaultClass]
asserts:
- equal:
path: metadata.annotations["helm.sh/hook-delete-policy"]
value: hook-succeeded,before-hook-creation