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

feat: Append job creation timestamp to cronjob annotations #118137

Merged
merged 6 commits into from
Jul 6, 2023

Conversation

helayoty
Copy link
Member

@helayoty helayoty commented May 19, 2023

What type of PR is this?

/kind feature

What this PR does / why we need it:

  • Add new annotation CronJobScheduledTimestampAnnotation to the batch API.
  • Update features with CronJobsScheduledAnnotation.
  • Pass context to the getJobFromTemplate2 func to be able to log the errors.
  • Update Unit test.

Which issue(s) this PR fixes:

Fixes #108609

Special notes for your reviewer:

Does this PR introduce a user-facing change?

Yes.

Add new annotation `batch.kubernetes.io/cronjob-scheduled-timestamp` to Job objects scheduled from CronJobs.

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

@helayoty helayoty added sig/apps Categorizes an issue or PR as relevant to SIG Apps. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. labels May 19, 2023
@helayoty helayoty requested review from soltysh and sftim May 19, 2023 16:11
@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. 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 May 19, 2023
@helayoty
Copy link
Member Author

/ok-to-test

@k8s-ci-robot k8s-ci-robot added the ok-to-test Indicates a non-member PR verified by an org member that is safe to test. label May 19, 2023
@k8s-ci-robot k8s-ci-robot added the kind/api-change Categorizes issue or PR as related to adding, removing, or otherwise changing an API label May 19, 2023
@k8s-triage-robot
Copy link

This PR may require API review.

If so, when the changes are ready, complete the pre-review checklist and request an API review.

Status of requested reviews is tracked in the API Review project.

@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 do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels May 19, 2023
Copy link
Member

@troy0820 troy0820 left a comment

Choose a reason for hiding this comment

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

Some nits and questions around if we want the timestamp from creation or the scheduledtime that gets appended to the job name.

If we want the former, then the test associated with this change is going to fail as the creationTimeStamp of the job and not the scheduledTime passed into the getJobFromTemplate2 function will be different.

pkg/controller/cronjob/utils_test.go Outdated Show resolved Hide resolved
pkg/controller/cronjob/utils.go Outdated Show resolved Hide resolved
pkg/controller/cronjob/utils.go Outdated Show resolved Hide resolved
@troy0820
Copy link
Member

/lgtm

@k8s-ci-robot k8s-ci-robot added lgtm "Looks good to me", indicates that a PR is ready to be merged. and removed lgtm "Looks good to me", indicates that a PR is ready to be merged. labels May 19, 2023
@helayoty
Copy link
Member Author

Some nits and questions around if we want the timestamp from creation or the scheduledtime that gets appended to the job name.

If we want the former, then the test associated with this change is going to fail as the creationTimeStamp of the job and not the scheduledTime passed into the getJobFromTemplate2 function will be different.

@troy0820 Thank you for your review. The annotation will have the original scheduled time, the scheduledTime will be set as the new value for theCreationTimestamp meta, and the user doesn't have a way to get the original/expected scheduled time.

@helayoty
Copy link
Member Author

/test pull-kubernetes-e2e-kind-ipv6

@kubernetes kubernetes deleted a comment from k8s-ci-robot May 20, 2023
@troy0820
Copy link
Member

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 20, 2023
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.

Also based on the discussion in #117986 I'm inclined to say that this particular change might also require a KEP, a small one, similar to kubernetes/enhancements#4019 but still would be good to describe the format of the date and ensure this is working as expected.
/hold

pkg/controller/cronjob/utils.go Outdated Show resolved Hide resolved
staging/src/k8s.io/api/batch/v1/types.go Outdated Show resolved Hide resolved
@helayoty helayoty changed the title Append job creation timestamp to job annotations feat: Append job creation timestamp to cronjob annotations Jul 6, 2023
pkg/features/kube_features.go Outdated Show resolved Hide resolved
staging/src/k8s.io/api/batch/v1/types.go Outdated Show resolved Hide resolved
Signed-off-by: Heba Elayoty <[email protected]>
@helayoty helayoty requested a review from liggitt July 6, 2023 15:44
// CronJobScheduledTimestampAnnotation is the scheduled timestamp annotation for the Job.
// It records the original/expected scheduled timestamp for the running job, represented in RFC3339.
// The CronJob controller adds this annotation if the CronJobsScheduledAnnotation feature gate (beta in 1.28) is enabled.
CronJobScheduledTimestampAnnotation = labelPrefix + "cronjob-scheduled-timestamp"
Copy link
Member

Choose a reason for hiding this comment

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

this looks good.. open a PR to the dev-1.28 branch of the website to add this doc to https://kubernetes.io/docs/reference/labels-annotations-taints/ as well

Copy link
Member Author

Choose a reason for hiding this comment

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

PR is ready already. kubernetes/website#41857

Copy link
Member

@liggitt liggitt left a comment

Choose a reason for hiding this comment

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

one nit on the feature gate order, then lgtm

@liggitt liggitt moved this from Changes requested to API review completed, 1.28 in API Reviews Jul 6, 2023
@liggitt
Copy link
Member

liggitt commented Jul 6, 2023

/approve
for API constant addition

/hold
for feature gate reorder and squash

@soltysh can lgtm and unhold once that is done

@k8s-ci-robot k8s-ci-robot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Jul 6, 2023
Signed-off-by: Heba Elayoty <[email protected]>
@helayoty helayoty requested a review from liggitt July 6, 2023 16:53
@liggitt
Copy link
Member

liggitt commented Jul 6, 2023

/lgtm
/hold cancel

@k8s-ci-robot k8s-ci-robot added lgtm "Looks good to me", indicates that a PR is ready to be merged. and removed do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. labels Jul 6, 2023
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 cancel

if utilfeature.DefaultFeatureGate.Enabled(features.CronJobsScheduledAnnotation) {

// Append job creation timestamp to the cronJob annotations. The time will be in RFC3339 form.
annotations[batchv1.CronJobScheduledTimestampAnnotation] = cj.Spec.JobTemplate.CreationTimestamp.Time.Format(time.RFC3339)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is still wrong, it should be:

annotations[batchv1.CronJobScheduledTimestampAnnotation] = scheduledTime.Format(time.RFC3339).In(timezone) 

this is because we want to present that annotation in the time that is local to the user, and the information in schedule is transformed to current time zone.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's hard to quickly explain, but when we invoke cron.ParseStandard we pass TZ=<time-zone which triggers the library to take into account the timezone when calculating the time, but the date returned from invoking Next on that schedule will give you time relative to your current time zone. In our case that is the time zone local to the kube-controller-manager.

It's best to try it locally with something like this:

sched, _ := cron.ParseStandard("TZ=America/New_York 0 */4 * * *")
next := sched.Next(time.Now())
loc, _ := time.LoadLocation("America/New_York")
fmt.Println("next in TZ", next.In(loc))
fmt.Println("next in local", next)

make sure to pick the time zone that is different than your local, the schedule is set to execute every 4h at 0 minutes.
In my case the above will print (it's currently 21:04pm CEST, which is 15:05 EST):

next in TZ 2023-07-06 16:00:00 -0400 EDT
next in local 2023-07-06 22:00:00 +0200 CEST

for the annotation we want to present the user with the time printed in TZ.

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed.

Copy link
Member

Choose a reason for hiding this comment

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

wait... what? we should either make the TZ UTC or the TZ specified in the cronjob, right? the timezone of k-c-m is a weird thing to surface into the API

Copy link
Member

Choose a reason for hiding this comment

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

the date returned from invoking Next on that schedule will give you time relative to your current time zone

ah, I see, it uses the location of the time passed in to Next()

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 6, 2023
@k8s-ci-robot k8s-ci-robot requested a review from soltysh July 6, 2023 18:58
Signed-off-by: Heba Elayoty <[email protected]>
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 6, 2023
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

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

LGTM label has been added.

Git tree hash: 4622f664fcee6e781261cca927c8b5398300448e

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: helayoty, liggitt, 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 merged commit 2fe38f9 into kubernetes:master Jul 6, 2023
10 of 13 checks passed
@helayoty helayoty deleted the expose-job-name-cronjob branch July 6, 2023 21:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-review Categorizes an issue or PR as actively needing an API review. 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/api-change Categorizes issue or PR as related to adding, removing, or otherwise changing an API kind/feature Categorizes issue or PR as related to a new feature. 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/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. 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. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
Status: API review completed, 1.28
Archived in project
Development

Successfully merging this pull request may close these issues.

Inject schedule name into newly created job
8 participants