From 5115a9cb4c17b01181c3bae856edf89c00193094 Mon Sep 17 00:00:00 2001 From: shloka-bhalgat-unskript <110628398+shloka-bhalgat-unskript@users.noreply.github.com> Date: Mon, 18 Mar 2024 11:46:38 +0530 Subject: [PATCH] Update info legos (#1014) Co-authored-by: abhishek-unskript <74047156+abhishek-unskript@users.noreply.github.com> --- .../k8s_get_all_resources_utilization_info.py | 23 +++++++++++-------- .../k8s_get_service_images.py | 14 ++++------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/Kubernetes/legos/k8s_get_all_resources_utilization_info/k8s_get_all_resources_utilization_info.py b/Kubernetes/legos/k8s_get_all_resources_utilization_info/k8s_get_all_resources_utilization_info.py index 7ed693503..7f751049f 100644 --- a/Kubernetes/legos/k8s_get_all_resources_utilization_info/k8s_get_all_resources_utilization_info.py +++ b/Kubernetes/legos/k8s_get_all_resources_utilization_info/k8s_get_all_resources_utilization_info.py @@ -55,16 +55,22 @@ def k8s_get_all_resources_utilization_info(handle, namespace: str = "") -> Dict: data['namespace'] = namespace # Store namespace in data dict # Fetch current utilization of pods - pod_utilization_cmd = f"kubectl top pods {namespace_option}" + pod_utilization_cmd = f"kubectl top pods {namespace_option} --no-headers" pod_utilization = handle.run_native_cmd(pod_utilization_cmd) - pod_utilization_lines = pod_utilization.stdout.split('\n')[1:] # Exclude header line + if pod_utilization.stderr: + print(f"Error occurred while fetching pod utilization: {pod_utilization.stderr}") + pass + pod_utilization_lines = pod_utilization.stdout.split('\n') utilization_map = {} for line in pod_utilization_lines: parts = line.split() - if len(parts) < 4: # Skip lines that do not contain enough information + if len(parts) < 3: # Skip if line doesn't contain enough parts continue - utilization_map[parts[1]] = (parts[0], parts[2], parts[3]) # Map pod name to (namespace, CPU, Memory) + pod_name, cpu_usage, memory_usage = parts[:3] + # Use a tuple of (namespace, pod_name) as the key to ensure uniqueness across namespaces + key = (namespace, pod_name) if namespace else (parts[0], pod_name) + utilization_map[key] = (cpu_usage, memory_usage) for resource in resources: cmd = f"kubectl get {resource} -o json {namespace_option}" @@ -85,9 +91,9 @@ def k8s_get_all_resources_utilization_info(handle, namespace: str = "") -> Dict: if resource == 'pods': status = item['status']['phase'] - ns_from_util, cpu_usage, memory_usage = utilization_map.get(name, (ns, 'N/A', 'N/A')) - - data[resource].append([ns_from_util, name, status, cpu_usage, memory_usage]) + key = (ns, name) + cpu_usage, memory_usage = utilization_map.get(key, ('N/A', 'N/A')) + data[resource].append([ns, name, status, cpu_usage, memory_usage]) else: if resource == 'jobs': conditions = item['status'].get('conditions', []) @@ -95,7 +101,6 @@ def k8s_get_all_resources_utilization_info(handle, namespace: str = "") -> Dict: status = conditions[-1]['type'] elif resource == 'persistentvolumeclaims': status = item['status']['phase'] - - data[resource].append([name, status]) + data[resource].append([ns, name, status]) return data diff --git a/Kubernetes/legos/k8s_get_service_images/k8s_get_service_images.py b/Kubernetes/legos/k8s_get_service_images/k8s_get_service_images.py index ef570eaa5..c92c63943 100644 --- a/Kubernetes/legos/k8s_get_service_images/k8s_get_service_images.py +++ b/Kubernetes/legos/k8s_get_service_images/k8s_get_service_images.py @@ -23,7 +23,7 @@ def k8s_get_service_images_printer(output): print("No data available") return for service, images in output.items(): - shortened_images = [shorten_image_name(image) for image in images] + shortened_images = list(images) if not shortened_images: table_data.append([service, "No images found"]) else: @@ -36,14 +36,6 @@ def k8s_get_service_images_printer(output): -def shorten_image_name(full_name: str): - # Return just the domain and name, which is the first part of the split - split_char = '@' if '@' in full_name else ':' - main_parts = full_name.split(split_char) - return main_parts[0] - - - def k8s_get_service_images(handle, namespace:str = "") -> Dict: """ k8s_get_service_images collects the images of running services in the provided namespace. @@ -91,7 +83,9 @@ def k8s_get_service_images(handle, namespace:str = "") -> Dict: get_images_command = f"kubectl get pods -n {ns} -l {label_selector} -o=jsonpath='{{.items[*].spec.containers[*].image}}'" response = handle.run_native_cmd(get_images_command) if response and not response.stderr: - images = list(set(response.stdout.strip().split())) # Deduplicate images + # Deduplicate images and replace 'docker.io' with 'docker_io' + images = list(set(response.stdout.strip().split())) + images = [image.replace('docker.io', 'docker_io') for image in images] service_key = f"{service_name} ({ns})" service_images[service_key] = images else: