从Google托管的Kubernetes群集中检测Google Cloud Project ID。
连接到Bigtable时;我需要提供Google项目ID。有没有一种方法可以从K8S内部自动检测?
在Python中,您可以以这种方式找到项目ID:
import google.auth
_, PROJECT_ID = google.auth.default()
原始问题没有提及正在使用哪种编程语言,我也有同样的问题。
您可以使用元数据服务。示例:
curl -H "Metadata-Flavor: Google" -w 'n' http://metadata.google.internal/computeMetadata/v1/project/numeric-project-id
这将从Google Compute Engine或Container Engine上运行的任何VM起作用。
请参阅https://cloud.google.com/compute/docs/storing-retrieving-metadata:
Google计算引擎定义了一组默认的元数据条目,可提供有关您的实例或项目的信息。默认元数据始终由服务器定义和设置。
...
numeric-project-id
实例的数字项目ID,它与Google Cloud Platform Console中可见的项目名称不同。此值与Project-ID元数据输入值不同。
project-id
项目ID。
Google也有一些库:ServiceOptions.getDefaultProjectId
https://googleapis.github.io/google-cloud-java/google-cloud-clients/index.html
https://github.com/googleapis/google-cloud-java/blob/master/google-cloud-cloud-client/google-core-core-core/src/src/main/java/java/java/google/google/google/cloud/serviceoptions.java.java
https://github.com/googleapis/google-cloud-java/tree/master/master/google-cloud-clients/google-cloud-core-core-core