Check only schedules, and verify UIDs are the same

Signed-off-by: Jeffrey Koehler <koehler@streem.tech>
This commit is contained in:
Jeffrey Koehler
2023-10-31 23:01:50 -05:00
parent 929af4f734
commit 8eec6865d1
3 changed files with 23 additions and 14 deletions

View File

@@ -1 +0,0 @@
Fix #7031. Added check for Owner References when synchronizing backups, removing references that are not found.

View File

@@ -0,0 +1 @@
Fix #6857. Added check for matching Owner References when synchronizing backups, removing references that are not found/have mismatched uid.

View File

@@ -172,24 +172,33 @@ 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.
//check for the ownership references. If they do not exist, remove them.
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
switch v.Kind {
case "Schedule":
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.Warn("Removing missing schedule ownership reference from backup")
continue
case schedule.UID != v.UID:
log.Warnf("Removing schedule ownership reference with mismatched UIDs. Expected %s, got %s", v.UID, schedule.UID)
continue
case err != nil && !apierrors.IsNotFound(err):
log.WithError(errors.WithStack(err)).Error("Error finding schedule ownership reference, keeping schedule on backup")
}
default:
foundReferences = append(foundReferences, v)
log.Warnf("Unable to check ownership reference for unknown kind, %s", v.Kind)
}
foundReferences = append(foundReferences, v)
}
backup.ObjectMeta.OwnerReferences = foundReferences