diff --git a/cmd/plugin/rpaasv2/cmd/autoscale.go b/cmd/plugin/rpaasv2/cmd/autoscale.go index 809ade158..5286a02ab 100644 --- a/cmd/plugin/rpaasv2/cmd/autoscale.go +++ b/cmd/plugin/rpaasv2/cmd/autoscale.go @@ -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, } diff --git a/cmd/plugin/rpaasv2/cmd/autoscale_test.go b/cmd/plugin/rpaasv2/cmd/autoscale_test.go index 165f01806..868e3b2a8 100644 --- a/cmd/plugin/rpaasv2/cmd/autoscale_test.go +++ b/cmd/plugin/rpaasv2/cmd/autoscale_test.go @@ -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",