2

How to get Kubernetes cluster name from K8s API mentions that

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

(from within the cluster), or

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

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

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

UPDATE

What I'm trying to do is to get the cluster-name from an app that runs either in a local computer or within a k8s cluster. the k8s client-go allows to initialise the clientset via in cluster or out of cluster authentication.

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

2 Answers2

1

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 client-go. So in order to get this data, you can use the Google Cloud Client Libraries for Go, designed to interact with GCP.

As a starting point, you can consult this document.

First you have to download the container package:

➜  go get google.golang.org/api/container/v1        

Before you will launch you code you will have authenticate to fetch the data: Google has a very good document how to achieve that.

Basically you have generate a ServiceAccount key and pass it in GOOGLE_APPLICATION_CREDENTIALS environment:

➜  export GOOGLE_APPLICATION_CREDENTIALS=sakey.json          

Regarding the information that you want, you can fetch the cluster information (including name) following this example.

Once you do do this you can launch your application like this:

➜  go run main.go -project <google_project_name> -zone us-central1-a

And the result would be information about your cluster:

Cluster "tom" (RUNNING) master_version: v1.14.10-gke.17  -> Pool "default-pool" (RUNNING) machineType=n1-standard-2 node_version=v1.14.10-gke.17 autoscaling=false% 

Also it is worth mentioning that if you run this command:

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

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.

You can read more about it under google`s Storing and retrieving instance metadata document.

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.

Let me know if it helps.

acid_fuji
  • 6,287
  • 7
  • 22
0

If you're running inside GKE, you can get the cluster name through the instance attributes: https://pkg.go.dev/cloud.google.com/go/compute/metadata#InstanceAttributeValue

More specifically, the following should give you the cluster name:

metadata.InstanceAttributeValue("cluster-name")

The example shared by Thomas lists all the clusters in your project, which may not be very helpful if you just want to query the name of the GKE cluster hosting your pod.