Skip to content

Commit

Permalink
feat: Support scaling workerload cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
carezkh committed Jul 11, 2022
1 parent 440103a commit 327095f
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ var createClusterOpts = &createClusterOptions{
virtinkMachineRootfsSize: resource.QuantityValue{Quantity: resource.MustParse("4Gi")},
}

type scaleClusterOptions struct {
nodeRole string
nodeReplicas int
}

var scaleClusterOpts = &scaleClusterOptions{
nodeRole: "all",
nodeReplicas: 3,
}

func main() {
rootCmd := &cobra.Command{
Use: "knest",
Expand Down Expand Up @@ -93,6 +103,34 @@ func main() {
return nil
},
})

scale := &cobra.Command{
Use: "scale CLUSTER",
Args: cobra.ExactArgs(1),
Short: "Scale workload cluster.",
RunE: func(cmd *cobra.Command, args []string) error {
clusterName := args[0]
isControlPlane, isWorker, err := getScaleNodeRole(scaleClusterOpts.nodeRole)
if err != nil {
return fmt.Errorf("get scale Node role: %s", err)
}
if isControlPlane {
if err := runCommandPiped("kubectl", "patch", "kubeadmcontrolplane.controlplane.cluster.x-k8s.io", fmt.Sprintf("%s-cp", clusterName), fmt.Sprintf("--patch={\"spec\":{\"replicas\":%v}}", scaleClusterOpts.nodeReplicas), "--type=merge"); err != nil {
return fmt.Errorf("scale control-plane: %s", err)
}
}
if isWorker {
if err := runCommandPiped("kubectl", "patch", "machinedeployment.cluster.x-k8s.io", fmt.Sprintf("%s-md", clusterName), fmt.Sprintf("--patch={\"spec\":{\"replicas\":%v}}", scaleClusterOpts.nodeReplicas), "--type=merge"); err != nil {
return fmt.Errorf("scale worker: %s", err)
}
}
return nil
},
}
scale.Flags().IntVar(&scaleClusterOpts.nodeReplicas, "node-replicas", scaleClusterOpts.nodeReplicas, "Desired number of Node replicas")
scale.Flags().StringVar(&scaleClusterOpts.nodeRole, "node-role", scaleClusterOpts.nodeRole, "Role of Node (options: \"control-plane\", \"worker\", \"all\")")
rootCmd.AddCommand(scale)

if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand Down Expand Up @@ -259,6 +297,19 @@ func createCluster(cmd *cobra.Command, args []string) error {
return nil
}

func getScaleNodeRole(role string) (bool, bool, error) {
switch role {
case "all":
return true, true, nil
case "control-plane":
return true, false, nil
case "worker":
return false, true, nil
default:
return false, false, fmt.Errorf("unkown role of Node: '%s'", role)
}
}

func runCommand(name string, args ...string) (string, error) {
cmd := exec.Command(name, args...)
out, err := cmd.CombinedOutput()
Expand Down

0 comments on commit 327095f

Please sign in to comment.