|
| 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 | +} |
0 commit comments