From c9544c471d4e94a71bbcd1d578aa04c862f884a8 Mon Sep 17 00:00:00 2001 From: Jason Gilfoil Date: Sun, 25 Feb 2024 21:32:19 +0000 Subject: [PATCH] add scripts for checking resource usage and wiping rook disks --- scripts/print_container_resource_requests.py | 66 ++++++++++++++++ scripts/print_deployment_resouce_requests.py | 79 ++++++++++++++++++++ scripts/wipe-rook-disks.sh | 11 +++ 3 files changed, 156 insertions(+) create mode 100644 scripts/print_container_resource_requests.py create mode 100644 scripts/print_deployment_resouce_requests.py create mode 100644 scripts/wipe-rook-disks.sh diff --git a/scripts/print_container_resource_requests.py b/scripts/print_container_resource_requests.py new file mode 100644 index 00000000..36a165ee --- /dev/null +++ b/scripts/print_container_resource_requests.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 +import argparse +from kubernetes import client, config +from tabulate import tabulate + +# Parse command-line arguments +parser = argparse.ArgumentParser(description='List resource requests for containers within pods.') +parser.add_argument('--sort-cpu', action='store_true', help='Sort the output by CPU requests for containers.') +parser.add_argument('--sort-memory', action='store_true', help='Sort the output by memory requests for containers.') +args = parser.parse_args() + +# Load kubeconfig and initialize Kubernetes client +config.load_kube_config() +v1_core = client.CoreV1Api() + +# Lists to hold the data and headers for the table +data = [] +headers = ["Namespace", "Pod", "Container", "CPU Requests (cores)", "Memory Requests (MiB)"] + +# Function to process pods and collect resource requests for each container +def process_pods(pods): + for pod in pods.items: + for container in pod.spec.containers: + cpu_request = memory_request = 0 + requests = container.resources.requests or {} + + # Parse CPU requests + cpu_val = str(requests.get('cpu', '0')) + if cpu_val.endswith('m'): + cpu_request = int(cpu_val[:-1]) / 1000 + elif cpu_val.isdigit(): + cpu_request = int(cpu_val) + + # Parse memory requests + memory_val = str(requests.get('memory', '0')) + if memory_val.endswith('Ki'): + memory_request = int(memory_val[:-2]) / 1024 + elif memory_val.endswith('Mi'): + memory_request = int(memory_val[:-2]) + elif memory_val.endswith('Gi'): + memory_request = int(memory_val[:-2]) * 1024 + elif memory_val.endswith('Ti'): + memory_request = int(memory_val[:-2]) * 1024 * 1024 + elif memory_val.isdigit(): + memory_request = int(memory_val) / (1024 * 1024) + + data.append([ + pod.metadata.namespace, + pod.metadata.name, + container.name, + cpu_request, + memory_request + ]) + +# Process all pods +process_pods(v1_core.list_pod_for_all_namespaces()) + +# Sort the data based on the flags +if args.sort_cpu: + data.sort(key=lambda x: x[3], reverse=True) +elif args.sort_memory: + data.sort(key=lambda x: x[4], reverse=True) + +# Convert numeric values to strings with formatting and print the table +formatted_data = [[row[0], row[1], row[2], f"{row[3]:.3f}", f"{row[4]:.1f}"] for row in data] +print(tabulate(formatted_data, headers, tablefmt="plain")) diff --git a/scripts/print_deployment_resouce_requests.py b/scripts/print_deployment_resouce_requests.py new file mode 100644 index 00000000..f524df08 --- /dev/null +++ b/scripts/print_deployment_resouce_requests.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 +import argparse +from kubernetes import client, config +from tabulate import tabulate + +# Parse command-line arguments +parser = argparse.ArgumentParser(description='List resource requests across deployments, daemonsets, and statefulsets.') +parser.add_argument('--sort-cpu', action='store_true', help='Sort the output by CPU requests.') +parser.add_argument('--sort-memory', action='store_true', help='Sort the output by memory requests.') +args = parser.parse_args() + +# Load kubeconfig and initialize Kubernetes client +config.load_kube_config() +v1_apps = client.AppsV1Api() + +# Lists to hold the data and headers for the table +data = [] +headers = ["Resource Type", "Name", "Namespace", "CPU Requests (cores)", "Memory Requests (MiB)"] +total_cpu_requests_sum = 0 +total_memory_requests_sum = 0 + +# Function to process resources and collect resource requests +def process_resource(resource_items, resource_type): + global total_cpu_requests_sum, total_memory_requests_sum + + for item in resource_items: + total_cpu_requests = 0 + total_memory_requests = 0 + + for container in item.spec.template.spec.containers: + requests = container.resources.requests or {} + cpu_request = str(requests.get('cpu', '0')) + memory_request = str(requests.get('memory', '0')) + + # Parse CPU and memory requests + if cpu_request.endswith('m'): + total_cpu_requests += int(cpu_request[:-1]) / 1000 + elif cpu_request.isdigit(): + total_cpu_requests += int(cpu_request) + + if memory_request.endswith('Ki'): + total_memory_requests += int(memory_request[:-2]) / 1024 + elif memory_request.endswith('Mi'): + total_memory_requests += int(memory_request[:-2]) + elif memory_request.endswith('Gi'): + total_memory_requests += int(memory_request[:-2]) * 1024 + elif memory_request.endswith('Ti'): + total_memory_requests += int(memory_request[:-2]) * 1024 * 1024 + elif memory_request.isdigit(): + total_memory_requests += int(memory_request) / (1024 * 1024) + + total_cpu_requests_sum += total_cpu_requests + total_memory_requests_sum += total_memory_requests + + data.append([ + resource_type, + item.metadata.name, + item.metadata.namespace, + total_cpu_requests, + total_memory_requests + ]) + +# Process Deployments, DaemonSets, and StatefulSets +process_resource(v1_apps.list_deployment_for_all_namespaces().items, "Deployment") +process_resource(v1_apps.list_daemon_set_for_all_namespaces().items, "DaemonSet") +process_resource(v1_apps.list_stateful_set_for_all_namespaces().items, "StatefulSet") + +# Append total row +data.append(["Total", "", "", total_cpu_requests_sum, total_memory_requests_sum]) + +# Sort the data based on the flags +if args.sort_cpu: + data = sorted(data[:-1], key=lambda x: x[3], reverse=True) + [data[-1]] +elif args.sort_memory: + data = sorted(data[:-1], key=lambda x: x[4], reverse=True) + [data[-1]] + +# Convert numeric values to strings with formatting and print the table +formatted_data = [[row[0], row[1], row[2], f"{row[3]:.3f}", f"{row[4]:.1f}"] for row in data] +print(tabulate(formatted_data, headers, tablefmt="plain")) diff --git a/scripts/wipe-rook-disks.sh b/scripts/wipe-rook-disks.sh new file mode 100644 index 00000000..6006f83f --- /dev/null +++ b/scripts/wipe-rook-disks.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +DISK="/dev/sda" + +sudo sgdisk --zap-all $DISK +sudo dd if=/dev/zero of="$DISK" bs=1M count=100 oflag=direct,dsync +sudo blkdiscard $DISK +sudo partprobe $DISK + +sudo rm -rf /dev/ceph-* +sudo rm -rf /dev/mapper/ceph--* \ No newline at end of file