mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-04 22:21:46 +09:00
A tenant Kubernetes cluster could only start once an ancestor Tenant had an etcd module turned on, which nobody creating a cluster may do, and every cluster below that tenant then shared one etcd, one CA and one disk. Anything that orders clusters on a tenant's behalf had to flip the tenant's etcd on first. A new cluster now renders its own EtcdCluster and DataStore, from the cozy-lib shapes the etcd chart uses, and is created and deleted with them. The awaiting-etcd state goes, since a datastore always exists. An existing cluster stays on the DataStore its live KamajiControlPlane names, and renders what it rendered before. Changing that name starts a Kamaji datastore migration, which freezes the tenant's writes and leaves every worker kubelet needing a restart, so no upgrade may do it. _namespace.etcd cannot decide this: tenant-root publishes one to every tenant whether or not an etcd runs behind it. On uninstall, Kamaji's datastore finalizer drops the tenant's role, prefix and user from an etcd this release deletes next, and it never completes while that etcd is down. The pre-delete hook therefore removes it from a TenantControlPlane on the cluster's own DataStore that outlives the wait, and deletes the etcd's PKI, which cert-manager issues without an owner. If the finalizer stays on, or the TenantControlPlane cannot be read, the hook fails before the PKI goes, so the uninstall is retried with the etcd still in place. A TenantControlPlane on a shared DataStore is left to Kamaji as before. A Cilium deny policy keeps the rest of the tenant namespace, ancestor tenants and off-cluster sources off the etcd ports; the Kubernetes backup strategy's dump Job carries the label it admits. The tenant etcd's scrape now selects its own members by instance, because every member etcd-operator runs carries app.kubernetes.io/name: etcd. The platform waits for etcd-operator before offering the application. The defrag job both charts share pulls its image through the configured registry, pinned by digest to the multi-architecture index, so a mirrored installation does not depend on ghcr.io for it. Tested with helm-unittest on both the own and the shared branch, the pre-delete hook's bats suite run against a stub kubectl for both, and a render of an existing cluster that matches main byte for byte. The guest e2e runs the defrag CronJob once, and probes the etcd client port from a labelled Pod, an unlabelled one and one in tenant-root. Co-authored-by: Yan Bondarenko <202671653+yankawai@users.noreply.github.com> Assisted-by: LLM Signed-off-by: Yan Bondarenko <202671653+yankawai@users.noreply.github.com> Signed-off-by: Timofei Larkin <lllamnyp@gmail.com>
154 lines
6.0 KiB
Bash
154 lines
6.0 KiB
Bash
#!/usr/bin/env bats
|
|
# -----------------------------------------------------------------------------
|
|
# Unit tests for the scoped tenant-drain helpers in run-kubernetes.sh.
|
|
#
|
|
# cozy_tenant_drained is the pure exit-condition of the inter-test drain loop
|
|
# (cozy_wait_tenant_drained). Each argument is one resource-probe capture: the
|
|
# stdout of a `kubectl get -o name` (empty once the resource is gone) or the
|
|
# literal "err" the loop substitutes when a probe itself fails, so an API blip
|
|
# is never misread as "the tenant has drained". The function returns 0 (drained)
|
|
# only when every capture is empty/whitespace, non-zero otherwise.
|
|
#
|
|
# CI runs this file under Bats through `make bats-unit-tests`. It also remains
|
|
# compatible with the legacy `hack/cozytest.sh` translator, whose awk parser
|
|
# recognizes only @test blocks and a bare `}` on its own line. The tests avoid
|
|
# `run` and `$status` for that compatibility path and use direct shell assertions.
|
|
#
|
|
# Run with: bats hack/run-kubernetes-drain_test.bats
|
|
# -----------------------------------------------------------------------------
|
|
|
|
load test_helper
|
|
|
|
@test "all-empty captures report drained" {
|
|
. hack/e2e-chainsaw/_lib/run-kubernetes.sh
|
|
if ! cozy_tenant_drained "" "" ""; then
|
|
echo "expected drained when every capture is empty" >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
@test "no arguments reports drained" {
|
|
. hack/e2e-chainsaw/_lib/run-kubernetes.sh
|
|
if ! cozy_tenant_drained; then
|
|
echo "expected drained when there are no captures" >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
@test "a remaining VirtualMachine reports not-drained" {
|
|
. hack/e2e-chainsaw/_lib/run-kubernetes.sh
|
|
if cozy_tenant_drained "virtualmachine.kubevirt.io/kubernetes-test-latest-version-md0-abcde" "" ""; then
|
|
echo "expected not-drained while a VirtualMachine is still present" >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
@test "a remaining PVC in the last capture reports not-drained" {
|
|
. hack/e2e-chainsaw/_lib/run-kubernetes.sh
|
|
if cozy_tenant_drained "" "" "persistentvolumeclaim/disk-system-kubernetes-test-latest-version-md0-abcde"; then
|
|
echo "expected not-drained while a worker-disk PVC is still present" >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
@test "a multi-line resource list reports not-drained" {
|
|
. hack/e2e-chainsaw/_lib/run-kubernetes.sh
|
|
vms=$(printf 'virtualmachine.kubevirt.io/a\nvirtualmachine.kubevirt.io/b\n')
|
|
if cozy_tenant_drained "$vms" "" ""; then
|
|
echo "expected not-drained for a multi-line VirtualMachine list" >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
@test "the err sentinel is never misread as drained" {
|
|
# A failed probe (transient API error) substitutes the literal "err". Even
|
|
# when every other capture is empty, an err capture MUST count as
|
|
# not-drained so the loop keeps polling instead of declaring victory on a
|
|
# blip and letting the next tenant schedule onto an un-vacated sandbox.
|
|
. hack/e2e-chainsaw/_lib/run-kubernetes.sh
|
|
if cozy_tenant_drained "" "err" ""; then
|
|
echo "expected not-drained when a probe returned the err sentinel" >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
@test "whitespace-only captures are treated as drained" {
|
|
# `kubectl get -o name` against an empty result set yields no resource
|
|
# names; a stray newline must not be mistaken for a live resource.
|
|
. hack/e2e-chainsaw/_lib/run-kubernetes.sh
|
|
blank=$(printf '\n')
|
|
if ! cozy_tenant_drained "$blank" "" " "; then
|
|
echo "expected drained when captures hold only whitespace" >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
@test "PVC filtering ignores leftovers from databases and other Kubernetes clusters" {
|
|
. hack/e2e-chainsaw/_lib/run-kubernetes.sh
|
|
pvcs=$(printf '%s\n' \
|
|
'persistentvolumeclaim/clickhouse-data-test' \
|
|
'persistentvolumeclaim/disk-system-kubernetes-test-previous-version-md0-worker-a' \
|
|
'persistentvolumeclaim/disk-system-kubernetes-test-latest-version-md0-worker-b')
|
|
got=$(cozy_filter_cluster_pvcs test-latest-version "$pvcs")
|
|
expected='persistentvolumeclaim/disk-system-kubernetes-test-latest-version-md0-worker-b'
|
|
[ "$got" = "$expected" ] || {
|
|
echo "PVC filter crossed cluster ownership: [$got]" >&2
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
@test "scoped drain queries the CAPI label and ignores unrelated PVCs" {
|
|
. hack/e2e-chainsaw/_lib/run-kubernetes.sh
|
|
calls=$(mktemp)
|
|
kubectl() {
|
|
printf '%s\n' "$*" >> "$calls"
|
|
case "$*" in
|
|
*" get pvc "*) printf 'persistentvolumeclaim/clickhouse-data-test\n' ;;
|
|
*) printf '' ;;
|
|
esac
|
|
}
|
|
|
|
cozy_wait_tenant_drained test-latest-version 0 >/dev/null
|
|
[ "$(grep -c 'cluster.x-k8s.io/cluster-name=kubernetes-test-latest-version' "$calls")" -eq 2 ] || {
|
|
echo "VM and VMI probes were not scoped to the current CAPI cluster" >&2
|
|
cat "$calls" >&2
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
@test "an etcd reclamation read past its deadline is not run and fails as timed out" {
|
|
. hack/e2e-chainsaw/_lib/run-kubernetes.sh
|
|
tmp=$(mktemp --directory)
|
|
rc=0
|
|
_cozy_etcd_reaped_read "$(( $(date +%s) - 1 ))" touch "$tmp/ran" || rc=$?
|
|
if [ "$rc" -ne 124 ]; then
|
|
echo "expected status 124 past the deadline, got $rc" >&2
|
|
exit 1
|
|
fi
|
|
if [ -e "$tmp/ran" ]; then
|
|
echo "the read ran although its deadline had passed" >&2
|
|
exit 1
|
|
fi
|
|
rm --recursive --force "$tmp"
|
|
}
|
|
|
|
@test "an etcd reclamation read is cut off at the deadline, not at its own 20s" {
|
|
. hack/e2e-chainsaw/_lib/run-kubernetes.sh
|
|
timeout() { printf '%s\n' "$*" > "$tmp/timeout.args"; }
|
|
tmp=$(mktemp --directory)
|
|
_cozy_etcd_reaped_read "$(( $(date +%s) + 3 ))" kubectl get pvc
|
|
case "$(cat "$tmp/timeout.args")" in
|
|
"--kill-after=5 "[1-3]" kubectl get pvc") ;;
|
|
*)
|
|
echo "expected a bound of at most 3s, got: $(cat "$tmp/timeout.args")" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
_cozy_etcd_reaped_read "$(( $(date +%s) + 600 ))" kubectl get pvc
|
|
if [ "$(cat "$tmp/timeout.args")" != "--kill-after=5 20 kubectl get pvc" ]; then
|
|
echo "expected the 20s cap far from the deadline, got: $(cat "$tmp/timeout.args")" >&2
|
|
exit 1
|
|
fi
|
|
rm --recursive --force "$tmp"
|
|
}
|