mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-30 04:06:34 +09:00
feat(virtual-machine): group a VM's claims for Velero volume group snapshots (#5240)
<!-- Thank you for making a contribution! Here are some tips for you: - Use Conventional Commits for the PR title: `type(scope): description` - Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore - Scopes are not an exhaustive list — pick the most specific scope for the change and extend the list when a genuinely new area appears. Examples: - System components: dashboard, platform, operator, cilium, kube-ovn, linstor, fluxcd, cluster-api - Managed apps: postgres, mariadb, redis, kafka, clickhouse, virtual-machine, kubernetes - Development and maintenance: api, hack, tests, ci, docs, maintenance - Breaking changes: append `!` after type/scope (`feat(api)!: ...`) or add a `BREAKING CHANGE:` footer - If it's a work in progress, consider creating this PR as a draft. - Don't hesistate to ask for opinion and review in the community chats, even if it's still a draft. - Add the label `kind/backport` if it's a bugfix that needs to be backported to a previous version. --> ## What this PR does An application-consistent Velero backup of a VM freezes the guest, snapshots its PVCs one after another, then thaws it. Windows holds a VSS freeze for 10 seconds at most, and on LINSTOR each CSI snapshot takes about 4 seconds. So a Windows VM with three volumes (system disk, data disk and the `persistent-state-for-*` claim KubeVirt keeps for TPM/EFI state) overruns the window: `virt-freezer --unfreeze` fails with `VSS_E_HOLD_WRITES_TIMEOUT` and the backup is only crash-consistent. Velero 1.18 snapshots the PVCs that share a `velero.io/volume-group` value as one VolumeGroupSnapshot, which linstor-csi takes as a single atomic snapshot. This adds a `post-install,post-upgrade` hook to `vm-instance` that labels every claim of the VM with `velero.io/volume-group: <VirtualMachine name>`. That covers the `vm-disk-<name>` claim of each disk in `disks`, and the claims KubeVirt labels `persistent-state-for: <VirtualMachine name>`. The hook also removes the label from claims still carrying this VM's group once they no longer belong to it (a detached disk, or a group that can no longer form). A claim grouped under another VM is left alone. A `vm-disk-<name>` claim belongs to the `vm-disk` release and outlives the VM, and the hook leaves out a claim grouped under another VM's name, so a disk reattached to a replacement VM would otherwise keep its old group for good. A `pre-delete` hook therefore removes this VM's group from every claim still carrying it when the release is uninstalled. Helm runs uninstall hooks from the stored manifest, so that hook is rendered unconditionally, and it selects the claims at run time with `-l velero.io/volume-group=<VM>` rather than from a render-time list that would be stale by then. Its Role needs `list` on the namespace's claims for that; the removal is the same JSON `test`+`remove` patch, so a claim another VM grouped in the meantime keeps its group. Why a hook and not a label in the charts' own manifests: - CDI copies DataVolume labels to the PVC only when it creates it (`newPersistentVolumeClaim` in `pkg/controller/datavolume/controller-base.go`, v1.66.1), so a label added to an existing VMDisk's DataVolume never reaches its claim. - A VMDisk is created, and its PVC with it, before any VMInstance names it, so `vm-disk` cannot know the VM at that point; the disk-to-VM mapping only exists in `vm-instance` values. `vm-disk` is therefore unchanged. - KubeVirt creates the persistent-state PVC with a fixed label set (`createPVC` in `pkg/storage/backend-storage/backend-storage.go`, v1.9.0); nothing on the VM or VMI spec adds labels to it. Velero 1.18.1 has no fallback for a labelled claim it cannot group (`getVolumeSnapshotReference` in `pkg/backup/actions/csi/pvc_action.go`). Any of these fails the claim's backup: an unbound member, two CSI drivers in the group, or anything other than exactly one VolumeGroupSnapshotClass labelled `velero.io/csi-volumegroupsnapshot-class: "true"` for the driver. The hook therefore forms the group only when all of these hold, and removes the label otherwise: - the claims are bound and use a single CSI driver; - at least two of them exist; - `groupsnapshot.storage.k8s.io/v1beta2` is served; - exactly one default class serves that driver. On a cluster without group snapshot support the hook does not render. That is every cluster on current `main`, which runs snapshot-controller v8.0.1 with v1alpha1 CRDs. A companion change upgrades snapshot-controller to v8.7.0, enables the `CSIVolumeGroupSnapshot` gate on linstor-csi and adds a default VolumeGroupSnapshotClass. This PR does nothing until something like it lands. The hook is gated on render-time lookups like `vm-update-hook.yaml`, so a converged VM renders no Job, and its Role is limited by `resourceNames` to the claims it patches. Existing VMs are picked up on their next Helm upgrade, which a Cozystack upgrade that bumps the chart triggers. Known limit: the hook labels only the claims the render saw. A persistent-state claim created after it stays unlabelled until the next upgrade. That happens on the first boot of a new VM, and on every live migration, where KubeVirt creates a new RWO claim. Such a claim is snapshotted on its own, next to one group snapshot of the disks: two snapshot operations, which still fit the freeze window, as two-volume VMs show today. Closing that gap needs something that sees every PVC creation. The lineage webhook is a candidate: it already mutates PVCs on create and walks the owner graph to the VMInstance. That is left for a follow-up. `tests/instancetype_resources_test.yaml` rendered every template, and the fake client of helm-unittest refuses a list lookup for a kind it does not register. That suite is therefore scoped to `vm.yaml` and `vm-update-hook.yaml`, the two templates it covers. `tests/volume_group_hook_test.yaml` covers labelling, convergence, label removal, the Job's security context and each gate: API not served, no default class, two default classes, two drivers, an unbound claim, a single claim. Each gate was checked to fail its test when removed. `tests/volume_group_release_hook_test.yaml` pins the uninstall hook's manifest, and `hack/vm-volume-group-release-hook.bats` runs its script against a fake kubectl: every claim the VM grouped is released, a detached disk included, another VM's claim is untouched, a claim regrouped or deleted between the list and the patch does not fail the uninstall, and a failed list or a patch that leaves the label in place does. `tests/volume_group_hook_test.yaml` also covers a differently named VM grouping the released disks. ### Screenshots <!-- REQUIRED for UI changes: attach screenshots or screen recordings demonstrating the visual impact of your changes. PRs with UI changes without screenshots will not be merged. --> ### 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 <!-- Write a release note: - Explain what has changed internally and for users. - Start with the same `type(scope):` prefix as in the PR title - Follow the guidelines at https://github.com/kubernetes/community/blob/master/contributors/guide/release-notes.md. --> ```release-note feat(virtual-machine): label every PVC of a VMInstance with velero.io/volume-group, so Velero snapshots them as one VolumeGroupSnapshot when the cluster serves groupsnapshot v1beta2 with a default VolumeGroupSnapshotClass for their CSI driver. ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **New Features** * VM disk and persistent-state volumes can now be grouped for snapshots when the storage provider and snapshot configuration support it. * Volume grouping is updated automatically as claims change; ineligible volumes are excluded, and claims assigned to another VM are left unchanged. * Grouping is applied only when multiple bound volumes share a supported storage driver and a matching default snapshot class is available. * Volumes no longer eligible for the VM’s group are ungrouped, and missing volumes are safely skipped. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
6 files changed
+1121
No files matched your search
@@ -0,0 +1,241 @@
|
||||
#!/usr/bin/env bats
|
||||
# Behavioural tests for the pre-delete hook that releases a VM's Velero volume
|
||||
# group, packages/apps/vm-instance/templates/volume-group-release-hook.yaml.
|
||||
#
|
||||
# A vm-disk-<name> claim belongs to the vm-disk release and outlives the VM.
|
||||
# Every VM leaves out a claim grouped under another VM's name, so a label left
|
||||
# behind by a deleted VM keeps the disk out of any replacement VM's group for
|
||||
# good. The script is extracted from the rendered chart and RUN against a fake
|
||||
# kubectl holding claim labels on disk, because what matters is the labels it
|
||||
# leaves and its exit code; the helm-unittest suite in
|
||||
# packages/apps/vm-instance/tests/ matches the source text instead.
|
||||
#
|
||||
# Run from the repo root through `make bats-unit-tests`. Each @test builds and
|
||||
# removes its fixture inline, and there is no EXIT trap: see
|
||||
# docs/agents/e2e-testing.md.
|
||||
|
||||
load test_helper
|
||||
|
||||
VM_CHART=packages/apps/vm-instance
|
||||
|
||||
# render_release_hook <release> <out>
|
||||
# The chart's vm.yaml looks up the default instance type and profile, which a
|
||||
# render with no cluster cannot find, so the VM is given explicit resources.
|
||||
render_release_hook() {
|
||||
release=$1 out=$2
|
||||
helm template "$release" "$VM_CHART" --namespace tenant-test \
|
||||
--set instanceType= --set instanceProfile= \
|
||||
--set resources.cpu=1 --set resources.sockets=1 --set resources.memory=1Gi \
|
||||
--set-json '_cluster={"scheduling":{}}' \
|
||||
--show-only templates/volume-group-release-hook.yaml 2>/dev/null \
|
||||
| yq eval 'select(.kind == "Job") | .spec.template.spec.containers[0].args[0]' - > "$out"
|
||||
[ -s "$out" ] || return 1
|
||||
}
|
||||
|
||||
# write_fake_kubectl <dir>
|
||||
# Each file in $CLAIMS is a claim; its content is the value of its
|
||||
# velero.io/volume-group label, an empty file a claim without one. The fake
|
||||
# serves the three calls the hook makes:
|
||||
# * a label-selector list of the claims carrying a value, or a failure when
|
||||
# $FAIL_LIST is set;
|
||||
# * a JSON test+remove patch, which fails with the API server's Invalid
|
||||
# answer when the value differs, as a JSON patch test does, and NotFound
|
||||
# when the claim is gone. $BEFORE_PATCH runs first, to model another writer
|
||||
# between the list and the patch; $FAIL_PATCH names a claim whose patch
|
||||
# fails for a reason unrelated to its label;
|
||||
# * a read of one claim's label.
|
||||
write_fake_kubectl() {
|
||||
cat > "$1/kubectl" <<'KEOF'
|
||||
#!/bin/sh
|
||||
echo "$*" >> "$KLOG"
|
||||
verb=$1 target=$2
|
||||
case "$verb:$target" in
|
||||
get:persistentvolumeclaims)
|
||||
if [ -n "${FAIL_LIST:-}" ]; then
|
||||
echo "error: the server could not list persistentvolumeclaims" >&2
|
||||
exit 1
|
||||
fi
|
||||
want=${6#*=}
|
||||
for f in "$CLAIMS"/*; do
|
||||
[ -e "$f" ] || continue
|
||||
if [ "$(cat "$f")" = "$want" ]; then echo "persistentvolumeclaim/${f##*/}"; fi
|
||||
done
|
||||
exit 0
|
||||
;;
|
||||
esac
|
||||
name=${target#persistentvolumeclaim/}
|
||||
if [ "$verb" = patch ] && [ -n "${BEFORE_PATCH:-}" ]; then
|
||||
sh -c "$BEFORE_PATCH"
|
||||
fi
|
||||
if [ ! -e "$CLAIMS/$name" ]; then
|
||||
echo "Error from server (NotFound): persistentvolumeclaims \"$name\" not found" >&2
|
||||
exit 1
|
||||
fi
|
||||
case "$verb" in
|
||||
get)
|
||||
cat "$CLAIMS/$name"
|
||||
;;
|
||||
patch)
|
||||
if [ "$name" = "${FAIL_PATCH:-}" ]; then
|
||||
echo "Error from server (InternalError): etcdserver: request timed out" >&2
|
||||
exit 1
|
||||
fi
|
||||
expected=$(printf '%s' "$7" | sed -n 's/.*"value":"\([^"]*\)".*/\1/p')
|
||||
if [ "$(cat "$CLAIMS/$name")" != "$expected" ]; then
|
||||
echo "The request is invalid: the server rejected our request due to an error in our request" >&2
|
||||
exit 1
|
||||
fi
|
||||
: > "$CLAIMS/$name"
|
||||
;;
|
||||
*)
|
||||
echo "fake kubectl: unexpected call $*" >&2
|
||||
exit 64
|
||||
;;
|
||||
esac
|
||||
KEOF
|
||||
chmod 0755 "$1/kubectl"
|
||||
}
|
||||
|
||||
@test "a render that produces no script fails instead of passing as a no-op" {
|
||||
tmp=$(mktemp -d)
|
||||
if VM_CHART="$tmp/no-such-chart" render_release_hook vm-instance-old "$tmp/hook.sh"; then
|
||||
echo "FAIL: render_release_hook reported success with no chart to render"
|
||||
false
|
||||
fi
|
||||
rm -rf "$tmp"
|
||||
}
|
||||
|
||||
@test "uninstall releases every claim the VM grouped, and a differently named VM can group them again" {
|
||||
# vm-disk-old-data was detached before the uninstall, with no upgrade since,
|
||||
# so no render-time list of the VM's disks would still name it.
|
||||
tmp=$(mktemp -d)
|
||||
write_fake_kubectl "$tmp"
|
||||
render_release_hook vm-instance-old "$tmp/hook.sh"
|
||||
export KLOG="$tmp/calls" CLAIMS="$tmp/claims"
|
||||
mkdir "$CLAIMS"
|
||||
printf 'vm-instance-old' > "$CLAIMS/vm-disk-old-system"
|
||||
printf 'vm-instance-old' > "$CLAIMS/vm-disk-old-data"
|
||||
printf 'vm-instance-old' > "$CLAIMS/persistent-state-for-vm-instance-old-abcde"
|
||||
printf 'vm-instance-other' > "$CLAIMS/vm-disk-other"
|
||||
: > "$CLAIMS/vm-disk-loose"
|
||||
|
||||
rc=0
|
||||
PATH="$tmp:$PATH" sh -e "$tmp/hook.sh" > "$tmp/out" 2> "$tmp/err" || rc=$?
|
||||
if [ "$rc" -ne 0 ]; then
|
||||
echo "FAIL: hook exited $rc when every patch succeeded"
|
||||
cat "$tmp/out" "$tmp/err"
|
||||
false
|
||||
fi
|
||||
for claim in vm-disk-old-system vm-disk-old-data persistent-state-for-vm-instance-old-abcde; do
|
||||
if [ -s "$CLAIMS/$claim" ]; then
|
||||
echo "FAIL: $claim still carries the group $(cat "$CLAIMS/$claim")"
|
||||
false
|
||||
fi
|
||||
done
|
||||
if [ "$(cat "$CLAIMS/vm-disk-other")" != vm-instance-other ]; then
|
||||
echo "FAIL: another VM's group was removed"
|
||||
cat "$tmp/calls"
|
||||
false
|
||||
fi
|
||||
if grep -q 'vm-disk-other\|vm-disk-loose' "$tmp/calls"; then
|
||||
echo "FAIL: the hook touched a claim this VM never grouped"
|
||||
cat "$tmp/calls"
|
||||
false
|
||||
fi
|
||||
# The post-install hook of a replacement VM groups a claim whose label is
|
||||
# absent or empty and leaves out one grouped under another name; the
|
||||
# helm-unittest suite pins that side.
|
||||
rm -rf "$tmp"
|
||||
}
|
||||
|
||||
@test "a claim another VM grouped between the list and the patch keeps that group" {
|
||||
tmp=$(mktemp -d)
|
||||
write_fake_kubectl "$tmp"
|
||||
render_release_hook vm-instance-old "$tmp/hook.sh"
|
||||
export KLOG="$tmp/calls" CLAIMS="$tmp/claims"
|
||||
mkdir "$CLAIMS"
|
||||
printf 'vm-instance-old' > "$CLAIMS/vm-disk-a"
|
||||
printf 'vm-instance-old' > "$CLAIMS/vm-disk-b"
|
||||
export BEFORE_PATCH="printf vm-instance-new > '$CLAIMS/vm-disk-a'"
|
||||
|
||||
rc=0
|
||||
PATH="$tmp:$PATH" sh -e "$tmp/hook.sh" > "$tmp/out" 2> "$tmp/err" || rc=$?
|
||||
if [ "$rc" -ne 0 ]; then
|
||||
echo "FAIL: hook exited $rc for a claim that is no longer this VM's"
|
||||
cat "$tmp/out" "$tmp/err"
|
||||
false
|
||||
fi
|
||||
if [ "$(cat "$CLAIMS/vm-disk-a")" != vm-instance-new ]; then
|
||||
echo "FAIL: the newer group was removed"
|
||||
false
|
||||
fi
|
||||
rm -rf "$tmp"
|
||||
}
|
||||
|
||||
@test "a claim deleted between the list and the patch does not fail the uninstall" {
|
||||
tmp=$(mktemp -d)
|
||||
write_fake_kubectl "$tmp"
|
||||
render_release_hook vm-instance-old "$tmp/hook.sh"
|
||||
export KLOG="$tmp/calls" CLAIMS="$tmp/claims"
|
||||
mkdir "$CLAIMS"
|
||||
printf 'vm-instance-old' > "$CLAIMS/vm-disk-a"
|
||||
printf 'vm-instance-old' > "$CLAIMS/vm-disk-b"
|
||||
export BEFORE_PATCH="rm -f '$CLAIMS/vm-disk-a'"
|
||||
|
||||
rc=0
|
||||
PATH="$tmp:$PATH" sh -e "$tmp/hook.sh" > "$tmp/out" 2> "$tmp/err" || rc=$?
|
||||
if [ "$rc" -ne 0 ]; then
|
||||
echo "FAIL: hook exited $rc for a claim that no longer exists"
|
||||
cat "$tmp/out" "$tmp/err"
|
||||
false
|
||||
fi
|
||||
if [ -s "$CLAIMS/vm-disk-b" ]; then
|
||||
echo "FAIL: the claim after the deleted one kept its group"
|
||||
false
|
||||
fi
|
||||
rm -rf "$tmp"
|
||||
}
|
||||
|
||||
@test "a claim that still carries the group after a failed patch fails the uninstall" {
|
||||
# Succeeding here would leave a label no release ever removes.
|
||||
tmp=$(mktemp -d)
|
||||
write_fake_kubectl "$tmp"
|
||||
render_release_hook vm-instance-old "$tmp/hook.sh"
|
||||
export KLOG="$tmp/calls" CLAIMS="$tmp/claims"
|
||||
mkdir "$CLAIMS"
|
||||
printf 'vm-instance-old' > "$CLAIMS/vm-disk-a"
|
||||
export FAIL_PATCH=vm-disk-a
|
||||
|
||||
rc=0
|
||||
PATH="$tmp:$PATH" sh -e "$tmp/hook.sh" > "$tmp/out" 2> "$tmp/err" || rc=$?
|
||||
if [ "$rc" -eq 0 ]; then
|
||||
echo "FAIL: hook exited 0 with the group still on vm-disk-a"
|
||||
cat "$tmp/out" "$tmp/err"
|
||||
false
|
||||
fi
|
||||
if ! grep -q 'request timed out' "$tmp/err"; then
|
||||
echo "FAIL: the patch error was not reported"
|
||||
cat "$tmp/err"
|
||||
false
|
||||
fi
|
||||
rm -rf "$tmp"
|
||||
}
|
||||
|
||||
@test "a failed list fails the uninstall instead of releasing nothing" {
|
||||
tmp=$(mktemp -d)
|
||||
write_fake_kubectl "$tmp"
|
||||
render_release_hook vm-instance-old "$tmp/hook.sh"
|
||||
export KLOG="$tmp/calls" CLAIMS="$tmp/claims"
|
||||
mkdir "$CLAIMS"
|
||||
printf 'vm-instance-old' > "$CLAIMS/vm-disk-a"
|
||||
export FAIL_LIST=1
|
||||
|
||||
rc=0
|
||||
PATH="$tmp:$PATH" sh -e "$tmp/hook.sh" > "$tmp/out" 2> "$tmp/err" || rc=$?
|
||||
if [ "$rc" -eq 0 ]; then
|
||||
echo "FAIL: hook exited 0 although the claims could not be listed"
|
||||
cat "$tmp/out" "$tmp/err"
|
||||
false
|
||||
fi
|
||||
rm -rf "$tmp"
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
{{- /* Velero snapshots the PVCs that share a velero.io/volume-group value as
|
||||
one VolumeGroupSnapshot, so a VM whose disks are frozen together for an
|
||||
application-consistent backup is thawed after a single snapshot instead
|
||||
of one per volume; Windows holds a VSS freeze for 10 seconds at most.
|
||||
|
||||
Nothing this chart renders can carry that label onto the claims: CDI
|
||||
copies DataVolume labels to the PVC only when it creates it, and a
|
||||
VMDisk exists, PVC included, before any VM names it; KubeVirt creates
|
||||
the persistent-state-for PVC (TPM, persistent EFI) with a fixed label
|
||||
set. A hook labels the claims instead, and only the ones the render saw,
|
||||
so a state PVC created after it (first boot, a migration onto a new RWO
|
||||
claim) waits for the next upgrade and is snapshotted on its own.
|
||||
|
||||
Velero 1.18 has no fallback for a labelled claim: an unbound member, a
|
||||
second CSI driver in the group, or no default VolumeGroupSnapshotClass
|
||||
for the driver fails it. The group is therefore formed only from bound
|
||||
claims of a single driver that exactly one default class serves, and the
|
||||
label is removed from every claim of this VM otherwise.
|
||||
|
||||
A claim already grouped under another VM is left out rather than taken
|
||||
over: relabelling it would split that VM's group. A disk detached from
|
||||
a VM that still exists is released when that VM is next upgraded; a
|
||||
VM's uninstall releases every claim it still groups, through the
|
||||
pre-delete hook in volume-group-release-hook.yaml. */}}
|
||||
{{- $vmName := include "virtual-machine.fullname" . -}}
|
||||
{{- $namespace := .Release.Namespace -}}
|
||||
{{- $groupKey := "velero.io/volume-group" -}}
|
||||
{{- $path := printf "/metadata/labels/%s" ($groupKey | replace "/" "~1") -}}
|
||||
{{- /* Job names and label values stop at 63 characters, and a release name
|
||||
may take 53 of them; a hash keeps two long names that share a prefix
|
||||
apart. */}}
|
||||
{{- $hookName := printf "%s-volume-group" .Release.Name -}}
|
||||
{{- if gt (len $hookName) 63 -}}
|
||||
{{- $hookName = printf "%s-%s-volume-group" (.Release.Name | trunc 41 | trimSuffix "-") (sha256sum .Release.Name | trunc 8) -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- $claims := dict -}}
|
||||
{{- range (lookup "v1" "PersistentVolumeClaim" $namespace "").items | default list -}}
|
||||
{{- $_ := set $claims .metadata.name . -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- $candidates := list -}}
|
||||
{{- range .Values.disks -}}
|
||||
{{- with get $claims (printf "vm-disk-%s" .name) -}}
|
||||
{{- $candidates = append $candidates . -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- range $claims -}}
|
||||
{{- if eq (dig "metadata" "labels" "persistent-state-for" "" . | toString) $vmName -}}
|
||||
{{- $candidates = append $candidates . -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- $members := list -}}
|
||||
{{- range $candidates -}}
|
||||
{{- if has (dig "metadata" "labels" $groupKey "" . | toString) (list "" $vmName) -}}
|
||||
{{- $members = append $members . -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- $drivers := dict -}}
|
||||
{{- $bound := list -}}
|
||||
{{- range $claim := $members -}}
|
||||
{{- with and (eq (dig "status" "phase" "" $claim | toString) "Bound") (dig "spec" "volumeName" "" $claim) -}}
|
||||
{{- with dig "spec" "csi" "driver" "" (lookup "v1" "PersistentVolume" "" . | default dict) -}}
|
||||
{{- $_ := set $drivers (toString .) true -}}
|
||||
{{- $bound = append $bound $claim.metadata.name -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- $desired := list -}}
|
||||
{{- $groupAPI := true -}}
|
||||
{{- range list "VolumeGroupSnapshotClass" "VolumeGroupSnapshot" "VolumeGroupSnapshotContent" -}}
|
||||
{{- if not ($.Capabilities.APIVersions.Has (printf "groupsnapshot.storage.k8s.io/v1beta2/%s" .)) -}}
|
||||
{{- $groupAPI = false -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- if and (eq (len $drivers) 1) (ge (len $bound) 2) $groupAPI -}}
|
||||
{{- $driver := first (keys $drivers) -}}
|
||||
{{- $defaultClasses := 0 -}}
|
||||
{{- range (lookup "groupsnapshot.storage.k8s.io/v1beta2" "VolumeGroupSnapshotClass" "" "").items | default list -}}
|
||||
{{- if and (eq (.driver | toString) $driver) (eq (dig "metadata" "labels" "velero.io/csi-volumegroupsnapshot-class" "" . | toString) "true") -}}
|
||||
{{- $defaultClasses = add1 $defaultClasses -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- if eq $defaultClasses 1 -}}
|
||||
{{- $desired = $bound -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- $toLabel := list -}}
|
||||
{{- range $desired -}}
|
||||
{{- if ne (dig "metadata" "labels" $groupKey "" (get $claims .) | toString) $vmName -}}
|
||||
{{- $toLabel = append $toLabel . -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- $toUnlabel := list -}}
|
||||
{{- range $name, $claim := $claims -}}
|
||||
{{- if and (eq (dig "metadata" "labels" $groupKey "" $claim | toString) $vmName) (not (has $name $desired)) -}}
|
||||
{{- $toUnlabel = append $toUnlabel $name -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- if or $toLabel $toUnlabel }}
|
||||
apiVersion: batch/v1
|
||||
kind: Job
|
||||
metadata:
|
||||
name: {{ $hookName }}
|
||||
annotations:
|
||||
helm.sh/hook: post-install,post-upgrade
|
||||
helm.sh/hook-weight: "0"
|
||||
helm.sh/hook-delete-policy: hook-succeeded,before-hook-creation
|
||||
spec:
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: {{ $hookName }}
|
||||
policy.cozystack.io/allow-to-apiserver: "true"
|
||||
spec:
|
||||
serviceAccountName: {{ $hookName }}
|
||||
restartPolicy: Never
|
||||
securityContext:
|
||||
runAsNonRoot: true
|
||||
runAsUser: 65534
|
||||
seccompProfile:
|
||||
type: RuntimeDefault
|
||||
containers:
|
||||
- name: label-claims
|
||||
image: docker.io/alpine/k8s:1.36.2
|
||||
securityContext:
|
||||
allowPrivilegeEscalation: false
|
||||
capabilities:
|
||||
drop: ["ALL"]
|
||||
resources:
|
||||
requests:
|
||||
memory: "16Mi"
|
||||
cpu: "10m"
|
||||
limits:
|
||||
memory: "128Mi"
|
||||
cpu: "100m"
|
||||
command: ["sh", "-exc"]
|
||||
{{- /* A claim deleted since the render is skipped, not fatal: the rest
|
||||
of the group stays valid for Velero without it. Any other
|
||||
failure still fails the Job.
|
||||
|
||||
Neither write may take a claim over, or drop a group, that
|
||||
another VM set since the render. kubectl label reads, then
|
||||
sends a merge patch carrying only the new value, so a group
|
||||
set in between would be replaced. group therefore reads the
|
||||
resourceVersion with the value and writes with
|
||||
--resource-version: the API server answers Conflict if the
|
||||
claim changed, and the claim is read again. The removal is a
|
||||
JSON patch that tests for this VM's value first.
|
||||
|
||||
A claim that already carries the value this VM wants is done,
|
||||
which is what a retried Pod finds for every change its
|
||||
predecessor made. */}}
|
||||
args:
|
||||
- |
|
||||
group() {
|
||||
name=$1
|
||||
for attempt in 1 2 3 4 5; do
|
||||
if ! state=$(kubectl get persistentvolumeclaims "$name" -n {{ $namespace }} -o jsonpath='{.metadata.resourceVersion}{" "}{.metadata.labels.{{ $groupKey | replace "." "\\." }}}' 2>&1); then
|
||||
case "$state" in
|
||||
*"(NotFound)"*) echo "$state"; return 0 ;;
|
||||
esac
|
||||
echo "$state" >&2
|
||||
return 1
|
||||
fi
|
||||
version=${state%% *}
|
||||
have=${state#* }
|
||||
if [ "$have" = {{ $vmName }} ]; then
|
||||
return 0
|
||||
fi
|
||||
if [ -n "$have" ]; then
|
||||
echo "$name is grouped under $have" >&2
|
||||
return 1
|
||||
fi
|
||||
if out=$(kubectl label persistentvolumeclaims "$name" -n {{ $namespace }} --overwrite --resource-version="$version" {{ $groupKey }}={{ $vmName }} 2>&1); then
|
||||
return 0
|
||||
fi
|
||||
case "$out" in
|
||||
*"(NotFound)"*) echo "$out"; return 0 ;;
|
||||
*"(Conflict)"*) ;;
|
||||
*) echo "$out" >&2; return 1 ;;
|
||||
esac
|
||||
done
|
||||
echo "$name kept changing" >&2
|
||||
return 1
|
||||
}
|
||||
claim() {
|
||||
want=$1 verb=$2 name=$3
|
||||
shift 3
|
||||
if ! out=$(kubectl "$verb" persistentvolumeclaims "$name" "$@" -n {{ $namespace }} 2>&1); then
|
||||
case "$out" in
|
||||
*"(NotFound)"*) echo "$out"; return 0 ;;
|
||||
esac
|
||||
have=$(kubectl get persistentvolumeclaims "$name" -n {{ $namespace }} -o jsonpath='{.metadata.labels.{{ $groupKey | replace "." "\\." }}}')
|
||||
if [ "$have" != "$want" ]; then
|
||||
echo "$out" >&2
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
}
|
||||
{{- range $toLabel }}
|
||||
group {{ . }}
|
||||
{{- end }}
|
||||
{{- range $toUnlabel }}
|
||||
claim "" patch {{ . }} --type=json -p '{{ list (dict "op" "test" "path" $path "value" $vmName) (dict "op" "remove" "path" $path) | toJson }}'
|
||||
{{- end }}
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: ServiceAccount
|
||||
metadata:
|
||||
name: {{ $hookName }}
|
||||
annotations:
|
||||
helm.sh/hook: post-install,post-upgrade
|
||||
helm.sh/hook-weight: "-5"
|
||||
helm.sh/hook-delete-policy: hook-succeeded,before-hook-creation
|
||||
---
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: Role
|
||||
metadata:
|
||||
name: {{ $hookName }}
|
||||
annotations:
|
||||
helm.sh/hook: post-install,post-upgrade
|
||||
helm.sh/hook-weight: "-5"
|
||||
helm.sh/hook-delete-policy: hook-succeeded,before-hook-creation
|
||||
rules:
|
||||
- apiGroups: [""]
|
||||
resources: ["persistentvolumeclaims"]
|
||||
resourceNames: {{ concat $toLabel $toUnlabel | toJson }}
|
||||
verbs: ["get", "patch"]
|
||||
---
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: RoleBinding
|
||||
metadata:
|
||||
name: {{ $hookName }}
|
||||
annotations:
|
||||
helm.sh/hook: post-install,post-upgrade
|
||||
helm.sh/hook-weight: "-5"
|
||||
helm.sh/hook-delete-policy: hook-succeeded,before-hook-creation
|
||||
subjects:
|
||||
- kind: ServiceAccount
|
||||
name: {{ $hookName }}
|
||||
roleRef:
|
||||
kind: Role
|
||||
name: {{ $hookName }}
|
||||
apiGroup: rbac.authorization.k8s.io
|
||||
{{- end }}
|
||||
@@ -0,0 +1,120 @@
|
||||
{{- /* A vm-disk-<name> claim belongs to the vm-disk release, so it outlives
|
||||
a deleted VM still labelled with that VM's group. Every later VM skips
|
||||
a claim grouped under another name, so without this hook a replacement
|
||||
VM could never group the disk, and a backup taken after the driver's
|
||||
default VolumeGroupSnapshotClass goes away would fail on it.
|
||||
|
||||
Helm runs uninstall hooks from the last stored manifest, so this one is
|
||||
rendered unconditionally rather than only when the post-install hook had
|
||||
work to do. The claims are selected when the Job runs, not at render:
|
||||
the render-time list is stale by the time the release is deleted. The
|
||||
selector can name no claim in advance, hence list on every claim of the
|
||||
namespace instead of resourceNames. */}}
|
||||
{{- $vmName := include "virtual-machine.fullname" . -}}
|
||||
{{- $namespace := .Release.Namespace -}}
|
||||
{{- $groupKey := "velero.io/volume-group" -}}
|
||||
{{- $path := printf "/metadata/labels/%s" ($groupKey | replace "/" "~1") -}}
|
||||
{{- $hookName := printf "%s-volume-ungroup" .Release.Name -}}
|
||||
{{- if gt (len $hookName) 63 -}}
|
||||
{{- $hookName = printf "%s-%s-volume-ungroup" (.Release.Name | trunc 39 | trimSuffix "-") (sha256sum .Release.Name | trunc 8) -}}
|
||||
{{- end -}}
|
||||
apiVersion: batch/v1
|
||||
kind: Job
|
||||
metadata:
|
||||
name: {{ $hookName }}
|
||||
annotations:
|
||||
helm.sh/hook: pre-delete
|
||||
helm.sh/hook-weight: "0"
|
||||
helm.sh/hook-delete-policy: hook-succeeded,before-hook-creation
|
||||
spec:
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: {{ $hookName }}
|
||||
policy.cozystack.io/allow-to-apiserver: "true"
|
||||
spec:
|
||||
serviceAccountName: {{ $hookName }}
|
||||
restartPolicy: Never
|
||||
securityContext:
|
||||
runAsNonRoot: true
|
||||
runAsUser: 65534
|
||||
seccompProfile:
|
||||
type: RuntimeDefault
|
||||
containers:
|
||||
- name: unlabel-claims
|
||||
image: docker.io/alpine/k8s:1.36.2
|
||||
securityContext:
|
||||
allowPrivilegeEscalation: false
|
||||
capabilities:
|
||||
drop: ["ALL"]
|
||||
resources:
|
||||
requests:
|
||||
memory: "16Mi"
|
||||
cpu: "10m"
|
||||
limits:
|
||||
memory: "128Mi"
|
||||
cpu: "100m"
|
||||
command: ["sh", "-exc"]
|
||||
{{- /* The JSON patch tests for this VM's value before removing it, so
|
||||
a claim another VM grouped since the list keeps its group. A
|
||||
claim that no longer carries this VM's value, or no longer
|
||||
exists, is what the uninstall wants; anything else fails the
|
||||
Job and the uninstall with it, rather than leave a label no
|
||||
release would ever remove. */}}
|
||||
args:
|
||||
- |
|
||||
claims=$(kubectl get persistentvolumeclaims -n {{ $namespace }} -l {{ $groupKey }}={{ $vmName }} -o name)
|
||||
for claim in $claims; do
|
||||
if out=$(kubectl patch "$claim" -n {{ $namespace }} --type=json -p '{{ list (dict "op" "test" "path" $path "value" $vmName) (dict "op" "remove" "path" $path) | toJson }}' 2>&1); then
|
||||
continue
|
||||
fi
|
||||
if ! have=$(kubectl get "$claim" -n {{ $namespace }} -o jsonpath='{.metadata.labels.{{ $groupKey | replace "." "\\." }}}' 2>&1); then
|
||||
case "$have" in
|
||||
*"(NotFound)"*) echo "$have"; continue ;;
|
||||
esac
|
||||
echo "$have" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [ "$have" = {{ $vmName }} ]; then
|
||||
echo "$out" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: ServiceAccount
|
||||
metadata:
|
||||
name: {{ $hookName }}
|
||||
annotations:
|
||||
helm.sh/hook: pre-delete
|
||||
helm.sh/hook-weight: "-5"
|
||||
helm.sh/hook-delete-policy: hook-succeeded,before-hook-creation
|
||||
---
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: Role
|
||||
metadata:
|
||||
name: {{ $hookName }}
|
||||
annotations:
|
||||
helm.sh/hook: pre-delete
|
||||
helm.sh/hook-weight: "-5"
|
||||
helm.sh/hook-delete-policy: hook-succeeded,before-hook-creation
|
||||
rules:
|
||||
- apiGroups: [""]
|
||||
resources: ["persistentvolumeclaims"]
|
||||
verbs: ["list", "get", "patch"]
|
||||
---
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: RoleBinding
|
||||
metadata:
|
||||
name: {{ $hookName }}
|
||||
annotations:
|
||||
helm.sh/hook: pre-delete
|
||||
helm.sh/hook-weight: "-5"
|
||||
helm.sh/hook-delete-policy: hook-succeeded,before-hook-creation
|
||||
subjects:
|
||||
- kind: ServiceAccount
|
||||
name: {{ $hookName }}
|
||||
roleRef:
|
||||
kind: Role
|
||||
name: {{ $hookName }}
|
||||
apiGroup: rbac.authorization.k8s.io
|
||||
@@ -12,6 +12,9 @@ suite: vm-instance instance type versus explicit resources
|
||||
release:
|
||||
name: test-vm
|
||||
namespace: tenant-test
|
||||
templates:
|
||||
- templates/vm.yaml
|
||||
- templates/vm-update-hook.yaml
|
||||
|
||||
set:
|
||||
fullnameOverride: test-vm
|
||||
|
||||
@@ -0,0 +1,440 @@
|
||||
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
|
||||
@@ -0,0 +1,67 @@
|
||||
suite: vm-instance releases its Velero volume group on uninstall
|
||||
|
||||
# Helm runs uninstall hooks from the stored manifest, so the hook has to be
|
||||
# there whatever the cluster looked like at render: no capability and no
|
||||
# lookup is mocked here. hack/vm-volume-group-release-hook.bats runs the
|
||||
# script itself against a fake kubectl.
|
||||
release:
|
||||
name: vm-instance-win
|
||||
namespace: tenant-test
|
||||
templates:
|
||||
- templates/volume-group-release-hook.yaml
|
||||
tests:
|
||||
- it: renders the pre-delete hook with nothing to look up
|
||||
asserts:
|
||||
- hasDocuments:
|
||||
count: 4
|
||||
- equal:
|
||||
path: metadata.annotations["helm.sh/hook"]
|
||||
value: pre-delete
|
||||
|
||||
- it: selects the claims when the Job runs, by this VM's group
|
||||
documentSelector: {path: kind, value: Job}
|
||||
asserts:
|
||||
- matchRegex:
|
||||
path: spec.template.spec.containers[0].args[0]
|
||||
pattern: 'kubectl get persistentvolumeclaims -n tenant-test -l velero\.io/volume-group=vm-instance-win -o name'
|
||||
- matchRegex:
|
||||
path: spec.template.spec.containers[0].args[0]
|
||||
pattern: 'kubectl patch "\$claim" -n tenant-test --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"\}\]'''
|
||||
|
||||
- it: lets the hook list and patch claims it cannot name in advance
|
||||
documentSelector: {path: kind, value: Role}
|
||||
asserts:
|
||||
- equal:
|
||||
path: rules
|
||||
value:
|
||||
- apiGroups: [""]
|
||||
resources: ["persistentvolumeclaims"]
|
||||
verbs: ["list", "get", "patch"]
|
||||
|
||||
- it: keeps apart from the post-install hook and within 63 characters for a 53-character release
|
||||
release:
|
||||
name: vm-instance-a-virtual-machine-name-of-41-characters-x
|
||||
asserts:
|
||||
- matchRegex:
|
||||
path: metadata.name
|
||||
pattern: '^vm-instance-a-virtual-machine-name-of-4-[0-9a-f]{8}-volume-ungroup$'
|
||||
- matchRegex:
|
||||
path: spec.template.metadata.labels.app
|
||||
pattern: '^.{1,63}$'
|
||||
documentSelector: {path: kind, value: Job}
|
||||
|
||||
- it: runs the Job under a restrictive non-root securityContext
|
||||
documentSelector: {path: kind, value: Job}
|
||||
asserts:
|
||||
- equal:
|
||||
path: spec.template.spec.securityContext.runAsNonRoot
|
||||
value: true
|
||||
- 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"
|
||||
Reference in new issue
Block a user