Skip to content

Commit

Permalink
Add BackupRepositories invalidation on BSL Create
Browse files Browse the repository at this point in the history
Simplify comments

Signed-off-by: Tiger Kaovilai <tkaovila@redhat.com>
  • Loading branch information
kaovilai committed Feb 5, 2024
1 parent 2f25c25 commit e9d1729
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/7380-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.
15 changes: 12 additions & 3 deletions pkg/controller/backup_repository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ 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(kube.NewCreateOrUpdateEventPredicate(r.needInvalidBackupRepo))).

Check warning on line 79 in pkg/controller/backup_repository_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/backup_repository_controller.go#L79

Added line #L79 was not covered by tests
Complete(r)
}

Expand All @@ -90,21 +90,30 @@ 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")

Check warning on line 93 in pkg/controller/backup_repository_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/backup_repository_controller.go#L93

Added line #L93 was not covered by tests
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 {

Check warning on line 99 in pkg/controller/backup_repository_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/backup_repository_controller.go#L99

Added line #L99 was not covered by tests
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 or if BSL was created.
func (r *BackupRepoReconciler) needInvalidBackupRepo(oldObj client.Object, newObj client.Object) bool {
if oldObj == nil {
// BSL was created because oldBSL is nil
// Return true so invalidateBackupReposForBSL is triggered
// BSL creationTimestamp should be newer than the BackupRepository's creationTimestamp if any.
// BSL was created after the BackupRepository, so we need to re-establish the BackupRepository on BSL creation.
return true
}

Check warning on line 115 in pkg/controller/backup_repository_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/backup_repository_controller.go#L110-L115

Added lines #L110 - L115 were not covered by tests

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
19 changes: 19 additions & 0 deletions pkg/util/kube/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,25 @@ func NewUpdateEventPredicate(f func(client.Object, client.Object) bool) predicat
}
}

// NewCreateOrUpdateEventPredicate creates a new Predicate that checks the create or update events with the provided func
// and ignore others
func NewCreateOrUpdateEventPredicate(f func(client.Object, client.Object) bool) predicate.Predicate {
return predicate.Funcs{
UpdateFunc: func(event event.UpdateEvent) bool {
return f(event.ObjectOld, event.ObjectNew)
},
CreateFunc: func(event event.CreateEvent) bool {
return f(nil, event.Object)
},
DeleteFunc: func(event event.DeleteEvent) bool {
return false
},
GenericFunc: func(event event.GenericEvent) bool {
return false
},

Check warning on line 111 in pkg/util/kube/predicate.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/kube/predicate.go#L98-L111

Added lines #L98 - L111 were not covered by tests
}
}

// FalsePredicate always returns false for all kinds of events
type FalsePredicate struct{}

Expand Down

0 comments on commit e9d1729

Please sign in to comment.