I'm trying to scale a deployment with golang client. The problem I've found is that the golang client does not have a scale method for deployments. I don't know how I should do it. The idea that I have is get a deployment, modify the replicas, and then do an update.
package main
import (
"context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
appsv1 "k8s.io/api/apps/v1"
"flag"
"fmt"
"os"
"path/filepath"
)
func main() {
var kubeconfig *string
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err)
}
//get and update replicas
deploymentsClient := clientset.AppsV1().Deployments(apiv1.NamespaceDefault)
deployment, _ := deploymentsClient.Get(context.TODO(), "demo-deployment", metav1.GetOptions{})
deploymentsClient.Update(context.TODO(), deployment, metav1.UpdateOptions{})
}
(Example taken from kubernetes client go github repository)
But I don't know if it is the best aproach.
patch
is more safe than using theget and update
approach? – Divulgence