1

I am using a kube config file to fetch the pod CPU and MEM data using go-lang. I am stuck to fetch the HPA details, i.e I am trying to write the equivalent of "kubectl get hpa", so I can know I have applied hpa to known services or not.

Any help on this is highly appreciated.

I have tried the below so far.

kubeClient "k8s.io/client-go/kubernetes/typed/autoscaling/v1"
hpaWatch, err := kubeClient.AutoscalingV1().HorizontalPodAutoscalers("default").Watch(metav1.ListOptions{})

But this is not working.

jose praveen
  • 1,298
  • 2
  • 10
  • 17
abinash
  • 13
  • 3
  • 1
    What does "this is not working" mean? Is there an error? If so, show it. – Marc Sep 09 '20 at 05:48
  • I am new to go-lang and trying to figure out out how to write the "kubectl" equivalents. I am unable to figure out the same for HPA. "kubectl get hpa" – abinash Sep 09 '20 at 06:28
  • `watch` will only tell you if any updates are applied or a state changes in the resource. I don't believe HPA will tell you consumption information, but it can tell you if a new replica has been added. What are you trying to achieve? – Dandy Sep 09 '20 at 06:29
  • @Dandy, I am trying to write equivalent of "kubectl get hpa". I have formatted the question. Sorry for the confusion very new to Stackoverflow and go-lang :) – abinash Sep 09 '20 at 06:44
  • @abinash I think you can just remove the `.Watch()` then and also make sure that string in the HPA function is correct `"default "` (With a space sounds wrong to me) – Dandy Sep 09 '20 at 06:47

1 Answers1

2

Here is the line you should have used:

hpas, err := clientset.AutoscalingV1().HorizontalPodAutoscalers("default").List(context.TODO(), metav1.ListOptions{})

and the following is a complete and working example for listing HPAs. You should be able just copy-paste it and run it.

It was tested with client-go@0.19.0.

package main

import (
    "context"
    "flag"
    "fmt"
    "os"
    "path/filepath"
    "time"

    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/tools/clientcmd"
)

func main() {
    var kubeconfig *string
    if home := 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()

    // use the current context in kubeconfig
    config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
    if err != nil {
        panic(err.Error())
    }

    // create the clientset
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        panic(err.Error())
    }
    for {
        hpas, err := clientset.AutoscalingV1().HorizontalPodAutoscalers("default").List(context.TODO(), metav1.ListOptions{})
        if err != nil {
            panic(err.Error())
        }

        for _, hpa := range hpas.Items {
            fmt.Printf("%q\n", hpa.GetName())
        }

        time.Sleep(10 * time.Second)
    }
}

func homeDir() string {
    if h := os.Getenv("HOME"); h != "" {
        return h
    }
    return os.Getenv("USERPROFILE") // windows
}
Matt
  • 7,419
  • 1
  • 11
  • 22