-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Scale components #105
Scale components #105
Changes from 7 commits
cc3589f
f6d4426
d45391d
ce5ee6b
50a2704
9a7321f
0e7ab0c
2310792
a3efa72
93fbc00
0605c2d
5cd1255
d2b7f46
c002627
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,123 @@ | ||||||||||||||||
// Copyright © 2023 | ||||||||||||||||
// | ||||||||||||||||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||
// you may not use this file except in compliance with the License. | ||||||||||||||||
// You may obtain a copy of the License at | ||||||||||||||||
// | ||||||||||||||||
// http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||
// | ||||||||||||||||
// Unless required by applicable law or agreed to in writing, software | ||||||||||||||||
// distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||
// See the License for the specific language governing permissions and | ||||||||||||||||
// limitations under the License. | ||||||||||||||||
|
||||||||||||||||
package cmd | ||||||||||||||||
|
||||||||||||||||
import ( | ||||||||||||||||
"errors" | ||||||||||||||||
"strconv" | ||||||||||||||||
|
||||||||||||||||
client2 "github.com/equinor/radix-cli/generated-client/client" | ||||||||||||||||
"github.com/equinor/radix-cli/generated-client/client/component" | ||||||||||||||||
"github.com/equinor/radix-cli/pkg/client" | ||||||||||||||||
"github.com/equinor/radix-cli/pkg/flagnames" | ||||||||||||||||
"github.com/sirupsen/logrus" | ||||||||||||||||
"github.com/spf13/cobra" | ||||||||||||||||
) | ||||||||||||||||
|
||||||||||||||||
// scaleCmd represents the scale command | ||||||||||||||||
var scaleComponentCmd = &cobra.Command{ | ||||||||||||||||
Use: "component", | ||||||||||||||||
Short: "Scale component replicas", | ||||||||||||||||
Long: `Used for manually scaling up or down replicas of a Radix application component. | ||||||||||||||||
Note: Manual scaling will persist across deployments, and will disable autoscaling. | ||||||||||||||||
|
||||||||||||||||
Examples: | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use the field There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed, also used the deprecation field 👍 |
||||||||||||||||
|
||||||||||||||||
# Scale up component to 2 replicas | ||||||||||||||||
rx scale --application radix-test --environment dev --component component-abc --replicas 2 | ||||||||||||||||
|
||||||||||||||||
# Short version of scaling up component to 0 replicas | ||||||||||||||||
rx scale -a radix-test -e dev -n component-abc -r 2 | ||||||||||||||||
|
||||||||||||||||
# Reset manual scaling to resume normal operations: | ||||||||||||||||
rx scale --application radix-test --environment dev --component component-abc --reset | ||||||||||||||||
`, | ||||||||||||||||
RunE: func(cmd *cobra.Command, args []string) error { | ||||||||||||||||
appName, err := getAppNameFromConfigOrFromParameter(cmd, flagnames.Application) | ||||||||||||||||
if err != nil { | ||||||||||||||||
return err | ||||||||||||||||
} | ||||||||||||||||
envName, err := cmd.Flags().GetString(flagnames.Environment) | ||||||||||||||||
if err != nil { | ||||||||||||||||
return err | ||||||||||||||||
} | ||||||||||||||||
cmpName, err := cmd.Flags().GetString(flagnames.Component) | ||||||||||||||||
if err != nil { | ||||||||||||||||
return err | ||||||||||||||||
} | ||||||||||||||||
replicas, err := cmd.Flags().GetInt(flagnames.Replicas) | ||||||||||||||||
if err != nil { | ||||||||||||||||
return err | ||||||||||||||||
} | ||||||||||||||||
reset, err := cmd.Flags().GetBool(flagnames.Reset) | ||||||||||||||||
if err != nil { | ||||||||||||||||
return err | ||||||||||||||||
} | ||||||||||||||||
if appName == nil || *appName == "" || envName == "" || cmpName == "" { | ||||||||||||||||
return errors.New("application name, environment name and component name are required fields") | ||||||||||||||||
} | ||||||||||||||||
if !reset && (replicas < 0 || replicas > 20) { | ||||||||||||||||
return errors.New("required field replicas must be between 0 and 20") | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
apiClient, err := client.GetForCommand(cmd) | ||||||||||||||||
if err != nil { | ||||||||||||||||
return err | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
cmd.SilenceUsage = true | ||||||||||||||||
|
||||||||||||||||
if reset { | ||||||||||||||||
return resetScaledComponent(apiClient, *appName, envName, cmpName) | ||||||||||||||||
} else { | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||||||||||||||||
return scaleComponent(apiClient, *appName, envName, cmpName, strconv.Itoa(replicas)) | ||||||||||||||||
} | ||||||||||||||||
}, | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
func scaleComponent(apiClient *client2.Radixapi, appName, envName, cmpName, replicas string) error { | ||||||||||||||||
parameters := component.NewScaleComponentParams(). | ||||||||||||||||
WithAppName(appName). | ||||||||||||||||
WithEnvName(envName). | ||||||||||||||||
WithComponentName(cmpName). | ||||||||||||||||
WithReplicas(replicas) | ||||||||||||||||
|
||||||||||||||||
_, err := apiClient.Component.ScaleComponent(parameters, nil) | ||||||||||||||||
if err != nil { | ||||||||||||||||
return err | ||||||||||||||||
} | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||||||||||||||||
|
||||||||||||||||
logrus.Infof("%s Successfully scaled to %s replicas", cmpName, replicas) | ||||||||||||||||
return nil | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
func resetScaledComponent(apiClient *client2.Radixapi, appName, envName, cmpName string) error { | ||||||||||||||||
parameters := component.NewResetScaledComponentParams(). | ||||||||||||||||
WithAppName(appName). | ||||||||||||||||
WithEnvName(envName). | ||||||||||||||||
WithComponentName(cmpName) | ||||||||||||||||
|
||||||||||||||||
_, err := apiClient.Component.ResetScaledComponent(parameters, nil) | ||||||||||||||||
if err != nil { | ||||||||||||||||
return err | ||||||||||||||||
} | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||||||||||||||||
|
||||||||||||||||
logrus.Infof("%s Successfully reset to normal scaling", cmpName) | ||||||||||||||||
return nil | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
func init() { | ||||||||||||||||
scaleCmd.AddCommand(scaleComponentCmd) | ||||||||||||||||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename to
apiclient
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed