Google Cloud Container API Client Library
The `google-cloud-container` Python client library provides programmatic access to the Google Kubernetes Engine (GKE) API. It allows developers to build and manage container-based applications using the open-source Kubernetes technology. The current version is 2.63.0, and it follows a frequent release cadence as part of the broader `google-cloud-python` monorepo.
Warnings
- gotcha Always use Application Default Credentials (ADC) for authentication. Avoid hardcoding service account key files, embedding them in Docker images, or committing them to source control. For local development, use `gcloud auth application-default login`. For deployed services, rely on attached service accounts or Workload Identity.
- gotcha When interacting with GKE regional clusters, the `get_cluster` or `list_clusters` methods require specifying the `name` or `parent` field with the region, not the `zone` field. Using `zone` for a regional cluster will result in an `InvalidArgument` error.
- breaking The `google-cloud-container` library, like other `google-cloud-python` libraries, no longer supports Python versions older than 3.7. Attempting to use it with Python 3.6 or earlier will lead to installation or runtime errors.
- gotcha The logging events from this library (and other `google-cloud-python` libraries) are subject to change. Google may refine the occurrence, level, and content of various log messages without flagging these changes as breaking. Do not depend on the immutability of specific logging events for application logic.
- deprecated It is not recommended to install the monolithic `google-api-python-client` package if you only need specific Google Cloud services. This approach leads to larger distribution sizes and can complicate dependency management.
Install
-
pip install google-cloud-container
Imports
- ContainerServiceClient
from google.cloud.container_v1 import ContainerServiceClient
Quickstart
import os
from google.cloud.container_v1 import ContainerServiceClient
from google.api_core.exceptions import GoogleAPIError
def list_gke_clusters(project_id: str, location: str = "-"):
"""Lists all Google Kubernetes Engine clusters in a given project and location.
Args:
project_id: Your Google Cloud project ID.
location: The Google Cloud location (e.g., "us-central1" for a region,
or "us-central1-a" for a zone). Use "-" for all locations.
"""
try:
client = ContainerServiceClient()
parent = f"projects/{project_id}/locations/{location}"
# The list_clusters method is paginated, iterate through the responses
response = client.list_clusters(parent=parent)
if response.clusters:
print(f"Clusters in project '{project_id}' and location '{location}':")
for cluster in response.clusters:
print(f"- Name: {cluster.name}")
print(f" Location: {cluster.location}")
print(f" Status: {cluster.status.name}")
print(f" Endpoint: {cluster.endpoint}")
else:
print(f"No clusters found in project '{project_id}' and location '{location}'.")
except GoogleAPIError as e:
print(f"An API error occurred: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
# Set your Google Cloud Project ID as an environment variable
# For local development, ensure GOOGLE_APPLICATION_CREDENTIALS is set
# or you've run `gcloud auth application-default login`
project_id = os.environ.get("GOOGLE_CLOUD_PROJECT", "your-gcp-project-id")
# Optionally specify a specific region or zone, e.g., "us-central1" or "us-central1-a"
# Use "-" to list clusters across all regions/zones
target_location = os.environ.get("GKE_LOCATION", "-")
if project_id == "your-gcp-project-id":
print("Please set the 'GOOGLE_CLOUD_PROJECT' environment variable to your project ID.")
print("For example: export GOOGLE_CLOUD_PROJECT='my-project-123'")
else:
list_gke_clusters(project_id, target_location)