From e5ac18e6fe077b0411626f5b216b9d95caea1a7f Mon Sep 17 00:00:00 2001 From: Jigisha Patil <89548848+jigisha620@users.noreply.github.com> Date: Mon, 4 Mar 2024 23:14:05 -0800 Subject: [PATCH] chore: cherry pick commit (#1063) (#1064) Co-authored-by: Nick Tran <10810510+njtran@users.noreply.github.com> --- pkg/apis/v1beta1/nodepool.go | 6 +++--- pkg/apis/v1beta1/nodepool_budgets_test.go | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/pkg/apis/v1beta1/nodepool.go b/pkg/apis/v1beta1/nodepool.go index 966bfe70b3..80cac9a35b 100644 --- a/pkg/apis/v1beta1/nodepool.go +++ b/pkg/apis/v1beta1/nodepool.go @@ -285,16 +285,16 @@ func (in *Budget) IsActive(c clock.Clock) (bool, error) { if in.Schedule == nil && in.Duration == nil { return true, nil } - schedule, err := cron.ParseStandard(lo.FromPtr(in.Schedule)) + schedule, err := cron.ParseStandard(fmt.Sprintf("TZ=UTC %s", lo.FromPtr(in.Schedule))) if err != nil { // Should only occur if there's a discrepancy // with the validation regex and the cron package. return false, fmt.Errorf("invariant violated, invalid cron %s", schedule) } // Walk back in time for the duration associated with the schedule - checkPoint := c.Now().Add(-lo.FromPtr(in.Duration).Duration) + checkPoint := c.Now().UTC().Add(-lo.FromPtr(in.Duration).Duration) nextHit := schedule.Next(checkPoint) - return !nextHit.After(c.Now()), nil + return !nextHit.After(c.Now().UTC()), nil } func GetIntStrFromValue(str string) intstr.IntOrString { diff --git a/pkg/apis/v1beta1/nodepool_budgets_test.go b/pkg/apis/v1beta1/nodepool_budgets_test.go index e6249e4315..16cedc277d 100644 --- a/pkg/apis/v1beta1/nodepool_budgets_test.go +++ b/pkg/apis/v1beta1/nodepool_budgets_test.go @@ -128,6 +128,16 @@ var _ = Describe("Budgets", func() { }) }) Context("IsActive", func() { + It("should always consider a schedule and time in UTC", func() { + // Set the time to start of June 2000 in a time zone 1 hour ahead of UTC + fakeClock = clock.NewFakeClock(time.Date(2000, time.June, 0, 0, 0, 0, 0, time.FixedZone("fake-zone", 3600))) + budgets[0].Schedule = lo.ToPtr("@daily") + budgets[0].Duration = lo.ToPtr(metav1.Duration{Duration: lo.Must(time.ParseDuration("30m"))}) + // IsActive should use UTC, not the location of the clock that's inputted. + active, err := budgets[0].IsActive(fakeClock) + Expect(err).To(Succeed()) + Expect(active).To(BeFalse()) + }) It("should return that a schedule is active when schedule and duration are nil", func() { budgets[0].Schedule = nil budgets[0].Duration = nil