<p>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 &quot;kubectl get hpa&quot;, so I can know I have applied hpa to known services or not.</p>
<p>Any help on this is highly appreciated.</p>
<p>I have tried the below so far.</p>
<pre><code>kubeClient &quot;k8s.io/client-go/kubernetes/typed/autoscaling/v1&quot;
hpaWatch, err := kubeClient.AutoscalingV1().HorizontalPodAutoscalers(&quot;default&quot;).Watch(metav1.ListOptions{})
</code></pre>
<p>But this is not working.</p>

abinash
<p>Here is the line you should have used:</p>
<pre><code>hpas, err := clientset.AutoscalingV1().HorizontalPodAutoscalers(&quot;default&quot;).List(context.TODO(), metav1.ListOptions{})
</code></pre>
<p>and the following is a complete and working example for listing HPAs. You should be able just copy-paste it and run it.</p>
<p>It was tested with <code>client-go@0.19.0</code>.</p>
<pre><code>package main

import (
    &quot;context&quot;
    &quot;flag&quot;
    &quot;fmt&quot;
    &quot;os&quot;
    &quot;path/filepath&quot;
    &quot;time&quot;

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

func main() {
    var kubeconfig *string
    if home := homeDir(); home != &quot;&quot; {
        kubeconfig = flag.String(&quot;kubeconfig&quot;, filepath.Join(home, &quot;.kube&quot;, &quot;config&quot;), &quot;(optional) absolute path to the kubeconfig file&quot;)
    } else {
        kubeconfig = flag.String(&quot;kubeconfig&quot;, &quot;&quot;, &quot;absolute path to the kubeconfig file&quot;)
    }
    flag.Parse()

    // use the current context in kubeconfig
    config, err := clientcmd.BuildConfigFromFlags(&quot;&quot;, *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(&quot;default&quot;).List(context.TODO(), metav1.ListOptions{})
        if err != nil {
            panic(err.Error())
        }

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

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

func homeDir() string {
    if h := os.Getenv(&quot;HOME&quot;); h != &quot;&quot; {
        return h
    }
    return os.Getenv(&quot;USERPROFILE&quot;) // windows
}
</code></pre>
