feat: Append job creation timestamp to cronjob annotations (#118137)

* Append job name to job annotations

Signed-off-by: Heba Elayoty <hebaelayoty@gmail.com>

* Update annotation description, remove timezone, and fix time

Signed-off-by: Heba Elayoty <hebaelayoty@gmail.com>

* Remove unused ctx

Signed-off-by: Heba Elayoty <hebaelayoty@gmail.com>

* code review comments

Signed-off-by: Heba Elayoty <hebaelayoty@gmail.com>

* code review comments

Signed-off-by: Heba Elayoty <hebaelayoty@gmail.com>

* Add timezone back

Signed-off-by: Heba Elayoty <hebaelayoty@gmail.com>

---------

Signed-off-by: Heba Elayoty <hebaelayoty@gmail.com>
This commit is contained in:
Heba Elayoty
2023-07-06 14:39:04 -07:00
committed by GitHub
parent aeed7da616
commit 2fe38f93e5
4 changed files with 104 additions and 16 deletions

View File

@@ -21,14 +21,17 @@ import (
"time"
"github.com/robfig/cron/v3"
"k8s.io/utils/pointer"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
"k8s.io/kubernetes/pkg/features"
)
// Utilities for dealing with Jobs and CronJobs and time.
@@ -213,6 +216,16 @@ func getJobFromTemplate2(cj *batchv1.CronJob, scheduledTime time.Time) (*batchv1
// We want job names for a given nominal start time to have a deterministic name to avoid the same job being created twice
name := getJobName(cj, scheduledTime)
if utilfeature.DefaultFeatureGate.Enabled(features.CronJobsScheduledAnnotation) {
timeZoneLocation, err := time.LoadLocation(pointer.StringDeref(cj.Spec.TimeZone, ""))
if err != nil {
return nil, err
}
// Append job creation timestamp to the cronJob annotations. The time will be in RFC3339 form.
annotations[batchv1.CronJobScheduledTimestampAnnotation] = scheduledTime.In(timeZoneLocation).Format(time.RFC3339)
}
job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,