Management of worker pools for builds and other compute tasks. Worker pools provide dedicated, managed compute resources with enhanced security, networking, and performance controls.
Create and configure worker pool clients for managing compute resources.
class WorkerPoolsClient:
"""Synchronous client for managing Cloud Run worker pools."""
def __init__(self, *, credentials=None, transport=None, client_options=None, client_info=None):
"""
Initialize the WorkerPools client.
Args:
credentials: Optional authentication credentials
transport: Transport to use for requests (grpc, grpc_asyncio, rest)
client_options: Client configuration options
client_info: Client information for user agent
"""
class WorkerPoolsAsyncClient:
"""Asynchronous client for managing Cloud Run worker pools."""Create new worker pools with custom networking, scaling, and security configurations.
def create_worker_pool(
self,
request: CreateWorkerPoolRequest = None,
*,
parent: str = None,
worker_pool: WorkerPool = None,
worker_pool_id: str = None,
**kwargs
) -> Operation:
"""
Create a new Cloud Run worker pool.
Args:
request: The request object containing all parameters
parent: Required. The location to create the worker pool. Format: projects/{project}/locations/{location}
worker_pool: Required. The worker pool configuration
worker_pool_id: Required. The unique identifier for the worker pool
Returns:
Operation: Long-running operation that resolves to the created WorkerPool
"""Usage Example:
from google.cloud import run_v2
client = run_v2.WorkerPoolsClient()
# Define worker pool configuration
worker_pool = run_v2.WorkerPool()
worker_pool.display_name = "High-Security Build Pool"
# Configure private networking
worker_pool.network_config = run_v2.NetworkConfig(
peered_network="projects/my-project/global/networks/my-vpc",
egress_option=run_v2.NetworkConfig.EgressOption.PRIVATE_RANGES_ONLY
)
# Configure scaling
worker_pool.worker_config = run_v2.WorkerConfig(
machine_type="e2-standard-4",
disk_size_gb=100
)
# Create the worker pool
request = run_v2.CreateWorkerPoolRequest(
parent="projects/my-project/locations/us-central1",
worker_pool=worker_pool,
worker_pool_id="secure-build-pool"
)
operation = client.create_worker_pool(request=request)
worker_pool_response = operation.result()
print(f"Worker pool created: {worker_pool_response.name}")Get worker pool configuration and status information.
def get_worker_pool(
self,
request: GetWorkerPoolRequest = None,
*,
name: str = None,
**kwargs
) -> WorkerPool:
"""
Get a Cloud Run worker pool.
Args:
request: The request object
name: Required. The name of the worker pool. Format: projects/{project}/locations/{location}/workerPools/{worker_pool}
Returns:
WorkerPool: The worker pool configuration and status
"""List worker pools with filtering and pagination.
def list_worker_pools(
self,
request: ListWorkerPoolsRequest = None,
*,
parent: str = None,
**kwargs
) -> ListWorkerPoolsResponse:
"""
List Cloud Run worker pools in a location.
Args:
request: The request object
parent: Required. The location to list worker pools. Format: projects/{project}/locations/{location}
Returns:
ListWorkerPoolsResponse: Paginated list of worker pools
"""Update worker pool configuration, scaling settings, and networking.
def update_worker_pool(
self,
request: UpdateWorkerPoolRequest = None,
*,
worker_pool: WorkerPool = None,
**kwargs
) -> Operation:
"""
Update a Cloud Run worker pool.
Args:
request: The request object
worker_pool: Required. The worker pool configuration to update
Returns:
Operation: Long-running operation that resolves to the updated WorkerPool
"""Delete worker pools and clean up associated resources.
def delete_worker_pool(
self,
request: DeleteWorkerPoolRequest = None,
*,
name: str = None,
**kwargs
) -> Operation:
"""
Delete a Cloud Run worker pool.
Args:
request: The request object
name: Required. The name of the worker pool to delete
Returns:
Operation: Long-running operation for the deletion
"""Manage Identity and Access Management policies for worker pools.
def get_iam_policy(self, request, *, resource: str = None, **kwargs):
"""Get the IAM access control policy for a worker pool."""
def set_iam_policy(self, request, *, resource: str = None, policy = None, **kwargs):
"""Set the IAM access control policy for a worker pool."""
def test_iam_permissions(self, request, *, resource: str = None, permissions: list[str] = None, **kwargs):
"""Test IAM permissions for a worker pool."""class WorkerPool:
"""
A Cloud Run worker pool configuration.
Attributes:
name (str): The unique name of the worker pool
display_name (str): User-friendly display name
uid (str): Unique identifier assigned by the system
annotations (dict): User-defined annotations
labels (dict): User-defined labels
create_time (Timestamp): The creation time
update_time (Timestamp): The last modification time
delete_time (Timestamp): The deletion time
state (State): The current state of the worker pool
worker_count (int): Number of workers in the pool
worker_config (WorkerConfig): Configuration for individual workers
network_config (NetworkConfig): Network configuration
private_pool_config (PrivatePoolConfig): Private pool configuration
etag (str): A fingerprint used for optimistic concurrency control
"""
class WorkerConfig:
"""
Configuration for worker instances.
Attributes:
machine_type (str): Machine type for workers (e.g., e2-standard-4)
disk_size_gb (int): Disk size in GB for each worker
memory_mb (int): Memory in MB for each worker
cpu_count (int): Number of CPUs for each worker
"""
class NetworkConfig:
"""
Network configuration for worker pool.
Attributes:
peered_network (str): VPC network to peer with
egress_option (EgressOption): Network egress configuration
peered_network_ip_range (str): IP range for peered network
"""class CreateWorkerPoolRequest:
"""
Request message for creating a worker pool.
Attributes:
parent (str): Required. The location to create the worker pool
worker_pool (WorkerPool): Required. The worker pool configuration
worker_pool_id (str): Required. The unique identifier for the worker pool
validate_only (bool): Indicates whether to validate only
"""
class GetWorkerPoolRequest:
"""
Request message for getting a worker pool.
Attributes:
name (str): Required. The name of the worker pool to retrieve
"""
class ListWorkerPoolsRequest:
"""
Request message for listing worker pools.
Attributes:
parent (str): Required. The location to list worker pools in
page_size (int): Maximum number of worker pools to return
page_token (str): Token for retrieving the next page
"""
class UpdateWorkerPoolRequest:
"""
Request message for updating a worker pool.
Attributes:
worker_pool (WorkerPool): Required. The worker pool configuration to update
validate_only (bool): Indicates whether to validate only
allow_missing (bool): Whether to allow creation if worker pool doesn't exist
"""
class DeleteWorkerPoolRequest:
"""
Request message for deleting a worker pool.
Attributes:
name (str): Required. The name of the worker pool to delete
validate_only (bool): Indicates whether to validate only
etag (str): A fingerprint for optimistic concurrency control
"""class ListWorkerPoolsResponse:
"""
Response message for listing worker pools.
Attributes:
worker_pools (list[WorkerPool]): The list of worker pools
next_page_token (str): Token for retrieving the next page
"""def create_secure_build_pool(project_id, location, vpc_network):
"""
Create a worker pool for high-security builds with private networking.
"""
client = run_v2.WorkerPoolsClient()
# Configure worker pool for security
worker_pool = run_v2.WorkerPool(
display_name="Secure Build Environment",
worker_config=run_v2.WorkerConfig(
machine_type="e2-highmem-4", # More memory for large builds
disk_size_gb=200 # Larger disk for build artifacts
),
network_config=run_v2.NetworkConfig(
peered_network=vpc_network,
egress_option=run_v2.NetworkConfig.EgressOption.PRIVATE_RANGES_ONLY
)
)
request = run_v2.CreateWorkerPoolRequest(
parent=f"projects/{project_id}/locations/{location}",
worker_pool=worker_pool,
worker_pool_id="secure-build-pool"
)
operation = client.create_worker_pool(request=request)
result = operation.result()
print(f"Secure worker pool created: {result.name}")
return resultdef create_performance_pool(project_id, location):
"""
Create a worker pool optimized for build performance.
"""
client = run_v2.WorkerPoolsClient()
worker_pool = run_v2.WorkerPool(
display_name="High-Performance Build Pool",
worker_config=run_v2.WorkerConfig(
machine_type="c2-standard-16", # High-CPU machine type
disk_size_gb=500, # Large SSD for fast I/O
cpu_count=16
)
)
request = run_v2.CreateWorkerPoolRequest(
parent=f"projects/{project_id}/locations/{location}",
worker_pool=worker_pool,
worker_pool_id="performance-build-pool"
)
operation = client.create_worker_pool(request=request)
return operation.result()def scale_worker_pool(worker_pool_name, target_worker_count):
"""
Scale a worker pool to a target number of workers.
"""
client = run_v2.WorkerPoolsClient()
# Get current worker pool configuration
worker_pool = client.get_worker_pool(name=worker_pool_name)
# Update worker count
worker_pool.worker_count = target_worker_count
# Apply the update
request = run_v2.UpdateWorkerPoolRequest(worker_pool=worker_pool)
operation = client.update_worker_pool(request=request)
updated_pool = operation.result()
print(f"Worker pool scaled to {updated_pool.worker_count} workers")
return updated_pooldef monitor_worker_pool_health(worker_pool_name):
"""
Monitor the health and status of a worker pool.
"""
client = run_v2.WorkerPoolsClient()
worker_pool = client.get_worker_pool(name=worker_pool_name)
print(f"Worker Pool: {worker_pool.display_name}")
print(f"State: {worker_pool.state}")
print(f"Workers: {worker_pool.worker_count}")
print(f"Machine Type: {worker_pool.worker_config.machine_type}")
print(f"Disk Size: {worker_pool.worker_config.disk_size_gb}GB")
if worker_pool.network_config:
print(f"Network: {worker_pool.network_config.peered_network}")
print(f"Egress: {worker_pool.network_config.egress_option}")
# Check if pool is ready for work
if worker_pool.state == run_v2.WorkerPool.State.RUNNING:
print("✓ Worker pool is ready for builds")
else:
print("⚠ Worker pool is not ready")
return worker_pooldef create_spot_worker_pool(project_id, location):
"""
Create a cost-optimized worker pool using preemptible instances.
"""
client = run_v2.WorkerPoolsClient()
worker_pool = run_v2.WorkerPool(
display_name="Cost-Optimized Build Pool",
worker_config=run_v2.WorkerConfig(
machine_type="e2-standard-2", # Smaller, cost-effective machines
disk_size_gb=50, # Minimal disk size
preemptible=True # Use preemptible instances for cost savings
)
)
request = run_v2.CreateWorkerPoolRequest(
parent=f"projects/{project_id}/locations/{location}",
worker_pool=worker_pool,
worker_pool_id="cost-optimized-pool"
)
operation = client.create_worker_pool(request=request)
return operation.result()