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() code with clock.Clock.Now() #3380

Open
PBundyra opened this issue Oct 30, 2024 · 21 comments · Fixed by #3421
Open

Replace time.Now() code with clock.Clock.Now() #3380

PBundyra opened this issue Oct 30, 2024 · 21 comments · Fixed by #3421
Assignees
Labels
kind/feature Categorizes issue or PR as related to a new feature.

Comments

@PBundyra
Copy link
Contributor

PBundyra commented Oct 30, 2024

What would you like to be added:
There are packages like e.g. pkg/scheduler that use time.Now(). As a result this code is hard to test and prone to flakiness as we are not able to directly control the time during tests. It should be replaced with clock.Clock.Now() as it is in WorkloadController

Why is this needed:

Completion requirements:

This enhancement requires the following artifacts:

The artifacts should be linked in subsequent comments.

@PBundyra PBundyra added the kind/feature Categorizes issue or PR as related to a new feature. label Oct 30, 2024
@PBundyra
Copy link
Contributor Author

/cc @mbobrovskyi

@TusharMohapatra07
Copy link
Contributor

can i work on this issue ? @PBundyra @mbobrovskyi

@mbobrovskyi
Copy link
Contributor

can i work on this issue ? @PBundyra @mbobrovskyi

Sure, please take it.

@TusharMohapatra07
Copy link
Contributor

@mbobrovskyi thanks! i'm on it

@mbobrovskyi
Copy link
Contributor

/assign @TusharMohapatra07

@TusharMohapatra07
Copy link
Contributor

@mbobrovskyi am i supposed to change the time.Now() instance everywhere inside the project or just those instances which are present inside pkg folder ??

@mbobrovskyi
Copy link
Contributor

Let's focus on pkg/scheduler. This is an example

.

@mimowo
Copy link
Contributor

mimowo commented Nov 4, 2024

I would say the end goal is to use it everywhere, but starting with scheduler might be a good idea to see if there are any obstacles, then feel free to follow up in the next PR.

@TusharMohapatra07
Copy link
Contributor

@mimowo understood

k8s-ci-robot pushed a commit that referenced this issue Nov 6, 2024
…e in pkg/scheduler(#3380) (#3421)

* refactor time.Now() in pkg/scheduler

* replace time.Since() with Since() present in clock package

* add new option to provide fakeclock to scheduler

* add realClock to options and pass in fakeClock while testing

* use options.clock instead of realClock

* add more fakeClock(s)
@mbobrovskyi
Copy link
Contributor

/reopen

To fix it in other places.

@k8s-ci-robot
Copy link
Contributor

@mbobrovskyi: Reopened this issue.

In response to this:

/reopen

To fix it in other places.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot reopened this Nov 6, 2024
@TusharMohapatra07
Copy link
Contributor

@mbobrovskyi yes, i'm on it

@TusharMohapatra07
Copy link
Contributor

@mbobrovskyi I don't see any significant place to replace time.Now in pkg/workload. Please confirm !

@mbobrovskyi
Copy link
Contributor

mbobrovskyi commented Nov 8, 2024

What about this one

LastTransitionTime: metav1.NewTime(time.Now()),
?

Also here

newCheck.LastTransitionTime = metav1.NewTime(time.Now())
.

Could you please check is it possible to propagate clock.Clock? Or maybe just now time.Time.

This is an example

func SyncAdmittedCondition(w *kueue.Workload, now time.Time) bool {

@TusharMohapatra07
Copy link
Contributor

@mbobrovskyi Actually, LastTransitionTime takes metav1.Time, so time.Time won't work ig. We could probably pass in the clock.Clock interface inside the function to proceed with this..

@mbobrovskyi
Copy link
Contributor

mbobrovskyi commented Nov 8, 2024

@mbobrovskyi Actually, LastTransitionTime takes metav1.Time, so time.Time won't work ig. We could probably pass in the clock.Clock interface inside the function to proceed with this..

Why we can't use like this LastTransitionTime: metav1.NewTime(clock.Now()) or LastTransitionTime: metav1.NewTime(now)?

I think just propagation to function is enough for it. Also time.Time is enough.

@TusharMohapatra07
Copy link
Contributor

@mbobrovskyi so, if we propogate it in the function, we have to pass it whenever we're calling the function ?
For ex:-

workload.ResetChecksOnEviction(w)

@mbobrovskyi
Copy link
Contributor

Exactly and Preemptor should have clock? If not please add to Preemptor as well.

@TusharMohapatra07
Copy link
Contributor

@mbobrovskyi understood

@TusharMohapatra07
Copy link
Contributor

Hey @mbobrovskyi, sorry for being late but i'm a bit busy with some commitments. I'll restart working on this in a day or two.

kannon92 pushed a commit to kannon92/kueue that referenced this issue Dec 5, 2024
…e in pkg/scheduler(kubernetes-sigs#3380) (kubernetes-sigs#3421)

* refactor time.Now() in pkg/scheduler

* replace time.Since() with Since() present in clock package

* add new option to provide fakeclock to scheduler

* add realClock to options and pass in fakeClock while testing

* use options.clock instead of realClock

* add more fakeClock(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature Categorizes issue or PR as related to a new feature.
Projects
None yet
5 participants