Skip to content

Instantly share code, notes, and snippets.

@faizanbashir
Created February 14, 2023 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faizanbashir/054df0e6c62b4a5e96c4aa724202a9b6 to your computer and use it in GitHub Desktop.
Save faizanbashir/054df0e6c62b4a5e96c4aa724202a9b6 to your computer and use it in GitHub Desktop.
Scale Openshift Machinesets
module main
go 1.19
require (
github.com/openshift/api v0.0.0-20221103085154-ea838af1820e
github.com/openshift/client-go v0.0.0-20221107163225-3335a34a1d24
k8s.io/apimachinery v0.25.0
k8s.io/client-go v0.25.0
)
package main
import (
"context"
"encoding/json"
"fmt"
v1 "github.com/openshift/client-go/machine/clientset/versioned/typed/machine/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
types "k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/clientcmd"
"log"
"os"
"path/filepath"
)
type integerPatch struct {
Op string `json:"op"`
Path string `json:"path"`
Value uint32 `json:"value"`
}
func main() {
userHomeDir, err := os.UserHomeDir()
if err != nil {
fmt.Printf("error getting user home dir: %v\n", err)
os.Exit(1)
}
kubeConfigPath := filepath.Join(userHomeDir, ".kube", "config")
fmt.Printf("Using kubeconfig: %s\n", kubeConfigPath)
kubeConfig, err := clientcmd.BuildConfigFromFlags("", kubeConfigPath)
if err != nil {
err := fmt.Errorf("Error getting kubernetes config: %v\n", err)
log.Fatal(err.Error)
}
client, err := v1.NewForConfig(kubeConfig)
fmt.Printf("%T\n", client)
if err != nil {
err := fmt.Errorf("error getting kubernetes config: %v\n", err)
log.Fatal(err.Error)
}
// List all Machineset running in a Cluster
machineSetList, err := client.MachineSets("").List(context.TODO(), metav1.ListOptions{})
if err != nil {
if errors.IsNotFound(err) {
fmt.Printf("No Machinesets found in namespace\n")
os.Exit(0)
}
panic(err.Error())
}
replicas := uint32(2)
payload := []integerPatch{{
Op: "replace",
Path: "/spec/replicas",
Value: replicas,
}}
payloadBytes, _ := json.Marshal(payload)
fmt.Printf("There are %d Machinesets in the namespace\n", len(machineSetList.Items))
for _, ms := range machineSetList.Items {
fmt.Printf("%+v\n", ms.Name)
// Scale Machinesets using the Patch approach
_, err := client.MachineSets(ms.Namespace).Patch(context.TODO(), ms.Name, types.JSONPatchType, payloadBytes, metav1.PatchOptions{})
if err != nil {
err := fmt.Errorf("[x] Err Updating Scale for MachineSet: %v\n", err)
panic(err)
}
fmt.Printf("Updated scale for Machine Set: %s\n", ms.Name)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment