<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>First thing I noticed is that you didn't mention how you are starting your program.</p>
<p>Looking into the code I saw you are creating a <code>kubePtr</code> flag that defaults to <code>false</code>.
When flag is set to <code>true</code> it sets kubeconfig variable to kubeconfig path, but beacuse it is false (by default) it sets it to &quot;&quot; and that's why it cant find the config.</p>
<hr />
<p>After you set this flag to true you saw the following error:</p>
<blockquote>
<p>cannot use kubeconfig (type string) as type clientcmd.KubeconfigGetter in argument to clientcmd.BuildConfigFromKubeconfigGetter</p>
</blockquote>
<p>whihc means that you have type mismatch.
Let's have a look at <a href="https://godoc.org/k8s.io/client-go/tools/clientcmd#BuildConfigFromKubeconfigGetter" rel="nofollow noreferrer">BuildConfigFromKubeconfigGetter() function</a> argument types:</p>
<pre><code>func BuildConfigFromKubeconfigGetter(masterUrl string, kubeconfigGetter KubeconfigGetter) (*restclient.Config, error)
</code></pre>
<p>Notice that you are passing a <em>string</em> as an argument that is expected to be of type <em>KubeconfigGetter</em>.</p>
<p>Preferably use different function like <a href="https://godoc.org/k8s.io/client-go/tools/clientcmd#BuildConfigFromFlags" rel="nofollow noreferrer">clientcmd.BuildConfigFromFlags()</a>, that as an argument is expecting a path (<em>string</em>) to kubeconfig file.</p>
<p>In official client-go library repository on github you can find <a href="https://github.com/kubernetes/client-go/tree/master/examples" rel="nofollow noreferrer">several examples</a> that can help you get started with clien-go library.</p>
<p>E.g. take a look at <a href="https://github.com/kubernetes/client-go/blob/master/examples/out-of-cluster-client-configuration/main.go#L44-L62" rel="nofollow noreferrer">this official example</a>, and notice how the client is configured.</p>
