diff --git a/internal/store/cronjob.go b/internal/store/cronjob.go index a6613f2dd..1b335b9d6 100644 --- a/internal/store/cronjob.go +++ b/internal/store/cronjob.go @@ -249,7 +249,7 @@ func cronJobMetricFamilies(allowAnnotationsList, allowLabelsList []string) []gen ms := []*metric.Metric{} // If the cron job is suspended, don't track the next scheduled time - nextScheduledTime, err := getNextScheduledTime(j.Spec.Schedule, j.Status.LastScheduleTime, j.CreationTimestamp) + nextScheduledTime, err := getNextScheduledTime(j.Spec.Schedule, j.Status.LastScheduleTime, j.CreationTimestamp, j.Spec.TimeZone) if err != nil { panic(err) } else if !*j.Spec.Suspend { @@ -351,7 +351,11 @@ func createCronJobListWatch(kubeClient clientset.Interface, ns string, fieldSele } } -func getNextScheduledTime(schedule string, lastScheduleTime *metav1.Time, createdTime metav1.Time) (time.Time, error) { +func getNextScheduledTime(schedule string, lastScheduleTime *metav1.Time, createdTime metav1.Time, timeZone *string) (time.Time, error) { + if timeZone != nil { + schedule = fmt.Sprintf("CRON_TZ=%s %s", *timeZone, schedule) + } + sched, err := cron.ParseStandard(schedule) if err != nil { return time.Time{}, fmt.Errorf("Failed to parse cron job schedule '%s': %w", schedule, err) diff --git a/internal/store/cronjob_test.go b/internal/store/cronjob_test.go index 333dd0499..011666dfa 100644 --- a/internal/store/cronjob_test.go +++ b/internal/store/cronjob_test.go @@ -40,7 +40,7 @@ var ( ActiveRunningCronJob1LastScheduleTime = time.Unix(1520742896, 0) SuspendedCronJob1LastScheduleTime = time.Unix(1520742896+5.5*3600, 0) // 5.5 hours later ActiveCronJob1NoLastScheduledCreationTimestamp = time.Unix(1520742896+6.5*3600, 0) - TimeZone = "Europe/Berlin" + TimeZone = "Asia/Shanghai" ) func TestCronJobStore(t *testing.T) { @@ -54,7 +54,7 @@ func TestCronJobStore(t *testing.T) { ActiveRunningCronJob1LastScheduleTime.Day(), 6, 0, - 0, 0, time.Local) + 0, 0, time.UTC) case hour < 12: ActiveRunningCronJob1NextScheduleTime = time.Date( ActiveRunningCronJob1LastScheduleTime.Year(), @@ -62,7 +62,7 @@ func TestCronJobStore(t *testing.T) { ActiveRunningCronJob1LastScheduleTime.Day(), 12, 0, - 0, 0, time.Local) + 0, 0, time.UTC) case hour < 18: ActiveRunningCronJob1NextScheduleTime = time.Date( ActiveRunningCronJob1LastScheduleTime.Year(), @@ -70,15 +70,15 @@ func TestCronJobStore(t *testing.T) { ActiveRunningCronJob1LastScheduleTime.Day(), 18, 0, - 0, 0, time.Local) + 0, 0, time.UTC) case hour < 24: ActiveRunningCronJob1NextScheduleTime = time.Date( ActiveRunningCronJob1LastScheduleTime.Year(), ActiveRunningCronJob1LastScheduleTime.Month(), - ActiveRunningCronJob1LastScheduleTime.Day(), - 24, + ActiveRunningCronJob1LastScheduleTime.Day()+1, + 0, 0, - 0, 0, time.Local) + 0, 0, time.UTC) } minute := ActiveCronJob1NoLastScheduledCreationTimestamp.Minute() @@ -91,7 +91,7 @@ func TestCronJobStore(t *testing.T) { ActiveCronJob1NoLastScheduledCreationTimestamp.Day(), ActiveCronJob1NoLastScheduledCreationTimestamp.Hour(), 25, - 0, 0, time.Local) + 0, 0, time.UTC) default: ActiveCronJob1NoLastScheduledNextScheduleTime = time.Date( ActiveCronJob1NoLastScheduledNextScheduleTime.Year(), @@ -99,10 +99,94 @@ func TestCronJobStore(t *testing.T) { ActiveCronJob1NoLastScheduledNextScheduleTime.Day(), ActiveCronJob1NoLastScheduledNextScheduleTime.Hour()+1, 25, - 0, 0, time.Local) + 0, 0, time.UTC) } cases := []generateMetricsTestCase{ + { + AllowAnnotationsList: []string{ + "app.k8s.io/owner", + }, + Obj: &batchv1.CronJob{ + ObjectMeta: metav1.ObjectMeta{ + Name: "ActiveRunningCronJobWithTZ1", + Namespace: "ns1", + Generation: 1, + ResourceVersion: "11111", + Labels: map[string]string{ + "app": "example-active-running-with-tz-1", + }, + Annotations: map[string]string{ + "app": "mysql-server", + "app.k8s.io/owner": "@foo", + }, + }, + Status: batchv1.CronJobStatus{ + Active: []v1.ObjectReference{{Name: "FakeJob1"}, {Name: "FakeJob2"}}, + LastScheduleTime: &metav1.Time{Time: ActiveRunningCronJob1LastScheduleTime}, + LastSuccessfulTime: nil, + }, + Spec: batchv1.CronJobSpec{ + StartingDeadlineSeconds: &StartingDeadlineSeconds300, + ConcurrencyPolicy: "Forbid", + Suspend: &SuspendFalse, + Schedule: "0 */6 * * *", + SuccessfulJobsHistoryLimit: &SuccessfulJobHistoryLimit3, + FailedJobsHistoryLimit: &FailedJobHistoryLimit1, + TimeZone: &TimeZone, + }, + }, + Want: ` + # HELP kube_cronjob_created [STABLE] Unix creation timestamp + # HELP kube_cronjob_info [STABLE] Info about cronjob. + # HELP kube_cronjob_annotations Kubernetes annotations converted to Prometheus labels. + # HELP kube_cronjob_labels [STABLE] Kubernetes labels converted to Prometheus labels. + # HELP kube_cronjob_next_schedule_time [STABLE] Next time the cronjob should be scheduled. The time after lastScheduleTime, or after the cron job's creation time if it's never been scheduled. Use this to determine if the job is delayed. + # HELP kube_cronjob_spec_failed_job_history_limit Failed job history limit tells the controller how many failed jobs should be preserved. + # HELP kube_cronjob_spec_starting_deadline_seconds [STABLE] Deadline in seconds for starting the job if it misses scheduled time for any reason. + # HELP kube_cronjob_spec_successful_job_history_limit Successful job history limit tells the controller how many completed jobs should be preserved. + # HELP kube_cronjob_spec_suspend [STABLE] Suspend flag tells the controller to suspend subsequent executions. + # HELP kube_cronjob_status_active [STABLE] Active holds pointers to currently running jobs. + # HELP kube_cronjob_metadata_resource_version [STABLE] Resource version representing a specific version of the cronjob. + # HELP kube_cronjob_status_last_schedule_time [STABLE] LastScheduleTime keeps information of when was the last time the job was successfully scheduled. + # TYPE kube_cronjob_created gauge + # TYPE kube_cronjob_info gauge + # TYPE kube_cronjob_annotations gauge + # TYPE kube_cronjob_labels gauge + # TYPE kube_cronjob_next_schedule_time gauge + # TYPE kube_cronjob_spec_failed_job_history_limit gauge + # TYPE kube_cronjob_spec_starting_deadline_seconds gauge + # TYPE kube_cronjob_spec_successful_job_history_limit gauge + # TYPE kube_cronjob_spec_suspend gauge + # TYPE kube_cronjob_status_active gauge + # TYPE kube_cronjob_metadata_resource_version gauge + # TYPE kube_cronjob_status_last_schedule_time gauge + kube_cronjob_info{concurrency_policy="Forbid",cronjob="ActiveRunningCronJobWithTZ1",namespace="ns1",schedule="0 */6 * * *",timezone="Asia/Shanghai"} 1 + kube_cronjob_annotations{annotation_app_k8s_io_owner="@foo",cronjob="ActiveRunningCronJobWithTZ1",namespace="ns1"} 1 + kube_cronjob_spec_failed_job_history_limit{cronjob="ActiveRunningCronJobWithTZ1",namespace="ns1"} 1 + kube_cronjob_spec_starting_deadline_seconds{cronjob="ActiveRunningCronJobWithTZ1",namespace="ns1"} 300 + kube_cronjob_spec_successful_job_history_limit{cronjob="ActiveRunningCronJobWithTZ1",namespace="ns1"} 3 + kube_cronjob_spec_suspend{cronjob="ActiveRunningCronJobWithTZ1",namespace="ns1"} 0 + kube_cronjob_status_active{cronjob="ActiveRunningCronJobWithTZ1",namespace="ns1"} 2 + kube_cronjob_metadata_resource_version{cronjob="ActiveRunningCronJobWithTZ1",namespace="ns1"} 11111 + kube_cronjob_status_last_schedule_time{cronjob="ActiveRunningCronJobWithTZ1",namespace="ns1"} 1.520742896e+09 +` + fmt.Sprintf("kube_cronjob_next_schedule_time{cronjob=\"ActiveRunningCronJobWithTZ1\",namespace=\"ns1\"} %ve+09\n", + float64(ActiveRunningCronJob1NextScheduleTime.Unix())/math.Pow10(9)), + MetricNames: []string{ + "kube_cronjob_next_schedule_time", + "kube_cronjob_spec_starting_deadline_seconds", + "kube_cronjob_status_active", + "kube_cronjob_metadata_resource_version", + "kube_cronjob_spec_suspend", + "kube_cronjob_info", + "kube_cronjob_created", + "kube_cronjob_annotations", + "kube_cronjob_labels", + "kube_cronjob_status_last_schedule_time", + "kube_cronjob_spec_successful_job_history_limit", + "kube_cronjob_spec_failed_job_history_limit", + }, + }, { AllowAnnotationsList: []string{ "app.k8s.io/owner", @@ -235,7 +319,7 @@ func TestCronJobStore(t *testing.T) { # TYPE kube_cronjob_metadata_resource_version gauge # TYPE kube_cronjob_status_last_schedule_time gauge # TYPE kube_cronjob_status_last_successful_time gauge - kube_cronjob_info{concurrency_policy="Forbid",cronjob="SuspendedCronJob1",namespace="ns1",schedule="0 */3 * * *",timezone="Europe/Berlin"} 1 + kube_cronjob_info{concurrency_policy="Forbid",cronjob="SuspendedCronJob1",namespace="ns1",schedule="0 */3 * * *",timezone="Asia/Shanghai"} 1 kube_cronjob_spec_failed_job_history_limit{cronjob="SuspendedCronJob1",namespace="ns1"} 1 kube_cronjob_spec_starting_deadline_seconds{cronjob="SuspendedCronJob1",namespace="ns1"} 300 kube_cronjob_spec_successful_job_history_limit{cronjob="SuspendedCronJob1",namespace="ns1"} 3 @@ -353,7 +437,7 @@ func TestCronJobStore(t *testing.T) { # TYPE kube_cronjob_spec_successful_job_history_limit gauge # TYPE kube_cronjob_spec_suspend gauge # TYPE kube_cronjob_status_active gauge - # TYPE kube_cronjob_metadata_resource_version gauge + # TYPE kube_cronjob_metadata_resource_version gauge # TYPE kube_cronjob_status_last_successful_time gauge kube_cronjob_spec_starting_deadline_seconds{cronjob="ActiveCronJob1NoLastScheduled",namespace="ns1"} 300 kube_cronjob_status_active{cronjob="ActiveCronJob1NoLastScheduled",namespace="ns1"} 0 @@ -377,3 +461,36 @@ func TestCronJobStore(t *testing.T) { } } } + +func TestGetNextScheduledTime(t *testing.T) { + + testCases := []struct { + schedule string + lastScheduleTime metav1.Time + createdTime metav1.Time + timeZone string + expected time.Time + }{ + { + schedule: "0 */6 * * *", + lastScheduleTime: metav1.Time{Time: ActiveRunningCronJob1LastScheduleTime}, + createdTime: metav1.Time{Time: ActiveRunningCronJob1LastScheduleTime}, + timeZone: "UTC", + expected: ActiveRunningCronJob1LastScheduleTime.Add(time.Second*4 + time.Minute*25 + time.Hour), + }, + { + schedule: "0 */6 * * *", + lastScheduleTime: metav1.Time{Time: ActiveRunningCronJob1LastScheduleTime}, + createdTime: metav1.Time{Time: ActiveRunningCronJob1LastScheduleTime}, + timeZone: TimeZone, + expected: ActiveRunningCronJob1LastScheduleTime.Add(time.Second*4 + time.Minute*25 + time.Hour*5), + }, + } + + for _, test := range testCases { + if actual, _ := getNextScheduledTime(test.schedule, &test.lastScheduleTime, test.createdTime, &test.timeZone); !actual.Equal(test.expected) { + t.Fatalf("%v: expected %v, actual %v", test.schedule, test.expected, actual) + } + } + +}