kube-apiserver/leaderelection/tests: use fake clock
Signed-off-by: Dr. Stefan Schimanski <stefan.schimanski@gmail.com>
This commit is contained in:
committed by
Jefftree
parent
8c971c5c15
commit
b8045f98a4
@@ -37,6 +37,7 @@ import (
|
||||
"k8s.io/client-go/tools/cache"
|
||||
"k8s.io/client-go/util/workqueue"
|
||||
"k8s.io/klog/v2"
|
||||
"k8s.io/utils/clock"
|
||||
"k8s.io/utils/ptr"
|
||||
)
|
||||
|
||||
@@ -68,6 +69,8 @@ type Controller struct {
|
||||
leaseCandidateRegistration cache.ResourceEventHandlerRegistration
|
||||
|
||||
queue workqueue.TypedRateLimitingInterface[types.NamespacedName]
|
||||
|
||||
clock clock.Clock
|
||||
}
|
||||
|
||||
func (c *Controller) Run(ctx context.Context, workers int) {
|
||||
@@ -114,6 +117,8 @@ func NewController(leaseInformer coordinationv1informers.LeaseInformer, leaseCan
|
||||
leaseCandidateClient: leaseCandidateClient,
|
||||
|
||||
queue: workqueue.NewTypedRateLimitingQueueWithConfig(workqueue.DefaultTypedControllerRateLimiter[types.NamespacedName](), workqueue.TypedRateLimitingQueueConfig[types.NamespacedName]{Name: controllerName}),
|
||||
|
||||
clock: clock.RealClock{},
|
||||
}
|
||||
leaseSynced, err := leaseInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
|
||||
AddFunc: func(obj interface{}) {
|
||||
@@ -199,13 +204,13 @@ func (c *Controller) electionNeeded(candidates []*v1alpha1.LeaseCandidate, lease
|
||||
return true, nil
|
||||
}
|
||||
|
||||
if isLeaseExpired(lease) || lease.Spec.HolderIdentity == nil || *lease.Spec.HolderIdentity == "" {
|
||||
if isLeaseExpired(c.clock, lease) || lease.Spec.HolderIdentity == nil || *lease.Spec.HolderIdentity == "" {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// every 15min enforce an election to update all candidates. Every 30min we garbage collect.
|
||||
for _, candidate := range candidates {
|
||||
if candidate.Spec.RenewTime != nil && candidate.Spec.RenewTime.Add(leaseCandidateValidDuration/2).Before(time.Now()) {
|
||||
if candidate.Spec.RenewTime != nil && candidate.Spec.RenewTime.Add(leaseCandidateValidDuration/2).Before(c.clock.Now()) {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
@@ -258,7 +263,7 @@ func (c *Controller) reconcileElectionStep(ctx context.Context, leaseNN types.Na
|
||||
return defaultRequeueInterval, err
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
now := c.clock.Now()
|
||||
canVoteYet := true
|
||||
for _, candidate := range candidates {
|
||||
if candidate.Spec.PingTime != nil && candidate.Spec.PingTime.Add(electionDuration).After(now) &&
|
||||
@@ -331,7 +336,7 @@ func (c *Controller) reconcileElectionStep(ctx context.Context, leaseNN types.Na
|
||||
Spec: v1.LeaseSpec{
|
||||
Strategy: &strategy,
|
||||
LeaseDurationSeconds: ptr.To(defaultLeaseDurationSeconds),
|
||||
RenewTime: &metav1.MicroTime{Time: time.Now()},
|
||||
RenewTime: &metav1.MicroTime{Time: c.clock.Now()},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -368,7 +373,7 @@ func (c *Controller) reconcileElectionStep(ctx context.Context, leaseNN types.Na
|
||||
}
|
||||
orig := existing.DeepCopy()
|
||||
|
||||
isExpired := isLeaseExpired(existing)
|
||||
isExpired := isLeaseExpired(c.clock, existing)
|
||||
noHolderIdentity := leaderLease.Spec.HolderIdentity != nil && existing.Spec.HolderIdentity == nil || *existing.Spec.HolderIdentity == ""
|
||||
expiredAndNewHolder := isExpired && leaderLease.Spec.HolderIdentity != nil && *existing.Spec.HolderIdentity != *leaderLease.Spec.HolderIdentity
|
||||
strategyChanged := existing.Spec.Strategy == nil || *existing.Spec.Strategy != strategy
|
||||
@@ -420,7 +425,7 @@ func (c *Controller) listAdmissableCandidates(leaseNN types.NamespacedName) ([]*
|
||||
if l.Spec.LeaseName != leaseNN.Name {
|
||||
continue
|
||||
}
|
||||
if !isLeaseCandidateExpired(l) {
|
||||
if !isLeaseCandidateExpired(c.clock, l) {
|
||||
results = append(results, l)
|
||||
} else {
|
||||
klog.Infof("LeaseCandidate %s is expired", l.Name)
|
||||
|
||||
Reference in New Issue
Block a user