5

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?

b4hand
  • 9,550
  • 4
  • 44
  • 49
brent
  • 1,095
  • 1
  • 11
  • 27

3 Answers3

5

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
  • 9,550
  • 4
  • 44
  • 49
3

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.

James Ward
  • 29,283
  • 9
  • 49
  • 85
Janos Lenart
  • 25,074
  • 5
  • 73
  • 75