Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace time.Now() with clock.Clock.Now() in pkg/pod #3775

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
7 changes: 5 additions & 2 deletions pkg/controller/jobs/pod/pod_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
"k8s.io/utils/clock"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -90,6 +91,7 @@ var (
errIncorrectReconcileRequest = errors.New("event handler error: got a single pod reconcile request for a pod group")
errPendingOps = jobframework.UnretryableError("waiting to observe previous operations on pods")
errPodGroupLabelsMismatch = errors.New("constructing workload: pods have different label values")
realClock = clock.RealClock{}
)

func init() {
Expand Down Expand Up @@ -118,7 +120,7 @@ type Reconciler struct {
}

func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
return r.ReconcileGenericJob(ctx, req, &Pod{excessPodExpectations: r.expectationsStore})
return r.ReconcileGenericJob(ctx, req, &Pod{excessPodExpectations: r.expectationsStore, clock: realClock})
}

func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
Expand Down Expand Up @@ -155,6 +157,7 @@ type Pod struct {
absentPods int
excessPodExpectations *expectations.Store
satisfiedExcessPods bool
clock clock.Clock
TusharMohapatra07 marked this conversation as resolved.
Show resolved Hide resolved
}

var (
Expand Down Expand Up @@ -445,7 +448,7 @@ func (p *Pod) Stop(ctx context.Context, c client.Client, _ []podset.PodSetInfo,
Type: ConditionTypeTerminationTarget,
Status: corev1.ConditionTrue,
LastTransitionTime: metav1.Time{
Time: time.Now(),
Time: p.clock.Now(),
TusharMohapatra07 marked this conversation as resolved.
Show resolved Hide resolved
},
Reason: string(stopReason),
Message: eventMsg,
Expand Down
7 changes: 5 additions & 2 deletions pkg/controller/jobs/pod/pod_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5194,6 +5194,8 @@ func TestReconciler(t *testing.T) {

func TestReconciler_ErrorFinalizingPod(t *testing.T) {
ctx, _ := utiltesting.ContextWithLog(t)
now := time.Now()
fakeClock := testingclock.NewFakeClock(now)
TusharMohapatra07 marked this conversation as resolved.
Show resolved Hide resolved

clientBuilder := utiltesting.NewClientBuilder()
if err := SetupIndexes(ctx, utiltesting.AsIndexer(clientBuilder)); err != nil {
Expand Down Expand Up @@ -5254,7 +5256,7 @@ func TestReconciler_ErrorFinalizingPod(t *testing.T) {
}
recorder := record.NewBroadcaster().NewRecorder(kClient.Scheme(), corev1.EventSource{Component: "test"})

reconciler := NewReconciler(kClient, recorder)
reconciler := NewReconciler(kClient, recorder, jobframework.WithClock(t, fakeClock))

podKey := client.ObjectKeyFromObject(&pod)
_, err := reconciler.Reconcile(ctx, reconcile.Request{
Expand Down Expand Up @@ -5388,6 +5390,7 @@ func TestGetWorkloadNameForPod(t *testing.T) {

func TestReconciler_DeletePodAfterTransientErrorsOnUpdateOrDeleteOps(t *testing.T) {
now := time.Now()
fakeClock := testingclock.NewFakeClock(now)
connRefusedErrMock := fmt.Errorf("connection refused: %w", syscall.ECONNREFUSED)
ctx, _ := utiltesting.ContextWithLog(t)
var triggerUpdateErr, triggerDeleteErr bool
Expand Down Expand Up @@ -5475,7 +5478,7 @@ func TestReconciler_DeletePodAfterTransientErrorsOnUpdateOrDeleteOps(t *testing.
}

recorder := record.NewBroadcaster().NewRecorder(kClient.Scheme(), corev1.EventSource{Component: "test"})
reconciler := NewReconciler(kClient, recorder)
reconciler := NewReconciler(kClient, recorder, jobframework.WithClock(t, fakeClock))
reconcileRequest := reconcileRequestForPod(&pods[0])

// Reconcile for the first time. It'll try to remove the finalizers but fail
Expand Down