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

Fix requeueing of cronjobs with every-style schedule #109250

Conversation

d-honeybadger
Copy link
Contributor

What type of PR is this?

/kind bug
/kind regression

What this PR does / why we need it:

In CronJob ControllerV2, cronjobs need to be requeued when it's time for the next schedule. This requeueing interval is currently calculated based off the time now. This works fine for classic cron schedule, b/c the "next" schedule is aligned with the specific day/hour/minute from the schedule, but breaks for @every X schedule, because the "next" schedule in this case becomes simply now + X, and is incorrect if a) kube-controller-manager was restarted and syncing all cronjobs; b) there was an update to cronjob schedule. Instead, for @every-style schedules, it becomes important to always base the calculation of the next time relative to the last scheduled time (or cronjob creation if it wasn't scheduled before).
Note that since before controllerv2, requeueing wasn't a thing, and therefore this bug didn't exist.

Which issue(s) this PR fixes:

Fixes #104012

Special notes for your reviewer:

Verified the fix with a local cluster (./hack/local-up-cluster.sh). Figured an e2e test for such a specific case would be an overkill. Either way, lmk if I'm over- or under-testing this.
There's also one unit test that has a comment on why it will fail. The reason for its failure is outside of the scope of this PR, and not introduced by this PR - lmk if it should be deleted of kept to indicate that a future fix in necessary.

Does this PR introduce a user-facing change?

Fixes scheduling of cronjobs with @every X schedules.

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. kind/bug Categorizes issue or PR as related to a bug. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. kind/regression Categorizes issue or PR as related to a regression from a prior release. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Apr 2, 2022
@k8s-ci-robot
Copy link
Contributor

Hi @d-honeybadger. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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/test-infra repository.

@k8s-ci-robot k8s-ci-robot added sig/apps Categorizes an issue or PR as relevant to SIG Apps. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Apr 2, 2022
@d-honeybadger d-honeybadger force-pushed the fix-cronjob-scheduling-every-syntax branch from 97f91e2 to 7ae21b9 Compare April 2, 2022 00:15
@d-honeybadger
Copy link
Contributor Author

/sig apps

@d-honeybadger
Copy link
Contributor Author

@soltysh, @alculquicondor You are the two people I saw dealing with cronjobs recently, could you please take a look?

@d-honeybadger d-honeybadger changed the title Fx requeueing of cronjobs with @every-style schedule Fx requeueing of cronjobs with every-style schedule Apr 2, 2022
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Apr 2, 2022
@alculquicondor
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Apr 4, 2022
@d-honeybadger
Copy link
Contributor Author

/retest

@d-honeybadger d-honeybadger changed the title Fx requeueing of cronjobs with every-style schedule Fix requeueing of cronjobs with every-style schedule Apr 4, 2022
@d-honeybadger
Copy link
Contributor Author

👋 @soltysh Could you please take a look whenever you have a minute?

Copy link
Contributor

@soltysh soltysh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm
/approve

// for Network Time Protocol(NTP) time skews. If the time drifts are adjusted which in most
// realistic cases would be around 100s, scheduled cron will still be executed without missing
// the schedule.
func nextScheduledTimeDuration(sched cron.Schedule, now time.Time) *time.Duration {
t := sched.Next(now).Add(nextScheduleDelta).Sub(now)
func nextScheduledTimeDuration(cj batchv1.CronJob, sched cron.Schedule, now time.Time) *time.Duration {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've noticed there's a quite a lot of similarities between this method and getNextScheduleTime, I'll followup with a squash. But this is very good - thank you!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's true, was trying to keep changes local but that had its cons.
Thank you!

@soltysh
Copy link
Contributor

soltysh commented Jun 28, 2022

/triage accepted
/priority backlog

@k8s-ci-robot k8s-ci-robot added the triage/accepted Indicates an issue or PR is ready to be actively worked on. label Jun 28, 2022
@k8s-ci-robot k8s-ci-robot added priority/backlog Higher priority than priority/awaiting-more-evidence. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jun 28, 2022
@k8s-ci-robot k8s-ci-robot added lgtm "Looks good to me", indicates that a PR is ready to be merged. and removed needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Jun 28, 2022
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: d-honeybadger, soltysh

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jun 28, 2022
@k8s-ci-robot k8s-ci-robot merged commit 6269784 into kubernetes:master Jun 28, 2022
@k8s-ci-robot k8s-ci-robot added this to the v1.25 milestone Jun 28, 2022
@sushanth0910 sushanth0910 mentioned this pull request Sep 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. kind/regression Categorizes issue or PR as related to a regression from a prior release. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. priority/backlog Higher priority than priority/awaiting-more-evidence. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/apps Categorizes an issue or PR as relevant to SIG Apps. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CronJob controller V2: breaks "@every [interval]" jobs
4 participants