@@ -53,22 +53,23 @@ import (
5353// failover.
5454//
5555// RBAC: the caller’s client must be allowed to get/list/watch/create/update/patch
56- // Leases in namespace `ns` .
56+ // Leases in namespace.
5757type leaseGuard struct {
58- c client.Client
59- ns , nm string
60- id string
61- ldur time.Duration // lease duration
62- renew time.Duration // renew period
63- onLost func () // callback when we lose the lease
58+ client client.Client
59+ namespace string
60+ name string
61+ id string
62+ ldur time.Duration // lease duration
63+ renew time.Duration // renew period
64+ onLost func () // callback when we lose the lease
6465
6566 held bool
6667 cancel context.CancelFunc
6768}
6869
6970// newLeaseGuard builds a guard; it does not contact the API server.
70- func newLeaseGuard (c client.Client , ns , name , id string , ldur , renew time.Duration , onLost func ()) * leaseGuard {
71- return & leaseGuard {c : c , ns : ns , nm : name , id : id , ldur : ldur , renew : renew , onLost : onLost }
71+ func newLeaseGuard (client client.Client , namespace , name , id string , ldur , renew time.Duration , onLost func ()) * leaseGuard {
72+ return & leaseGuard {client : client , namespace : namespace , name : name , id : id , ldur : ldur , renew : renew , onLost : onLost }
7273}
7374
7475// TryAcquire creates/adopts the Lease for g.id and starts renewing it.
@@ -79,25 +80,25 @@ func (g *leaseGuard) TryAcquire(ctx context.Context) bool {
7980 return true
8081 }
8182
82- key := types.NamespacedName {Namespace : g .ns , Name : g .nm }
83+ key := types.NamespacedName {Namespace : g .namespace , Name : g .name }
8384 now := metav1 .NowMicro ()
8485
8586 ldurSec := int32 (g .ldur / time .Second )
8687
8788 var ls coordinationv1.Lease
88- err := g .c .Get (ctx , key , & ls )
89+ err := g .client .Get (ctx , key , & ls )
8990 switch {
9091 case apierrors .IsNotFound (err ):
9192 ls = coordinationv1.Lease {
92- ObjectMeta : metav1.ObjectMeta {Namespace : g .ns , Name : g .nm },
93+ ObjectMeta : metav1.ObjectMeta {Namespace : g .namespace , Name : g .name },
9394 Spec : coordinationv1.LeaseSpec {
9495 HolderIdentity : & g .id ,
9596 LeaseDurationSeconds : & ldurSec ,
9697 AcquireTime : & now ,
9798 RenewTime : & now ,
9899 },
99100 }
100- if err := g .c .Create (ctx , & ls ); err != nil {
101+ if err := g .client .Create (ctx , & ls ); err != nil {
101102 return false
102103 }
103104 case err != nil :
@@ -120,7 +121,7 @@ func (g *leaseGuard) TryAcquire(ctx context.Context) bool {
120121 ls .Spec .AcquireTime = & now
121122 }
122123 ls .Spec .RenewTime = & now
123- if err := g .c .Update (ctx , & ls ); err != nil {
124+ if err := g .client .Update (ctx , & ls ); err != nil {
124125 return false
125126 }
126127 }
@@ -158,7 +159,7 @@ func (g *leaseGuard) renewLoop(ctx context.Context, key types.NamespacedName) {
158159func (g * leaseGuard ) renewOnce (ctx context.Context , key types.NamespacedName ) bool {
159160 now := metav1 .NowMicro ()
160161 var ls coordinationv1.Lease
161- if err := g .c .Get (ctx , key , & ls ); err != nil {
162+ if err := g .client .Get (ctx , key , & ls ); err != nil {
162163 return false
163164 }
164165 // another holder?
@@ -170,7 +171,7 @@ func (g *leaseGuard) renewOnce(ctx context.Context, key types.NamespacedName) bo
170171 ls .Spec .HolderIdentity = & g .id
171172 ls .Spec .LeaseDurationSeconds = & ldurSec
172173 ls .Spec .RenewTime = & now
173- if err := g .c .Update (ctx , & ls ); err != nil {
174+ if err := g .client .Update (ctx , & ls ); err != nil {
174175 return false
175176 }
176177 return true
@@ -186,14 +187,14 @@ func (g *leaseGuard) Release(ctx context.Context) {
186187 }
187188 g .held = false
188189
189- key := types.NamespacedName {Namespace : g .ns , Name : g .nm }
190+ key := types.NamespacedName {Namespace : g .namespace , Name : g .name }
190191 var ls coordinationv1.Lease
191- if err := g .c .Get (ctx , key , & ls ); err == nil {
192+ if err := g .client .Get (ctx , key , & ls ); err == nil {
192193 if ls .Spec .HolderIdentity != nil && * ls .Spec .HolderIdentity == g .id {
193194 empty := ""
194195 ls .Spec .HolderIdentity = & empty
195196 // keep RenewTime/AcquireTime; just clear holder
196- _ = g .c .Update (ctx , & ls ) // ignore errors
197+ _ = g .client .Update (ctx , & ls ) // ignore errors
197198 }
198199 }
199200}
0 commit comments