From f3b928a23da592c6e7a3b9405549e8751a8a5dfb Mon Sep 17 00:00:00 2001 From: Ross Peoples Date: Thu, 24 Mar 2022 17:09:45 -0500 Subject: [PATCH] Import tzdata in apiserver, CronJob.timeZone fixes --- cmd/kube-apiserver/apiserver.go | 1 + pkg/controller/cronjob/cronjob_controllerv2.go | 9 ++++++--- pkg/controller/cronjob/cronjob_controllerv2_test.go | 7 +++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/cmd/kube-apiserver/apiserver.go b/cmd/kube-apiserver/apiserver.go index 42956a9bf6e..9767fa6ab35 100644 --- a/cmd/kube-apiserver/apiserver.go +++ b/cmd/kube-apiserver/apiserver.go @@ -20,6 +20,7 @@ package main import ( "os" + _ "time/tzdata" // for timeZone support in CronJob "k8s.io/component-base/cli" _ "k8s.io/component-base/logs/json/register" // for JSON log format registration diff --git a/pkg/controller/cronjob/cronjob_controllerv2.go b/pkg/controller/cronjob/cronjob_controllerv2.go index 9f607856a35..d31affdf719 100644 --- a/pkg/controller/cronjob/cronjob_controllerv2.go +++ b/pkg/controller/cronjob/cronjob_controllerv2.go @@ -386,8 +386,8 @@ func (jm *ControllerV2) updateCronJob(old interface{}, curr interface{}) { // it will be handled here by the queue. If the next requeue is further than previous schedule, // the sync loop will essentially be a no-op for the already queued key with old schedule. if oldCJ.Spec.Schedule != newCJ.Spec.Schedule || (timeZoneEnabled && !pointer.StringEqual(oldCJ.Spec.TimeZone, newCJ.Spec.TimeZone)) { - // schedule changed, change the requeue time - sched, err := cron.ParseStandard(formatSchedule(timeZoneEnabled, newCJ, jm.recorder)) + // schedule changed, change the requeue time, pass nil recorder so that syncCronJob will output any warnings + sched, err := cron.ParseStandard(formatSchedule(timeZoneEnabled, newCJ, nil)) if err != nil { // this is likely a user error in defining the spec value // we should log the error and not reconcile this cronjob until an update to spec @@ -752,7 +752,10 @@ func getRef(object runtime.Object) (*corev1.ObjectReference, error) { func formatSchedule(timeZoneEnabled bool, cj *batchv1.CronJob, recorder record.EventRecorder) string { if strings.Contains(cj.Spec.Schedule, "TZ") { - recorder.Eventf(cj, corev1.EventTypeWarning, "UnsupportedSchedule", "CRON_TZ or TZ used in schedule %q is not officially supported, see https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/ for more details", cj.Spec.Schedule) + if recorder != nil { + recorder.Eventf(cj, corev1.EventTypeWarning, "UnsupportedSchedule", "CRON_TZ or TZ used in schedule %q is not officially supported, see https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/ for more details", cj.Spec.Schedule) + } + return cj.Spec.Schedule } diff --git a/pkg/controller/cronjob/cronjob_controllerv2_test.go b/pkg/controller/cronjob/cronjob_controllerv2_test.go index 19578bd1f39..b782597b7c6 100644 --- a/pkg/controller/cronjob/cronjob_controllerv2_test.go +++ b/pkg/controller/cronjob/cronjob_controllerv2_test.go @@ -900,7 +900,10 @@ func TestControllerV2SyncCronJob(t *testing.T) { for name, tc := range testCases { name := name tc := tc + t.Run(name, func(t *testing.T) { + defer featuregatetesting.SetFeatureGateDuringTest(t, feature.DefaultFeatureGate, features.CronJobTimeZone, tc.enableTimeZone) + cj := cronJob() cj.Spec.ConcurrencyPolicy = tc.concurrencyPolicy cj.Spec.Suspend = &tc.suspend @@ -910,10 +913,6 @@ func TestControllerV2SyncCronJob(t *testing.T) { cj.Spec.StartingDeadlineSeconds = &tc.deadline } - if tc.enableTimeZone { - defer featuregatetesting.SetFeatureGateDuringTest(t, feature.DefaultFeatureGate, features.CronJobTimeZone, true) - } - var ( job *batchv1.Job err error