Skip to content

Commit

Permalink
Merge pull request #7399 from kaovilai/restic-recreate-repo-vel1.13
Browse files Browse the repository at this point in the history
release-1.13: BackupRepositories associated with a BSL are invalidated when BSL is (re-)created. (#7380)
  • Loading branch information
Lyndon-Li authored Feb 20, 2024
2 parents 5dd1d3b + 62a5317 commit 51a90e7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/7399-kaovilai
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BackupRepositories associated with a BSL are invalidated when BSL is (re-)created.
11 changes: 8 additions & 3 deletions pkg/controller/backup_repository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ func (r *BackupRepoReconciler) SetupWithManager(mgr ctrl.Manager) error {
For(&velerov1api.BackupRepository{}).
Watches(s, nil).
Watches(&source.Kind{Type: &velerov1api.BackupStorageLocation{}}, kube.EnqueueRequestsFromMapUpdateFunc(r.invalidateBackupReposForBSL),
builder.WithPredicates(kube.NewUpdateEventPredicate(r.needInvalidBackupRepo))).
builder.WithPredicates(
// When BSL updates, check if the backup repositories need to be invalidated
kube.NewUpdateEventPredicate(r.needInvalidBackupRepo),
// When BSL is created, invalidate any backup repositories that reference it
kube.NewCreateEventPredicate(func(client.Object) bool { return true }))).
Complete(r)
}

Expand All @@ -90,20 +94,21 @@ func (r *BackupRepoReconciler) invalidateBackupReposForBSL(bslObj client.Object)
}).AsSelector(),
}
if err := r.List(context.TODO(), list, options); err != nil {
r.logger.WithField("BSL", bsl.Name).WithError(err).Error("unable to list BackupRepositorys")
r.logger.WithField("BSL", bsl.Name).WithError(err).Error("unable to list BackupRepositories")
return []reconcile.Request{}
}

for i := range list.Items {
r.logger.WithField("BSL", bsl.Name).Infof("Invalidating Backup Repository %s", list.Items[i].Name)
if err := r.patchBackupRepository(context.Background(), &list.Items[i], repoNotReady("re-establish on BSL change")); err != nil {
if err := r.patchBackupRepository(context.Background(), &list.Items[i], repoNotReady("re-establish on BSL change or create")); err != nil {
r.logger.WithField("BSL", bsl.Name).WithError(err).Errorf("fail to patch BackupRepository %s", list.Items[i].Name)
}
}

return []reconcile.Request{}
}

// needInvalidBackupRepo returns true if the BSL's storage type, bucket, prefix, CACert, or config has changed
func (r *BackupRepoReconciler) needInvalidBackupRepo(oldObj client.Object, newObj client.Object) bool {
oldBSL := oldObj.(*velerov1api.BackupStorageLocation)
newBSL := newObj.(*velerov1api.BackupStorageLocation)
Expand Down
6 changes: 2 additions & 4 deletions pkg/util/kube/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ import (

type MapUpdateFunc func(client.Object) []reconcile.Request

// EnqueueRequestsFromMapUpdateFunc is for the same purpose with EnqueueRequestsFromMapFunc.
// Merely, it is more friendly to updating the mapped objects in the MapUpdateFunc, because
// on Update event, MapUpdateFunc is called for only once with the new object, so if MapUpdateFunc
// does some update to the mapped objects, the update is done for once
// EnqueueRequestsFromMapUpdateFunc has the same purpose with handler.EnqueueRequestsFromMapFunc.
// MapUpdateFunc is simpler on Update event because mapAndEnqueue is called once with the new object. EnqueueRequestsFromMapFunc is called twice with the old and new object.
func EnqueueRequestsFromMapUpdateFunc(fn MapUpdateFunc) handler.EventHandler {
return &enqueueRequestsFromMapFunc{
toRequests: fn,
Expand Down

0 comments on commit 51a90e7

Please sign in to comment.