<p>I am trying to write a Go program to get pod logs from the cluster. I am using AKS kubernetes cluster. How can I access the kubeconfig file inside my script? Following is my code:</p>
<pre><code>package main

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

    &quot;k8s.io/apimachinery/pkg/api/errors&quot;
    metav1 &quot;k8s.io/apimachinery/pkg/apis/meta/v1&quot;
    &quot;k8s.io/client-go/kubernetes&quot;
    //&quot;k8s.io/client-go/rest&quot;
    &quot;k8s.io/client-go/tools/clientcmd&quot;
    //
    // Uncomment to load all auth plugins
    // _ &quot;k8s.io/client-go/plugin/pkg/client/auth&quot;
    //
    // Or uncomment to load specific auth plugins
    // _ &quot;k8s.io/client-go/plugin/pkg/client/auth/azure&quot;
    // _ &quot;k8s.io/client-go/plugin/pkg/client/auth/gcp&quot;
    // _ &quot;k8s.io/client-go/plugin/pkg/client/auth/oidc&quot;
    // _ &quot;k8s.io/client-go/plugin/pkg/client/auth/openstack&quot;
)

func main() {
    /*// creates the in-cluster config
    config, err := rest.InClusterConfig()
    if err != nil {
        panic(err.Error())
    }*/
    
    fmt.Printf(&quot;Creating cluster config&quot;)
    
    kubePtr := flag.Bool(&quot;use-kubeconfig&quot;, false, &quot;use kubeconfig on local system&quot;)
    flag.Parse()
    
    fmt.Printf(&quot;Updating the existing config&quot;)

    var kubeconfig string

    if *kubePtr == true {
        kubeconfig = filepath.Join(os.Getenv(&quot;HOME&quot;), &quot;.kube&quot;, &quot;config&quot;)
    } else {
        kubeconfig = &quot;&quot;
    }
    
    fmt.Printf(&quot;Building config from flags&quot;)

    config, err := clientcmd.BuildConfigFromKubeconfigGetter(&quot;&quot;, kubeconfig)
    
    fmt.Printf(&quot;creating the clientset&quot;)
    
    
    // creates the clientset
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        panic(err.Error())
    }
    for {
        // get pods in all the namespaces by omitting namespace
        // Or specify namespace to get pods in particular namespace
        pods, err := clientset.CoreV1().Pods(&quot;&quot;).List(context.TODO(), metav1.ListOptions{})
        if err != nil {
            panic(err.Error())
        }
        fmt.Printf(&quot;There are %d pods in the cluster\n&quot;, len(pods.Items))

        // Examples for error handling:
        // - Use helper functions e.g. errors.IsNotFound()
        // - And/or cast to StatusError and use its properties like e.g. ErrStatus.Message
        _, err = clientset.CoreV1().Pods(&quot;default&quot;).Get(context.TODO(), &quot;example-xxxxx&quot;, metav1.GetOptions{})
        if errors.IsNotFound(err) {
            fmt.Printf(&quot;Pod example-xxxxx not found in default namespace\n&quot;)
        } else if statusError, isStatus := err.(*errors.StatusError); isStatus {
            fmt.Printf(&quot;Error getting pod %v\n&quot;, statusError.ErrStatus.Message)
        } else if err != nil {
            panic(err.Error())
        } else {
            fmt.Printf(&quot;Found example-xxxxx pod in default namespace\n&quot;)
        }

        time.Sleep(10 * time.Second)
    }
}
</code></pre>
<p>I am getting error in line 51. Following is my error:</p>
<pre><code>error creating inClusterConfig, falling back to default config: unable to load in-cluster configuration, KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT must be defined
</code></pre>
<p>Where do I find <code>KUBERNETES_SERVICE_HOST</code> and <code>KUBERNETES_SERVICE_PORT</code> and how do I pass it? I could not find any example.</p>

Sormita Chakraborty
<p>From the example <a href="https://github.com/kubernetes/client-go/blob/master/examples/create-update-delete-deployment/main.go" rel="nofollow noreferrer">here</a> the code should be like below. Notice the usage of <code>BuildConfigFromFlags</code> function to load the <code>kubeconfig</code> file.</p>
<pre><code>func main() {
    var kubeconfig *string
    if home := homedir.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()

    config, err := clientcmd.BuildConfigFromFlags(&quot;&quot;, *kubeconfig)
    if err != nil {
        panic(err)
    }
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        panic(err)
    }
    ...
</code></pre>
<p>Make sure that there is a valid kubeconfig file at <code>~/.kube/config</code>
location. On AKS you can run below command to get the kubeconfig file which puts the file at <code>~/.kube/config</code></p>
<pre><code>az aks get-credentials --resource-group myResourceGroup --name myAKSCluster
</code></pre>
