fix(cozystack-basics): refuse a direct delete of a tenant namespace

A tenant namespace is meant to go away with its Tenant. The tenant
chart's pre-delete hook deletes the applications first and the tenant
modules such as etcd after them, and helm-controller deletes the
namespace at the end. A direct delete hands the namespace to the
namespace controller instead. It deletes the pods first and then
everything else together, so the tenant etcd and its etcd-ca-tls Secret
can be gone before the TenantControlPlanes that use them. Kamaji then
cannot reach the datastore to clean up, the TenantControlPlane finalizer
never clears and the namespace stays Terminating.

The policy refuses DELETE of a namespace labelled
tenant.cozystack.io/tenant-root, which the tenant chart puts on every
tenant namespace below tenant-root. tenant-root carries no such label
and stays with the no-delete guardrail. Two callers are let through.
helm-controller and its shards run as the flux ServiceAccount in
cozy-fluxcd and delete the namespace during a Tenant uninstall. The
garbage collector runs in kube-system and removes a child namespace
through its ownerReference once the parent namespace is gone. The
namespace controller only finalizes a namespace and never sends a
DELETE for it, so it is not affected. Everyone else is refused,
system:masters included, as with the no-delete guardrail. The message
names the Tenant to delete and the label to remove when the Tenant is
already gone.

The policy test helpers take the template path now, so the new test
compiles the shipped template with the apiserver CEL compiler like the
external TLS policy test does.

Assisted-by: LLM
Signed-off-by: Yan Bondarenko <202671653+yankawai@users.noreply.github.com>
This commit is contained in:
Yan Bondarenko committed 2026-10-21 04:30:33 +03:00
1 parent 1f92bcd20e
commit 6bb0fda706
7 files changed
+331 -11

No files matched your search

+37
View File
@@ -561,3 +561,40 @@ data:
version: "${version}"
EOF
}
@test "Tenant namespace delete policy refuses a direct delete and keeps the Tenant uninstall" {
if ! kubectl api-resources --api-group=admissionregistration.k8s.io \
2>/dev/null | grep -qw validatingadmissionpolicies; then
skip "ValidatingAdmissionPolicy API not available on this cluster"
fi
kubectl get validatingadmissionpolicy cozystack-tenant-namespace-delete-policy
kubectl get validatingadmissionpolicybinding cozystack-tenant-namespace-delete-policy
kubectl apply -f - <<EOF
apiVersion: apps.cozystack.io/v1alpha1
kind: Tenant
metadata:
name: deleteprobe
namespace: tenant-root
spec: {}
EOF
timeout 60 sh -ec 'until kubectl get hr/tenant-deleteprobe -n tenant-root >/dev/null 2>&1; do sleep 2; done'
kubectl wait hr/tenant-deleteprobe -n tenant-root --timeout=1m --for=condition=ready
local selected output rc still_there
selected=$(kubectl get namespaces --selector tenant.cozystack.io/tenant-root --field-selector metadata.name=tenant-deleteprobe -o name)
[ "$selected" = "namespace/tenant-deleteprobe" ]
output=$(kubectl delete namespace tenant-deleteprobe --dry-run=server 2>&1) && rc=0 || rc=$?
echo "kubectl delete exit=$rc, output=$output"
[ "$rc" -ne 0 ]
echo "$output" | grep -qF "ValidatingAdmissionPolicy 'cozystack-tenant-namespace-delete-policy'"
echo "$output" | grep -qF 'kubectl delete tenants.apps.cozystack.io deleteprobe --namespace tenant-root.'
kubectl delete tenants.apps.cozystack.io deleteprobe -n tenant-root --timeout=5m
kubectl wait namespace/tenant-deleteprobe --for=delete --timeout=5m
if ! still_there=$(kubectl get namespace tenant-deleteprobe --ignore-not-found -o name); then
echo "FAIL: reading the tenant-deleteprobe namespace did not answer"; false
fi
if [ -n "$still_there" ]; then echo "FAIL: deleting the Tenant must remove its namespace"; false; fi
}