Skip to content

Instantly share code, notes, and snippets.

Created February 14, 2023 17:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Scale Openshift Machinesets
module main
go 1.19
require ( v0.0.0-20221103085154-ea838af1820e v0.0.0-20221107163225-3335a34a1d24 v0.25.0 v0.25.0
package main
import (
v1 ""
metav1 ""
types ""
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)
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)
client, err := v1.NewForConfig(kubeConfig)
fmt.Printf("%T\n", client)
if err != nil {
err := fmt.Errorf("error getting kubernetes config: %v\n", err)
// 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")
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)
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