Skip to content

Commit

Permalink
support min-max-replicas-routine (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanposhiho authored Aug 29, 2023
1 parent 9363f40 commit 320f4dc
Show file tree
Hide file tree
Showing 13 changed files with 251 additions and 118 deletions.
8 changes: 5 additions & 3 deletions api/v1alpha1/tortoise_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,11 @@ type ReplicasRecommendation struct {
// From represented in hour.
From int `json:"from" protobuf:"variant,1,name=from"`
// To represented in hour.
To int `json:"to" protobuf:"variant,2,name=to"`
WeekDay string `json:"weekday" protobuf:"bytes,3,name=weekday"`
TimeZone string `json:"timezone" protobuf:"bytes,4,name=timezone"`
To int `json:"to" protobuf:"variant,2,name=to"`
// WeekDay is the day of the week.
// If empty, it means it applies to all days of the week.
WeekDay *string `json:"weekday,omitempty" protobuf:"bytes,3,opt,name=weekday"`
TimeZone string `json:"timezone" protobuf:"bytes,4,name=timezone"`
// Value is the recommendation value.
// It's calculated every reconciliation,
// and updated if the calculated recommendation value is more than the current recommendation value on tortoise.
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions config/crd/bases/autoscaling.mercari.com_tortoises.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,14 @@ spec:
format: int32
type: integer
weekday:
description: WeekDay is the day of the week. If empty,
it means it applies to all days of the week.
type: string
required:
- from
- timezone
- to
- value
- weekday
type: object
type: array
minReplicas:
Expand All @@ -254,13 +255,14 @@ spec:
format: int32
type: integer
weekday:
description: WeekDay is the day of the week. If empty,
it means it applies to all days of the week.
type: string
required:
- from
- timezone
- to
- value
- weekday
type: object
type: array
targetUtilizations:
Expand Down
50 changes: 25 additions & 25 deletions controllers/tortoise_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ var _ = Describe("Test TortoiseController", func() {
})
Expect(err).ShouldNot(HaveOccurred())

tortoiseService, err := tortoise.New(mgr.GetClient(), 1, "Asia/Tokyo", 1000*time.Minute)
tortoiseService, err := tortoise.New(mgr.GetClient(), 1, "Asia/Tokyo", 1000*time.Minute, "weekly")
Expect(err).ShouldNot(HaveOccurred())
cli, err := vpa.New(mgr.GetConfig())
Expect(err).ShouldNot(HaveOccurred())
Expand Down Expand Up @@ -146,7 +146,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 15, // will be updated
UpdatedAt: metav1.NewTime(now),
Expand All @@ -156,7 +156,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 3, // will be updated
UpdatedAt: metav1.NewTime(now),
Expand Down Expand Up @@ -269,7 +269,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 20,
UpdatedAt: metav1.NewTime(now),
Expand All @@ -279,7 +279,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 5,
UpdatedAt: metav1.NewTime(now),
Expand Down Expand Up @@ -367,7 +367,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 15, // will be updated
UpdatedAt: metav1.NewTime(now),
Expand All @@ -377,7 +377,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 3, // will be updated
UpdatedAt: metav1.NewTime(now),
Expand Down Expand Up @@ -463,7 +463,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 20,
UpdatedAt: metav1.NewTime(now),
Expand All @@ -473,7 +473,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 5,
UpdatedAt: metav1.NewTime(now),
Expand Down Expand Up @@ -559,7 +559,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 15, // will be updated
UpdatedAt: metav1.NewTime(now),
Expand All @@ -569,7 +569,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 3, // will be updated
UpdatedAt: metav1.NewTime(now),
Expand Down Expand Up @@ -683,7 +683,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 20,
UpdatedAt: metav1.NewTime(now),
Expand All @@ -693,7 +693,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 5,
UpdatedAt: metav1.NewTime(now),
Expand Down Expand Up @@ -793,7 +793,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 15, // will be updated
UpdatedAt: metav1.NewTime(now),
Expand All @@ -803,7 +803,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 3, // will be updated
UpdatedAt: metav1.NewTime(now),
Expand Down Expand Up @@ -970,7 +970,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 20,
UpdatedAt: metav1.NewTime(now),
Expand All @@ -980,7 +980,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 5,
UpdatedAt: metav1.NewTime(now),
Expand Down Expand Up @@ -1098,7 +1098,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 15, // will be updated
UpdatedAt: metav1.NewTime(now),
Expand All @@ -1108,7 +1108,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 3, // will be updated
UpdatedAt: metav1.NewTime(now),
Expand Down Expand Up @@ -1276,7 +1276,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 20,
UpdatedAt: metav1.NewTime(now),
Expand All @@ -1286,7 +1286,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 5,
UpdatedAt: metav1.NewTime(now),
Expand Down Expand Up @@ -1406,7 +1406,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 15, // will be updated
UpdatedAt: metav1.NewTime(now),
Expand All @@ -1416,7 +1416,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 3, // will be updated
UpdatedAt: metav1.NewTime(now),
Expand Down Expand Up @@ -1517,7 +1517,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 15, // will be updated
UpdatedAt: metav1.NewTime(now),
Expand All @@ -1527,7 +1527,7 @@ var _ = Describe("Test TortoiseController", func() {
{
From: 0,
To: 24,
WeekDay: now.Weekday().String(),
WeekDay: pointer.String(now.Weekday().String()),
TimeZone: now.Location().String(),
Value: 3, // will be updated
UpdatedAt: metav1.NewTime(now),
Expand Down
15 changes: 14 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package main

import (
"flag"
"fmt"
"os"
"time"

Expand Down Expand Up @@ -76,6 +77,7 @@ func main() {

// Tortoise specific flags
var rangeOfMinMaxReplicasRecommendationHours int
var minMaxReplicasRoutine string
var tTLHoursOfMinMaxReplicasRecommendation int
var maxReplicasFactor float64
var minReplicasFactor float64
Expand All @@ -88,6 +90,7 @@ func main() {
var timeZone string
var tortoiseUpdateInterval time.Duration
flag.IntVar(&rangeOfMinMaxReplicasRecommendationHours, "range-of-min-max-replicas-recommendation-hours", 1, "the time (hours) range of minReplicas and maxReplicas recommendation (default: 1)")
flag.StringVar(&minMaxReplicasRoutine, "min-max-replicas-routine", "weekly", "the routine of minReplicas and maxReplicas recommendation (default: weekly)")
flag.IntVar(&tTLHoursOfMinMaxReplicasRecommendation, "ttl-hours-of-min-max-replicas-recommendation", 24*30, "the TTL (hours) of minReplicas and maxReplicas recommendation (default: 720 (=30 days))")
flag.Float64Var(&maxReplicasFactor, "max-replicas-factor", 2.0, "the factor to calculate the maxReplicas recommendation from the current replica number (default: 2.0)")
flag.Float64Var(&minReplicasFactor, "min-replicas-factor", 0.5, "the factor to calculate the minReplicas recommendation from the current replica number (default: 0.5)")
Expand All @@ -100,6 +103,16 @@ func main() {
flag.StringVar(&timeZone, "timezone", "Asia/Tokyo", "The timezone used to record time in tortoise objects (default: Asia/Tokyo)")
flag.DurationVar(&tortoiseUpdateInterval, "tortoise-update-interval", 15*time.Second, "The interval of updating each tortoise (default: 15s)")

if rangeOfMinMaxReplicasRecommendationHours > 24 || rangeOfMinMaxReplicasRecommendationHours < 1 {
setupLog.Error(fmt.Errorf("range-of-min-max-replicas-recommendation-hours should be between 1 and 24"), "invalid value")
os.Exit(1)
}

if minMaxReplicasRoutine != "daily" && minMaxReplicasRoutine != "weekly" {
setupLog.Error(fmt.Errorf("min-max-replicas-routine should be either \"daily\" or \"weekly\""), "invalid value")
os.Exit(1)
}

opts := zap.Options{
Development: true,
}
Expand Down Expand Up @@ -131,7 +144,7 @@ func main() {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
tortoiseService, err := tortoise.New(mgr.GetClient(), rangeOfMinMaxReplicasRecommendationHours, timeZone, tortoiseUpdateInterval)
tortoiseService, err := tortoise.New(mgr.GetClient(), rangeOfMinMaxReplicasRecommendationHours, timeZone, tortoiseUpdateInterval, minMaxReplicasRoutine)
if err != nil {
setupLog.Error(err, "unable to start tortoise service")
os.Exit(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,13 @@ spec:
format: int32
type: integer
weekday:
description: WeekDay is the day of the week. If empty, it means it applies to all days of the week.
type: string
required:
- from
- timezone
- to
- value
- weekday
type: object
type: array
minReplicas:
Expand All @@ -198,13 +198,13 @@ spec:
format: int32
type: integer
weekday:
description: WeekDay is the day of the week. If empty, it means it applies to all days of the week.
type: string
required:
- from
- timezone
- to
- value
- weekday
type: object
type: array
targetUtilizations:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,13 @@ spec:
format: int32
type: integer
weekday:
description: WeekDay is the day of the week. If empty, it means it applies to all days of the week.
type: string
required:
- from
- timezone
- to
- value
- weekday
type: object
type: array
minReplicas:
Expand All @@ -198,13 +198,13 @@ spec:
format: int32
type: integer
weekday:
description: WeekDay is the day of the week. If empty, it means it applies to all days of the week.
type: string
required:
- from
- timezone
- to
- value
- weekday
type: object
type: array
targetUtilizations:
Expand Down
Loading

0 comments on commit 320f4dc

Please sign in to comment.