Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get programmatically the current GKE project id from one of its clusters?

I'd like to get the current GKE project id from within one of its clusters via the Java client or the GCloud API itself.

  • I'm running java containers in a GKE cluster of a specific Google Cloud project
  • I initialize the ClusterManagerClient with the appropriate ClusterManagerSettings

-> Is it possible to fetch this specific project id with this client?

(I'm expecting that there would be a global context within each GKE cluster where we could know the current project we're running on).

Thank you

like image 775
Vlad Avatar asked May 28 '19 19:05

Vlad


People also ask

How do I find my project ID in GCP?

To locate your project ID: Go to the API Console. From the projects list, select Manage all projects. The names and IDs for all the projects you're a member of are displayed.

How do I find my Gke cluster IP?

To find the cluster IP address of a Kubernetes pod, use the kubectl get pod command on your local machine, with the option -o wide . This option will list more information, including the node the pod resides on, and the pod's cluster IP. The IP column will contain the internal cluster IP address for each pod.

How do I change my project ID in GCP?

You cannot change project ID. You have an option to use a custom domain, e.g. www.myCompany.com, in which case projectID is something that only your internal code needs to know.

Which of the following commands is used to start Kubernetes in a cluster in Gke?

To deploy and manage applications on a GKE cluster, you must communicate with the Kubernetes cluster management system. You typically do this by using the kubectl command-line tool. Kubernetes represents applications as Pods, which are units that represent a container (or group of tightly-coupled containers).


1 Answers

As John Hanley mentioned in his comment above, you can use the instance metadata on the node in your cluster to determine the project that the node is a part of. The easiest way to see it is to use curl from a shell (either on the node or in a container).

If you want the project name, it can be seen at:

curl "http://metadata.google.internal/computeMetadata/v1/project/project-id" -H "Metadata-Flavor: Google"

And if you want the project number, it can be seen at:

curl "http://metadata.google.internal/computeMetadata/v1/project/numeric-project-id" -H "Metadata-Flavor: Google"

This isn't part of the container API surface, so the ClusterManagerClient isn't the right API client to use. You need to create a client to fetch the instance metadata, which I would expect might be part of the compute client libraries, or you can just make a local HTTP request if you add the right headers (as shown above) since you don't need any special client authentication / authorization to access the local metadata.

like image 103
Robert Bailey Avatar answered Sep 23 '22 09:09

Robert Bailey