@@ -29,6 +29,7 @@ import (
29
29
30
30
"github.com/go-logr/logr"
31
31
. "github.com/onsi/ginkgo"
32
+ . "github.com/onsi/ginkgo/extensions/table"
32
33
. "github.com/onsi/gomega"
33
34
"github.com/prometheus/client_golang/prometheus"
34
35
"go.uber.org/goleak"
@@ -296,30 +297,48 @@ var _ = Describe("manger.Manager", func() {
296
297
<- m2done
297
298
})
298
299
300
+ It ("should return an error if it can't create a ResourceLock" , func () {
301
+ m , err := New (cfg , Options {
302
+ newResourceLock : func (_ * rest.Config , _ recorder.Provider , _ leaderelection.Options ) (resourcelock.Interface , error ) {
303
+ return nil , fmt .Errorf ("expected error" )
304
+ },
305
+ })
306
+ Expect (m ).To (BeNil ())
307
+ Expect (err ).To (MatchError (ContainSubstring ("expected error" )))
308
+ })
299
309
It ("should return an error if namespace not set and not running in cluster" , func () {
300
310
m , err := New (cfg , Options {LeaderElection : true , LeaderElectionID : "controller-runtime" })
301
311
Expect (m ).To (BeNil ())
302
312
Expect (err ).To (HaveOccurred ())
303
313
Expect (err .Error ()).To (ContainSubstring ("unable to find leader election namespace: not running in-cluster, please specify LeaderElectionNamespace" ))
304
314
})
305
315
306
- // We must keep this default until we are sure all controller-runtime users have upgraded from the original default
307
- // ConfigMap lock to a controller-runtime version that has this new default. Many users of controller-runtime skip
308
- // versions, so we should be extremely conservative here.
309
- It ("should default to ConfigMapsLeasesResourceLock" , func () {
310
- m , err := New (cfg , Options {LeaderElection : true , LeaderElectionID : "controller-runtime" , LeaderElectionNamespace : "my-ns" })
316
+ DescribeTable ("should create the correct ResourceLock" , func (resourceLock string , expectMultiLock bool , expectedLocks ... resourcelock.Interface ) {
317
+ m , err := New (cfg , Options {LeaderElection : true , LeaderElectionResourceLock : resourceLock , LeaderElectionID : "controller-runtime" , LeaderElectionNamespace : "my-ns" })
311
318
Expect (m ).ToNot (BeNil ())
312
319
Expect (err ).ToNot (HaveOccurred ())
313
320
cm , ok := m .(* controllerManager )
314
321
Expect (ok ).To (BeTrue ())
315
- multilock , isMultiLock := cm .resourceLock .(* resourcelock.MultiLock )
316
- Expect (isMultiLock ).To (BeTrue ())
317
- _ , primaryIsConfigMapLock := multilock .Primary .(* resourcelock.ConfigMapLock )
318
- Expect (primaryIsConfigMapLock ).To (BeTrue ())
319
- _ , secondaryIsLeaseLock := multilock .Secondary .(* resourcelock.LeaseLock )
320
- Expect (secondaryIsLeaseLock ).To (BeTrue ())
321
322
322
- })
323
+ if expectMultiLock {
324
+ Expect (cm .resourceLock ).To (BeAssignableToTypeOf (& resourcelock.MultiLock {}))
325
+ multiLock := cm .resourceLock .(* resourcelock.MultiLock )
326
+ Expect (multiLock .Primary ).To (BeAssignableToTypeOf (expectedLocks [0 ]))
327
+ Expect (multiLock .Secondary ).To (BeAssignableToTypeOf (expectedLocks [1 ]))
328
+ } else {
329
+ Expect (cm .resourceLock ).To (BeAssignableToTypeOf (expectedLocks [0 ]))
330
+ }
331
+ },
332
+ // We must keep this default until we are sure all controller-runtime users have upgraded from the original default
333
+ // ConfigMap lock to a controller-runtime version that has this new default. Many users of controller-runtime skip
334
+ // versions, so we should be extremely conservative here.
335
+ Entry ("not specified (should default to ConfigMapsLeasesResourceLock)" , "" , true , & resourcelock.ConfigMapLock {}, & resourcelock.LeaseLock {}),
336
+ Entry ("EndpointsResourceLock" , resourcelock .EndpointsResourceLock , false , & resourcelock.EndpointsLock {}),
337
+ Entry ("ConfigMapsResourceLock" , resourcelock .ConfigMapsResourceLock , false , & resourcelock.ConfigMapLock {}),
338
+ Entry ("LeasesResourceLock" , resourcelock .LeasesResourceLock , false , & resourcelock.LeaseLock {}),
339
+ Entry ("EndpointsLeasesResourceLock" , resourcelock .EndpointsLeasesResourceLock , true , & resourcelock.EndpointsLock {}, & resourcelock.LeaseLock {}),
340
+ Entry ("ConfigMapsLeasesResourceLock" , resourcelock .ConfigMapsLeasesResourceLock , true , & resourcelock.ConfigMapLock {}, & resourcelock.LeaseLock {}),
341
+ )
323
342
})
324
343
325
344
It ("should create a listener for the metrics if a valid address is provided" , func () {
0 commit comments