Skip to content

Commit c0ab9f4

Browse files
authored
Scale components (#105)
* Manually Scale components, add reset command * deprecate start component command * cleanup, use deprecated and example fields * better description of reset command
1 parent 2160624 commit c0ab9f4

17 files changed

+1725
-195
lines changed

cmd/scale.go

+14-60
Original file line numberDiff line numberDiff line change
@@ -15,75 +15,29 @@
1515
package cmd
1616

1717
import (
18-
"errors"
19-
"strconv"
20-
21-
"github.com/equinor/radix-cli/generated-client/client/component"
22-
"github.com/equinor/radix-cli/pkg/client"
2318
"github.com/equinor/radix-cli/pkg/flagnames"
2419
"github.com/spf13/cobra"
2520
)
2621

27-
// scaleCmd represents the scale command
22+
// startCmd represents the start command
2823
var scaleCmd = &cobra.Command{
29-
Use: "scale",
30-
Short: "Scale component replicas",
31-
Long: `Used for scaling up or down replicas of a Radix application component.
32-
33-
Examples:
34-
35-
# Scale up component to 2 replicas
36-
rx scale --application radix-test --environment dev --component component-abc --replicas 2
37-
38-
# Short version of scaling up component to 0 replicas
39-
rx scale -a radix-test -e dev -n component-abc -r 2
40-
`,
24+
Use: "scale",
25+
Short: "Scale component replicas",
26+
Long: `Scale component replicas.`,
27+
Deprecated: "Please use 'rx scale component' instead. Will be removed after September 2025",
4128
RunE: func(cmd *cobra.Command, args []string) error {
42-
appName, err := getAppNameFromConfigOrFromParameter(cmd, flagnames.Application)
43-
if err != nil {
44-
return err
45-
}
46-
envName, err := cmd.Flags().GetString(flagnames.Environment)
47-
if err != nil {
48-
return err
49-
}
50-
cmpName, err := cmd.Flags().GetString(flagnames.Component)
51-
if err != nil {
52-
return err
53-
}
54-
replicas, err := cmd.Flags().GetInt(flagnames.Replicas)
55-
if err != nil {
56-
return err
57-
}
58-
if appName == nil || *appName == "" || envName == "" || cmpName == "" {
59-
return errors.New("application name, environment name and component name are required fields")
60-
}
61-
if replicas < 0 || replicas > 20 {
62-
return errors.New("required field replicas must be between 0 and 20")
63-
}
64-
65-
cmd.SilenceUsage = true
66-
67-
parameters := component.NewScaleComponentParams().
68-
WithAppName(*appName).
69-
WithEnvName(envName).
70-
WithComponentName(cmpName).
71-
WithReplicas(strconv.Itoa(replicas))
72-
73-
apiClient, err := client.GetForCommand(cmd)
74-
if err != nil {
75-
return err
76-
}
77-
_, err = apiClient.Component.ScaleComponent(parameters, nil)
78-
return err
29+
return scaleComponentCmd.RunE(cmd, args)
7930
},
8031
}
8132

8233
func init() {
8334
rootCmd.AddCommand(scaleCmd)
84-
scaleCmd.Flags().StringP(flagnames.Application, "a", "", "Name of the application namespace")
85-
scaleCmd.Flags().StringP(flagnames.Environment, "e", "", "Name of the environment of the application")
86-
scaleCmd.Flags().StringP(flagnames.Component, "n", "", "Name of the component to scale")
87-
scaleCmd.Flags().IntP(flagnames.Replicas, "r", 1, "The new desired number of replicas")
88-
setContextSpecificPersistentFlags(scaleCmd)
35+
scaleCmd.PersistentFlags().StringP(flagnames.Application, "a", "", "Name of the application namespace")
36+
scaleCmd.PersistentFlags().StringP(flagnames.Environment, "e", "", "Name of the environment of the application")
37+
scaleCmd.PersistentFlags().StringP(flagnames.Component, "n", "", "Name of the component to scale")
38+
scaleCmd.PersistentFlags().IntP(flagnames.Replicas, "r", 1, "The new desired number of replicas")
39+
scaleCmd.PersistentFlags().Bool(flagnames.Reset, false, "Reset manualy scaled component to use replica count from RadixConfig or managed by horizontal autoscaling")
40+
scaleCmd.MarkFlagsOneRequired(flagnames.Replicas, flagnames.Reset)
41+
scaleCmd.MarkFlagsMutuallyExclusive(flagnames.Replicas, flagnames.Reset)
42+
setContextSpecificPersistentFlags(scaleComponentCmd)
8943
}

cmd/scaleComponent.go

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright © 2023
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"errors"
19+
"strconv"
20+
21+
apiclient "github.com/equinor/radix-cli/generated-client/client"
22+
"github.com/equinor/radix-cli/generated-client/client/component"
23+
"github.com/equinor/radix-cli/pkg/client"
24+
"github.com/equinor/radix-cli/pkg/flagnames"
25+
"github.com/sirupsen/logrus"
26+
"github.com/spf13/cobra"
27+
)
28+
29+
// scaleCmd represents the scale command
30+
var scaleComponentCmd = &cobra.Command{
31+
Use: "component",
32+
Short: "Scale component replicas",
33+
Long: `Used for manually scaling up or down replicas of a Radix application component.
34+
Note: Manual scaling will persist across deployments, and will disable autoscaling.
35+
`,
36+
Example: `
37+
# Scale up component to 2 replicas
38+
rx scale component --application radix-test --environment dev --component component-abc --replicas 2
39+
40+
# Short version of scaling up component to 0 replicas
41+
rx scale component -a radix-test -e dev -n component-abc -r 2
42+
43+
# Reset manual scaling to resume normal operations:
44+
rx scale component --application radix-test --environment dev --component component-abc --reset
45+
`,
46+
RunE: func(cmd *cobra.Command, args []string) error {
47+
appName, err := getAppNameFromConfigOrFromParameter(cmd, flagnames.Application)
48+
if err != nil {
49+
return err
50+
}
51+
envName, err := cmd.Flags().GetString(flagnames.Environment)
52+
if err != nil {
53+
return err
54+
}
55+
cmpName, err := cmd.Flags().GetString(flagnames.Component)
56+
if err != nil {
57+
return err
58+
}
59+
replicas, err := cmd.Flags().GetInt(flagnames.Replicas)
60+
if err != nil {
61+
return err
62+
}
63+
reset, err := cmd.Flags().GetBool(flagnames.Reset)
64+
if err != nil {
65+
return err
66+
}
67+
if appName == nil || *appName == "" || envName == "" || cmpName == "" {
68+
return errors.New("application name, environment name and component name are required fields")
69+
}
70+
if !reset && (replicas < 0 || replicas > 20) {
71+
return errors.New("required field replicas must be between 0 and 20")
72+
}
73+
74+
apiClient, err := client.GetForCommand(cmd)
75+
if err != nil {
76+
return err
77+
}
78+
79+
cmd.SilenceUsage = true
80+
81+
if reset {
82+
return resetScaledComponent(apiClient, *appName, envName, cmpName)
83+
}
84+
return scaleComponent(apiClient, *appName, envName, cmpName, strconv.Itoa(replicas))
85+
},
86+
}
87+
88+
func scaleComponent(apiClient *apiclient.Radixapi, appName, envName, cmpName, replicas string) error {
89+
parameters := component.NewScaleComponentParams().
90+
WithAppName(appName).
91+
WithEnvName(envName).
92+
WithComponentName(cmpName).
93+
WithReplicas(replicas)
94+
95+
if _, err := apiClient.Component.ScaleComponent(parameters, nil); err != nil {
96+
return err
97+
}
98+
99+
logrus.Infof("%s Successfully scaled to %s replicas", cmpName, replicas)
100+
return nil
101+
}
102+
103+
func resetScaledComponent(apiClient *apiclient.Radixapi, appName, envName, cmpName string) error {
104+
parameters := component.NewResetScaledComponentParams().
105+
WithAppName(appName).
106+
WithEnvName(envName).
107+
WithComponentName(cmpName)
108+
109+
if _, err := apiClient.Component.ResetScaledComponent(parameters, nil); err != nil {
110+
return err
111+
}
112+
113+
logrus.Infof("%s Successfully reset to normal scaling", cmpName)
114+
return nil
115+
}
116+
117+
func init() {
118+
scaleCmd.AddCommand(scaleComponentCmd)
119+
}

cmd/startComponent.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/equinor/radix-cli/generated-client/client/component"
2121
"github.com/equinor/radix-cli/pkg/client"
2222
"github.com/equinor/radix-cli/pkg/flagnames"
23+
"github.com/sirupsen/logrus"
2324
"github.com/spf13/cobra"
2425
)
2526

@@ -28,8 +29,11 @@ var startComponentCmd = &cobra.Command{
2829
Use: "component",
2930
Short: "Start a component",
3031
Long: `Start a component
31-
- Pulls new image from image hub in radix configuration
32-
- Starts the container using up to date image`,
32+
33+
Deprecated: Use 'rx scale component --reset' instead
34+
35+
Resets a manully scaled component to resume normal operations again.`,
36+
Deprecated: " Use 'rx scale component --reset' instead. Will be removed after September 2025",
3337
RunE: func(cmd *cobra.Command, args []string) error {
3438
appName, err := getAppNameFromConfigOrFromParameter(cmd, flagnames.Application)
3539
if err != nil {
@@ -60,7 +64,12 @@ var startComponentCmd = &cobra.Command{
6064
}
6165

6266
_, err = apiClient.Component.StartComponent(parameters, nil)
63-
return err
67+
if err != nil {
68+
return err
69+
}
70+
71+
logrus.Infof("%s Successfully reset to normal scaling", cmpName)
72+
return nil
6473
},
6574
}
6675

generated-client/client/application/application_client.go

+42-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)