@@ -15,17 +15,35 @@ import (
15
15
"sigs.k8s.io/controller-runtime/pkg/log"
16
16
)
17
17
18
- // LeaseHijacker implements lease stealing to accelerate development workflows
19
- // TODO: migrate to https://kubernetes.io/docs/concepts/cluster-administration/coordinated-leader-election/ when it's past alpha.
18
+ /*
19
+ LeaseHijacker implements lease stealing to accelerate development workflows.
20
+ When starting your controller manager, your local process will forcibly
21
+ become leader. This is useful when developing locally against a cluster
22
+ which already has a controller running in it
23
+
24
+ TODO: migrate to https://kubernetes.io/docs/concepts/cluster-administration/coordinated-leader-election/ when it's past alpha.
25
+
26
+ Include this in your controller manager as follows:
27
+ ```
28
+ controllerruntime.NewManager(..., controllerruntime.Options{
29
+
30
+ // Used if HIJACK_LEASE is not set
31
+ LeaderElectionID: name,
32
+ LeaderElectionNamespace: "namespace",
33
+
34
+ // Used if HIJACK_LEASE=true
35
+ LeaderElectionResourceLockInterface: leaderelection.LeaseHijacker(...)
36
+ }
37
+ ```
38
+ */
20
39
func LeaseHijacker (ctx context.Context , config * rest.Config , namespace string , name string ) resourcelock.Interface {
21
40
if os .Getenv ("HIJACK_LEASE" ) != "true" {
22
41
return nil // If not set, fallback to other controller-runtime lease settings
23
42
}
24
- id := fmt .Sprintf ("%s_%s" , lo .Must (os .Hostname ()), uuid .NewUUID ())
25
43
log .FromContext (ctx ).Info ("hijacking lease" , "namespace" , namespace , "name" , name )
26
44
kubeClient := coordinationv1client .NewForConfigOrDie (config )
27
45
lease := lo .Must (kubeClient .Leases (namespace ).Get (ctx , name , metav1.GetOptions {}))
28
- lease .Spec .HolderIdentity = lo .ToPtr (id )
46
+ lease .Spec .HolderIdentity = lo .ToPtr (fmt . Sprintf ( "%s_%s" , lo . Must ( os . Hostname ()), uuid . NewUUID ()) )
29
47
lease .Spec .AcquireTime = lo .ToPtr (metav1 .NowMicro ())
30
48
lo .Must (kubeClient .Leases (namespace ).Update (ctx , lease , metav1.UpdateOptions {}))
31
49
return lo .Must (resourcelock .New (
@@ -34,6 +52,6 @@ func LeaseHijacker(ctx context.Context, config *rest.Config, namespace string, n
34
52
name ,
35
53
corev1client .NewForConfigOrDie (config ),
36
54
coordinationv1client .NewForConfigOrDie (config ),
37
- resourcelock.ResourceLockConfig {Identity : id },
55
+ resourcelock.ResourceLockConfig {Identity : * lease . Spec . HolderIdentity },
38
56
))
39
57
}
0 commit comments