@@ -25,9 +25,11 @@ import (
25
25
"k8s.io/apimachinery/pkg/api/meta"
26
26
"k8s.io/apimachinery/pkg/runtime"
27
27
"k8s.io/client-go/rest"
28
+ "k8s.io/client-go/tools/leaderelection/resourcelock"
28
29
"sigs.k8s.io/controller-runtime/pkg/cache"
29
30
"sigs.k8s.io/controller-runtime/pkg/cache/informertest"
30
31
"sigs.k8s.io/controller-runtime/pkg/client"
32
+ "sigs.k8s.io/controller-runtime/pkg/leaderelection"
31
33
fakeleaderelection "sigs.k8s.io/controller-runtime/pkg/leaderelection/fake"
32
34
"sigs.k8s.io/controller-runtime/pkg/reconcile"
33
35
"sigs.k8s.io/controller-runtime/pkg/recorder"
@@ -98,18 +100,27 @@ var _ = Describe("manger.Manager", func() {
98
100
close (done )
99
101
})
100
102
Context ("with leader election enabled" , func () {
101
- It ("should return an error if ID not set" , func () {
102
- m , err := New (cfg , Options {LeaderElection : true , LeaderElectionNamespace : "default" })
103
- Expect (m ).To (BeNil ())
104
- Expect (err ).To (HaveOccurred ())
105
- Expect (err .Error ()).To (ContainSubstring ("if leader election is enabled, both LeaderElectionID and LeaderElectionNamespace must be set" ))
103
+ It ("should default ID to controller-runtime if ID is not set" , func () {
104
+ var rl resourcelock.Interface
105
+ m , err := New (cfg , Options {
106
+ LeaderElection : true ,
107
+ LeaderElectionNamespace : "default" ,
108
+ newResourceLock : func (config * rest.Config , recorderProvider recorder.Provider , options leaderelection.Options ) (resourcelock.Interface , error ) {
109
+ var err error
110
+ rl , err = leaderelection .NewResourceLock (config , recorderProvider , options )
111
+ return rl , err
112
+ },
113
+ })
114
+ Expect (m ).ToNot (BeNil ())
115
+ Expect (err ).ToNot (HaveOccurred ())
116
+ Expect (rl .Describe ()).To (Equal ("default/controller-leader-election-helper" ))
106
117
})
107
118
108
- It ("should return an error if namespace not set" , func () {
119
+ It ("should return an error if namespace not set and not running in cluster " , func () {
109
120
m , err := New (cfg , Options {LeaderElection : true , LeaderElectionID : "controller-runtime" })
110
121
Expect (m ).To (BeNil ())
111
122
Expect (err ).To (HaveOccurred ())
112
- Expect (err .Error ()).To (ContainSubstring ("if leader election is enabled, both LeaderElectionID and LeaderElectionNamespace must be set " ))
123
+ Expect (err .Error ()).To (ContainSubstring ("unable to find leader election namespace: not running in-cluster, please specify LeaderElectionNamespace " ))
113
124
})
114
125
})
115
126
})
0 commit comments