Skip to content

Commit

Permalink
Store the minReplicas/maxReplicas recommendations to correct slots ba…
Browse files Browse the repository at this point in the history
…sed on timezone (#137)
  • Loading branch information
sanposhiho authored Oct 4, 2023
1 parent 77c2687 commit 5e3e8ca
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/recommender/recommender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions pkg/recommender/recommender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
},
Expand All @@ -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()),
},
Expand All @@ -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()),
},
Expand All @@ -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()),
},
Expand Down Expand Up @@ -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()),
},
Expand All @@ -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()),
},
Expand All @@ -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()),
Expand All @@ -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()),
Expand All @@ -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()),
},
Expand All @@ -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()),
},
Expand All @@ -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()),
},
Expand All @@ -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()),
},
Expand All @@ -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()),
},
Expand All @@ -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()),
},
Expand All @@ -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()),
},
Expand All @@ -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()),
},
Expand Down

0 comments on commit 5e3e8ca

Please sign in to comment.