Skip to content

Commit

Permalink
Merge pull request #217 from zgalor/autoscaling
Browse files Browse the repository at this point in the history
* Add autoscling to ocm cli

Commands updated:
ocm list machinepools - added autoscaling field, and range
ocm create cluster - added autoscaling params
ocm describe cluster - added autoscaling indication and range
ocm edit machinepool - can now edit default machine pool - and autoscaling params
ocm edit cluster - no longer able to edit compute nodes
  • Loading branch information
vkareh authored Feb 1, 2021
2 parents 0b1c250 + 09a08a9 commit bb767f5
Show file tree
Hide file tree
Showing 13 changed files with 651 additions and 67 deletions.
2 changes: 1 addition & 1 deletion cmd/ocm/cluster/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func run(cmd *cobra.Command, argv []string) error {
// Print the result:
cluster = response.Body()

err = clusterpkg.PrintClusterDesctipion(connection, cluster)
err = clusterpkg.PrintClusterDescription(connection, cluster)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/ocm/cluster/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func run(cmd *cobra.Command, argv []string) error {
}

} else {
err = clusterpkg.PrintClusterDesctipion(connection, cluster)
err = clusterpkg.PrintClusterDescription(connection, cluster)
if err != nil {
return err
}
Expand Down
73 changes: 68 additions & 5 deletions cmd/ocm/create/cluster/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var args struct {
// Scaling options
computeMachineType string
computeNodes int
autoscaling c.Autoscaling

// Networking options
hostPrefix int
Expand Down Expand Up @@ -178,6 +179,7 @@ func init() {
minComputeNodes(false, true), minComputeNodes(true, true),
),
)
arguments.AddAutoscalingFlags(fs, &args.autoscaling)

fs.IPNetVar(
&args.machineCIDR,
Expand Down Expand Up @@ -378,15 +380,27 @@ func preRun(cmd *cobra.Command, argv []string) error {
return err
}

// Default compute nodes:
if args.computeNodes == 0 {
args.computeNodes = minComputeNodes(args.ccs.Enabled, args.multiAZ)
err = promptAutoscaling(fs)
if err != nil {
return err
}
err = arguments.PromptInt(fs, "compute-nodes", validateComputeNodes)

err = arguments.CheckIgnoredAutoscalingFlags(args.autoscaling, args.computeNodes)
if err != nil {
return err
}

if !args.autoscaling.Enabled {
// Default compute nodes:
if args.computeNodes == 0 {
args.computeNodes = minComputeNodes(args.ccs.Enabled, args.multiAZ)
}
err = arguments.PromptInt(fs, "compute-nodes", validateComputeNodes)
if err != nil {
return err
}
}

err = promptNetwork(fs)
if err != nil {
return err
Expand Down Expand Up @@ -422,6 +436,7 @@ func run(cmd *cobra.Command, argv []string) error {
Expiration: expiration,
ComputeMachineType: args.computeMachineType,
ComputeNodes: args.computeNodes,
Autoscaling: args.autoscaling,
MachineCIDR: args.machineCIDR,
ServiceCIDR: args.serviceCIDR,
PodCIDR: args.podCIDR,
Expand All @@ -440,7 +455,7 @@ func run(cmd *cobra.Command, argv []string) error {
fmt.Println("dry run: Would be successful.")
}
} else {
err = c.PrintClusterDesctipion(connection, cluster)
err = c.PrintClusterDescription(connection, cluster)
if err != nil {
return err
}
Expand Down Expand Up @@ -540,6 +555,30 @@ func validateComputeNodes() error {
return nil
}

func validateAutoscalingMin() error {
min := minComputeNodes(args.ccs.Enabled, args.multiAZ)

if args.autoscaling.MinReplicas < min {
return fmt.Errorf("Minimum is %d nodes", min)
}

if args.multiAZ && args.autoscaling.MinReplicas%3 != 0 {
return fmt.Errorf("Multi AZ clusters require that the number of compute nodes be a multiple of 3")
}
return nil
}

func validateAutoscalingMax() error {
if args.autoscaling.MinReplicas > args.autoscaling.MaxReplicas {
return fmt.Errorf("max-replicas must be greater or equal to min-replicas")
}

if args.multiAZ && args.autoscaling.MaxReplicas%3 != 0 {
return fmt.Errorf("Multi AZ clusters require that the number of compute nodes be a multiple of 3")
}
return nil
}

func fetchFlavours(client *cmv1.Client) (flavours []*cmv1.Flavour, err error) {
collection := client.Flavours()
page := 1
Expand Down Expand Up @@ -576,3 +615,27 @@ func constructGCPCredentials(filePath arguments.FilePath, value *c.CCS) error {
}
return nil
}

func promptAutoscaling(fs *pflag.FlagSet) error {
err := arguments.PromptBool(fs, "enable-autoscaling")
if err != nil {
return err
}
if args.autoscaling.Enabled {
// set default
args.autoscaling.MinReplicas = minComputeNodes(args.ccs.Enabled, args.multiAZ)
err = arguments.PromptInt(fs, "min-replicas", validateAutoscalingMin)
if err != nil {
return err
}

// set default
args.autoscaling.MaxReplicas = args.autoscaling.MinReplicas
err = arguments.PromptInt(fs, "max-replicas", validateAutoscalingMax)
if err != nil {
return err
}

}
return nil
}
46 changes: 40 additions & 6 deletions cmd/ocm/create/machinepool/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var args struct {
clusterKey string
instanceType string
replicas int
autoscaling c.Autoscaling
labels string
taints string
}
Expand All @@ -40,6 +41,9 @@ var Cmd = &cobra.Command{
Long: "Add a machine pool to the cluster.",
Example: ` # Add a machine pool mp-1 with 3 replicas and m5.xlarge instance type to a cluster
ocm create machinepool --cluster mycluster --instance-type m5.xlarge --replicas 3 mp-1
# Add a machine pool mp-1 with autoscaling enabled and 3 to 6 replicas of m5.xlarge to a cluster
ocm create machinepool --cluster=mycluster --name=mp-1 --enable-autoscaling \
--min-replicas=3 --max-replicas=6 --instance-type=m5.xlarge
# Add a machine pool mp-1 with labels and m5.xlarge instance type to a cluster
ocm create machinepool --cluster mycluster --instance-type m5.xlarge --replicas 3 --labels "foo=bar,bar=baz" mp-1
# Add a machine pool mp-1 with taints and m5.xlarge instance type to a cluster
Expand Down Expand Up @@ -77,8 +81,7 @@ func init() {
"Count of machines for this machine pool.",
)

//nolint:gosec
Cmd.MarkFlagRequired("replicas")
arguments.AddAutoscalingFlags(flags, &args.autoscaling)

flags.StringVar(
&args.labels,
Expand Down Expand Up @@ -138,6 +141,28 @@ func run(cmd *cobra.Command, argv []string) error {
}
}

isMinReplicasSet := cmd.Flags().Changed("min-replicas")
isMaxReplicasSet := cmd.Flags().Changed("max-replicas")
isReplicasSet := cmd.Flags().Changed("replicas")

if args.autoscaling.Enabled {
if isReplicasSet {
return fmt.Errorf("--replicas is only allowed when --enable-autoscaling=false")
}

if !isMaxReplicasSet || !isMinReplicasSet {
return fmt.Errorf("Both --min-replicas and --max-replicas are required when --enable-autoscaling=true")
}
} else {
if !isReplicasSet {
return fmt.Errorf("--replicas is required when --enable-autoscaling=false")
}

if isMaxReplicasSet || isMinReplicasSet {
return fmt.Errorf("--min-replicas and --max-replicas are not allowed when --enable-autoscaling=false")
}
}

// Create the client for the OCM API:
connection, err := ocm.NewConnection().Build()
if err != nil {
Expand Down Expand Up @@ -167,13 +192,22 @@ func run(cmd *cobra.Command, argv []string) error {
return err
}

machinePool, err := cmv1.NewMachinePool().ID(machinePoolID).
mpBuilder := cmv1.NewMachinePool().
ID(machinePoolID).
InstanceType(args.instanceType).
Replicas(args.replicas).
Labels(labels).
Taints(taintBuilders...).
Build()
Taints(taintBuilders...)

if args.autoscaling.Enabled {
mpBuilder = mpBuilder.Autoscaling(
cmv1.NewMachinePoolAutoscaling().
MinReplicas(args.autoscaling.MinReplicas).
MaxReplicas(args.autoscaling.MaxReplicas))
} else {
mpBuilder = mpBuilder.Replicas(args.replicas)
}

machinePool, err := mpBuilder.Build()
if err != nil {
return fmt.Errorf("Failed to create machine pool for cluster '%s': %v", clusterKey, err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/ocm/describe/cluster/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func run(cmd *cobra.Command, argv []string) error {
}

} else {
err = clusterpkg.PrintClusterDesctipion(connection, cluster)
err = clusterpkg.PrintClusterDescription(connection, cluster)
if err != nil {
return err
}
Expand Down
22 changes: 2 additions & 20 deletions cmd/ocm/edit/cluster/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ var args struct {
expirationTime string
expirationDuration time.Duration

// Scaling options
computeNodes int

// Networking options
private bool
}
Expand Down Expand Up @@ -79,15 +76,6 @@ func init() {
//nolint:gosec
flags.MarkHidden("expiration")

// Scaling options
flags.IntVar(
&args.computeNodes,
"compute-nodes",
0,
"Number of worker nodes to provision per zone. Single zone clusters need at least 4 nodes, "+
"while multizone clusters need at least 9 nodes (3 per zone) for resiliency.",
)

// Networking options
flags.BoolVar(
&args.private,
Expand Down Expand Up @@ -137,15 +125,9 @@ func run(cmd *cobra.Command, argv []string) error {
private = &args.private
}

var computeNodes int
if cmd.Flags().Changed("compute-nodes") {
computeNodes = args.computeNodes
}

clusterConfig := c.Spec{
Expiration: expiration,
ComputeNodes: computeNodes,
Private: private,
Expiration: expiration,
Private: private,
}
err = c.UpdateCluster(clusterCollection, cluster.ID(), clusterConfig)
if err != nil {
Expand Down
Loading

0 comments on commit bb767f5

Please sign in to comment.