From 929af4f73496c3e091a66f5db746f2e63b56b374 Mon Sep 17 00:00:00 2001 From: Jeffrey Koehler Date: Sun, 29 Oct 2023 22:06:14 -0500 Subject: [PATCH] Add check for owner reference in backup sync, removing if missing Signed-off-by: Jeffrey Koehler --- changelogs/unreleased/7031-deefdragon | 1 + pkg/controller/backup_sync_controller.go | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 changelogs/unreleased/7031-deefdragon diff --git a/changelogs/unreleased/7031-deefdragon b/changelogs/unreleased/7031-deefdragon new file mode 100644 index 0000000000..430b8c0560 --- /dev/null +++ b/changelogs/unreleased/7031-deefdragon @@ -0,0 +1 @@ +Fix #7031. Added check for Owner References when synchronizing backups, removing references that are not found. \ No newline at end of file diff --git a/pkg/controller/backup_sync_controller.go b/pkg/controller/backup_sync_controller.go index 2fb61d6032..3b2c758ba3 100644 --- a/pkg/controller/backup_sync_controller.go +++ b/pkg/controller/backup_sync_controller.go @@ -29,6 +29,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/sets" "sigs.k8s.io/controller-runtime/pkg/builder" @@ -171,6 +172,27 @@ func (b *backupSyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) } backup.Labels[velerov1api.StorageLocationLabel] = label.GetValidName(backup.Spec.StorageLocation) + //check for the backup schedule. If it does not exist, remove it. + listedReferences := backup.ObjectMeta.OwnerReferences + foundReferences := make([]metav1.OwnerReference, 0) + for _, v := range listedReferences { + schedule := new(velerov1api.Schedule) + err := b.client.Get(ctx, types.NamespacedName{ + Name: v.Name, + Namespace: backup.Namespace, + }, schedule) + switch { + case err != nil && apierrors.IsNotFound(err): + log.Debug("Removing missing schedule ownership reference from backup") + case err != nil && !apierrors.IsNotFound(err): + log.WithError(errors.WithStack(err)).Error("Error finding ownership reference schedule") + fallthrough + default: + foundReferences = append(foundReferences, v) + } + } + backup.ObjectMeta.OwnerReferences = foundReferences + // attempt to create backup custom resource via API err = b.client.Create(ctx, backup, &client.CreateOptions{}) switch {