Detect Google Cloud Project Id from a container in Google hosted Kubernetes cluster

9/4/2017

Detect Google Cloud Project Id from a container in Google hosted Kubernetes cluster.

When connecting to BigTable; I need to provide the Google Project Id. Is there a way to detect this automatically from within K8s?

-- brent
google-cloud-bigtable
google-cloud-platform
google-kubernetes-engine
kubernetes

3 Answers

9/4/2017

You can use the metadata service. Example:

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

This will work from any VM running on Google Compute Engine or Container Engine.

See https://cloud.google.com/compute/docs/storing-retrieving-metadata:

Google Compute Engine defines a set of default metadata entries that provide information about your instance or project. Default metadata is always defined and set by the server.

...

numeric-project-id The numeric project ID of the instance, which is not the same as the project name visible in the Google Cloud Platform Console. This value is different from the project-id metadata entry value.

project-id The project ID.

-- Janos Lenart
Source: StackOverflow


3/2/2019

In Python, you can find the project id this way:

import google.auth
_, PROJECT_ID = google.auth.default()

The original question didn't mention what programming language was being used, and I had the same question for Python.

-- b4hand
Source: StackOverflow