Skip to content

Commit

Permalink
Use *int32 for AutoScaleSpecBehaviorScaleDown
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Nov 14, 2024
1 parent 960d19d commit 2acd050
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 43 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/pmorie/go-open-service-broker-client v0.0.0-20180330214919-dca737037ce6
github.com/sabhiram/go-gitignore v0.0.0-20171017070213-362f9845770f
github.com/tsuru/gnuflag v0.0.0-20151217162021-86b8c1b864aa
github.com/tsuru/go-tsuruclient v0.0.0-20241029183502-e219a905d873
github.com/tsuru/go-tsuruclient v0.0.0-20241114131333-e45b7f4741d8
github.com/tsuru/tablecli v0.0.0-20190131152944-7ded8a3383c6
github.com/tsuru/tsuru v0.0.0-20240703132558-bfd1d9c89602
golang.org/x/net v0.25.0
Expand All @@ -25,6 +25,7 @@ require (
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
gopkg.in/yaml.v2 v2.4.0
k8s.io/apimachinery v0.26.2
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
)

require (
Expand Down Expand Up @@ -90,7 +91,6 @@ require (
k8s.io/api v0.26.2 // indirect
k8s.io/client-go v0.26.2 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ github.com/tsuru/config v0.0.0-20201023175036-375aaee8b560 h1:fniQ/BmYAHdnNmY333
github.com/tsuru/config v0.0.0-20201023175036-375aaee8b560/go.mod h1:mj6t8JKWU51GScTT50XRmDj65T5XhTyNvO5FUNV5zS4=
github.com/tsuru/gnuflag v0.0.0-20151217162021-86b8c1b864aa h1:JlLQP1xa13a994p/Aau2e3K9xXYaHNoNvTDVIMHSUa4=
github.com/tsuru/gnuflag v0.0.0-20151217162021-86b8c1b864aa/go.mod h1:UibOSvkMFKRe/eiwktAPAvQG8L+p8nYsECJvu3Dgw7I=
github.com/tsuru/go-tsuruclient v0.0.0-20241029183502-e219a905d873 h1:Rs3urDCvqLpmGpUKOJNRiOCij/A+EcemdeOaGmGcs/E=
github.com/tsuru/go-tsuruclient v0.0.0-20241029183502-e219a905d873/go.mod h1:qwh/KJ6ypa2GISRI79XFOHhnSjGOe1cZVPHF3nfrf18=
github.com/tsuru/go-tsuruclient v0.0.0-20241114131333-e45b7f4741d8 h1:qYTfCDEqp1Zc4dT3J4F16eTvrkuXvHkeBTtJZrdvwRw=
github.com/tsuru/go-tsuruclient v0.0.0-20241114131333-e45b7f4741d8/go.mod h1:qwh/KJ6ypa2GISRI79XFOHhnSjGOe1cZVPHF3nfrf18=
github.com/tsuru/tablecli v0.0.0-20190131152944-7ded8a3383c6 h1:1XDdWFAjIbCSG1OjN9v9KdWhuM8UtYlFcfHe/Ldkchk=
github.com/tsuru/tablecli v0.0.0-20190131152944-7ded8a3383c6/go.mod h1:ztYpOhW+u1k21FEqp7nZNgpWbr0dUKok5lgGCZi+1AQ=
github.com/tsuru/tsuru v0.0.0-20240703132558-bfd1d9c89602 h1:HiF99OFCkd2F0DwyMzBDStxm43rtrK8sBnVA2ZyfIZ4=
Expand Down
33 changes: 6 additions & 27 deletions tsuru/client/apps_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,22 @@
package client

import (
"encoding/json"
"fmt"

"github.com/tsuru/go-tsuruclient/pkg/tsuru"
)

type behaviorScaleDownJson struct {
ScaleDown scaleDownJson `json:"scaleDown"`
}

type scaleDownJson struct {
UnitsPolicyValue *int32 `json:"unitsPolicyValue,omitempty"`
PercentagePolicyValue *int32 `json:"percentagePolicyValue,omitempty"`
StabilizationWindow *int32 `json:"stabilizationWindow,omitempty"`
}

func getParamsScaleDownLines(behavior tsuru.AutoScaleSpecBehavior) []string {
b, err := json.Marshal(behavior)
if err != nil {
return nil
}
var behaviorJson behaviorScaleDownJson
err = json.Unmarshal(b, &behaviorJson)
if err != nil {
return nil
}

lines := []string{}

if behaviorJson.ScaleDown.UnitsPolicyValue != nil {
lines = append(lines, fmt.Sprintf("Units: %d", *behaviorJson.ScaleDown.UnitsPolicyValue))
if behavior.ScaleDown.UnitsPolicyValue != nil {
lines = append(lines, fmt.Sprintf("Units: %d", *behavior.ScaleDown.UnitsPolicyValue))
}
if behaviorJson.ScaleDown.PercentagePolicyValue != nil {
lines = append(lines, fmt.Sprintf("Percentage: %d%%", *behaviorJson.ScaleDown.PercentagePolicyValue))
if behavior.ScaleDown.PercentagePolicyValue != nil {
lines = append(lines, fmt.Sprintf("Percentage: %d%%", *behavior.ScaleDown.PercentagePolicyValue))
}
if behaviorJson.ScaleDown.StabilizationWindow != nil {
lines = append(lines, fmt.Sprintf("Stabilization window: %ds", *behaviorJson.ScaleDown.StabilizationWindow))
if behavior.ScaleDown.StabilizationWindow != nil {
lines = append(lines, fmt.Sprintf("Stabilization window: %ds", *behavior.ScaleDown.StabilizationWindow))
}
return lines
}
35 changes: 29 additions & 6 deletions tsuru/client/autoscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,29 @@ func (i *int32Value) Set(s string) error {
func (i *int32Value) Get() interface{} { return int32(*i) }
func (i *int32Value) String() string { return fmt.Sprintf("%v", *i) }

type int32PointerValue struct {
value **int32
}

func (i *int32PointerValue) Set(s string) error {
if s == "" {
*i.value = nil
return nil
}
v, err := strconv.ParseInt(s, 0, 32)
var v32 int32 = int32(v)

*i.value = &v32
return err
}
func (i *int32PointerValue) Get() interface{} { return *i.value }
func (i *int32PointerValue) String() string {
if *i.value == nil {
return ""
}
return fmt.Sprintf("%v", *i.value)
}

type AutoScaleSet struct {
tsuruClientApp.AppNameMixIn
fs *gnuflag.FlagSet
Expand Down Expand Up @@ -78,14 +101,14 @@ func (c *AutoScaleSet) Flags() *gnuflag.FlagSet {
c.fs.Var(&c.schedules, "schedule", "Schedule window to up/down scale. Example: {\"minReplicas\": 2, \"start\": \"0 6 * * *\", \"end\": \"0 18 * * *\"}")
c.fs.Var(&c.prometheus, "prometheus", "Prometheus settings to up/down scale. Example: {\"name\": \"my_metric_identification\", \"threshold\": 10, \"query\":\"sum(my_metric{tsuru_app=\\\"my_app\\\"})\"}")

c.fs.Var((*int32Value)(&c.autoscale.Behavior.ScaleDown.PercentagePolicyValue), "scale-down-percentage", "Percentage of units to downscale when the metric is below the threshold")
c.fs.Var((*int32Value)(&c.autoscale.Behavior.ScaleDown.PercentagePolicyValue), "sdp", "Percentage of units to downscale when the metric is below the threshold")
c.fs.Var(&int32PointerValue{&c.autoscale.Behavior.ScaleDown.PercentagePolicyValue}, "scale-down-percentage", "Percentage of units to downscale when the metric is below the threshold")
c.fs.Var(&int32PointerValue{&c.autoscale.Behavior.ScaleDown.PercentagePolicyValue}, "sdp", "Percentage of units to downscale when the metric is below the threshold")

c.fs.Var((*int32Value)(&c.autoscale.Behavior.ScaleDown.StabilizationWindow), "scale-down-stabilization-window", "Stabilization window in seconds to avoid scale down")
c.fs.Var((*int32Value)(&c.autoscale.Behavior.ScaleDown.StabilizationWindow), "sdsw", "Stabilization window in seconds to avoid scale down")
c.fs.Var(&int32PointerValue{&c.autoscale.Behavior.ScaleDown.StabilizationWindow}, "scale-down-stabilization-window", "Stabilization window in seconds to avoid scale down")
c.fs.Var(&int32PointerValue{&c.autoscale.Behavior.ScaleDown.StabilizationWindow}, "sdsw", "Stabilization window in seconds to avoid scale down")

c.fs.Var((*int32Value)(&c.autoscale.Behavior.ScaleDown.UnitsPolicyValue), "scale-down-units", "Number of units to downscale when the metric is below the threshold")
c.fs.Var((*int32Value)(&c.autoscale.Behavior.ScaleDown.UnitsPolicyValue), "sdu", "Number of units to downscale when the metric is below the threshold")
c.fs.Var(&int32PointerValue{&c.autoscale.Behavior.ScaleDown.UnitsPolicyValue}, "scale-down-units", "Number of units to downscale when the metric is below the threshold")
c.fs.Var(&int32PointerValue{&c.autoscale.Behavior.ScaleDown.UnitsPolicyValue}, "sdu", "Number of units to downscale when the metric is below the threshold")
}
return c.fs
}
Expand Down
13 changes: 7 additions & 6 deletions tsuru/client/autoscale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/tsuru/tsuru/cmd"
"github.com/tsuru/tsuru/cmd/cmdtest"
check "gopkg.in/check.v1"
"k8s.io/utils/ptr"
)

func (s *S) TestAutoScaleSet(c *check.C) {
Expand Down Expand Up @@ -62,19 +63,19 @@ func (s *S) TestAutoScaleBehaviorSet(c *check.C) {
param: []string{"-a", "myapp", "-p", "proc1", "--min", "2", "--max", "5", "--sdp", "3", "--sdsw", "22", "--sdu", "9"},
expected: tsuru.AutoScaleSpecBehavior{
ScaleDown: tsuru.AutoScaleSpecBehaviorScaleDown{
StabilizationWindow: 22,
PercentagePolicyValue: 3,
UnitsPolicyValue: 9,
StabilizationWindow: ptr.To(int32(22)),
PercentagePolicyValue: ptr.To(int32(3)),
UnitsPolicyValue: ptr.To(int32(9)),
},
},
},
{
param: []string{"-a", "myapp", "-p", "proc1", "--min", "2", "--max", "5", "--scale-down-percentage", "5", "--scale-down-stabilization-window", "7", "--scale-down-units", "40"},
expected: tsuru.AutoScaleSpecBehavior{
ScaleDown: tsuru.AutoScaleSpecBehaviorScaleDown{
StabilizationWindow: 7,
PercentagePolicyValue: 5,
UnitsPolicyValue: 40,
StabilizationWindow: ptr.To(int32(7)),
PercentagePolicyValue: ptr.To(int32(5)),
UnitsPolicyValue: ptr.To(int32(40)),
},
},
},
Expand Down

0 comments on commit 2acd050

Please sign in to comment.