Skip to content

Commit

Permalink
Merge pull request #231 from ricardobranco777/poo126521
Browse files Browse the repository at this point in the history
Add missing EKS.all_clusters() method
  • Loading branch information
asmorodskyi authored Mar 23, 2023
2 parents fa9b063 + d185c7f commit a28d364
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 20 deletions.
3 changes: 2 additions & 1 deletion ocw/lib/cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from ocw.lib.azure import Azure
from ocw.lib.EC2 import EC2
from ocw.lib.gce import GCE
from ocw.lib.eks import EKS
from ocw.lib.emailnotify import send_mail, send_cluster_notification
from ocw.apps import getScheduler
from ocw.enums import ProviderChoice
Expand Down Expand Up @@ -33,7 +34,7 @@ def cleanup_run():
def list_clusters():
for namespace in PCWConfig.get_namespaces_for('clusters'):
try:
clusters = EC2(namespace).all_clusters()
clusters = EKS(namespace).all_clusters()
quantity = sum(len(clusters[c1]) for c1 in clusters)
logger.info("%d cluster(s) found", quantity)
if quantity > 0:
Expand Down
18 changes: 18 additions & 0 deletions ocw/lib/eks.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@ def kubectl_client(self, region: str, cluster_name: str):

return self.__kubectl_client[region_cluster]

def all_clusters(self) -> dict:
clusters = {}
for region in self.list_regions():
self.log_dbg("Checking clusters in {}", region)
response = self.eks_client(region).list_clusters()
if 'clusters' in response and len(response['clusters']) > 0:
clusters[region] = []
self.log_dbg("Found {} clusters in {}", len(response['clusters']), region)
for cluster in response['clusters']:
cluster_description = self.eks_client(region).describe_cluster(name=cluster)
if 'cluster' not in cluster_description or 'tags' not in cluster_description['cluster']:
self.log_err("Unexpected cluster description: {}", cluster_description)
elif TAG_IGNORE not in cluster_description['cluster']['tags']:
clusters[region].append(cluster)
if len(clusters[region]) == 0:
del clusters[region]
return clusters

def cleanup_k8s_jobs(self):
for region in self.list_regions():
self.log_dbg(f"Region {region}")
Expand Down
19 changes: 0 additions & 19 deletions tests/test_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,25 +131,6 @@ def describe_vpc_peering_connections(self, Filters):
return MockedEC2Client.response


class MockedEKSClient():
clusters_list = {}

def list_clusters(self):
return self.clusters_list

def describe_cluster(self, name=None):
if name == 'empty':
return {}
elif name == 'hascluster':
return {'cluster': {}}
elif name == 'hastags':
return {'cluster': {'tags': {}}}
elif name == 'ignored':
return {'cluster': {'tags': {'pcw_ignore': '1'}}}
else:
return None


class MockedSMTP:
mimetext = ''

Expand Down
34 changes: 34 additions & 0 deletions tests/test_eks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,47 @@
from tests.generators import mock_get_feature_property


def test_list_clusters(eks_patch, monkeypatch):
mocked_eks = MockedEKSClient()
monkeypatch.setattr(EKS, 'eks_client', lambda self, region: mocked_eks)
all_clusters = eks_patch.all_clusters()
assert all_clusters == {}

mocked_eks.clusters_list = {'clusters': ['empty']}
all_clusters = eks_patch.all_clusters()
assert all_clusters == {}

mocked_eks.clusters_list = {'clusters': ['hascluster']}
all_clusters = eks_patch.all_clusters()
assert all_clusters == {}

mocked_eks.clusters_list = {'clusters': ['hastags']}
all_clusters = eks_patch.all_clusters()
assert all_clusters == {'region1': ['hastags']}

mocked_eks.clusters_list = {'clusters': ['hastags', 'ignored']}
all_clusters = eks_patch.all_clusters()
assert all_clusters == {'region1': ['hastags']}


class MockedEKSClient():
def __init__(self):
self.clusters_list = []

def list_clusters(self):
return self.clusters_list

def describe_cluster(self, name=None):
if name == 'empty':
return {}
elif name == 'hascluster':
return {'cluster': {}}
elif name == 'hastags':
return {'cluster': {'tags': {}}}
elif name == 'ignored':
return {'cluster': {'tags': {'pcw_ignore': '1'}}}
return None


class MockedKubernetesConfig():
def load_kube_config(self, *args, **kwargs):
Expand Down

0 comments on commit a28d364

Please sign in to comment.