Google Cloud Container API client library for managing Google Kubernetes Engine clusters.
—
Long-running operation monitoring and management for Google Kubernetes Engine operations. This module provides functionality for tracking cluster and node pool changes, monitoring operation status, and managing operation lifecycle.
Retrieve all operations in a project within a specified zone or across all zones.
def list_operations(
self,
request=None, *,
project_id=None,
zone=None,
parent=None,
retry=gapic_v1.method.DEFAULT,
timeout=None,
metadata=()
) -> ListOperationsResponse:
"""
Lists all operations in a project in a specific zone or all zones.
Args:
project_id (str): Deprecated. The Google Developers Console project ID or project number.
zone (str): Deprecated. The name of the Google Compute Engine zone.
parent (str): The parent (project and location) where the operations will be listed.
Format: projects/{project_id}/locations/{location}
retry: Retry configuration.
timeout (float): Request timeout in seconds.
metadata: Additional gRPC metadata.
Returns:
ListOperationsResponse: Response containing the list of operations.
"""Usage example:
from google.cloud import container
client = container.ClusterManagerClient()
# List all operations in a zone
operations = client.list_operations(
project_id="my-project",
zone="us-central1-a"
)
# Or use the new parent format
operations = client.list_operations(
parent="projects/my-project/locations/us-central1-a"
)
for operation in operations.operations:
print(f"Operation: {operation.name}")
print(f"Type: {operation.operation_type}")
print(f"Status: {operation.status}")
print(f"Target: {operation.target_link}")
if operation.start_time:
print(f"Started: {operation.start_time}")
if operation.end_time:
print(f"Ended: {operation.end_time}")Retrieve detailed information about a specific operation.
def get_operation(
self,
request=None, *,
project_id=None,
zone=None,
operation_id=None,
name=None,
retry=gapic_v1.method.DEFAULT,
timeout=None,
metadata=()
) -> Operation:
"""
Gets the specified operation.
Args:
project_id (str): Deprecated. The Google Developers Console project ID or project number.
zone (str): Deprecated. The name of the Google Compute Engine zone.
operation_id (str): Deprecated. The server-assigned name of the operation.
name (str): The name (project, location, operation) of the operation to get.
Format: projects/{project_id}/locations/{location}/operations/{operation_id}
retry: Retry configuration.
timeout (float): Request timeout in seconds.
metadata: Additional gRPC metadata.
Returns:
Operation: The operation information.
"""Usage example:
operation = client.get_operation(
project_id="my-project",
zone="us-central1-a",
operation_id="operation-1234567890123-5f2a7b4d-a1b2c3d4"
)
print(f"Operation name: {operation.name}")
print(f"Operation type: {operation.operation_type}")
print(f"Status: {operation.status}")
print(f"Detail: {operation.detail}")
if operation.progress:
print(f"Progress: {operation.progress}")
if operation.error:
print(f"Error: {operation.error}")
# Monitor operation status
import time
def wait_for_operation(client, operation_name):
"""Wait for an operation to complete."""
while True:
op = client.get_operation(name=operation_name)
if op.status == "DONE":
if op.error:
print(f"Operation failed: {op.error}")
return False
else:
print("Operation completed successfully")
return True
elif op.status == "ABORTING":
print("Operation is aborting")
return False
else:
print(f"Operation status: {op.status}")
time.sleep(10) # Wait 10 seconds before checking again
# Example usage
operation = client.create_cluster(...)
success = wait_for_operation(client, operation.name)Cancel a running operation.
def cancel_operation(
self,
request=None, *,
project_id=None,
zone=None,
operation_id=None,
name=None,
retry=gapic_v1.method.DEFAULT,
timeout=None,
metadata=()
) -> None:
"""
Cancels the specified operation.
Args:
project_id (str): Deprecated. The Google Developers Console project ID or project number.
zone (str): Deprecated. The name of the Google Compute Engine zone.
operation_id (str): Deprecated. The server-assigned name of the operation.
name (str): The name (project, location, operation) of the operation to cancel.
Format: projects/{project_id}/locations/{location}/operations/{operation_id}
retry: Retry configuration.
timeout (float): Request timeout in seconds.
metadata: Additional gRPC metadata.
"""Usage example:
# Cancel a running operation
client.cancel_operation(
project_id="my-project",
zone="us-central1-a",
operation_id="operation-1234567890123-5f2a7b4d-a1b2c3d4"
)
print("Operation cancellation requested")
# Verify cancellation
operation = client.get_operation(
project_id="my-project",
zone="us-central1-a",
operation_id="operation-1234567890123-5f2a7b4d-a1b2c3d4"
)
print(f"Operation status after cancellation: {operation.status}")Operations in GKE are long-running and can take several minutes to complete. The library provides ways to monitor progress:
def monitor_operation_with_callback(client, operation_name, callback=None):
"""
Monitor operation with optional progress callback.
Args:
client: ClusterManagerClient instance
operation_name: Full operation name
callback: Optional function called with operation progress
Returns:
Boolean indicating success/failure
"""
import time
while True:
operation = client.get_operation(name=operation_name)
# Call progress callback if provided
if callback:
callback(operation)
if operation.status == "DONE":
if operation.error:
print(f"Operation failed: {operation.error.message}")
return False
else:
print("Operation completed successfully")
return True
elif operation.status in ["ABORTING", "ABORTED"]:
print(f"Operation was aborted: {operation.status_message}")
return False
else:
# Operation is still running
progress_info = []
if operation.progress:
if hasattr(operation.progress, 'stages'):
for stage in operation.progress.stages:
progress_info.append(f"{stage.name}: {stage.status}")
status_msg = f"Status: {operation.status}"
if progress_info:
status_msg += f" - {', '.join(progress_info)}"
if operation.status_message:
status_msg += f" - {operation.status_message}"
print(status_msg)
time.sleep(15) # Check every 15 seconds
# Example with progress callback
def progress_callback(operation):
print(f"Operation {operation.name}: {operation.status}")
if operation.progress and operation.progress.stages:
for stage in operation.progress.stages:
print(f" Stage {stage.name}: {stage.status}")
# Use the monitor
operation = client.create_cluster(...)
success = monitor_operation_with_callback(
client,
operation.name,
callback=progress_callback
)class ListOperationsRequest:
"""ListOperationsRequest lists operations."""
project_id: str # Deprecated
zone: str # Deprecated
parent: str # Required. Format: projects/{project}/locations/{location}
class ListOperationsResponse:
"""ListOperationsResponse is the result of ListOperationsRequest."""
operations: MutableSequence[Operation]
missing_zones: MutableSequence[str]
class GetOperationRequest:
"""GetOperationRequest gets a single operation."""
project_id: str # Deprecated
zone: str # Deprecated
operation_id: str # Deprecated
name: str # Required. Format: projects/{project}/locations/{location}/operations/{operation}
class CancelOperationRequest:
"""CancelOperationRequest cancels a single operation."""
project_id: str # Deprecated
zone: str # Deprecated
operation_id: str # Deprecated
name: str # Required. Format: projects/{project}/locations/{location}/operations/{operation}
class Operation:
"""This operation resource represents operations that may have happened or are happening on the cluster."""
name: str # The server-assigned ID for the operation
zone: str # The name of the Google Compute Engine zone (deprecated)
operation_type: str # The operation type
status: str # The current status of the operation
detail: str # Detailed operation progress, if available
status_message: str # Output only. If an error has occurred, a textual description of the error
self_link: str # Server-defined URL for this resource
target_link: str # Server-defined URL for the target of the operation
location: str # The name of the Google Compute Engine location
start_time: str # The time the operation started
end_time: str # The time the operation completed
progress: OperationProgress # Output only. Progress information for an operation
cluster_conditions: MutableSequence[StatusCondition] # Which conditions caused the current cluster state
nodepool_conditions: MutableSequence[StatusCondition] # Which conditions caused the current node pool state
error: Status # The error result of the operation in case of failure
class OperationProgress:
"""Information about operation (or operation stage) progress."""
name: str # A non-parameterized string describing an operation stage
status: str # Status of an operation stage
metrics: MutableSequence[OperationProgress.Metric] # Progress metric bundle
stages: MutableSequence[OperationProgress] # Substages of an operation or a stage
class Metric:
"""Progress metric is (string, int|float|string) pair."""
name: str # Required. Metric name
int_value: int # For metrics with integer value
double_value: float # For metrics with floating point value
string_value: str # For metrics with string value
class StatusCondition:
"""StatusCondition describes why a cluster or a node pool has a certain status."""
code: str # Machine-friendly representation of the condition
message: str # Human-friendly representation of the condition
canonical_code: str # Canonical code of the conditionCommon operation types you'll encounter:
CREATE_CLUSTER - Cluster creationDELETE_CLUSTER - Cluster deletionUPGRADE_MASTER - Master version upgradeUPGRADE_NODES - Node version upgradeREPAIR_CLUSTER - Cluster repairUPDATE_CLUSTER - Cluster configuration updateCREATE_NODE_POOL - Node pool creationDELETE_NODE_POOL - Node pool deletionSET_NODE_POOL_MANAGEMENT - Node pool management updateAUTO_REPAIR_NODES - Automatic node repairAUTO_UPGRADE_NODES - Automatic node upgradeSET_LABELS - Label updatesSET_MASTER_AUTH - Master authentication updatesSET_NODE_POOL_SIZE - Node pool size changesSET_NETWORK_POLICY - Network policy updatesSET_MAINTENANCE_POLICY - Maintenance policy updatesPENDING - Operation is queuedRUNNING - Operation is in progressDONE - Operation completed successfullyABORTING - Operation is being cancelledABORTED - Operation was cancelledInstall with Tessl CLI
npx tessl i tessl/pypi-google-cloud-container