Skip to content

Commit d092513

Browse files
committed
Use fake clock for controller/leaderelection:TestController
1 parent 7a4c962 commit d092513

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

pkg/controlplane/controller/leaderelection/leaderelection_controller.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ func (c *Controller) reconcileElectionStep(ctx context.Context, leaseNN types.Na
278278

279279
if candidate.Spec.PingTime == nil ||
280280
// If PingTime is outdated, send another PingTime only if it already acked the first one.
281-
(candidate.Spec.PingTime.Add(electionDuration).Before(now) && candidate.Spec.PingTime.Before(candidate.Spec.RenewTime)) {
281+
// This checks for pingTime <= renewTime because equality is possible in unit tests using a fake clock.
282+
(candidate.Spec.PingTime.Add(electionDuration).Before(now) && !candidate.Spec.RenewTime.Before(candidate.Spec.PingTime)) {
282283
// TODO(jefftree): We should randomize the order of sending pings and do them in parallel
283284
// so that all candidates have equal opportunity to ack.
284285
clone := candidate.DeepCopy()
@@ -300,7 +301,7 @@ func (c *Controller) reconcileElectionStep(ctx context.Context, leaseNN types.Na
300301
continue // shouldn't be the case after the above
301302
}
302303

303-
if candidate.Spec.RenewTime != nil && candidate.Spec.PingTime.Before(candidate.Spec.RenewTime) {
304+
if candidate.Spec.RenewTime != nil && !candidate.Spec.RenewTime.Before(candidate.Spec.PingTime) {
304305
continue // this has renewed already
305306
}
306307

pkg/controlplane/controller/leaderelection/leaderelection_controller_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"k8s.io/apimachinery/pkg/util/wait"
3232
"k8s.io/client-go/informers"
3333
"k8s.io/client-go/kubernetes/fake"
34+
"k8s.io/client-go/tools/cache"
3435
testingclock "k8s.io/utils/clock/testing"
3536
"k8s.io/utils/ptr"
3637
)
@@ -697,6 +698,7 @@ func TestController(t *testing.T) {
697698
if err != nil {
698699
t.Fatal(err)
699700
}
701+
controller.clock = fakeClock
700702

701703
for _, obj := range tc.leases {
702704
t.Logf("Pre-creating lease %s/%s", obj.Namespace, obj.Name)
@@ -715,6 +717,9 @@ func TestController(t *testing.T) {
715717

716718
informerFactory.Start(ctx.Done())
717719
informerFactory.WaitForCacheSync(ctx.Done())
720+
if !cache.WaitForNamedCacheSync(controllerName, ctx.Done(), controller.leaseRegistration.HasSynced, controller.leaseCandidateRegistration.HasSynced) {
721+
return
722+
}
718723
go controller.Run(ctx, 1)
719724

720725
if rand.Intn(2) == 0 {
@@ -747,9 +752,9 @@ func TestController(t *testing.T) {
747752
continue // only candidate-aware controllers will follow preferredHolder
748753
}
749754

750-
t.Logf("Deleting lease %s/%s because of preferredHolder %q != %q", l.Namespace, l.Name, *ph, *l.Spec.HolderIdentity)
755+
fmt.Printf("Deleting lease %s/%s because of preferredHolder %q != %q", l.Namespace, l.Name, *ph, *l.Spec.HolderIdentity)
751756
if err = client.CoordinationV1().Leases(expectedLease.Namespace).Delete(ctx, expectedLease.Name, metav1.DeleteOptions{}); err != nil {
752-
t.Logf("Error deleting lease %s/%s: %v", l.Namespace, l.Name, err)
757+
fmt.Printf("Error deleting lease %s/%s: %v", l.Namespace, l.Name, err)
753758
}
754759
}
755760
}
@@ -768,12 +773,12 @@ func TestController(t *testing.T) {
768773
for _, lc := range tc.createAfterControllerStart {
769774
c, err := client.CoordinationV1alpha1().LeaseCandidates(lc.Namespace).Get(ctx, lc.Name, metav1.GetOptions{})
770775
if err == nil {
771-
if c.Spec.PingTime != nil {
772-
t.Logf("Answering ping for %s/%s", c.Namespace, c.Name)
776+
if c.Spec.PingTime != nil && c.Spec.PingTime.Before(c.Spec.RenewTime) {
777+
fmt.Printf("Answering ping for %s/%s", c.Namespace, c.Name)
773778
c.Spec.RenewTime = &metav1.MicroTime{Time: fakeClock.Now()}
774779
_, err = client.CoordinationV1alpha1().LeaseCandidates(lc.Namespace).Update(ctx, c, metav1.UpdateOptions{})
775780
if err != nil {
776-
t.Logf("Error updating lease candidate %s/%s: %v", c.Namespace, c.Name, err)
781+
fmt.Printf("Error updating lease candidate %s/%s: %v", c.Namespace, c.Name, err)
777782
}
778783
}
779784
}

0 commit comments

Comments
 (0)