Skip to content

Commit 6d6e9c1

Browse files
authored
Merge pull request #678 from alvaroaleman/cache-no-leader-election
🐛 InformerCache: Do not require leader lease
2 parents 1fbc224 + e3ad016 commit 6d6e9c1

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

pkg/cache/informer_cache.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ func (ip *informerCache) GetInformer(obj runtime.Object) (Informer, error) {
140140
return i.Informer, err
141141
}
142142

143+
// NeedLeaderElection implements the LeaderElectionRunnable interface
144+
// to indicate that this can be started without requiring the leader lock
145+
func (ip *informerCache) NeedLeaderElection() bool {
146+
return false
147+
}
148+
143149
// IndexField adds an indexer to the underlying cache, using extraction function to get
144150
// value(s) from the given field. This index can then be used by passing a field selector
145151
// to List. For one-to-one compatibility with "normal" field selectors, only return one value.

pkg/cache/informer_cache_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cache_test
2+
3+
import (
4+
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/gomega"
6+
7+
"k8s.io/client-go/rest"
8+
9+
"sigs.k8s.io/controller-runtime/pkg/cache"
10+
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
11+
"sigs.k8s.io/controller-runtime/pkg/manager"
12+
)
13+
14+
var _ = Describe("informerCache", func() {
15+
It("should not require LeaderElection", func() {
16+
cfg := &rest.Config{}
17+
18+
mapper, err := apiutil.NewDynamicRESTMapper(cfg, apiutil.WithLazyDiscovery)
19+
Expect(err).ToNot(HaveOccurred())
20+
21+
c, err := cache.New(cfg, cache.Options{Mapper: mapper})
22+
Expect(err).ToNot(HaveOccurred())
23+
24+
leaderElectionRunnable, ok := c.(manager.LeaderElectionRunnable)
25+
Expect(ok).To(BeTrue())
26+
Expect(leaderElectionRunnable.NeedLeaderElection()).To(BeFalse())
27+
})
28+
})

0 commit comments

Comments
 (0)