<p><a href="https://stackoverflow.com/questions/38242062/how-to-get-kubernetes-cluster-name-from-k8s-api">How to get Kubernetes cluster name from K8s API</a> mentions that </p>

<pre><code>curl http://metadata/computeMetadata/v1/instance/attributes/cluster-name -H "Metadata-Flavor: Google"
</code></pre>

<p>(from within the cluster), or </p>

<pre><code>kubectl run curl --rm --restart=Never -it --image=appropriate/curl -- -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/instance/attributes/cluster-name
</code></pre>

<p>(from outside the cluster), can be used to retrieve the cluster name. That works.</p>

<p>Is there a way to perform the same programmatically using the <code>k8s client-go</code> library? Maybe using the RESTClient()? I've tried but kept getting <code>the server could not find the requested resource</code>.</p>

<p><strong>UPDATE</strong></p>

<p>What I'm trying to do is to get the <code>cluster-name</code> from an app that runs either in a local computer or within a k8s cluster. the k8s <code>client-go</code> allows to initialise the <code>clientset</code> via <a href="https://github.com/kubernetes/client-go/tree/master/examples/in-cluster-client-configuration" rel="nofollow noreferrer">in cluster</a> or <a href="https://github.com/kubernetes/client-go/tree/master/examples/out-of-cluster-client-configuration" rel="nofollow noreferrer">out of cluster</a> authentication.</p>

<p>With the two commands mentioned at the top that is achievable. I was wondering if there was a way from the <code>client-go</code> library to achieve the same, instead of having to do <code>kubectl</code> or <code>curl</code> depending on where the service is run from.</p>

<p>The data that you're looking for (name of the cluster) is available at GCP level. The name itself is a resource within GKE, not Kubernetes. This means that this specific information is not available using the <a href="https://github.com/kubernetes/client-go" rel="nofollow noreferrer">client-go</a>.
So in order to get this data, you can use the <a href="https://github.com/googleapis/google-cloud-go" rel="nofollow noreferrer">Google Cloud Client Libraries for Go</a>, designed to interact with GCP.</p>

<p>As a starting point, you can consult this  <a href="https://github.com/googleapis/google-api-go-client/blob/master/GettingStarted.md" rel="nofollow noreferrer">document</a>. </p>

<p>First you have to download the <code>container</code> package: </p>

<pre><code>➜  go get google.golang.org/api/container/v1        
</code></pre>

<p>Before you will launch you code you will have authenticate to fetch the data: 
Google has a very <a href="https://cloud.google.com/docs/authentication/production" rel="nofollow noreferrer">good document</a> how to achieve that. </p>

<p>Basically you have <a href="https://console.cloud.google.com/apis/credentials/serviceaccountkey?_ga=2.197131116.1778639707.1582885931-1426982194.1570097934" rel="nofollow noreferrer">generate</a> a <code>ServiceAccount</code> key and pass it in <code>GOOGLE_APPLICATION_CREDENTIALS</code> environment: </p>

<pre><code>➜  export GOOGLE_APPLICATION_CREDENTIALS=sakey.json          
</code></pre>

<p>Regarding the information that you want, you can fetch the cluster information (including name) following <a href="https://github.com/GoogleCloudPlatform/golang-samples/blob/master/container/listclusters/listclusters.go" rel="nofollow noreferrer">this example</a>.</p>

<p>Once you do do this you can launch your application like this: </p>

<pre><code>➜  go run main.go -project &lt;google_project_name&gt; -zone us-central1-a
</code></pre>

<p>And the result would be information about your cluster: </p>

<pre><code>Cluster "tom" (RUNNING) master_version: v1.14.10-gke.17  -&gt; Pool "default-pool" (RUNNING) machineType=n1-standard-2 node_version=v1.14.10-gke.17 autoscaling=false% 
</code></pre>

<p>Also it is worth mentioning that if you run this command: </p>

<pre><code>curl http://metadata/computeMetadata/v1/instance/attributes/cluster-name -H "Metadata-Flavor: Google"
</code></pre>

<p>You are also interacting with the GCP APIs and can go unauthenticated as long as it's run within a GCE machine/GKE cluster. This provided automatic authentication.</p>

<p>You can read more about it under google`s <a href="https://cloud.google.com/compute/docs/storing-retrieving-metadata" rel="nofollow noreferrer">Storing and retrieving instance metadata</a> document.</p>

<p>Finally, one great advantage of doing this with the Cloud Client Libraries, is that it can be launched externally (as long as it's authenticated) or internally within pods in a deployment.</p>

<p>Let me know if it helps. </p>
