Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
Signed-off-by: Tiger Kaovilai <[email protected]>
  • Loading branch information
kaovilai committed Feb 6, 2024
1 parent e9d1729 commit e02808f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 29 deletions.
17 changes: 7 additions & 10 deletions pkg/controller/backup_repository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ 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.NewCreateOrUpdateEventPredicate(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 Down Expand Up @@ -104,16 +109,8 @@ func (r *BackupRepoReconciler) invalidateBackupReposForBSL(bslObj client.Object)
return []reconcile.Request{}
}

// needInvalidBackupRepo returns true if the BSL's storage type, bucket, prefix, CACert, or config has changed or if BSL was created.
// 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 {
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
}

oldBSL := oldObj.(*velerov1api.BackupStorageLocation)
newBSL := newObj.(*velerov1api.BackupStorageLocation)

Expand Down
19 changes: 0 additions & 19 deletions pkg/util/kube/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,25 +93,6 @@ 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
},
}
}

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

Expand Down

0 comments on commit e02808f

Please sign in to comment.