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
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>
This commit is contained in:
1 parent
02b8a26738
commit
3a5ae23797
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