Skip to content

Commit 1f27757

Browse files
stttsk8s-publishing-bot
authored andcommitted
Review feedback
Signed-off-by: Dr. Stefan Schimanski <[email protected]> Kubernetes-commit: 68226b0501996fc86c9c2bddb7d61e6a64c91304
1 parent 2099375 commit 1f27757

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

tools/leaderelection/leasecandidate.go

+26-22
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package leaderelection
1818

1919
import (
2020
"context"
21+
"reflect"
2122
"time"
2223

2324
v1 "k8s.io/api/coordination/v1"
@@ -37,11 +38,15 @@ import (
3738

3839
const requeueInterval = 5 * time.Minute
3940

41+
type CacheSyncWaiter interface {
42+
WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool
43+
}
44+
4045
type LeaseCandidate struct {
41-
LeaseClient coordinationv1alpha1client.LeaseCandidateInterface
42-
LeaseCandidateInformer cache.SharedIndexInformer
43-
InformerFactory informers.SharedInformerFactory
44-
HasSynced cache.InformerSynced
46+
leaseClient coordinationv1alpha1client.LeaseCandidateInterface
47+
leaseCandidateInformer cache.SharedIndexInformer
48+
informerFactory informers.SharedInformerFactory
49+
hasSynced cache.InformerSynced
4550

4651
// At most there will be one item in this Queue (since we only watch one item)
4752
queue workqueue.TypedRateLimitingInterface[int]
@@ -52,7 +57,7 @@ type LeaseCandidate struct {
5257
// controller lease
5358
leaseName string
5459

55-
Clock clock.Clock
60+
clock clock.Clock
5661

5762
binaryVersion, emulationVersion string
5863
preferredStrategies []v1.CoordinatedLeaseStrategy
@@ -62,10 +67,9 @@ func NewCandidate(clientset kubernetes.Interface,
6267
candidateName string,
6368
candidateNamespace string,
6469
targetLease string,
65-
clock clock.Clock,
6670
binaryVersion, emulationVersion string,
6771
preferredStrategies []v1.CoordinatedLeaseStrategy,
68-
) (*LeaseCandidate, error) {
72+
) (*LeaseCandidate, CacheSyncWaiter, error) {
6973
fieldSelector := fields.OneTermEqualSelector("metadata.name", candidateName).String()
7074
// A separate informer factory is required because this must start before informerFactories
7175
// are started for leader elected components
@@ -78,20 +82,20 @@ func NewCandidate(clientset kubernetes.Interface,
7882
leaseCandidateInformer := informerFactory.Coordination().V1alpha1().LeaseCandidates().Informer()
7983

8084
lc := &LeaseCandidate{
81-
LeaseClient: clientset.CoordinationV1alpha1().LeaseCandidates(candidateNamespace),
82-
LeaseCandidateInformer: leaseCandidateInformer,
83-
InformerFactory: informerFactory,
85+
leaseClient: clientset.CoordinationV1alpha1().LeaseCandidates(candidateNamespace),
86+
leaseCandidateInformer: leaseCandidateInformer,
87+
informerFactory: informerFactory,
8488
name: candidateName,
8589
namespace: candidateNamespace,
8690
leaseName: targetLease,
87-
Clock: clock,
91+
clock: clock.RealClock{},
8892
binaryVersion: binaryVersion,
8993
emulationVersion: emulationVersion,
9094
preferredStrategies: preferredStrategies,
9195
}
9296
lc.queue = workqueue.NewTypedRateLimitingQueueWithConfig(workqueue.DefaultTypedControllerRateLimiter[int](), workqueue.TypedRateLimitingQueueConfig[int]{Name: "leasecandidate"})
9397

94-
synced, err := leaseCandidateInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
98+
h, err := leaseCandidateInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
9599
UpdateFunc: func(oldObj, newObj interface{}) {
96100
if leasecandidate, ok := newObj.(*v1alpha1.LeaseCandidate); ok {
97101
if leasecandidate.Spec.PingTime != nil {
@@ -101,18 +105,18 @@ func NewCandidate(clientset kubernetes.Interface,
101105
},
102106
})
103107
if err != nil {
104-
return nil, err
108+
return nil, nil, err
105109
}
106-
lc.HasSynced = synced.HasSynced
110+
lc.hasSynced = h.HasSynced
107111

108-
return lc, nil
112+
return lc, informerFactory, nil
109113
}
110114

111115
func (c *LeaseCandidate) Run(ctx context.Context) {
112116
defer c.queue.ShutDown()
113117

114-
go c.InformerFactory.Start(ctx.Done())
115-
if !cache.WaitForNamedCacheSync("leasecandidateclient", ctx.Done(), c.HasSynced) {
118+
go c.informerFactory.Start(ctx.Done())
119+
if !cache.WaitForNamedCacheSync("leasecandidateclient", ctx.Done(), c.hasSynced) {
116120
return
117121
}
118122

@@ -153,12 +157,12 @@ func (c *LeaseCandidate) enqueueLease() {
153157
// ensureLease creates the lease if it does not exist and renew it if it exists. Returns the lease and
154158
// a bool (true if this call created the lease), or any error that occurs.
155159
func (c *LeaseCandidate) ensureLease(ctx context.Context) error {
156-
lease, err := c.LeaseClient.Get(ctx, c.name, metav1.GetOptions{})
160+
lease, err := c.leaseClient.Get(ctx, c.name, metav1.GetOptions{})
157161
if apierrors.IsNotFound(err) {
158162
klog.V(2).Infof("Creating lease candidate")
159163
// lease does not exist, create it.
160164
leaseToCreate := c.newLease()
161-
_, err := c.LeaseClient.Create(ctx, leaseToCreate, metav1.CreateOptions{})
165+
_, err := c.leaseClient.Create(ctx, leaseToCreate, metav1.CreateOptions{})
162166
if err != nil {
163167
return err
164168
}
@@ -169,9 +173,9 @@ func (c *LeaseCandidate) ensureLease(ctx context.Context) error {
169173
}
170174
klog.V(2).Infof("lease candidate exists.. renewing")
171175
clone := lease.DeepCopy()
172-
clone.Spec.RenewTime = &metav1.MicroTime{Time: c.Clock.Now()}
176+
clone.Spec.RenewTime = &metav1.MicroTime{Time: c.clock.Now()}
173177
clone.Spec.PingTime = nil
174-
_, err = c.LeaseClient.Update(ctx, clone, metav1.UpdateOptions{})
178+
_, err = c.leaseClient.Update(ctx, clone, metav1.UpdateOptions{})
175179
if err != nil {
176180
return err
177181
}
@@ -191,6 +195,6 @@ func (c *LeaseCandidate) newLease() *v1alpha1.LeaseCandidate {
191195
PreferredStrategies: c.preferredStrategies,
192196
},
193197
}
194-
lease.Spec.RenewTime = &metav1.MicroTime{Time: c.Clock.Now()}
198+
lease.Spec.RenewTime = &metav1.MicroTime{Time: c.clock.Now()}
195199
return lease
196200
}

tools/leaderelection/leasecandidate_test.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
"k8s.io/apimachinery/pkg/util/wait"
2828
"k8s.io/client-go/kubernetes/fake"
29-
"k8s.io/utils/clock"
3029
)
3130

3231
type testcase struct {
@@ -47,12 +46,11 @@ func TestLeaseCandidateCreation(t *testing.T) {
4746
defer cancel()
4847

4948
client := fake.NewSimpleClientset()
50-
candidate, err := NewCandidate(
49+
candidate, _, err := NewCandidate(
5150
client,
5251
tc.candidateName,
5352
tc.candidateNamespace,
5453
tc.leaseName,
55-
clock.RealClock{},
5654
tc.binaryVersion,
5755
tc.emulationVersion,
5856
[]v1.CoordinatedLeaseStrategy{v1.OldestEmulationVersion},
@@ -82,12 +80,11 @@ func TestLeaseCandidateAck(t *testing.T) {
8280

8381
client := fake.NewSimpleClientset()
8482

85-
candidate, err := NewCandidate(
83+
candidate, _, err := NewCandidate(
8684
client,
8785
tc.candidateName,
8886
tc.candidateNamespace,
8987
tc.leaseName,
90-
clock.RealClock{},
9188
tc.binaryVersion,
9289
tc.emulationVersion,
9390
[]v1.CoordinatedLeaseStrategy{v1.OldestEmulationVersion},

0 commit comments

Comments
 (0)