Skip to content

Commit

Permalink
fix(plugin): just set autoscale params iff CLI params are set
Browse files Browse the repository at this point in the history
  • Loading branch information
nettoclaudio committed Aug 21, 2023
1 parent a01d7e5 commit a625520
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
21 changes: 18 additions & 3 deletions cmd/plugin/rpaasv2/cmd/autoscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,27 @@ func runUpdateAutoscale(c *cli.Context) error {
schedules = append(schedules, sw)
}

var cpu *int32
if n := c.Int("cpu"); c.IsSet("cpu") && n > 0 {
cpu = autogenerated.PtrInt32(int32(n))
}

var memory *int32
if n := c.Int("memory"); c.IsSet("memory") && n > 0 {
memory = autogenerated.PtrInt32(int32(n))
}

var rps *int32
if n := c.Int("rps"); c.IsSet("rps") && n > 0 {
rps = autogenerated.PtrInt32(int32(n))
}

autoscale := autogenerated.Autoscale{
MinReplicas: int32(c.Int("min")),
MaxReplicas: int32(c.Int("max")),
Cpu: autogenerated.PtrInt32(int32(c.Int("cpu"))),
Memory: autogenerated.PtrInt32(int32(c.Int("memory"))),
Rps: autogenerated.PtrInt32(int32(c.Int("rps"))),
Cpu: cpu,
Memory: memory,
Rps: rps,
Schedules: schedules,
}

Expand Down
53 changes: 53 additions & 0 deletions cmd/plugin/rpaasv2/cmd/autoscale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,59 @@ func TestUpdateAutoscale(t *testing.T) {
"when autoscale is successufully updated": {
args: []string{"autoscale", "update", "-s", "my-service", "-i", "my-instance", "--min", "0", "--max", "10", "--cpu", "75"},
handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "application/json", r.Header.Get("Content-Type"))

var data map[string]any
err := json.NewDecoder(r.Body).Decode(&data)
require.NoError(t, err)
assert.Equal(t, map[string]any{"minReplicas": float64(0), "maxReplicas": float64(10), "cpu": float64(75)}, data)

w.WriteHeader(http.StatusNoContent)
}),
expected: "Autoscale of my-service/my-instance successfully updated!\n",
},

"with CPU + RPS scalers": {
args: []string{"autoscale", "update", "-s", "my-service", "-i", "my-instance", "--min", "0", "--max", "10", "--cpu", "80", "--rps", "100"},
handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "application/json", r.Header.Get("Content-Type"))

var data map[string]any
err := json.NewDecoder(r.Body).Decode(&data)
require.NoError(t, err)

expected := map[string]any{
"minReplicas": float64(0),
"maxReplicas": float64(10),
"cpu": float64(80),
"rps": float64(100),
}
assert.Equal(t, expected, data)

w.WriteHeader(http.StatusNoContent)
}),
expected: "Autoscale of my-service/my-instance successfully updated!\n",
},

"with schedules": {
args: []string{"autoscale", "update", "-s", "my-service", "-i", "my-instance", "--min", "0", "--max", "10", "--schedule", `{"minReplicas": 1, "start": "00 08 * * 1-5", "end": "00 20 * * 1-5"}`, "--schedule", `{"minReplicas": 3, "start": "00 12 * * 1-5", "end": "00 13 * * 1-5"}`},
handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "application/json", r.Header.Get("Content-Type"))

var data map[string]any
err := json.NewDecoder(r.Body).Decode(&data)
require.NoError(t, err)

expected := map[string]any{
"minReplicas": float64(0),
"maxReplicas": float64(10),
"schedules": []any{
map[string]any{"minReplicas": float64(1), "start": "00 08 * * 1-5", "end": "00 20 * * 1-5"},
map[string]any{"minReplicas": float64(3), "start": "00 12 * * 1-5", "end": "00 13 * * 1-5"},
},
}
assert.Equal(t, expected, data)

w.WriteHeader(http.StatusNoContent)
}),
expected: "Autoscale of my-service/my-instance successfully updated!\n",
Expand Down

0 comments on commit a625520

Please sign in to comment.