From 3b2f5be4a27d67dfe93641c58a9d9b0731b86c98 Mon Sep 17 00:00:00 2001 From: Kensei Nakada Date: Wed, 4 Oct 2023 11:17:55 +0900 Subject: [PATCH] Store the minReplicas/maxReplicas recommendations to correct slots based on timezone --- pkg/recommender/recommender.go | 5 +++++ pkg/recommender/recommender_test.go | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/pkg/recommender/recommender.go b/pkg/recommender/recommender.go index ac87f9a0..5c596f7b 100644 --- a/pkg/recommender/recommender.go +++ b/pkg/recommender/recommender.go @@ -217,6 +217,11 @@ func (s *Service) updateMaxMinReplicasRecommendation(value int32, recommendation // find the corresponding recommendations. index := -1 for i, r := range recommendations { + tz, err := time.LoadLocation(r.TimeZone) + if err == nil { + // if the timezone is invalid, just ignore it. + now = now.In(tz) + } if now.Hour() < r.To && now.Hour() >= r.From && (r.WeekDay == nil || now.Weekday().String() == *r.WeekDay) { index = i break diff --git a/pkg/recommender/recommender_test.go b/pkg/recommender/recommender_test.go index 6009ae1c..06a3f29a 100644 --- a/pkg/recommender/recommender_test.go +++ b/pkg/recommender/recommender_test.go @@ -268,6 +268,7 @@ func Test_updateHPAMinMaxReplicasRecommendations(t *testing.T) { From: 0, To: 1, UpdatedAt: metav1.NewTime(time.Date(2023, 3, 12, 0, 0, 0, 0, jst)), + TimeZone: timeZone, Value: 1, WeekDay: pointer.String(time.Sunday.String()), }, @@ -277,6 +278,7 @@ func Test_updateHPAMinMaxReplicasRecommendations(t *testing.T) { From: 0, To: 1, UpdatedAt: metav1.NewTime(time.Date(2023, 3, 12, 0, 0, 0, 0, jst)), + TimeZone: timeZone, Value: 7, WeekDay: pointer.String(time.Sunday.String()), }, @@ -301,6 +303,7 @@ func Test_updateHPAMinMaxReplicasRecommendations(t *testing.T) { From: 0, To: 1, UpdatedAt: metav1.NewTime(time.Date(2023, 3, 19, 0, 0, 0, 0, jst)), + TimeZone: timeZone, Value: 5, WeekDay: pointer.String(time.Sunday.String()), }, @@ -310,6 +313,7 @@ func Test_updateHPAMinMaxReplicasRecommendations(t *testing.T) { From: 0, To: 1, UpdatedAt: metav1.NewTime(time.Date(2023, 3, 19, 0, 0, 0, 0, jst)), + TimeZone: timeZone, Value: 20, WeekDay: pointer.String(time.Sunday.String()), }, @@ -364,6 +368,7 @@ func Test_updateHPAMinMaxReplicasRecommendations(t *testing.T) { From: 0, To: 1, UpdatedAt: metav1.NewTime(time.Date(2023, 3, 12, 0, 0, 0, 0, jst)), + TimeZone: timeZone, Value: 10, WeekDay: pointer.String(time.Sunday.String()), }, @@ -373,6 +378,7 @@ func Test_updateHPAMinMaxReplicasRecommendations(t *testing.T) { From: 0, To: 1, UpdatedAt: metav1.NewTime(time.Date(2023, 3, 12, 0, 0, 0, 0, jst)), + TimeZone: timeZone, Value: 25, WeekDay: pointer.String(time.Sunday.String()), }, @@ -397,6 +403,7 @@ func Test_updateHPAMinMaxReplicasRecommendations(t *testing.T) { From: 0, To: 1, // UpdatedAt is updated. + TimeZone: timeZone, UpdatedAt: metav1.NewTime(time.Date(2023, 3, 12, 0, 0, 0, 0, jst)), Value: 10, WeekDay: pointer.String(time.Sunday.String()), @@ -407,6 +414,7 @@ func Test_updateHPAMinMaxReplicasRecommendations(t *testing.T) { From: 0, To: 1, // UpdatedAt is updated. + TimeZone: timeZone, UpdatedAt: metav1.NewTime(time.Date(2023, 3, 12, 0, 0, 0, 0, jst)), Value: 25, WeekDay: pointer.String(time.Sunday.String()), @@ -430,6 +438,7 @@ func Test_updateHPAMinMaxReplicasRecommendations(t *testing.T) { From: 0, To: 1, UpdatedAt: metav1.NewTime(time.Date(2023, 3, 12, 0, 0, 0, 0, jst)), + TimeZone: timeZone, Value: 3, WeekDay: pointer.String(time.Sunday.String()), }, @@ -439,6 +448,7 @@ func Test_updateHPAMinMaxReplicasRecommendations(t *testing.T) { From: 0, To: 1, UpdatedAt: metav1.NewTime(time.Date(2023, 3, 12, 0, 0, 0, 0, jst)), + TimeZone: timeZone, Value: 8, WeekDay: pointer.String(time.Sunday.String()), }, @@ -463,6 +473,7 @@ func Test_updateHPAMinMaxReplicasRecommendations(t *testing.T) { From: 0, To: 1, UpdatedAt: metav1.NewTime(time.Date(2023, 3, 19, 0, 0, 0, 0, jst)), + TimeZone: timeZone, Value: 5, WeekDay: pointer.String(time.Sunday.String()), }, @@ -472,6 +483,7 @@ func Test_updateHPAMinMaxReplicasRecommendations(t *testing.T) { From: 0, To: 1, UpdatedAt: metav1.NewTime(time.Date(2023, 3, 19, 0, 0, 0, 0, jst)), + TimeZone: timeZone, Value: 20, WeekDay: pointer.String(time.Sunday.String()), }, @@ -494,6 +506,7 @@ func Test_updateHPAMinMaxReplicasRecommendations(t *testing.T) { From: 0, To: 1, UpdatedAt: metav1.NewTime(time.Date(2023, 3, 12, 0, 0, 0, 0, jst)), + TimeZone: timeZone, Value: 3, WeekDay: pointer.String(time.Sunday.String()), }, @@ -503,6 +516,7 @@ func Test_updateHPAMinMaxReplicasRecommendations(t *testing.T) { From: 0, To: 1, UpdatedAt: metav1.NewTime(time.Date(2023, 3, 12, 0, 0, 0, 0, jst)), + TimeZone: timeZone, Value: 8, WeekDay: pointer.String(time.Sunday.String()), }, @@ -527,6 +541,7 @@ func Test_updateHPAMinMaxReplicasRecommendations(t *testing.T) { From: 0, To: 1, UpdatedAt: metav1.NewTime(time.Date(2023, 3, 19, 0, 0, 0, 0, jst)), + TimeZone: timeZone, Value: 3, WeekDay: pointer.String(time.Sunday.String()), }, @@ -536,6 +551,7 @@ func Test_updateHPAMinMaxReplicasRecommendations(t *testing.T) { From: 0, To: 1, UpdatedAt: metav1.NewTime(time.Date(2023, 3, 19, 0, 0, 0, 0, jst)), + TimeZone: timeZone, Value: 12, WeekDay: pointer.String(time.Sunday.String()), },