fix(mariadb): report the operator Job's own failure on JobFailed

The operator's JobFailed condition message is only "Failed", and with
the operator's default restartPolicy OnFailure the Job controller
deletes the pod once the backoff limit is spent, before the operator
sets JobFailed. Pointing the reader at the Job's logs therefore sent
them to logs that no longer exist. The Job, which the operator names
after the Backup or Restore, keeps its Failed condition with the
reason and message, so the BackupJob or RestoreJob now carries those
instead; a Job that cannot be read is named in the message rather
than dropped.

Assisted-by: LLM
Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
(cherry picked from commit 9f51b9db04)
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
This commit is contained in:
Alexey Artamonov authored and Myasnikov Daniil committed 2026-11-10 12:40:56 +00:00
1 parent 107271f6f6
commit 192e4c9052
2 files changed
+94 -16

No files matched your search

@@ -7,6 +7,7 @@ import (
"fmt"
"time"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
apimeta "k8s.io/apimachinery/pkg/api/meta"
@@ -205,7 +206,7 @@ func (r *BackupJobReconciler) reconcileMariaDB(ctx context.Context, j *backupsv1
switch {
case cond != nil && cond.Reason == mariadbtypes.ConditionReasonJobFailed:
return r.markBackupJobFailed(ctx, j, mariadbJobFailedMessage("Backup", mdbBackup.Namespace, mdbBackup.Name, cond))
return r.markBackupJobFailed(ctx, j, mariadbJobFailedMessage(ctx, r.Client, "Backup", mdbBackup.Namespace, mdbBackup.Name, cond))
case cond != nil && cond.Status == metav1.ConditionTrue && cond.Reason == mariadbtypes.ConditionReasonJobComplete:
// Backup completed successfully. Materialise the Cozystack artifact
@@ -252,12 +253,24 @@ func (r *BackupJobReconciler) reconcileMariaDB(ctx context.Context, j *backupsv1
}
}
// mariadbJobFailedMessage names the operator object whose Job failed. The
// operator's own condition message is only "Failed"; the cause is in the
// logs of that object's Job.
func mariadbJobFailedMessage(kind, namespace, name string, cond *metav1.Condition) string {
return fmt.Sprintf("k8s.mariadb.com/%s %s/%s failed (Complete=%s/%s); the cause is in the logs of its Job",
kind, namespace, name, cond.Status, cond.Reason)
// mariadbJobFailedMessage names the operator object whose Job failed and
// carries that Job's own Failed condition, because the operator's condition
// message is only "Failed". The operator names the Job after the object.
func mariadbJobFailedMessage(ctx context.Context, c client.Reader, kind, namespace, name string, cond *metav1.Condition) string {
message := fmt.Sprintf("k8s.mariadb.com/%s %s/%s failed (Complete=%s/%s)", kind, namespace, name, cond.Status, cond.Reason)
job := &batchv1.Job{}
if err := c.Get(ctx, types.NamespacedName{Namespace: namespace, Name: name}, job); err != nil {
if apierrors.IsNotFound(err) {
return fmt.Sprintf("%s; Job %s/%s not found", message, namespace, name)
}
return fmt.Sprintf("%s; reading Job %s/%s: %v", message, namespace, name, err)
}
for _, jc := range job.Status.Conditions {
if jc.Type == batchv1.JobFailed && jc.Status == corev1.ConditionTrue {
return fmt.Sprintf("%s; Job %s/%s: %s: %s", message, namespace, name, jc.Reason, jc.Message)
}
}
return fmt.Sprintf("%s; Job %s/%s reports no Failed condition", message, namespace, name)
}
// mariadbBackupDeadlineExceeded reports whether enough wall-clock time has
@@ -572,7 +585,7 @@ func (r *RestoreJobReconciler) reconcileMariaDBRestore(ctx context.Context, rest
cond := apimeta.FindStatusCondition(mdbRestore.Status.Conditions, mariadbtypes.ConditionTypeComplete)
switch {
case cond != nil && cond.Reason == mariadbtypes.ConditionReasonJobFailed:
return r.markRestoreJobFailed(ctx, restoreJob, mariadbJobFailedMessage("Restore", mdbRestore.Namespace, mdbRestore.Name, cond))
return r.markRestoreJobFailed(ctx, restoreJob, mariadbJobFailedMessage(ctx, r.Client, "Restore", mdbRestore.Namespace, mdbRestore.Name, cond))
case cond != nil && cond.Status == metav1.ConditionTrue && cond.Reason == mariadbtypes.ConditionReasonJobComplete:
now := metav1.Now()
@@ -4,12 +4,15 @@ package backupcontroller
import (
"context"
"encoding/json"
"errors"
"strings"
"testing"
"time"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
@@ -17,6 +20,7 @@ import (
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
strategyv1alpha1 "github.com/cozystack/cozystack/api/backups/strategy/v1alpha1"
backupsv1alpha1 "github.com/cozystack/cozystack/api/backups/v1alpha1"
@@ -877,15 +881,18 @@ var mariadbCompleteOutcomeCases = []struct {
reason string
message string
expired bool
failedJob bool
wantFailed bool
wantDone bool
wantMessage []string
}{
{name: "Complete=True/JobComplete succeeds", status: metav1.ConditionTrue, reason: mariadbtypes.ConditionReasonJobComplete, message: "Success", wantDone: true},
{name: "Complete=True/JobFailed fails", status: metav1.ConditionTrue, reason: mariadbtypes.ConditionReasonJobFailed, message: "Failed", wantFailed: true,
wantMessage: []string{"tenant/op"}},
{name: "Complete=False/JobFailed fails", status: metav1.ConditionFalse, reason: mariadbtypes.ConditionReasonJobFailed, message: "Failed", wantFailed: true,
wantMessage: []string{"tenant/op"}},
{name: "Complete=True/JobFailed fails with the Job's failure", status: metav1.ConditionTrue, reason: mariadbtypes.ConditionReasonJobFailed, message: "Failed", failedJob: true, wantFailed: true,
wantMessage: []string{"tenant/op", "BackoffLimitExceeded", "Job has reached the specified backoff limit"}},
{name: "Complete=False/JobFailed fails with the Job's failure", status: metav1.ConditionFalse, reason: mariadbtypes.ConditionReasonJobFailed, message: "Failed", failedJob: true, wantFailed: true,
wantMessage: []string{"tenant/op", "BackoffLimitExceeded", "Job has reached the specified backoff limit"}},
{name: "Complete=True/JobFailed with its Job gone fails", status: metav1.ConditionTrue, reason: mariadbtypes.ConditionReasonJobFailed, message: "Failed", wantFailed: true,
wantMessage: []string{"tenant/op", "Job tenant/op not found"}},
{name: "Complete=True with an unknown reason keeps waiting", status: metav1.ConditionTrue, reason: "SomethingElse", message: "Something"},
{name: "Complete=False/JobComplete keeps waiting", status: metav1.ConditionFalse, reason: mariadbtypes.ConditionReasonJobComplete, message: "Success"},
{name: "Complete=False/JobRunning keeps waiting", status: metav1.ConditionFalse, reason: mariadbtypes.ConditionReasonJobRunning, message: "Running"},
@@ -920,8 +927,29 @@ func mariadbOutcomeStartedAt(expired bool) *metav1.Time {
return &past
}
// mariadbOutcomeObjects adds the operator's Job, named after the operator
// Backup or Restore, failed the way the Job controller fails it once the
// backoff limit is spent.
func mariadbOutcomeObjects(failedJob bool, objs ...client.Object) []client.Object {
if !failedJob {
return objs
}
return append(objs, &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{Namespace: "tenant", Name: "op"},
Status: batchv1.JobStatus{Conditions: []batchv1.JobCondition{{
Type: batchv1.JobFailed,
Status: corev1.ConditionTrue,
Reason: batchv1.JobReasonBackoffLimitExceeded,
Message: "Job has reached the specified backoff limit",
}}},
})
}
func assertMariaDBFailureMessage(t *testing.T, message string, want []string) {
t.Helper()
if strings.Contains(message, "logs") {
t.Errorf("failure message must not send the reader to Job logs, which are gone with the deleted pod: %q", message)
}
for _, w := range want {
if !strings.Contains(message, w) {
t.Errorf("failure message must contain %q, got %q", w, message)
@@ -961,8 +989,8 @@ func TestReconcileMariaDBBackup_CompleteConditionOutcome(t *testing.T) {
},
Status: mariadbtypes.BackupStatus{Conditions: mariadbCompleteConditions(tc.noCondition, tc.status, tc.reason, tc.message)},
}
c := newMariaDBStrategyTestClient(t, seed, newMariaDBApp(seed.Spec.ApplicationRef.Name, "tenant"),
strategy, operatorMariaDB, operatorBackup)
c := newMariaDBStrategyTestClient(t, mariadbOutcomeObjects(tc.failedJob, seed, newMariaDBApp(seed.Spec.ApplicationRef.Name, "tenant"),
strategy, operatorMariaDB, operatorBackup)...)
r := &BackupJobReconciler{Client: c, Scheme: c.Scheme(), Recorder: record.NewFakeRecorder(10)}
bj := &backupsv1alpha1.BackupJob{}
@@ -1013,6 +1041,43 @@ func TestReconcileMariaDBBackup_CompleteConditionOutcome(t *testing.T) {
}
}
func TestMariaDBJobFailedMessage_JobUnreadable(t *testing.T) {
cond := &metav1.Condition{Type: mariadbtypes.ConditionTypeComplete, Status: metav1.ConditionTrue, Reason: mariadbtypes.ConditionReasonJobFailed}
readErr := apierrors.NewForbidden(batchv1.Resource("jobs"), "op", errors.New("rbac"))
c := interceptor.NewClient(newMariaDBStrategyTestClient(t).(client.WithWatch), interceptor.Funcs{
Get: func(ctx context.Context, cl client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
if _, ok := obj.(*batchv1.Job); ok {
return readErr
}
return cl.Get(ctx, key, obj, opts...)
},
})
got := mariadbJobFailedMessage(context.Background(), c, "Backup", "tenant", "op", cond)
for _, want := range []string{"k8s.mariadb.com/Backup tenant/op failed (Complete=True/JobFailed)", "reading Job tenant/op", readErr.Error()} {
if !strings.Contains(got, want) {
t.Errorf("message must contain %q, got %q", want, got)
}
}
}
func TestMariaDBJobFailedMessage_JobWithoutFailedCondition(t *testing.T) {
cond := &metav1.Condition{Type: mariadbtypes.ConditionTypeComplete, Status: metav1.ConditionTrue, Reason: mariadbtypes.ConditionReasonJobFailed}
c := newMariaDBStrategyTestClient(t, &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{Namespace: "tenant", Name: "op"},
Status: batchv1.JobStatus{Conditions: []batchv1.JobCondition{
{Type: batchv1.JobFailureTarget, Status: corev1.ConditionTrue, Reason: batchv1.JobReasonPodFailurePolicy},
{Type: batchv1.JobFailed, Status: corev1.ConditionFalse, Reason: batchv1.JobReasonBackoffLimitExceeded},
}},
})
got := mariadbJobFailedMessage(context.Background(), c, "Restore", "tenant", "op", cond)
if !strings.Contains(got, "Job tenant/op reports no Failed condition") ||
strings.Contains(got, batchv1.JobReasonBackoffLimitExceeded) || strings.Contains(got, batchv1.JobReasonPodFailurePolicy) {
t.Errorf("a Failed condition that is not True must not be reported as the cause, got %q", got)
}
}
func TestReconcileMariaDBRestore_CompleteConditionOutcome(t *testing.T) {
apps := mariadbapp.GroupName
@@ -1044,9 +1109,9 @@ func TestReconcileMariaDBRestore_CompleteConditionOutcome(t *testing.T) {
},
Status: mariadbtypes.RestoreStatus{Conditions: mariadbCompleteConditions(tc.noCondition, tc.status, tc.reason, tc.message)},
}
c := newMariaDBStrategyTestClient(t, cozyBackup, seed, operatorRestore,
c := newMariaDBStrategyTestClient(t, mariadbOutcomeObjects(tc.failedJob, cozyBackup, seed, operatorRestore,
&mariadbtypes.Backup{ObjectMeta: metav1.ObjectMeta{Namespace: "tenant", Name: "src-op-bk"}},
&mariadbtypes.MariaDB{ObjectMeta: metav1.ObjectMeta{Namespace: "tenant", Name: mariadbNameForApp("target")}})
&mariadbtypes.MariaDB{ObjectMeta: metav1.ObjectMeta{Namespace: "tenant", Name: mariadbNameForApp("target")}})...)
r := &RestoreJobReconciler{Client: c, Recorder: record.NewFakeRecorder(10)}
rj := &backupsv1alpha1.RestoreJob{}