fix(clickhouse): hold an archive delete while a quota's usage is unknown

The ResourceQuota admission plugin refuses a Job with "status unknown
for quota:" until the quota controller has computed its usage, as well
as with "exceeded quota:" once it is used up. The cleanup only retried
the latter; on the former it released the Backup and left its archive
in the bucket for good, though the refusal clears by itself.

Assisted-by: LLM
Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
This commit is contained in:
Alexey Artamonov committed 2026-11-23 16:28:27 +03:00
1 parent 8f0082a1c8
commit 64d614d0d1
2 files changed
+51 -2

No files matched your search

@@ -21,6 +21,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
@@ -1374,6 +1375,9 @@ func TestCleanupAltinityBackup_ReleasesWhenTheJobIsRefused(t *testing.T) {
for name, refuse := range map[string]error{
"invalid": apierrors.NewInvalid(batchv1.SchemeGroupVersion.WithKind("Job").GroupKind(), "ch-bk-cleanup", nil),
"forbidden": apierrors.NewForbidden(batchv1.Resource("jobs"), "ch-bk-cleanup", errors.New("service account cannot create jobs")),
// Only a Forbidden is a quota refusal, whatever an Invalid's text says.
"invalid naming a quota": apierrors.NewInvalid(batchv1.SchemeGroupVersion.WithKind("Job").GroupKind(), "ch-bk-cleanup",
field.ErrorList{field.Invalid(field.NewPath("metadata", "name"), "ch-bk-cleanup", "exceeded quota: jobs-quota")}),
} {
t.Run(name, func(t *testing.T) {
backup := altinityCleanupBackup("ch-bk")
@@ -1404,6 +1408,39 @@ func TestCleanupAltinityBackup_ReleasesWhenTheJobIsRefused(t *testing.T) {
}
}
// A namespace ResourceQuota on Jobs refuses the cleanup Job with Forbidden while
// the quota is used up, or before its usage is known. That clears once other
// Jobs go or the quota controller has computed the usage, and releasing the
// Backup on it would leave the archive for good, so the Backup is held and the
// create retried. The messages are the ones the ResourceQuota admission plugin
// of k8s.io/apiserver returns.
func TestCleanupAltinityBackup_RetriesAJobRefusedByAQuota(t *testing.T) {
for name, msg := range map[string]string{
"quota used up": "exceeded quota: jobs-quota, requested: count/jobs.batch=1, used: count/jobs.batch=5, limited: count/jobs.batch=5",
"quota usage unknown": "status unknown for quota: jobs-quota, resources: count/jobs.batch",
} {
t.Run(name, func(t *testing.T) {
backup := altinityCleanupBackup("ch-bk")
c, s := newAltinityCleanupClientWith(interceptor.Funcs{
Create: func(ctx context.Context, c client.WithWatch, obj client.Object, opts ...client.CreateOption) error {
if _, ok := obj.(*batchv1.Job); ok {
return apierrors.NewForbidden(batchv1.Resource("jobs"), "ch-bk-cleanup", errors.New(msg))
}
return c.Create(ctx, obj, opts...)
},
}, altinityCleanupFixtures(backup)...)
rec := record.NewFakeRecorder(10)
r := &BackupReconciler{Client: c, Scheme: s, Recorder: rec}
if _, err := r.cleanupOnDelete(context.Background(), backup); err == nil {
t.Fatal("a cleanup Job refused by a quota must go back to the work queue with the Backup held, not release it")
}
if len(rec.Events) != 0 {
t.Errorf("unexpected event %q: the Backup is not released", <-rec.Events)
}
})
}
}
// Any other Create error is transient and goes back to the work queue with the
// Backup held.
func TestCleanupAltinityBackup_RetriesATransientCreateError(t *testing.T) {
@@ -1157,8 +1157,7 @@ func (r *BackupReconciler) cleanupAltinityBackup(ctx context.Context, backup *ba
rendered.Spec,
fmt.Sprintf("chi-clickhouse-%s-clickhouse-0-0.%s.svc", release, backup.Namespace), authSecret, archive)
if cerr := r.Create(ctx, desired); cerr != nil && !apierrors.IsAlreadyExists(cerr) {
// ResourceQuota also returns Forbidden. Once a Job slot frees up the same cleanup can run.
if apierrors.IsForbidden(cerr) && strings.Contains(cerr.Error(), "exceeded quota: ") {
if altinityQuotaRefusal(cerr) {
return ctrl.Result{}, cerr
}
if apierrors.IsForbidden(cerr) || apierrors.IsInvalid(cerr) {
@@ -1271,6 +1270,19 @@ func (r *BackupReconciler) altinityAppOnSystemBucket(ctx context.Context, backup
return app, v, true, nil
}
// altinityQuotaRefusal tells the Forbidden a namespace ResourceQuota returns
// while it is used up or its usage is not yet known, by the messages of the
// ResourceQuota admission plugin in k8s.io/apiserver. It clears once other
// Jobs go or the quota controller has computed the usage, so the create is
// retried instead of the Backup being released.
func altinityQuotaRefusal(err error) bool {
if !apierrors.IsForbidden(err) {
return false
}
msg := err.Error()
return strings.Contains(msg, "exceeded quota:") || strings.Contains(msg, "status unknown for quota:")
}
// altinitySystemBucket reads the bucket the projected credentials name in the
// namespace, the one the sidecar of a system-bucket application reads. It is
// empty when no credentials are projected there or they name no bucket.