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
+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