From f6dc711925d79d5daaff1a2e81b03a938409a0c2 Mon Sep 17 00:00:00 2001 From: oharan2 Date: Thu, 20 Jun 2024 17:03:15 +0300 Subject: [PATCH] Print the clusters names that the resources are associated with --- cloudwash/providers/aws.py | 15 ++++++++++----- cloudwash/utils.py | 7 ++++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/cloudwash/providers/aws.py b/cloudwash/providers/aws.py index 57c09179..ee3d1696 100644 --- a/cloudwash/providers/aws.py +++ b/cloudwash/providers/aws.py @@ -120,19 +120,22 @@ def dry_ocps(): resources=instances, ): dry_data["OCPS"]["delete"].extend(cluster_resources) + dry_data["OCPS"]["clusters"].extend([cluster_name]) else: # For resources with no associated EC2 Instances, identify as leftovers - dry_data["OCPS"]["delete"].extend( - filter_resources_by_time_modified( - time_threshold, resources=cluster_resources - ) + filtered_leftovers = filter_resources_by_time_modified( + time_threshold, resources=cluster_resources ) + if filtered_leftovers: + dry_data["OCPS"]["delete"].extend(filtered_leftovers) + # Print cluster_name only if it's resources are deletable + dry_data["OCPS"]["clusters"].extend([cluster_name]) # Sort resources by type dry_data["OCPS"]["delete"] = sorted( dry_data["OCPS"]["delete"], key=lambda x: x.resource_type ) - return dry_data["OCPS"]["delete"] + return dry_data["OCPS"] # Remove / Stop VMs def remove_vms(avms): @@ -183,6 +186,8 @@ def remove_stacks(stacks): if kwargs["ocps"] or kwargs["_all"]: # Differentiate between the cleanup region and the Resource Explorer client region ocp_client_region = settings.aws.criteria.ocps.ocp_client_region + dry_data["OCPS"]["clusters"] = [] + with compute_client( "aws", aws_region=ocp_client_region ) as resource_explorer_client: diff --git a/cloudwash/utils.py b/cloudwash/utils.py index 8e36a2da..70db2777 100644 --- a/cloudwash/utils.py +++ b/cloudwash/utils.py @@ -44,6 +44,7 @@ def echo_dry(dry_data=None) -> None: ] for ocp in dry_data["OCPS"]["delete"] } + ocp_clusters = dry_data["OCPS"]["clusters"] deletable_resources = dry_data["RESOURCES"]["delete"] deletable_stacks = dry_data["STACKS"]["delete"] if "STACKS" in dry_data else None if deletable_vms or stopable_vms or skipped_vms: @@ -61,7 +62,7 @@ def echo_dry(dry_data=None) -> None: if deletable_pips: logger.info(f"PIPs:\n\tDeletable: {deletable_pips}") if deletable_ocps: - logger.info(f"OCPs:\n\tDeletable: {deletable_ocps}") + logger.info(f"OCPs:\n\tDeletable: {deletable_ocps}\n\tClusters: {ocp_clusters}") if deletable_resources: logger.info(f"RESOURCEs:\n\tDeletable: {deletable_resources}") if deletable_stacks: @@ -76,6 +77,7 @@ def echo_dry(dry_data=None) -> None: deletable_resources, deletable_stacks, deletable_images, + deletable_ocps, ] ): logger.info("\nNo resources are eligible for cleanup!") @@ -172,7 +174,7 @@ def calculate_time_threshold(time_ref=""): # Time Ref is Optional; if empty, time_threshold will be set as "now" time_threshold = dateparser.parse(f"now-{time_ref}-UTC") - logger.info( + logger.debug( f"\nAssociated OCP resources are filtered by last creation time of: {time_threshold}" ) return time_threshold @@ -187,7 +189,6 @@ def filter_resources_by_time_modified( :param datetime time_threshold: Time filtering criteria :param list resources: List of resources to be filtered out - :return: list of resources that last modified before time threshold :Example: