Skip to content

Commit

Permalink
use clock inside pod_controller
Browse files Browse the repository at this point in the history
  • Loading branch information
TusharMohapatra07 committed Dec 9, 2024
1 parent ee6c176 commit bd1b989
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
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
}

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(),
},
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)

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

0 comments on commit bd1b989

Please sign in to comment.