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

3

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?

Campy answered 4/9, 2017 at 7:7 Comment(0)
A
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.

Appetency answered 2/3, 2019 at 1:13 Comment(0)
F
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.

Fourposter answered 4/9, 2017 at 11:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.