<p>I have a kubernetes cluster on GKE. Even after setting KUBECONFIG="/tmp/kubeconfigxvz" correctly, when I execute <code>kubectl get pods</code> the command fails with the following error </p>

<pre><code>bash-4.3# kubectl get pods
Unable to connect to the server: error executing access token command 
"/google-cloud-sdk/bin/gcloud config config-helper --format=json": err=exit 
status 1 output= stderr=ERROR: (gcloud.config.config-helper) You do not 
currently have an active account selected.
Please run:

  $ gcloud auth login

to obtain new credentials, or if you have already logged in with a
different account:

  $ gcloud config set account ACCOUNT

to select an already authenticated account to use.
</code></pre>

<p>When I set the <code>CLOUDSDK_CONFIG=/tmp/customdir</code> the command starts working.</p>

<p>How can I achieve the same with the go client?</p>

<p>=== UPDATE ===</p>

<p>When creating the go client I pass the correct file pointer to this function 
<code>clientcmd.BuildConfigFromFlags("", *tmpKubeConfigFile)</code> where <code>tmpKubeConfigFile</code> points to <code>/tmp/kubeconfigxvz</code>.
But I think this is not sufficient, the go-client also needs some more information from the <code>CLOUDSDK_CONFIG</code> directory, I think it needs the session information or credentials or something. </p>

<p>Is it possible to pass this CLOUDSDK_CONFIG too when creating the go-client?</p>

<p><code>BuildConfigFromFlags</code> that takes in input the pointer to kubeconfig file and returns a <code>config</code> object, which can be passed to <code>kubernetes.NewForConfig(config)</code> which creates the client. Is it possible or does there exists a similar function to pass the CLOUDSDK_CONFIG and returns a go-client or create a config? </p>

Mradul
<p>You basically need to create a <code>~/.kube/config</code> file to access your GKE cluster directly. </p>

<p>You can see <a href="https://github.com/kubernetes/client-go/blob/master/examples/out-of-cluster-client-configuration/main.go#L38" rel="nofollow noreferrer">in this go client example</a> that it's picking up the config from <code>~/.kube/config</code></p>

<p>A GKE config would look something like this:</p>

<pre><code>apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: [REDACTED]
    server: https://x.x.x.x
  name: gke_project_us-central1-a_your-first-cluster-1
contexts:
- context:
    cluster: gke_project_us-central1-a_your-first-cluster-1
    user: gke_project_us-central1-a_your-first-cluster-1
  name: gke_project_us-central1-a_your-first-cluster-1
current-context: gke_project_us-central1-a_your-first-cluster-1
kind: Config
preferences: {}
users:
- name: gke_project_us-central1-a_your-first-cluster-1
  user:
    auth-provider:
      config:
        cmd-args: config config-helper --format=json
        cmd-path: /google/google-cloud-sdk/bin/gcloud
        expiry-key: '{.credential.token_expiry}'
        token-key: '{.credential.access_token}'
      name: gcp
</code></pre>

<p>You would have to change the users section with something like:</p>

<pre><code>- name: myuser
  user:
     token: [REDACTED]
</code></pre>

<p>The user is a service account with a token, if you want to add this user to manage everything in your cluster you can <code>ClusterRoleBind</code> it to an <code>admin</code> role.</p>

<p>For more information about <code>RBAC</code>, <code>ServiceAccounts</code>, <code>Roles</code>, <code>ClusterRoles</code>, and <code>Users</code> you can see <a href="https://kubernetes.io/docs/reference/access-authn-authz/rbac/" rel="nofollow noreferrer">here</a>.</p>

<p>Btw, unfortunately, GKE doesn't give you access to the master node so you can't create certificate authentication because you don't have access to the <code>CA.key</code> file.</p>
