|
| 1 | +"""Example script demonstrating how to update scaling options for a container deployment. |
| 2 | +
|
| 3 | +This script shows how to update scaling configurations for an existing container deployment on DataCrunch. |
| 4 | +""" |
| 5 | + |
| 6 | +import os |
| 7 | + |
| 8 | +from datacrunch import DataCrunchClient |
| 9 | +from datacrunch.exceptions import APIException |
| 10 | +from datacrunch.containers.containers import ( |
| 11 | + ScalingOptions, |
| 12 | + ScalingPolicy, |
| 13 | + ScalingTriggers, |
| 14 | + QueueLoadScalingTrigger, |
| 15 | + UtilizationScalingTrigger |
| 16 | +) |
| 17 | + |
| 18 | +# Configuration - replace with your deployment name |
| 19 | +DEPLOYMENT_NAME = "my-deployment" |
| 20 | + |
| 21 | +# Environment variables |
| 22 | +DATACRUNCH_CLIENT_ID = os.environ.get('DATACRUNCH_CLIENT_ID') |
| 23 | +DATACRUNCH_CLIENT_SECRET = os.environ.get('DATACRUNCH_CLIENT_SECRET') |
| 24 | + |
| 25 | + |
| 26 | +def check_deployment_exists(client: DataCrunchClient, deployment_name: str) -> bool: |
| 27 | + """Check if a deployment exists. |
| 28 | +
|
| 29 | + Args: |
| 30 | + client: DataCrunch API client |
| 31 | + deployment_name: Name of the deployment to check |
| 32 | +
|
| 33 | + Returns: |
| 34 | + bool: True if deployment exists, False otherwise |
| 35 | + """ |
| 36 | + try: |
| 37 | + client.containers.get_by_name(deployment_name) |
| 38 | + return True |
| 39 | + except APIException as e: |
| 40 | + print(f"Error: {e}") |
| 41 | + return False |
| 42 | + |
| 43 | + |
| 44 | +def update_deployment_scaling(client: DataCrunchClient, deployment_name: str) -> None: |
| 45 | + """Update scaling options using the dedicated scaling options API. |
| 46 | +
|
| 47 | + Args: |
| 48 | + client: DataCrunch API client |
| 49 | + deployment_name: Name of the deployment to update |
| 50 | + """ |
| 51 | + try: |
| 52 | + # Create scaling options using ScalingOptions dataclass |
| 53 | + scaling_options = ScalingOptions( |
| 54 | + min_replica_count=1, |
| 55 | + max_replica_count=5, |
| 56 | + scale_down_policy=ScalingPolicy( |
| 57 | + delay_seconds=600), # Longer cooldown period |
| 58 | + scale_up_policy=ScalingPolicy(delay_seconds=60), # Quick scale-up |
| 59 | + queue_message_ttl_seconds=500, |
| 60 | + concurrent_requests_per_replica=1, |
| 61 | + scaling_triggers=ScalingTriggers( |
| 62 | + queue_load=QueueLoadScalingTrigger(threshold=1.0), |
| 63 | + cpu_utilization=UtilizationScalingTrigger( |
| 64 | + enabled=True, |
| 65 | + threshold=75 |
| 66 | + ), |
| 67 | + gpu_utilization=UtilizationScalingTrigger( |
| 68 | + enabled=False # Disable GPU utilization trigger |
| 69 | + ) |
| 70 | + ) |
| 71 | + ) |
| 72 | + |
| 73 | + # Update scaling options |
| 74 | + updated_options = client.containers.update_scaling_options( |
| 75 | + deployment_name, scaling_options) |
| 76 | + print(f"Updated deployment scaling options") |
| 77 | + print(f"New min replicas: {updated_options.min_replica_count}") |
| 78 | + print(f"New max replicas: {updated_options.max_replica_count}") |
| 79 | + print( |
| 80 | + f"CPU utilization trigger enabled: {updated_options.scaling_triggers.cpu_utilization.enabled}") |
| 81 | + print( |
| 82 | + f"CPU utilization threshold: {updated_options.scaling_triggers.cpu_utilization.threshold}%") |
| 83 | + except APIException as e: |
| 84 | + print(f"Error updating scaling options: {e}") |
| 85 | + |
| 86 | + |
| 87 | +def main() -> None: |
| 88 | + """Main function demonstrating scaling updates.""" |
| 89 | + try: |
| 90 | + # Check required environment variables |
| 91 | + if not DATACRUNCH_CLIENT_ID or not DATACRUNCH_CLIENT_SECRET: |
| 92 | + print( |
| 93 | + "Please set DATACRUNCH_CLIENT_ID and DATACRUNCH_CLIENT_SECRET environment variables") |
| 94 | + return |
| 95 | + |
| 96 | + # Initialize client |
| 97 | + client = DataCrunchClient( |
| 98 | + DATACRUNCH_CLIENT_ID, DATACRUNCH_CLIENT_SECRET) |
| 99 | + |
| 100 | + # Verify deployment exists |
| 101 | + if not check_deployment_exists(client, DEPLOYMENT_NAME): |
| 102 | + print(f"Deployment {DEPLOYMENT_NAME} does not exist.") |
| 103 | + return |
| 104 | + |
| 105 | + # Update scaling options using the API |
| 106 | + update_deployment_scaling(client, DEPLOYMENT_NAME) |
| 107 | + |
| 108 | + # Get current scaling options |
| 109 | + scaling_options = client.containers.get_scaling_options( |
| 110 | + DEPLOYMENT_NAME) |
| 111 | + print(f"\nCurrent scaling configuration:") |
| 112 | + print(f"Min replicas: {scaling_options.min_replica_count}") |
| 113 | + print(f"Max replicas: {scaling_options.max_replica_count}") |
| 114 | + print( |
| 115 | + f"Scale-up delay: {scaling_options.scale_up_policy.delay_seconds} seconds") |
| 116 | + print( |
| 117 | + f"Scale-down delay: {scaling_options.scale_down_policy.delay_seconds} seconds") |
| 118 | + |
| 119 | + print("\nScaling update completed successfully.") |
| 120 | + |
| 121 | + except Exception as e: |
| 122 | + print(f"Unexpected error: {e}") |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + main() |
0 commit comments