Skip to content

Commit

Permalink
Add check for owner reference in backup sync, removing if missing
Browse files Browse the repository at this point in the history
Signed-off-by: Jeffrey Koehler <[email protected]>
  • Loading branch information
Jeffrey Koehler committed Oct 30, 2023
1 parent 23921e5 commit 929af4f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/7031-deefdragon
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix #7031. Added check for Owner References when synchronizing backups, removing references that are not found.
22 changes: 22 additions & 0 deletions pkg/controller/backup_sync_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)

Check warning on line 191 in pkg/controller/backup_sync_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/backup_sync_controller.go#L179-L191

Added lines #L179 - L191 were not covered by tests
}
}
backup.ObjectMeta.OwnerReferences = foundReferences

// attempt to create backup custom resource via API
err = b.client.Create(ctx, backup, &client.CreateOptions{})
switch {
Expand Down

0 comments on commit 929af4f

Please sign in to comment.