agent: |
Delete AKS Clusters with Low CPU Utilization
In Azure, Kubernetes clusters provisioned using Azure Kubernetes Service (AKS) can be monitored for resource utilization, including CPU usage. If certain AKS clusters consistently exhibit low CPU utilization over a period of time, it may indicate that the resources allocated to them are underutilized. To optimize costs and resource usage, it's beneficial to identify such underperforming clusters and delete them.
- 1nQgDM95JkpjjQ71IRHWpGet Azure Subscription Id from CLI
1
Get Azure Subscription Id from CLI
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.This task retrieves the unique identifier for an Azure subscription using the Azure CLI. This ID is essential for managing resources and services tied to a specific Azure subscription programmatically.
inputsoutputsimport json try: result = _exe(None, "az account show") account_info = json.loads(result) subscription_id = account_info["id"] print("Fetched Subscription Id") print(subscription_id) # for debugging except json.JSONDecodeError: print("Error decoding JSON response from Azure CLI.") subscription_id = Nonecopied1 - 2CTp2aHRTzqYXwLsJIGAuList All AKS Clusters
2
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.This task retrieves and displays all Azure Kubernetes Service (AKS) clusters present within a specific Azure subscription. This helps users keep track of their deployed clusters, manage resources, and monitor cluster configurations.
inputsoutputsimport azure.identity from azure.mgmt.containerservice import ContainerServiceClient if subscription_id: # Initialize Azure credentials credential = azure.identity.DefaultAzureCredential() # Setup Azure AKS client aks_client = ContainerServiceClient(credential, subscription_id) try: # List all AKS clusters all_clusters = [cluster for cluster in aks_client.managed_clusters.list()] # Filter clusters based on location if specified if location: all_clusters = [cluster for cluster in all_clusters if cluster.location == location] print(f"Total AKS Clusters Found: {len(all_clusters)}\n") for cluster in all_clusters: node_count = sum([pool.count for pool in cluster.agent_pool_profiles]) print(f"Cluster Name: {cluster.name}") print(f"Region: {cluster.location}") print(f"Node Count: {node_count}") print(f"Kubernetes Version: {cluster.kubernetes_version}\n") #print(f"cluster_id: {cluster.id}") #for debugging #print(all_clusters) # for debugging except azure.core.exceptions.AzureError as ae: print(f"Azure SDK Error: {ae.message}") except Exception as e: print(f"An unexpected error occurred: {e}")copied2 - 3cUrjFQYaXn9jsAYR4C7NFilter Out Idle AKS Clusters
3
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.This task involves identifying and listing Azure Kubernetes Service (AKS) clusters that are underutilized or not actively running workloads. By pinpointing these clusters, users can optimize costs and resources by potentially shutting down or deleting them.
inputsoutputsimport azure.identity from azure.mgmt.monitor import MonitorManagementClient def filter_low_cpu_clusters(clusters, monitor_client, low_cpu_threshold): low_cpu_clusters = [] for cluster in clusters: try: # Fetch CPU utilization metrics for the cluster metrics_data = monitor_client.metrics.list( resource_uri=cluster.id, metricnames='node_cpu_usage_percentage', result_type='Data' ) # Extract non-None average values valid_averages = [time_series.average for time_series in metrics_data.value[0].timeseries[0].data if time_series.average is not None] # Check the average CPU utilization if valid_averages: # Ensure there are valid values before calculating average avg_cpu = sum(valid_averages) / len(valid_averages) if avg_cpu < int(low_cpu_threshold): low_cpu_clusters.append(cluster) except Exception as e: print(f"Failed to fetch metrics for cluster {cluster.name}: {e}") return low_cpu_clusters if subscription_id: # Initialize Azure credentials credential = azure.identity.DefaultAzureCredential() # Setup Azure Monitor client monitor_client = MonitorManagementClient(credential, subscription_id) # Filter clusters with low CPU utilization clusters_filtered = filter_low_cpu_clusters(all_clusters, monitor_client) print(clusters_filtered) # for debugging print(f"Clusters with low CPU utilization:") for cluster in clusters_filtered: print(f" - Name: {cluster.name}, Location: {cluster.location}")copied3 - 4Gv2xwubuO5QRvwcFHeLrDelete AKS Clusters
4
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.This task involves terminating specific Azure Kubernetes Service (AKS) clusters. By doing so, users can streamline their infrastructure, reduce costs, and ensure only relevant clusters remain active within their Azure setup.
inputsoutputsimport azure.identity from azure.mgmt.containerservice import ContainerServiceClient from azure.core.exceptions import HttpResponseError, ResourceNotFoundError # Set this to True if you want to actually delete the clusters #ACTUALLY_DELETE = True if subscription_id: try: # Initialize Azure credentials credential = azure.identity.DefaultAzureCredential() # Setup Azure AKS client aks_client = ContainerServiceClient(credential, subscription_id) # Displaying clusters that will be deleted print(f"Clusters queued for deletion:") for cluster in clusters_to_delete: print(f" - Name: {cluster.name}, Location: {cluster.location}") if ACTUALLY_DELETE: for cluster in clusters_to_delete: try: aks_client.managed_clusters.begin_delete(cluster.resource_group, cluster.name) print(f"Initiated deletion for cluster: {cluster.name}") except ResourceNotFoundError: print(f"Cluster {cluster.name} not found. Skipping deletion.") except HttpResponseError as hr_err: print(f"Failed to delete cluster {cluster.name} due to Azure SDK error: {hr_err.message}") except Exception as e: print(f"Failed to delete cluster {cluster.name}: {e}") else: print("ACTUALLY_DELETE is set to False. Clusters will not be deleted.") except azure.core.exceptions.AzureError as ae: print(f"Azure SDK Error: {ae.message}") except Exception as e: print(f"An unexpected error occurred: {e}") else: print("Failed to obtain subscription ID. Cannot proceed with deletion.")copied4