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

CronJob controller cleanups #110838

Merged
merged 2 commits into from Jan 18, 2023
Merged

Conversation

soltysh
Copy link
Contributor

@soltysh soltysh commented Jun 28, 2022

What type of PR is this?

/kind cleanup
/sig apps
/priority backlog

What this PR does / why we need it:

There are 2 major changes introduced in this PR:

  1. Minor controller cleanups to eliminate code duplication (byJobStartTimeStar sorter) and moving helpers into utils.go file.
  2. Cleaning up two very close methods nextScheduledTimeDuration and getNextScheduledTime (especially after we landed Fix requeueing of cronjobs with every-style schedule #109250).

Special notes for your reviewer:

/assign @atiratree
/cc @d-honeybadger

Does this PR introduce a user-facing change?

Improve cronjob controller performance

@k8s-ci-robot
Copy link
Contributor

@soltysh: GitHub didn't allow me to request PR reviews from the following users: d-honeybadger.

Note that only kubernetes members and repo collaborators can review this PR, and authors cannot review their own PRs.

In response to this:

What type of PR is this?

/kind cleanup
/sig apps
/priority backlog

What this PR does / why we need it:

There are 2 major changes introduced in this PR:

  1. Minor controller cleanups to eliminate code duplication (byJobStartTimeStar sorter) and moving helpers into utils.go file.
  2. Cleaning up two very close methods nextScheduledTimeDuration and getNextScheduledTime (especially after we landed Fix requeueing of cronjobs with every-style schedule #109250).

Special notes for your reviewer:

/assign @atiratree
/cc @d-honeybadger

Does this PR introduce a user-facing change?

NONE

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 release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. sig/apps Categorizes an issue or PR as relevant to SIG Apps. priority/backlog Higher priority than priority/awaiting-more-evidence. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jun 28, 2022
@soltysh
Copy link
Contributor Author

soltysh commented Jun 28, 2022

/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/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: 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
Copy link
Contributor

@d-honeybadger d-honeybadger left a comment

Choose a reason for hiding this comment

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

Thanks for letting me know!
Had to think for a bit how StartingDeadlineSeconds was unnecessary in the mostRecentTime() calculations, but pretty convinced now that that's correct.
I took the libery of leaving a couple nit comments.

@@ -33,6 +33,7 @@ import (

// Utilities for dealing with Jobs and CronJobs and time.

// inActiveListByName take a job and checks if activeJobs has a job with the same UID.
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like the comment got copied here by mistake

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated comment to make it more explanatory.

)
// mostRecentScheduleTime returns:
// - the last schedule time or CronJob's creation time,
// - the most recent time a Job should be created or nil, if that's before now,
Copy link
Contributor

Choose a reason for hiding this comment

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

or nil, if that's before now shoud be or nil, if that's after now I believe

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you're right, thx!

pkg/controller/cronjob/utils.go Show resolved Hide resolved
@@ -42,6 +43,17 @@ func inActiveList(cj batchv1.CronJob, uid types.UID) bool {
return false
}

// inActiveListByName take a job and checks if activeJobs has a job with the same
// name and namespace.
func inActiveListByName(job *batchv1.Job, activeJobs []corev1.ObjectReference) bool {
Copy link
Member

Choose a reason for hiding this comment

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

this could be more similar to inActiveList

  • we could switch flip the arguments: (what, where) -> (where, what)
  • pass CronJob instead of []corev1.ObjectReference

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good suggestion, updated.

// - the last schedule time or CronJob's creation time,
// - the most recent time a Job should be created or nil, if that's before now,
// - number of missed schedules
// - error in an edge case where the schedule specification is gramatically correct,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// - error in an edge case where the schedule specification is gramatically correct,
// - error in an edge case where the schedule specification is grammatically correct,

;-)

}{
{
name: "both have nil start times",
input: []batchv1.Job{bNil, aNil},
expected: []batchv1.Job{aNil, bNil},
input: []*batchv1.Job{&bNil, &aNil},
Copy link
Member

Choose a reason for hiding this comment

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

might be simpler to create a pointer when declaring the variables

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point - fixed.

},
expectedTime: nil,
now: topOfTheHour().Add(time.Second * 30),
expectedRecentTime: nil,
Copy link
Member

Choose a reason for hiding this comment

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

this would not throw an error if we got non nil result

t.Error("mostRecentScheduleTime() got error when none expected")
}
if gotEarliestTime.IsZero() && tt.expectedEarliestTime != nil {
t.Errorf("mostRecentScheduleTime() got 0, want %v", tt.expectedEarliestTime)
Copy link
Member

Choose a reason for hiding this comment

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

can we describe in all of these, what variable name are we expecting?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

pkg/controller/cronjob/utils_test.go Outdated Show resolved Hide resolved
pkg/controller/cronjob/utils_test.go Outdated Show resolved Hide resolved
pkg/controller/cronjob/utils_test.go Show resolved Hide resolved
@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 26, 2022
pkg/controller/cronjob/utils.go Outdated Show resolved Hide resolved
pkg/controller/cronjob/utils.go Outdated Show resolved Hide resolved
pkg/controller/cronjob/utils_test.go Outdated Show resolved Hide resolved
pkg/controller/cronjob/utils_test.go Outdated Show resolved Hide resolved
pkg/controller/cronjob/utils_test.go Outdated Show resolved Hide resolved
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle stale
  • Mark this issue or PR as rotten with /lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Oct 25, 2022
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Nov 24, 2022
@dims
Copy link
Member

dims commented Dec 12, 2022

If you still need this PR then please rebase, if not, please close the PR

@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Dec 22, 2022
@soltysh
Copy link
Contributor Author

soltysh commented Jan 3, 2023

Unrelated failure on CustomResourcePublishOpenAPI test
/test pull-kubernetes-e2e-gce

@k8s-ci-robot
Copy link
Contributor

k8s-ci-robot commented Jan 3, 2023

@soltysh: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-kubernetes-e2e-gce-ubuntu-containerd 8190a73ccad0641a86b7df6fa83c0aa8e3c43cc9 link true /test pull-kubernetes-e2e-gce-ubuntu-containerd

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

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. I understand the commands that are listed here.

@soltysh
Copy link
Contributor Author

soltysh commented Jan 4, 2023

Unrelated failure on CustomResourcePublishOpenAPI test
/test pull-kubernetes-e2e-gce

Copy link
Member

@atiratree atiratree left a comment

Choose a reason for hiding this comment

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

the PR looks in a good shape

pkg/controller/cronjob/utils_test.go Outdated Show resolved Hide resolved
}
if gotTime != nil && tt.expectedTime != nil && !gotTime.Equal(*tt.expectedTime) {
t.Errorf("getMostRecentScheduleTime() got = %v, want %v", gotTime, tt.expectedTime)
if gotRecentTime != nil && tt.expectedRecentTime != nil && !gotRecentTime.Equal(*tt.expectedRecentTime) {
Copy link
Member

Choose a reason for hiding this comment

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

might be more readable to use reflect.DeepEqual here

1. Squash two identical sorters byTime
2. Move helper for searching active jobs into utils to exist next to its
  counterpart
@soltysh soltysh force-pushed the cronjob_improvements branch 2 times, most recently from 20c1527 to 76218ff Compare January 18, 2023 15:40
…Duration

The two methods nextScheduledTimeDuration and getNextScheduleTime have a
lot of similarities, so this commit squashes the common parts together
along with getMostRecentScheduleTime to avoid code duplication.
@atiratree
Copy link
Member

looks good, thx
/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 18, 2023
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: fbb7921b5768228d7e1dd01db83443a2257182fd

@k8s-ci-robot k8s-ci-robot merged commit c63434a into kubernetes:master Jan 18, 2023
@k8s-ci-robot k8s-ci-robot added this to the v1.27 milestone Jan 18, 2023
@soltysh soltysh deleted the cronjob_improvements branch January 18, 2023 18:49
@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-none Denotes a PR that doesn't merit a release note. labels Oct 26, 2023
k8s-ci-robot added a commit that referenced this pull request Oct 27, 2023
…838-#121327-upstream-release-1.26

Automated cherry pick of #110838: Cleanups in controller utils #121327: Fix next schedule time duration
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/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. 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.

None yet

6 participants