Skip to content

Commit

Permalink
Update info legos (#1014)
Browse files Browse the repository at this point in the history
Co-authored-by: abhishek-unskript <[email protected]>
  • Loading branch information
1 parent da08641 commit 5115a9c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -85,17 +91,16 @@ 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', [])
if conditions:
status = conditions[-1]['type']
elif resource == 'persistentvolumeclaims':
status = item['status']['phase']

data[resource].append([name, status])
data[resource].append([ns, name, status])

return data
14 changes: 4 additions & 10 deletions Kubernetes/legos/k8s_get_service_images/k8s_get_service_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 5115a9c

Please sign in to comment.