From 2acd050f999fee79ef784d4a9bbc5f85d9c07f6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilson=20J=C3=BAnior?= Date: Thu, 14 Nov 2024 10:33:30 -0300 Subject: [PATCH] Use *int32 for AutoScaleSpecBehaviorScaleDown --- go.mod | 4 ++-- go.sum | 4 ++-- tsuru/client/apps_type.go | 33 ++++++-------------------------- tsuru/client/autoscale.go | 35 ++++++++++++++++++++++++++++------ tsuru/client/autoscale_test.go | 13 +++++++------ 5 files changed, 46 insertions(+), 43 deletions(-) diff --git a/go.mod b/go.mod index 3bf0c00a..252a26ba 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 ( @@ -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 diff --git a/go.sum b/go.sum index 1324faf2..f048eb35 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/tsuru/client/apps_type.go b/tsuru/client/apps_type.go index fc42f045..3e39e07b 100644 --- a/tsuru/client/apps_type.go +++ b/tsuru/client/apps_type.go @@ -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 } diff --git a/tsuru/client/autoscale.go b/tsuru/client/autoscale.go index 2fe24b8f..eac95494 100644 --- a/tsuru/client/autoscale.go +++ b/tsuru/client/autoscale.go @@ -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 @@ -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 } diff --git a/tsuru/client/autoscale_test.go b/tsuru/client/autoscale_test.go index 7e7a229a..3001eebe 100644 --- a/tsuru/client/autoscale_test.go +++ b/tsuru/client/autoscale_test.go @@ -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) { @@ -62,9 +63,9 @@ 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)), }, }, }, @@ -72,9 +73,9 @@ func (s *S) TestAutoScaleBehaviorSet(c *check.C) { 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)), }, }, },