@@ -40,69 +40,163 @@ var (
4040 ActiveRunningCronJob1LastScheduleTime = time .Unix (1520742896 , 0 )
4141 SuspendedCronJob1LastScheduleTime = time .Unix (1520742896 + 5.5 * 3600 , 0 ) // 5.5 hours later
4242 ActiveCronJob1NoLastScheduledCreationTimestamp = time .Unix (1520742896 + 6.5 * 3600 , 0 )
43- TimeZone = "Europe/Berlin "
43+ TimeZone = "Asia/Shanghai "
4444)
4545
46- func TestCronJobStore ( t * testing. T ) {
47- hour := ActiveRunningCronJob1LastScheduleTime . Hour ( )
48- ActiveRunningCronJob1NextScheduleTime := time. Time {}
46+ func calculateNextSchedule6h ( timestamp time. Time , timezone string ) time. Time {
47+ loc , _ := time . LoadLocation ( timezone )
48+ hour := timestamp . In ( loc ). Hour ()
4949 switch {
5050 case hour < 6 :
51- ActiveRunningCronJob1NextScheduleTime = time .Date (
52- ActiveRunningCronJob1LastScheduleTime .Year (),
53- ActiveRunningCronJob1LastScheduleTime .Month (),
54- ActiveRunningCronJob1LastScheduleTime .Day (),
51+ return time .Date (
52+ timestamp .Year (),
53+ timestamp .Month (),
54+ timestamp .Day (),
5555 6 ,
5656 0 ,
57- 0 , 0 , time . Local )
57+ 0 , 0 , loc )
5858 case hour < 12 :
59- ActiveRunningCronJob1NextScheduleTime = time .Date (
60- ActiveRunningCronJob1LastScheduleTime .Year (),
61- ActiveRunningCronJob1LastScheduleTime .Month (),
62- ActiveRunningCronJob1LastScheduleTime .Day (),
59+ return time .Date (
60+ timestamp .Year (),
61+ timestamp .Month (),
62+ timestamp .Day (),
6363 12 ,
6464 0 ,
65- 0 , 0 , time . Local )
65+ 0 , 0 , loc )
6666 case hour < 18 :
67- ActiveRunningCronJob1NextScheduleTime = time .Date (
68- ActiveRunningCronJob1LastScheduleTime .Year (),
69- ActiveRunningCronJob1LastScheduleTime .Month (),
70- ActiveRunningCronJob1LastScheduleTime .Day (),
67+ return time .Date (
68+ timestamp .Year (),
69+ timestamp .Month (),
70+ timestamp .Day (),
7171 18 ,
7272 0 ,
73- 0 , 0 , time . Local )
74- case hour < 24 :
75- ActiveRunningCronJob1NextScheduleTime = time .Date (
76- ActiveRunningCronJob1LastScheduleTime .Year (),
77- ActiveRunningCronJob1LastScheduleTime .Month (),
78- ActiveRunningCronJob1LastScheduleTime .Day (),
79- 24 ,
73+ 0 , 0 , loc )
74+ default :
75+ return time .Date (
76+ timestamp .Year (),
77+ timestamp .Month (),
78+ timestamp .Day ()+ 1 ,
79+ 0 ,
8080 0 ,
81- 0 , 0 , time . Local )
81+ 0 , 0 , loc )
8282 }
83+ }
8384
84- minute := ActiveCronJob1NoLastScheduledCreationTimestamp .Minute ()
85- ActiveCronJob1NoLastScheduledNextScheduleTime := time.Time {}
85+ func calculateNextSchedule25m (timestamp time.Time , timezone string ) time.Time {
86+ loc , _ := time .LoadLocation (timezone )
87+ minute := timestamp .In (loc ).Minute ()
8688 switch {
8789 case minute < 25 :
88- ActiveCronJob1NoLastScheduledNextScheduleTime = time .Date (
89- ActiveCronJob1NoLastScheduledCreationTimestamp .Year (),
90- ActiveCronJob1NoLastScheduledCreationTimestamp .Month (),
91- ActiveCronJob1NoLastScheduledCreationTimestamp .Day (),
92- ActiveCronJob1NoLastScheduledCreationTimestamp .Hour (),
90+ return time .Date (
91+ timestamp .Year (),
92+ timestamp .Month (),
93+ timestamp .Day (),
94+ timestamp .Hour (),
9395 25 ,
94- 0 , 0 , time . Local )
96+ 0 , 0 , loc )
9597 default :
96- ActiveCronJob1NoLastScheduledNextScheduleTime = time .Date (
97- ActiveCronJob1NoLastScheduledNextScheduleTime .Year (),
98- ActiveCronJob1NoLastScheduledNextScheduleTime .Month (),
99- ActiveCronJob1NoLastScheduledNextScheduleTime .Day (),
100- ActiveCronJob1NoLastScheduledNextScheduleTime .Hour ()+ 1 ,
98+ return time .Date (
99+ timestamp .Year (),
100+ timestamp .Month (),
101+ timestamp .Day (),
102+ timestamp .Hour ()+ 1 ,
101103 25 ,
102- 0 , 0 , time . Local )
104+ 0 , 0 , loc )
103105 }
104106
107+ }
108+ func TestCronJobStore (t * testing.T ) {
109+
110+ ActiveRunningCronJob1NextScheduleTime := calculateNextSchedule6h (ActiveRunningCronJob1LastScheduleTime , "Local" )
111+ ActiveRunningCronJobWithTZ1NextScheduleTime := calculateNextSchedule6h (ActiveRunningCronJob1LastScheduleTime , TimeZone )
112+
113+ ActiveCronJob1NoLastScheduledNextScheduleTime := calculateNextSchedule25m (ActiveCronJob1NoLastScheduledCreationTimestamp , "Local" )
114+
105115 cases := []generateMetricsTestCase {
116+ {
117+ AllowAnnotationsList : []string {
118+ "app.k8s.io/owner" ,
119+ },
120+ Obj : & batchv1.CronJob {
121+ ObjectMeta : metav1.ObjectMeta {
122+ Name : "ActiveRunningCronJobWithTZ1" ,
123+ Namespace : "ns1" ,
124+ Generation : 1 ,
125+ ResourceVersion : "11111" ,
126+ Labels : map [string ]string {
127+ "app" : "example-active-running-with-tz-1" ,
128+ },
129+ Annotations : map [string ]string {
130+ "app" : "mysql-server" ,
131+ "app.k8s.io/owner" : "@foo" ,
132+ },
133+ },
134+ Status : batchv1.CronJobStatus {
135+ Active : []v1.ObjectReference {{Name : "FakeJob1" }, {Name : "FakeJob2" }},
136+ LastScheduleTime : & metav1.Time {Time : ActiveRunningCronJob1LastScheduleTime },
137+ LastSuccessfulTime : nil ,
138+ },
139+ Spec : batchv1.CronJobSpec {
140+ StartingDeadlineSeconds : & StartingDeadlineSeconds300 ,
141+ ConcurrencyPolicy : "Forbid" ,
142+ Suspend : & SuspendFalse ,
143+ Schedule : "0 */6 * * *" ,
144+ SuccessfulJobsHistoryLimit : & SuccessfulJobHistoryLimit3 ,
145+ FailedJobsHistoryLimit : & FailedJobHistoryLimit1 ,
146+ TimeZone : & TimeZone ,
147+ },
148+ },
149+ Want : `
150+ # HELP kube_cronjob_created [STABLE] Unix creation timestamp
151+ # HELP kube_cronjob_info [STABLE] Info about cronjob.
152+ # HELP kube_cronjob_annotations Kubernetes annotations converted to Prometheus labels.
153+ # HELP kube_cronjob_labels [STABLE] Kubernetes labels converted to Prometheus labels.
154+ # 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.
155+ # HELP kube_cronjob_spec_failed_job_history_limit Failed job history limit tells the controller how many failed jobs should be preserved.
156+ # HELP kube_cronjob_spec_starting_deadline_seconds [STABLE] Deadline in seconds for starting the job if it misses scheduled time for any reason.
157+ # HELP kube_cronjob_spec_successful_job_history_limit Successful job history limit tells the controller how many completed jobs should be preserved.
158+ # HELP kube_cronjob_spec_suspend [STABLE] Suspend flag tells the controller to suspend subsequent executions.
159+ # HELP kube_cronjob_status_active [STABLE] Active holds pointers to currently running jobs.
160+ # HELP kube_cronjob_metadata_resource_version [STABLE] Resource version representing a specific version of the cronjob.
161+ # HELP kube_cronjob_status_last_schedule_time [STABLE] LastScheduleTime keeps information of when was the last time the job was successfully scheduled.
162+ # TYPE kube_cronjob_created gauge
163+ # TYPE kube_cronjob_info gauge
164+ # TYPE kube_cronjob_annotations gauge
165+ # TYPE kube_cronjob_labels gauge
166+ # TYPE kube_cronjob_next_schedule_time gauge
167+ # TYPE kube_cronjob_spec_failed_job_history_limit gauge
168+ # TYPE kube_cronjob_spec_starting_deadline_seconds gauge
169+ # TYPE kube_cronjob_spec_successful_job_history_limit gauge
170+ # TYPE kube_cronjob_spec_suspend gauge
171+ # TYPE kube_cronjob_status_active gauge
172+ # TYPE kube_cronjob_metadata_resource_version gauge
173+ # TYPE kube_cronjob_status_last_schedule_time gauge
174+ kube_cronjob_info{concurrency_policy="Forbid",cronjob="ActiveRunningCronJobWithTZ1",namespace="ns1",schedule="0 */6 * * *",timezone="Asia/Shanghai"} 1
175+ kube_cronjob_annotations{annotation_app_k8s_io_owner="@foo",cronjob="ActiveRunningCronJobWithTZ1",namespace="ns1"} 1
176+ kube_cronjob_spec_failed_job_history_limit{cronjob="ActiveRunningCronJobWithTZ1",namespace="ns1"} 1
177+ kube_cronjob_spec_starting_deadline_seconds{cronjob="ActiveRunningCronJobWithTZ1",namespace="ns1"} 300
178+ kube_cronjob_spec_successful_job_history_limit{cronjob="ActiveRunningCronJobWithTZ1",namespace="ns1"} 3
179+ kube_cronjob_spec_suspend{cronjob="ActiveRunningCronJobWithTZ1",namespace="ns1"} 0
180+ kube_cronjob_status_active{cronjob="ActiveRunningCronJobWithTZ1",namespace="ns1"} 2
181+ kube_cronjob_metadata_resource_version{cronjob="ActiveRunningCronJobWithTZ1",namespace="ns1"} 11111
182+ kube_cronjob_status_last_schedule_time{cronjob="ActiveRunningCronJobWithTZ1",namespace="ns1"} 1.520742896e+09
183+ ` + fmt .Sprintf ("kube_cronjob_next_schedule_time{cronjob=\" ActiveRunningCronJobWithTZ1\" ,namespace=\" ns1\" } %ve+09\n " ,
184+ float64 (ActiveRunningCronJobWithTZ1NextScheduleTime .Unix ())/ math .Pow10 (9 )),
185+ MetricNames : []string {
186+ "kube_cronjob_next_schedule_time" ,
187+ "kube_cronjob_spec_starting_deadline_seconds" ,
188+ "kube_cronjob_status_active" ,
189+ "kube_cronjob_metadata_resource_version" ,
190+ "kube_cronjob_spec_suspend" ,
191+ "kube_cronjob_info" ,
192+ "kube_cronjob_created" ,
193+ "kube_cronjob_annotations" ,
194+ "kube_cronjob_labels" ,
195+ "kube_cronjob_status_last_schedule_time" ,
196+ "kube_cronjob_spec_successful_job_history_limit" ,
197+ "kube_cronjob_spec_failed_job_history_limit" ,
198+ },
199+ },
106200 {
107201 AllowAnnotationsList : []string {
108202 "app.k8s.io/owner" ,
@@ -235,7 +329,7 @@ func TestCronJobStore(t *testing.T) {
235329 # TYPE kube_cronjob_metadata_resource_version gauge
236330 # TYPE kube_cronjob_status_last_schedule_time gauge
237331 # TYPE kube_cronjob_status_last_successful_time gauge
238- kube_cronjob_info{concurrency_policy="Forbid",cronjob="SuspendedCronJob1",namespace="ns1",schedule="0 */3 * * *",timezone="Europe/Berlin "} 1
332+ kube_cronjob_info{concurrency_policy="Forbid",cronjob="SuspendedCronJob1",namespace="ns1",schedule="0 */3 * * *",timezone="Asia/Shanghai "} 1
239333 kube_cronjob_spec_failed_job_history_limit{cronjob="SuspendedCronJob1",namespace="ns1"} 1
240334 kube_cronjob_spec_starting_deadline_seconds{cronjob="SuspendedCronJob1",namespace="ns1"} 300
241335 kube_cronjob_spec_successful_job_history_limit{cronjob="SuspendedCronJob1",namespace="ns1"} 3
@@ -353,7 +447,7 @@ func TestCronJobStore(t *testing.T) {
353447 # TYPE kube_cronjob_spec_successful_job_history_limit gauge
354448 # TYPE kube_cronjob_spec_suspend gauge
355449 # TYPE kube_cronjob_status_active gauge
356- # TYPE kube_cronjob_metadata_resource_version gauge
450+ # TYPE kube_cronjob_metadata_resource_version gauge
357451 # TYPE kube_cronjob_status_last_successful_time gauge
358452 kube_cronjob_spec_starting_deadline_seconds{cronjob="ActiveCronJob1NoLastScheduled",namespace="ns1"} 300
359453 kube_cronjob_status_active{cronjob="ActiveCronJob1NoLastScheduled",namespace="ns1"} 0
@@ -377,3 +471,37 @@ func TestCronJobStore(t *testing.T) {
377471 }
378472 }
379473}
474+
475+ func TestGetNextScheduledTime (t * testing.T ) {
476+
477+ testCases := []struct {
478+ schedule string
479+ lastScheduleTime metav1.Time
480+ createdTime metav1.Time
481+ timeZone string
482+ expected time.Time
483+ }{
484+ {
485+ schedule : "0 */6 * * *" ,
486+ lastScheduleTime : metav1.Time {Time : ActiveRunningCronJob1LastScheduleTime },
487+ createdTime : metav1.Time {Time : ActiveRunningCronJob1LastScheduleTime },
488+ timeZone : "UTC" ,
489+ expected : ActiveRunningCronJob1LastScheduleTime .Add (time .Second * 4 + time .Minute * 25 + time .Hour ),
490+ },
491+ {
492+ schedule : "0 */6 * * *" ,
493+ lastScheduleTime : metav1.Time {Time : ActiveRunningCronJob1LastScheduleTime },
494+ createdTime : metav1.Time {Time : ActiveRunningCronJob1LastScheduleTime },
495+ timeZone : TimeZone ,
496+ expected : ActiveRunningCronJob1LastScheduleTime .Add (time .Second * 4 + time .Minute * 25 + time .Hour * 5 ),
497+ },
498+ }
499+
500+ for _ , test := range testCases {
501+ actual , _ := getNextScheduledTime (test .schedule , & test .lastScheduleTime , test .createdTime , & test .timeZone ) // #nosec G601
502+ if ! actual .Equal (test .expected ) {
503+ t .Fatalf ("%v: expected %v, actual %v" , test .schedule , test .expected , actual )
504+ }
505+ }
506+
507+ }
0 commit comments