mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-30 04:06:34 +09:00
feat(linstor, platform): upgrade snapshot-controller to v8.7.0 and enable VolumeGroupSnapshots (#5239)
## What this PR does Windows VMs with three or more disks cannot get an application-consistent Velero backup on Cozystack today. Velero takes one CSI snapshot per PVC, one after the other, while virt-freezer holds the guest frozen, and on LINSTOR each snapshot takes 2 to 4 s. With three disks or more the freeze runs past the 10 s that Windows VSS allows, VSS aborts with `0x80042314`, and the backup fails. The only workaround is `kubevirt.io/skip-backup-hooks=true` on the VM, which gives up application consistency. The fix is a VolumeGroupSnapshot: one snapshot request for all the PVCs of a VM, taken inside the freeze. linstor-csi has implemented the CSI GroupController since v1.11 (the package ships v1.11.2), and it creates the whole group in a single LINSTOR `Resources.CreateSnapshots` call, so every volume of the VM is captured at the same instant and the freeze lasts one snapshot instead of N. Velero 1.18 drives VolumeGroupSnapshots through `groupsnapshot.storage.k8s.io/v1beta2`. What is missing is on the Cozystack side: snapshot-controller v8.0.1 serves the group snapshot API only as v1alpha1 and runs without the `CSIVolumeGroupSnapshot` gate, and piraeus-operator turns the gate off on the csi-snapshotter sidecar when it does not find v1beta2. This PR: - Upgrades the vendored Piraeus `snapshot-controller` chart from 3.0.5 (v8.0.1) to 5.4.0 (v8.7.0) with the package's `make update`. The `snapshot.storage.k8s.io` CRDs stay at v1. The `groupsnapshot.storage.k8s.io` CRDs go from v1alpha1 only to v1beta1, v1beta2 and v1, with v1beta2 as the storage version. The controller runs with `--feature-gates=CSIVolumeGroupSnapshot=true`, the chart's default. The validating webhook is gone upstream, replaced by CEL rules in the CRDs. The new optional conversion webhook stays disabled, as upstream ships it, since no cluster holds group snapshot objects to convert. - Sets `keepCRDs: true`. The chart now renders its CRDs from a template instead of `crds/`, so without it an uninstall of the release would delete every VolumeSnapshot with the CRDs. - Patches the vendored CRD template (`patches/skip-foreign-crds.diff`, applied by `make update`) so a CRD is rendered only when it is absent or already carries this release's `meta.helm.sh/release-name` and `meta.helm.sh/release-namespace`. A CRD a distribution ships, or that another release owns, stays out of the manifest, so helm-controller neither takes it over nor rewrites it on upgrade. On a fresh install, and for the CRDs migration 62 adopts, the output is unchanged. - Adds migration 62 and bumps `migrations.targetVersion` to 63 (renumbered from 61 after upstream took migration 61 for the Strimzi v1 conversion). The CRDs `crds/` created carry no Helm ownership metadata, and the patched chart renders only the CRDs that do, so the migration annotates the three `snapshot.storage.k8s.io` CRDs the release installed for adoption, which keeps them in its manifest. The three group snapshot CRDs cannot be adopted: the apiserver rejects an update that drops v1alpha1 while it is listed in `status.storedVersions`. The old controller never ran with the gate, so no v1alpha1 object was ever acted on, and the migration deletes each of these CRDs when it is empty and creates it again straight away at v1beta2, from copies `make image-migrations` renders out of the snapshot-controller chart with the release's ownership metadata (as it already bakes the etcd-operator CRDs for migration 50). Every HelmRelease waits for this pre-upgrade hook, so the linstor release renders against the v1beta2 API even when it upgrades before snapshot-controller. A CRD that still holds objects is kept and recorded on the `cozystack-version` ConfigMap as `cozystack.io/migration-62-needs-attention`, and the CRD stays out of the release, which carries no Helm ownership on it, so snapshot-controller still upgrades with that group snapshot API at v1alpha1 and group snapshots off. Nothing re-renders either release once the CRD is cleaned up, so the migration's warning gives the whole recovery: delete the objects and the CRD, force a snapshot-controller HelmRelease upgrade so the chart creates the CRD at v1beta2, restart the snapshot-controller Deployment, which sets up its group snapshot watches only when it starts, then force a linstor HelmRelease upgrade. The migration only touches CRDs that helm-controller labelled as installed by `cozy-snapshot-controller/snapshot-controller`, and the chart leaves the others out of the release, so a distribution that ships its own snapshot CRDs keeps them in both places. Whether the release is installed is read from its HelmRelease rather than from those labels, so a retry after a failed create still recreates the group CRDs it deleted when only they carried the labels; without the HelmRelease the migration changes nothing. The version is stamped only once every release-owned v1beta2 group snapshot CRD is Established, whichever path brought it there, so a retry after a timed-out wait waits again rather than letting linstor render before the API is served. - Sets `FEATURE_GATE_CSI_VOLUME_GROUP_SNAPSHOT=true` on the linstor-csi `csi-snapshotter` sidecar through the LinstorCluster pod template. piraeus-operator v2.10.2 probes for the v1beta2 API once, when it starts, and adds a patch that sets the variable to `false` when the probe fails. On a cluster upgraded from v1alpha1 the operator started before the new CRDs existed, so the gate would stay off until the operator next restarted. The pod template is merged after the operator's own patch. The variable is always set, to `true` only when the apiserver serves all three `groupsnapshot.storage.k8s.io/v1beta2` kinds and to `false` otherwise: the operator's probe looks at VolumeGroupSnapshot alone, and csi-snapshotter with the gate on and a group API missing waits for its group snapshot caches before it starts the ordinary snapshot workers. The class below renders under the same check. - Adds a default `VolumeGroupSnapshotClass` `linstor-group-snapshots` for `linstor.csi.linbit.com`, next to the existing `linstor-snapshots` VolumeSnapshotClass, with the `velero.io/csi-volumegroupsnapshot-class: "true"` label Velero selects a group snapshot class by. Only on a cluster where migration 62 kept a populated v1alpha1 CRD do they wait for the forced linstor upgrade at the end of that recovery. Not in this PR: grouping a VM's PVCs for Velero. Velero builds a group from the PVCs that share the value of the `velero.io/volume-group` label, so the `vm-instance` and `vm-disk` charts would have to set it to the VM name on every disk. That is a separate change, and an operator can set the label by hand meanwhile. Testing: - Kubernetes v1.34.3, Velero 1.18.1, linstor-csi v1.11.2, piraeus-operator v2.10.2. On that cluster the group snapshot CRDs are v1alpha1 only, list `["v1alpha1"]` in `storedVersions`, hold no object, and carry the helm-controller labels of `cozy-snapshot-controller/snapshot-controller`. The csi-snapshotter sidecar runs with `FEATURE_GATE_CSI_VOLUME_GROUP_SNAPSHOT=false`, set by the operator's patch. - `hack/migration-62-snapshot-crds.bats` (16 cases) runs migration 62 in the migrations base image against a fake kubectl that keeps the CRDs in a state file: adoption and deletion, a second run that changes nothing, a CRD holding objects, CRDs another installer owns, CRDs that already serve v1beta2, a cluster without the release, a release without the group CRDs, a failing HelmRelease read, CRD read or delete that aborts before the stamp, a failed diagnostic record that still stamps, a failed create that aborts and is completed by the retry, release labels on the group CRDs alone, with and without a failed first create, a timed-out Established wait whose retry must wait again before stamping, and a v1beta2 CRD that still stores v1alpha1, which is recorded rather than adopted. With the delete removed from the migration, the first case fails; with the release identity taken from the CRD labels instead of the HelmRelease, the failed-create case with group-only labels stamps 63 without recreating the CRDs and fails. Without the wait on the adoption path, the timed-out-wait case stamps 63 on the retry without waiting and fails. - New helm-unittest suites: `packages/system/snapshot-controller/tests/crds_test.yaml` pins `keepCRDs`, v1beta2 served and stored on the three group snapshot CRDs and the controller gate; `packages/system/snapshot-controller/tests/crd_ownership_test.yaml` mocks the live CRDs and checks that one without Helm ownership and one owned by another release are left out while an absent one and one this release owns are rendered (it fails with the patch reverted); `packages/system/linstor/tests/volume_group_snapshot_test.yaml` pins the sidecar gate on with and without strict topology, off when only VolumeGroupSnapshot or no group API is served, and the VolumeGroupSnapshotClass rendered only when the v1beta2 API is served. `csi-topology_test.yaml` now counts the `csi-snapshotter` entry in the pod template. - `helm unittest` passes for `packages/system/linstor` (30 tests), `packages/system/snapshot-controller` (7) and `packages/core/platform` (285). `hack/cozystack-version-stamp.bats` and `hack/check-migrations-target.sh` pass. - Not yet run end to end: the upgrade of a live cluster through migration 62, and a Velero backup with a VolumeGroupSnapshot through linstor-csi. ### Screenshots Not a UI change. ### Downstream repositories - [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: The only change to `packages/core/platform/values.yaml` is the value of `migrations.targetVersion`. The website's `platform-package.md` lists that key without a value, so it needs no change. No website page mentions VolumeGroupSnapshots or documents the snapshot-controller webhook. The rest of the diff is a vendored chart, two system packages, a migration and its tests, none of which the trigger map reaches. ### Release note ```release-note feat(platform): snapshot-controller is upgraded to v8.7.0 and serves VolumeGroupSnapshots as groupsnapshot.storage.k8s.io/v1beta2, and linstor-csi takes group snapshots with a default VolumeGroupSnapshotClass labelled for Velero. A platform migration hands the snapshot CRDs over to the release and removes the empty v1alpha1 group snapshot CRDs. ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **New Features** * Added LINSTOR volume group snapshot support when all required APIs are available, including a default snapshot class. * Updated the snapshot controller to support newer snapshot APIs and a conversion webhook. * Added safer CRD installation and upgrade handling, preserving CRDs with existing objects and recording items that need attention. * **Bug Fixes** * Improved snapshot-controller upgrade reliability, including recovery from certain CRD creation failures. * **Documentation** * Updated chart guidance for CRD management, conversion webhooks, and snapshot class configuration. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
51 files changed
+4547
-2152
No files matched your search
@@ -0,0 +1,378 @@
|
||||
#!/usr/bin/env bats
|
||||
# -----------------------------------------------------------------------------
|
||||
# Unit tests for platform migration 62 --> 63 (hand the snapshot CRDs over to
|
||||
# the snapshot-controller release and replace the empty v1alpha1
|
||||
# VolumeGroupSnapshot CRDs with v1beta2 ones). See
|
||||
# packages/core/platform/images/migrations/migrations/62 for the mechanism.
|
||||
#
|
||||
# The fake keeps the CRDs in a state file and applies each annotate, label,
|
||||
# delete and create to it, so a second run reads what the first one wrote. The
|
||||
# v1beta2 CRDs the migration creates come from group-crds/, standing in for the
|
||||
# copies `make image-migrations` bakes into the image.
|
||||
#
|
||||
# The harness (docker, jq baked onto the migrations base image, the explicit
|
||||
# `return` in run_migration) is the one hack/migration-55-fluxcd-orphan.bats
|
||||
# describes; see there for why each piece is load-bearing.
|
||||
#
|
||||
# Run with: hack/cozytest.sh hack/migration-62-snapshot-crds.bats
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
load test_helper
|
||||
|
||||
FIXTURES="$PWD/hack/testdata/migration-62-snapshot-crds"
|
||||
MIG_DIR="$PWD/packages/core/platform/images/migrations/migrations"
|
||||
ALPINE=$(sed -n 's/^FROM \(alpine:[^ ]*\).*$/\1/p' \
|
||||
"$PWD/packages/core/platform/images/migrations/Dockerfile" | head -1)
|
||||
TESTIMG="cozystack-migration62-test:$(printf '%s' "$ALPINE" | sed 's/[^a-zA-Z0-9]/-/g')"
|
||||
WORKROOT="${TMPDIR:-/tmp}/cozy-migration-62-$$"
|
||||
|
||||
cozy_cleanup() {
|
||||
rm -rf "$WORKROOT"
|
||||
return 0
|
||||
}
|
||||
|
||||
run_migration() {
|
||||
_run_migration_rc=0
|
||||
docker run --rm --network none \
|
||||
--user "$(id -u):$(id -g)" \
|
||||
-v "$MIG_DIR:/migrations:ro" \
|
||||
-v "$FIXTURES:/fakebin:ro" \
|
||||
-v "$WORK:/work" \
|
||||
-e PATH=/fakebin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
|
||||
-e FAKE_CMDLOG=/work/cmdlog \
|
||||
-e FAKE_STATE=/work/state \
|
||||
-e NAMESPACE="${NAMESPACE-}" \
|
||||
-e FAKE_HR_FAIL="${FAKE_HR_FAIL-}" \
|
||||
-e FAKE_CRD_FAIL="${FAKE_CRD_FAIL-}" \
|
||||
-e FAKE_DELETE_FAIL="${FAKE_DELETE_FAIL-}" \
|
||||
-e FAKE_RECORD_FAIL="${FAKE_RECORD_FAIL-}" \
|
||||
-e FAKE_CREATE_FAIL="${FAKE_CREATE_FAIL-}" \
|
||||
-e FAKE_WAIT_FAIL="${FAKE_WAIT_FAIL-}" \
|
||||
-e GROUP_CRD_DIR=/fakebin/group-crds \
|
||||
"$TESTIMG" "/migrations/$1" || _run_migration_rc=$?
|
||||
return "$_run_migration_rc"
|
||||
}
|
||||
|
||||
prep() {
|
||||
docker info >/dev/null 2>&1 || {
|
||||
echo "docker is required: these tests run migration 62 inside a jq-enabled" >&2
|
||||
echo "build of $ALPINE, the migrations image's base." >&2
|
||||
return 1
|
||||
}
|
||||
docker build -q -t "$TESTIMG" - >/dev/null <<DOCKERFILE
|
||||
FROM $ALPINE
|
||||
RUN apk add --no-cache jq
|
||||
DOCKERFILE
|
||||
chmod +x "$FIXTURES/kubectl"
|
||||
mkdir -p "$WORKROOT"
|
||||
WORK=$(mktemp -d "$WORKROOT/XXXXXX")
|
||||
mkdir -p "$WORK/state"
|
||||
cp "$FIXTURES/crds.json" "$WORK/state/"
|
||||
echo '{}' > "$WORK/state/objects.json"
|
||||
: > "$WORK/state/helmrelease"
|
||||
FAKE_CMDLOG="$WORK/cmdlog"
|
||||
: > "$FAKE_CMDLOG"
|
||||
export NAMESPACE=cozy-system
|
||||
unset FAKE_HR_FAIL FAKE_CRD_FAIL FAKE_DELETE_FAIL FAKE_RECORD_FAIL FAKE_CREATE_FAIL FAKE_WAIT_FAIL || true
|
||||
return 0
|
||||
}
|
||||
|
||||
# edit_crds <jq filter> -- rewrite the CRD state before a run.
|
||||
edit_crds() {
|
||||
jq "$1" "$WORK/state/crds.json" > "$WORK/state/crds.json.new"
|
||||
mv "$WORK/state/crds.json.new" "$WORK/state/crds.json"
|
||||
return 0
|
||||
}
|
||||
|
||||
crd_field() {
|
||||
jq -r --arg n "$1" ".items[] | select(.metadata.name == \$n) | $2" "$WORK/state/crds.json"
|
||||
}
|
||||
|
||||
@test "adopts the snapshot CRDs and replaces the empty v1alpha1 group snapshot CRDs" {
|
||||
prep
|
||||
rc=0
|
||||
run_migration 62 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -eq 0 ]
|
||||
|
||||
for p in volumesnapshotclasses volumesnapshotcontents volumesnapshots; do
|
||||
crd="$p.snapshot.storage.k8s.io"
|
||||
[ "$(crd_field "$crd" '.metadata.annotations["meta.helm.sh/release-name"]')" = "snapshot-controller" ]
|
||||
[ "$(crd_field "$crd" '.metadata.annotations["meta.helm.sh/release-namespace"]')" = "cozy-snapshot-controller" ]
|
||||
[ "$(crd_field "$crd" '.metadata.annotations["helm.sh/resource-policy"]')" = "keep" ]
|
||||
[ "$(crd_field "$crd" '.metadata.labels["app.kubernetes.io/managed-by"]')" = "Helm" ]
|
||||
done
|
||||
|
||||
for p in volumegroupsnapshotclasses volumegroupsnapshotcontents volumegroupsnapshots; do
|
||||
crd="$p.groupsnapshot.storage.k8s.io"
|
||||
grep -qxF "DELETE $crd" "$FAKE_CMDLOG"
|
||||
grep -qxF "CREATE $crd" "$FAKE_CMDLOG"
|
||||
[ "$(crd_field "$crd" '[.spec.versions[] | select(.served) | .name] | index("v1beta2") != null')" = "true" ]
|
||||
[ "$(crd_field "$crd" '.metadata.annotations["meta.helm.sh/release-name"]')" = "snapshot-controller" ]
|
||||
done
|
||||
# Every delete comes before the first create, so a kept CRD is known before
|
||||
# anything is created.
|
||||
[ "$(grep -nE '^(DELETE|CREATE) ' "$FAKE_CMDLOG" | sed -n '3p' | cut -d' ' -f1 | cut -d: -f2)" = "DELETE" ]
|
||||
if grep -qF "ANNOTATE volumegroupsnapshot" "$FAKE_CMDLOG"; then echo "a v1alpha1 CRD was adopted" >&2; return 1; fi
|
||||
if grep -qF "RECORD" "$FAKE_CMDLOG"; then echo "unexpected record" >&2; return 1; fi
|
||||
grep -qxF "STAMP 63" "$FAKE_CMDLOG"
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
|
||||
@test "a second run finds nothing to change" {
|
||||
prep
|
||||
run_migration 62 >"$WORK/out" 2>&1
|
||||
: > "$FAKE_CMDLOG"
|
||||
rc=0
|
||||
run_migration 62 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -eq 0 ]
|
||||
if grep -qE "^(ANNOTATE|LABEL|DELETE|CREATE)" "$FAKE_CMDLOG"; then echo "second run changed a CRD" >&2; return 1; fi
|
||||
grep -qxF "STAMP 63" "$FAKE_CMDLOG"
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
|
||||
@test "a group snapshot CRD that holds objects is kept and recorded" {
|
||||
prep
|
||||
echo '{"volumegroupsnapshots.groupsnapshot.storage.k8s.io": 2}' > "$WORK/state/objects.json"
|
||||
rc=0
|
||||
run_migration 62 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -eq 0 ]
|
||||
if grep -qxF "DELETE volumegroupsnapshots.groupsnapshot.storage.k8s.io" "$FAKE_CMDLOG"; then
|
||||
echo "a CRD holding objects was deleted" >&2; return 1
|
||||
fi
|
||||
grep -qxF "DELETE volumegroupsnapshotcontents.groupsnapshot.storage.k8s.io" "$FAKE_CMDLOG"
|
||||
if grep -qF "CREATE" "$FAKE_CMDLOG"; then echo "a v1beta2 CRD was created next to a kept v1alpha1 one" >&2; return 1; fi
|
||||
grep -qF "cozystack.io/migration-62-needs-attention=volumegroupsnapshots.groupsnapshot.storage.k8s.io=2" "$FAKE_CMDLOG"
|
||||
# The kept CRD stays out of the release, so the warning carries the whole
|
||||
# recovery: recreate it through the release, restart the controller, then
|
||||
# re-render linstor.
|
||||
grep -qF "flux reconcile helmrelease --namespace cozy-snapshot-controller snapshot-controller --force" "$WORK/out"
|
||||
grep -qF "kubectl rollout restart --namespace cozy-snapshot-controller deployment/snapshot-controller" "$WORK/out"
|
||||
grep -qF "flux reconcile helmrelease --namespace cozy-linstor linstor --force" "$WORK/out"
|
||||
grep -qxF "STAMP 63" "$FAKE_CMDLOG"
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
|
||||
@test "a failed record of the kept CRDs still stamps the version" {
|
||||
prep
|
||||
echo '{"volumegroupsnapshots.groupsnapshot.storage.k8s.io": 2}' > "$WORK/state/objects.json"
|
||||
export FAKE_RECORD_FAIL="Error from server (InternalError): etcdserver: request timed out"
|
||||
rc=0
|
||||
run_migration 62 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -eq 0 ]
|
||||
grep -qF "failed to record the CRDs needing attention" "$WORK/out"
|
||||
if grep -qF "RECORD" "$FAKE_CMDLOG"; then echo "the failed record was logged as written" >&2; return 1; fi
|
||||
grep -qxF "STAMP 63" "$FAKE_CMDLOG"
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
|
||||
@test "CRDs the release did not install are left alone" {
|
||||
prep
|
||||
edit_crds '.items |= map(.metadata.labels = {})'
|
||||
rc=0
|
||||
run_migration 62 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -eq 0 ]
|
||||
if grep -qE "^(ANNOTATE|LABEL|DELETE|CREATE)" "$FAKE_CMDLOG"; then echo "a foreign CRD was changed" >&2; return 1; fi
|
||||
grep -qxF "STAMP 63" "$FAKE_CMDLOG"
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
|
||||
@test "a group snapshot CRD that already serves v1beta2 is adopted, not deleted" {
|
||||
prep
|
||||
edit_crds '.items |= map(if .spec.group == "groupsnapshot.storage.k8s.io"
|
||||
then .spec.versions = [{name: "v1beta2", served: true, storage: true}]
|
||||
| .status.storedVersions = ["v1beta2"]
|
||||
else . end)'
|
||||
rc=0
|
||||
run_migration 62 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -eq 0 ]
|
||||
if grep -qE "^(DELETE|CREATE)" "$FAKE_CMDLOG"; then echo "a v1beta2 CRD was replaced" >&2; return 1; fi
|
||||
[ "$(crd_field volumegroupsnapshots.groupsnapshot.storage.k8s.io '.metadata.annotations["meta.helm.sh/release-name"]')" = "snapshot-controller" ]
|
||||
grep -qxF "STAMP 63" "$FAKE_CMDLOG"
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
|
||||
@test "a cluster without the snapshot-controller release only stamps the version" {
|
||||
prep
|
||||
rm "$WORK/state/helmrelease"
|
||||
rc=0
|
||||
run_migration 62 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -eq 0 ]
|
||||
if grep -qE "^(ANNOTATE|LABEL|DELETE|CREATE)" "$FAKE_CMDLOG"; then echo "unexpected change" >&2; return 1; fi
|
||||
grep -qxF "STAMP 63" "$FAKE_CMDLOG"
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
|
||||
@test "a cluster with the release but no group snapshot CRDs gets them created" {
|
||||
prep
|
||||
edit_crds '.items |= map(select(.spec.group != "groupsnapshot.storage.k8s.io"))'
|
||||
rc=0
|
||||
run_migration 62 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -eq 0 ]
|
||||
for p in volumegroupsnapshotclasses volumegroupsnapshotcontents volumegroupsnapshots; do
|
||||
grep -qxF "CREATE $p.groupsnapshot.storage.k8s.io" "$FAKE_CMDLOG"
|
||||
done
|
||||
grep -qxF "STAMP 63" "$FAKE_CMDLOG"
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
|
||||
@test "a failing HelmRelease read aborts instead of treating the release as absent" {
|
||||
prep
|
||||
export FAKE_HR_FAIL="Error from server (InternalError): etcdserver: request timed out"
|
||||
rc=0
|
||||
run_migration 62 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -ne 0 ]
|
||||
grep -qF "$FAKE_HR_FAIL" "$WORK/out"
|
||||
if grep -qE "^(ANNOTATE|LABEL|DELETE|CREATE|STAMP)" "$FAKE_CMDLOG"; then echo "changed something after a failed HelmRelease read" >&2; return 1; fi
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
|
||||
@test "a failing CRD read aborts instead of treating the CRD as absent" {
|
||||
prep
|
||||
export FAKE_CRD_FAIL="Error from server (InternalError): etcdserver: request timed out"
|
||||
rc=0
|
||||
run_migration 62 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -ne 0 ]
|
||||
if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "unexpected STAMP after failed CRD read" >&2; return 1; fi
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
|
||||
@test "a failing delete aborts before stamping" {
|
||||
prep
|
||||
export FAKE_DELETE_FAIL="Error from server (InternalError): etcdserver: request timed out"
|
||||
rc=0
|
||||
run_migration 62 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -ne 0 ]
|
||||
if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "unexpected STAMP after failed delete" >&2; return 1; fi
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
|
||||
@test "a failing create aborts before stamping, and the retry creates the CRDs" {
|
||||
prep
|
||||
export FAKE_CREATE_FAIL="Error from server (InternalError): etcdserver: request timed out"
|
||||
rc=0
|
||||
run_migration 62 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -ne 0 ]
|
||||
if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "unexpected STAMP after failed create" >&2; return 1; fi
|
||||
|
||||
unset FAKE_CREATE_FAIL
|
||||
: > "$FAKE_CMDLOG"
|
||||
rc=0
|
||||
run_migration 62 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -eq 0 ]
|
||||
for p in volumegroupsnapshotclasses volumegroupsnapshotcontents volumegroupsnapshots; do
|
||||
grep -qxF "CREATE $p.groupsnapshot.storage.k8s.io" "$FAKE_CMDLOG"
|
||||
done
|
||||
grep -qxF "STAMP 63" "$FAKE_CMDLOG"
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
|
||||
@test "a retry after a timed-out Established wait waits again before stamping" {
|
||||
prep
|
||||
export FAKE_WAIT_FAIL="error: timed out waiting for the condition on customresourcedefinitions/volumegroupsnapshotclasses.groupsnapshot.storage.k8s.io"
|
||||
rc=0
|
||||
run_migration 62 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -ne 0 ]
|
||||
if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "unexpected STAMP after a failed wait" >&2; return 1; fi
|
||||
|
||||
# The created CRDs now serve v1beta2 and carry the release's labels, so the
|
||||
# retry takes the adoption path; the API is still not established.
|
||||
: > "$FAKE_CMDLOG"
|
||||
rc=0
|
||||
run_migration 62 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -ne 0 ]
|
||||
if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "a retry stamped before the CRDs were established" >&2; return 1; fi
|
||||
|
||||
unset FAKE_WAIT_FAIL
|
||||
: > "$FAKE_CMDLOG"
|
||||
rc=0
|
||||
run_migration 62 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -eq 0 ]
|
||||
if grep -qF "CREATE" "$FAKE_CMDLOG"; then echo "the retry created a CRD that already exists" >&2; return 1; fi
|
||||
for p in volumegroupsnapshotclasses volumegroupsnapshotcontents volumegroupsnapshots; do
|
||||
grep -qxF "WAIT $p.groupsnapshot.storage.k8s.io" "$FAKE_CMDLOG"
|
||||
done
|
||||
# Every wait comes before the stamp.
|
||||
[ "$(grep -aE '^(WAIT|STAMP) ' "$FAKE_CMDLOG" | tail -1)" = "STAMP 63" ]
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
|
||||
@test "release labels on the group snapshot CRDs alone still get them recreated" {
|
||||
prep
|
||||
edit_crds '.items |= map(if .spec.group == "snapshot.storage.k8s.io" then .metadata.labels = {} else . end)'
|
||||
rc=0
|
||||
run_migration 62 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -eq 0 ]
|
||||
for p in volumegroupsnapshotclasses volumegroupsnapshotcontents volumegroupsnapshots; do
|
||||
grep -qxF "DELETE $p.groupsnapshot.storage.k8s.io" "$FAKE_CMDLOG"
|
||||
grep -qxF "CREATE $p.groupsnapshot.storage.k8s.io" "$FAKE_CMDLOG"
|
||||
done
|
||||
if grep -qF "ANNOTATE volumesnapshot" "$FAKE_CMDLOG"; then echo "a foreign snapshot CRD was adopted" >&2; return 1; fi
|
||||
grep -qxF "STAMP 63" "$FAKE_CMDLOG"
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
|
||||
@test "release labels on the group snapshot CRDs alone survive a failed create" {
|
||||
prep
|
||||
edit_crds '.items |= map(if .spec.group == "snapshot.storage.k8s.io" then .metadata.labels = {} else . end)'
|
||||
export FAKE_CREATE_FAIL="Error from server (InternalError): etcdserver: request timed out"
|
||||
rc=0
|
||||
run_migration 62 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -ne 0 ]
|
||||
if grep -qF "STAMP" "$FAKE_CMDLOG"; then echo "unexpected STAMP after failed create" >&2; return 1; fi
|
||||
|
||||
unset FAKE_CREATE_FAIL
|
||||
: > "$FAKE_CMDLOG"
|
||||
rc=0
|
||||
run_migration 62 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -eq 0 ]
|
||||
for p in volumegroupsnapshotclasses volumegroupsnapshotcontents volumegroupsnapshots; do
|
||||
grep -qxF "CREATE $p.groupsnapshot.storage.k8s.io" "$FAKE_CMDLOG"
|
||||
[ "$(crd_field "$p.groupsnapshot.storage.k8s.io" '[.spec.versions[] | select(.served) | .name] | index("v1beta2") != null')" = "true" ]
|
||||
done
|
||||
grep -qxF "STAMP 63" "$FAKE_CMDLOG"
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
|
||||
@test "a v1beta2 group snapshot CRD that still stores v1alpha1 is recorded, not adopted" {
|
||||
prep
|
||||
edit_crds '.items |= map(if .metadata.name == "volumegroupsnapshotcontents.groupsnapshot.storage.k8s.io"
|
||||
then .spec.versions = [{name: "v1alpha1", served: true, storage: false}, {name: "v1beta2", served: true, storage: true}]
|
||||
| .status.storedVersions = ["v1alpha1", "v1beta2"]
|
||||
else . end)'
|
||||
rc=0
|
||||
run_migration 62 >"$WORK/out" 2>&1 || rc=$?
|
||||
cat "$WORK/out"; cat "$FAKE_CMDLOG"
|
||||
[ "$rc" -eq 0 ]
|
||||
if grep -qE "^(ANNOTATE|LABEL|DELETE) volumegroupsnapshotcontents" "$FAKE_CMDLOG"; then
|
||||
echo "a CRD still storing v1alpha1 was adopted or deleted" >&2; return 1
|
||||
fi
|
||||
if grep -qF "CREATE" "$FAKE_CMDLOG"; then echo "a CRD was created next to the recorded one" >&2; return 1; fi
|
||||
grep -qF "cozystack.io/migration-62-needs-attention=volumegroupsnapshotcontents.groupsnapshot.storage.k8s.io=stored-v1alpha1" "$FAKE_CMDLOG"
|
||||
# The kept CRD stays out of the release, so the warning carries the whole
|
||||
# recovery: recreate it through the release, restart the controller, then
|
||||
# re-render linstor.
|
||||
grep -qF "flux reconcile helmrelease --namespace cozy-snapshot-controller snapshot-controller --force" "$WORK/out"
|
||||
grep -qF "kubectl rollout restart --namespace cozy-snapshot-controller deployment/snapshot-controller" "$WORK/out"
|
||||
grep -qF "flux reconcile helmrelease --namespace cozy-linstor linstor --force" "$WORK/out"
|
||||
grep -qxF "STAMP 63" "$FAKE_CMDLOG"
|
||||
rm -rf "$WORK"
|
||||
}
|
||||
+201
@@ -0,0 +1,201 @@
|
||||
{
|
||||
"apiVersion": "v1",
|
||||
"kind": "List",
|
||||
"items": [
|
||||
{
|
||||
"apiVersion": "apiextensions.k8s.io/v1",
|
||||
"kind": "CustomResourceDefinition",
|
||||
"metadata": {
|
||||
"name": "volumesnapshotclasses.snapshot.storage.k8s.io",
|
||||
"labels": {
|
||||
"helm.toolkit.fluxcd.io/name": "snapshot-controller",
|
||||
"helm.toolkit.fluxcd.io/namespace": "cozy-snapshot-controller"
|
||||
},
|
||||
"annotations": {
|
||||
"api-approved.kubernetes.io": "https://github.com/kubernetes-csi/external-snapshotter/pull/814",
|
||||
"controller-gen.kubebuilder.io/version": "v0.15.0"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
"group": "snapshot.storage.k8s.io",
|
||||
"versions": [
|
||||
{
|
||||
"name": "v1",
|
||||
"served": true,
|
||||
"storage": true
|
||||
},
|
||||
{
|
||||
"name": "v1beta1",
|
||||
"served": false,
|
||||
"storage": false
|
||||
}
|
||||
]
|
||||
},
|
||||
"status": {
|
||||
"storedVersions": [
|
||||
"v1"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"apiVersion": "apiextensions.k8s.io/v1",
|
||||
"kind": "CustomResourceDefinition",
|
||||
"metadata": {
|
||||
"name": "volumesnapshotcontents.snapshot.storage.k8s.io",
|
||||
"labels": {
|
||||
"helm.toolkit.fluxcd.io/name": "snapshot-controller",
|
||||
"helm.toolkit.fluxcd.io/namespace": "cozy-snapshot-controller"
|
||||
},
|
||||
"annotations": {
|
||||
"api-approved.kubernetes.io": "https://github.com/kubernetes-csi/external-snapshotter/pull/814",
|
||||
"controller-gen.kubebuilder.io/version": "v0.15.0"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
"group": "snapshot.storage.k8s.io",
|
||||
"versions": [
|
||||
{
|
||||
"name": "v1",
|
||||
"served": true,
|
||||
"storage": true
|
||||
},
|
||||
{
|
||||
"name": "v1beta1",
|
||||
"served": false,
|
||||
"storage": false
|
||||
}
|
||||
]
|
||||
},
|
||||
"status": {
|
||||
"storedVersions": [
|
||||
"v1"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"apiVersion": "apiextensions.k8s.io/v1",
|
||||
"kind": "CustomResourceDefinition",
|
||||
"metadata": {
|
||||
"name": "volumesnapshots.snapshot.storage.k8s.io",
|
||||
"labels": {
|
||||
"helm.toolkit.fluxcd.io/name": "snapshot-controller",
|
||||
"helm.toolkit.fluxcd.io/namespace": "cozy-snapshot-controller"
|
||||
},
|
||||
"annotations": {
|
||||
"api-approved.kubernetes.io": "https://github.com/kubernetes-csi/external-snapshotter/pull/814",
|
||||
"controller-gen.kubebuilder.io/version": "v0.15.0"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
"group": "snapshot.storage.k8s.io",
|
||||
"versions": [
|
||||
{
|
||||
"name": "v1",
|
||||
"served": true,
|
||||
"storage": true
|
||||
},
|
||||
{
|
||||
"name": "v1beta1",
|
||||
"served": false,
|
||||
"storage": false
|
||||
}
|
||||
]
|
||||
},
|
||||
"status": {
|
||||
"storedVersions": [
|
||||
"v1"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"apiVersion": "apiextensions.k8s.io/v1",
|
||||
"kind": "CustomResourceDefinition",
|
||||
"metadata": {
|
||||
"name": "volumegroupsnapshotclasses.groupsnapshot.storage.k8s.io",
|
||||
"labels": {
|
||||
"helm.toolkit.fluxcd.io/name": "snapshot-controller",
|
||||
"helm.toolkit.fluxcd.io/namespace": "cozy-snapshot-controller"
|
||||
},
|
||||
"annotations": {
|
||||
"api-approved.kubernetes.io": "https://github.com/kubernetes-csi/external-snapshotter/pull/814",
|
||||
"controller-gen.kubebuilder.io/version": "v0.15.0"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
"group": "groupsnapshot.storage.k8s.io",
|
||||
"versions": [
|
||||
{
|
||||
"name": "v1alpha1",
|
||||
"served": true,
|
||||
"storage": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"status": {
|
||||
"storedVersions": [
|
||||
"v1alpha1"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"apiVersion": "apiextensions.k8s.io/v1",
|
||||
"kind": "CustomResourceDefinition",
|
||||
"metadata": {
|
||||
"name": "volumegroupsnapshotcontents.groupsnapshot.storage.k8s.io",
|
||||
"labels": {
|
||||
"helm.toolkit.fluxcd.io/name": "snapshot-controller",
|
||||
"helm.toolkit.fluxcd.io/namespace": "cozy-snapshot-controller"
|
||||
},
|
||||
"annotations": {
|
||||
"api-approved.kubernetes.io": "https://github.com/kubernetes-csi/external-snapshotter/pull/814",
|
||||
"controller-gen.kubebuilder.io/version": "v0.15.0"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
"group": "groupsnapshot.storage.k8s.io",
|
||||
"versions": [
|
||||
{
|
||||
"name": "v1alpha1",
|
||||
"served": true,
|
||||
"storage": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"status": {
|
||||
"storedVersions": [
|
||||
"v1alpha1"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"apiVersion": "apiextensions.k8s.io/v1",
|
||||
"kind": "CustomResourceDefinition",
|
||||
"metadata": {
|
||||
"name": "volumegroupsnapshots.groupsnapshot.storage.k8s.io",
|
||||
"labels": {
|
||||
"helm.toolkit.fluxcd.io/name": "snapshot-controller",
|
||||
"helm.toolkit.fluxcd.io/namespace": "cozy-snapshot-controller"
|
||||
},
|
||||
"annotations": {
|
||||
"api-approved.kubernetes.io": "https://github.com/kubernetes-csi/external-snapshotter/pull/814",
|
||||
"controller-gen.kubebuilder.io/version": "v0.15.0"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
"group": "groupsnapshot.storage.k8s.io",
|
||||
"versions": [
|
||||
{
|
||||
"name": "v1alpha1",
|
||||
"served": true,
|
||||
"storage": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"status": {
|
||||
"storedVersions": [
|
||||
"v1alpha1"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"apiVersion": "apiextensions.k8s.io/v1",
|
||||
"kind": "CustomResourceDefinition",
|
||||
"metadata": {
|
||||
"name": "volumegroupsnapshotclasses.groupsnapshot.storage.k8s.io",
|
||||
"labels": {
|
||||
"helm.toolkit.fluxcd.io/name": "snapshot-controller",
|
||||
"helm.toolkit.fluxcd.io/namespace": "cozy-snapshot-controller",
|
||||
"app.kubernetes.io/managed-by": "Helm"
|
||||
},
|
||||
"annotations": {
|
||||
"meta.helm.sh/release-name": "snapshot-controller",
|
||||
"meta.helm.sh/release-namespace": "cozy-snapshot-controller",
|
||||
"helm.sh/resource-policy": "keep"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
"group": "groupsnapshot.storage.k8s.io",
|
||||
"versions": [
|
||||
{
|
||||
"name": "v1",
|
||||
"served": true,
|
||||
"storage": false
|
||||
},
|
||||
{
|
||||
"name": "v1beta1",
|
||||
"served": true,
|
||||
"storage": false
|
||||
},
|
||||
{
|
||||
"name": "v1beta2",
|
||||
"served": true,
|
||||
"storage": true
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"apiVersion": "apiextensions.k8s.io/v1",
|
||||
"kind": "CustomResourceDefinition",
|
||||
"metadata": {
|
||||
"name": "volumegroupsnapshotcontents.groupsnapshot.storage.k8s.io",
|
||||
"labels": {
|
||||
"helm.toolkit.fluxcd.io/name": "snapshot-controller",
|
||||
"helm.toolkit.fluxcd.io/namespace": "cozy-snapshot-controller",
|
||||
"app.kubernetes.io/managed-by": "Helm"
|
||||
},
|
||||
"annotations": {
|
||||
"meta.helm.sh/release-name": "snapshot-controller",
|
||||
"meta.helm.sh/release-namespace": "cozy-snapshot-controller",
|
||||
"helm.sh/resource-policy": "keep"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
"group": "groupsnapshot.storage.k8s.io",
|
||||
"versions": [
|
||||
{
|
||||
"name": "v1",
|
||||
"served": true,
|
||||
"storage": false
|
||||
},
|
||||
{
|
||||
"name": "v1beta1",
|
||||
"served": true,
|
||||
"storage": false
|
||||
},
|
||||
{
|
||||
"name": "v1beta2",
|
||||
"served": true,
|
||||
"storage": true
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"apiVersion": "apiextensions.k8s.io/v1",
|
||||
"kind": "CustomResourceDefinition",
|
||||
"metadata": {
|
||||
"name": "volumegroupsnapshots.groupsnapshot.storage.k8s.io",
|
||||
"labels": {
|
||||
"helm.toolkit.fluxcd.io/name": "snapshot-controller",
|
||||
"helm.toolkit.fluxcd.io/namespace": "cozy-snapshot-controller",
|
||||
"app.kubernetes.io/managed-by": "Helm"
|
||||
},
|
||||
"annotations": {
|
||||
"meta.helm.sh/release-name": "snapshot-controller",
|
||||
"meta.helm.sh/release-namespace": "cozy-snapshot-controller",
|
||||
"helm.sh/resource-policy": "keep"
|
||||
}
|
||||
},
|
||||
"spec": {
|
||||
"group": "groupsnapshot.storage.k8s.io",
|
||||
"versions": [
|
||||
{
|
||||
"name": "v1",
|
||||
"served": true,
|
||||
"storage": false
|
||||
},
|
||||
{
|
||||
"name": "v1beta1",
|
||||
"served": true,
|
||||
"storage": false
|
||||
},
|
||||
{
|
||||
"name": "v1beta2",
|
||||
"served": true,
|
||||
"storage": true
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
#!/bin/sh
|
||||
# Fake kubectl for hack/migration-62-snapshot-crds.bats. It serves only the
|
||||
# calls migration 62 and lib/cozystack-version.sh make, and logs each one to
|
||||
# $FAKE_CMDLOG.
|
||||
#
|
||||
# The CRDs live in $FAKE_STATE/crds.json and every annotate, label and delete
|
||||
# is applied to that file, so a second run of the migration reads what the
|
||||
# first one wrote. $FAKE_STATE/objects.json maps a CRD name to the number of
|
||||
# objects it holds. The snapshot-controller HelmRelease exists while
|
||||
# $FAKE_STATE/helmrelease does.
|
||||
#
|
||||
# FAKE_CMDLOG file to append a one-line record of each call to
|
||||
# FAKE_STATE directory holding crds.json and objects.json
|
||||
# FAKE_HR_FAIL non-empty => reading the HelmRelease exits 1 with this stderr
|
||||
# FAKE_CRD_FAIL non-empty => reading a CRD exits 1 with this stderr
|
||||
# FAKE_DELETE_FAIL non-empty => deleting a CRD exits 1 with this stderr
|
||||
# FAKE_RECORD_FAIL non-empty => annotating cozystack-version exits 1 with this stderr
|
||||
# FAKE_CREATE_FAIL non-empty => creating a CRD exits 1 with this stderr
|
||||
# FAKE_WAIT_FAIL non-empty => waiting for a CRD to be Established exits 1
|
||||
# with this stderr, as a CRD the apiserver never establishes
|
||||
#
|
||||
# The CRD files the migration creates from are JSON, which kubectl reads as
|
||||
# well as YAML, so the fake can load them with jq.
|
||||
set -u
|
||||
|
||||
log() { [ -n "${FAKE_CMDLOG:-}" ] && printf '%s\n' "$*" >> "$FAKE_CMDLOG"; }
|
||||
log "KUBECTL $*"
|
||||
|
||||
CRDS="$FAKE_STATE/crds.json"
|
||||
|
||||
# set_meta <field> <crd> <key=value>...
|
||||
set_meta() {
|
||||
field="$1"; crd="$2"; shift 2
|
||||
for kv in "$@"; do
|
||||
case "$kv" in
|
||||
--*) continue ;;
|
||||
esac
|
||||
k="${kv%%=*}"; v="${kv#*=}"
|
||||
jq --arg n "$crd" --arg f "$field" --arg k "$k" --arg v "$v" \
|
||||
'.items |= map(if .metadata.name == $n then .metadata[$f][$k] = $v else . end)' \
|
||||
"$CRDS" > "$CRDS.new" && mv "$CRDS.new" "$CRDS"
|
||||
done
|
||||
}
|
||||
|
||||
case "$*" in
|
||||
*"apply --filename -"*|*"apply -f -"*)
|
||||
manifest=$(cat)
|
||||
case "$manifest" in
|
||||
*"kind: ConfigMap"*)
|
||||
v=$(printf '%s\n' "$manifest" | sed -n 's/^ version: "\{0,1\}\([0-9]\{1,\}\)"\{0,1\}$/\1/p' | head -1)
|
||||
log "STAMP ${v:-unknown}"
|
||||
exit 0
|
||||
;;
|
||||
esac
|
||||
printf 'fake kubectl: unmodelled apply\n' >&2
|
||||
exit 1
|
||||
;;
|
||||
|
||||
"get helmreleases.helm.toolkit.fluxcd.io --namespace cozy-snapshot-controller snapshot-controller --ignore-not-found -o name")
|
||||
if [ -n "${FAKE_HR_FAIL:-}" ]; then printf '%s\n' "$FAKE_HR_FAIL" >&2; exit 1; fi
|
||||
[ -e "$FAKE_STATE/helmrelease" ] && echo "helmrelease.helm.toolkit.fluxcd.io/snapshot-controller"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
"get customresourcedefinitions.apiextensions.k8s.io "*)
|
||||
if [ -n "${FAKE_CRD_FAIL:-}" ]; then printf '%s\n' "$FAKE_CRD_FAIL" >&2; exit 1; fi
|
||||
jq --arg n "$3" '.items[] | select(.metadata.name == $n)' "$CRDS"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
"annotate customresourcedefinitions.apiextensions.k8s.io "*)
|
||||
crd="$3"; shift 3
|
||||
log "ANNOTATE $crd $*"
|
||||
set_meta annotations "$crd" "$@"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
"label customresourcedefinitions.apiextensions.k8s.io "*)
|
||||
crd="$3"; shift 3
|
||||
log "LABEL $crd $*"
|
||||
set_meta labels "$crd" "$@"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
"delete customresourcedefinitions.apiextensions.k8s.io "*)
|
||||
if [ -n "${FAKE_DELETE_FAIL:-}" ]; then printf '%s\n' "$FAKE_DELETE_FAIL" >&2; exit 1; fi
|
||||
log "DELETE $3"
|
||||
jq --arg n "$3" '.items |= map(select(.metadata.name != $n))' "$CRDS" > "$CRDS.new" && mv "$CRDS.new" "$CRDS"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
"create -f "*)
|
||||
if [ -n "${FAKE_CREATE_FAIL:-}" ]; then printf '%s\n' "$FAKE_CREATE_FAIL" >&2; exit 1; fi
|
||||
name=$(jq -r '.metadata.name' "$3") || exit 1
|
||||
if jq -e --arg n "$name" '.items[] | select(.metadata.name == $n)' "$CRDS" >/dev/null; then
|
||||
printf 'Error from server (AlreadyExists): %s already exists\n' "$name" >&2
|
||||
exit 1
|
||||
fi
|
||||
log "CREATE $name"
|
||||
jq --slurpfile crd "$3" '.items += $crd' "$CRDS" > "$CRDS.new" && mv "$CRDS.new" "$CRDS"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
"wait --for=condition=Established "*)
|
||||
log "WAIT ${4#customresourcedefinitions.apiextensions.k8s.io/}"
|
||||
if [ -n "${FAKE_WAIT_FAIL:-}" ]; then printf '%s\n' "$FAKE_WAIT_FAIL" >&2; exit 1; fi
|
||||
exit 0
|
||||
;;
|
||||
|
||||
"annotate configmap "*)
|
||||
if [ -n "${FAKE_RECORD_FAIL:-}" ]; then printf '%s\n' "$FAKE_RECORD_FAIL" >&2; exit 1; fi
|
||||
log "RECORD $*"
|
||||
exit 0
|
||||
;;
|
||||
|
||||
"get "*".groupsnapshot.storage.k8s.io --all-namespaces -o json")
|
||||
n=$(jq --arg n "$2" '.[$n] // 0' "$FAKE_STATE/objects.json")
|
||||
jq -n --argjson n "$n" '{apiVersion: "v1", kind: "List", items: [range($n) | {}]}'
|
||||
exit 0
|
||||
;;
|
||||
esac
|
||||
|
||||
log "UNMODELLED $*"
|
||||
printf 'fake kubectl: unmodelled call: %s\n' "$*" >&2
|
||||
exit 1
|
||||
Reference in new issue
Block a user