Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List OCP clusters in EC2 #455

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions wrapanapi/systems/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ def __init__(self, **kwargs):
self.ssm_connection = boto3client('ssm', **connection_kwargs)
self.sns_connection = boto3client('sns', **connection_kwargs)
self.cw_events_connection = boto3client('events', **connection_kwargs)
self.resourcegroupstaggingapi_connection = boto3client('resourcegroupstaggingapi', **connection_kwargs)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a heck of an endpoint name 😆

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw not at all suggesting you change it - its consistent, and is exactly what it should be. It's just ... verbose.


self.kwargs = kwargs

Expand Down Expand Up @@ -1595,3 +1596,41 @@ def cleanup_resources(self):
self.remove_all_unused_nics()
self.remove_all_unused_volumes()
self.remove_all_unused_ips()

def list_ocps(self):
"""
List openshift clusters (name, metadata, resources) where metadata can be used for uninstalling the cluster
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why limit the return to clusters with this metadata?

"""
ocp_list = []
resources = self.resourcegroupstaggingapi_connection.get_resources()["ResourceTagMappingList"]
for resource in resources:
tags = resource["Tags"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to label tags here if its only used in the for statement, just reference resource['Tags'] directly

for tag in tags:
key = tag["Key"]
if key.startswith("kubernetes.io/cluster/"):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The GetResources endpoint supports TagFilters, and you can use a key regex expression to limit the response to only resources with this tag. Then you don't have to iterate so much here, and can directly fetch the objects that you want and parse their cluster key.

https://docs.aws.amazon.com/resourcegroupstagging/latest/APIReference/API_GetResources.html
https://docs.aws.amazon.com/resourcegroupstagging/latest/APIReference/API_TagFilter.html

ocp = None
ocp_name = key[22:]
for ocp_item in ocp_list:
if ocp_item["name"] == ocp_name:
ocp = ocp_item
break
if not ocp:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using for/else syntax here, removes the need to initalize ocp and you're already using a break.

ocp = {
"name": ocp_name,
"metadata": {
"clusterName": ocp_name,
"infraID": ocp_name,
"aws": {
"region": self._region_name,
"identifier": [
{
"kubernetes.io/cluster/" + ocp_name: "owned"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use f-strings for concatenation

}
]
}
},
"resources": []
}
ocp_list.append(ocp)
ocp["resources"].append(resource)
return ocp_list