Add check for owner reference in backup sync, removing if missing

Signed-off-by: Jeffrey Koehler <koehler@streem.tech>
This commit is contained in:
Jeffrey Koehler
2023-10-29 22:06:14 -05:00
parent 23921e5d29
commit 929af4f734
2 changed files with 23 additions and 0 deletions

View File

@@ -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 {