Skip to content

Commit

Permalink
✨ Add error check for multiple apiTypes for the reconciliation
Browse files Browse the repository at this point in the history
  • Loading branch information
prafull01 committed Sep 24, 2020
1 parent 5757a38 commit 67e49ce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/builder/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,18 @@ func ControllerManagedBy(m manager.Manager) *Builder {
type ForInput struct {
object runtime.Object
predicates []predicate.Predicate
err error
}

// For defines the type of Object being *reconciled*, and configures the ControllerManagedBy to respond to create / delete /
// update events by *reconciling the object*.
// This is the equivalent of calling
// Watches(&source.Kind{Type: apiType}, &handler.EnqueueRequestForObject{})
func (blder *Builder) For(object runtime.Object, opts ...ForOption) *Builder {
if blder.forInput.object != nil {
blder.forInput.err = fmt.Errorf("For(...) should only be called once, could not assign multiple objects for reconciliation")
return blder
}
input := ForInput{object: object}
for _, opt := range opts {
opt.ApplyToFor(&input)
Expand Down Expand Up @@ -159,6 +164,9 @@ func (blder *Builder) Build(r reconcile.Reconciler) (controller.Controller, erro
if blder.mgr == nil {
return nil, fmt.Errorf("must provide a non-nil Manager")
}
if blder.forInput.err != nil {
return nil, blder.forInput.err
}

// Set the Config
blder.loadRestConfig()
Expand Down
14 changes: 14 additions & 0 deletions pkg/builder/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ var _ = Describe("application", func() {
Expect(instance).NotTo(BeNil())
})

It("should return error if given two apiType objects in For function", func() {
By("creating a controller manager")
m, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

instance, err := ControllerManagedBy(m).
For(&appsv1.ReplicaSet{}).
For(&appsv1.Deployment{}).
Owns(&appsv1.ReplicaSet{}).
Build(noop)
Expect(err).To(MatchError(ContainSubstring("For(...) should only be called once, could not assign multiple objects for reconciliation")))
Expect(instance).To(BeNil())
})

It("should return an error if there is no GVK for an object, and thus we can't default the controller name", func() {
By("creating a controller manager")
m, err := manager.New(cfg, manager.Options{})
Expand Down

0 comments on commit 67e49ce

Please sign in to comment.