Skip to content

🐛 InformerCache: Do not require leader lease #678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/cache/informer_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ func (ip *informerCache) GetInformer(obj runtime.Object) (Informer, error) {
return i.Informer, err
}

// NeedLeaderElection implements the LeaderElectionRunnable interface
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately we can not verify the interface implementation via a var _ manager.LeaderElectionRunnable = &informerCache{} as that would result in an import cycle

// to indicate that this can be started without requiring the leader lock
func (ip *informerCache) NeedLeaderElection() bool {
return false
}

// IndexField adds an indexer to the underlying cache, using extraction function to get
// value(s) from the given field. This index can then be used by passing a field selector
// to List. For one-to-one compatibility with "normal" field selectors, only return one value.
Expand Down
28 changes: 28 additions & 0 deletions pkg/cache/informer_cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cache_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"k8s.io/client-go/rest"

"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/manager"
)

var _ = Describe("informerCache", func() {
It("should not require LeaderElection", func() {
cfg := &rest.Config{}

mapper, err := apiutil.NewDynamicRESTMapper(cfg, apiutil.WithLazyDiscovery)
Expect(err).ToNot(HaveOccurred())

c, err := cache.New(cfg, cache.Options{Mapper: mapper})
Expect(err).ToNot(HaveOccurred())
Copy link
Member Author

@alvaroaleman alvaroaleman Nov 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find a matcher for "type X implements interface Y", the closed to that I found is BeAssignableToTypeOf but that errors out if one of the two is nil.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is clear enough. If you're really worried, you can always add an explanation after the "be true" part as a format string, but I think this is readable enough for now.


leaderElectionRunnable, ok := c.(manager.LeaderElectionRunnable)
Expect(ok).To(BeTrue())
Expect(leaderElectionRunnable.NeedLeaderElection()).To(BeFalse())
})
})