Skip to content

Commit

Permalink
Merge pull request #167 from asmorodskyi/ingore_tag_cluster
Browse files Browse the repository at this point in the history
eks: add pcw_ignore feature to cluster counting logic
  • Loading branch information
asmorodskyi authored Oct 25, 2022
2 parents d939866 + a68e9e0 commit 77ef7e6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
12 changes: 10 additions & 2 deletions ocw/lib/EC2.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,16 @@ def all_clusters(self):
clusters = dict()
for region in self.cluster_regions:
response = self.eks_client(region).list_clusters()
if len(response['clusters']):
clusters[region] = response['clusters']
if 'clusters' in response and len(response['clusters']) > 0:
clusters[region] = []
for cl in response['clusters']:
cluster_description = self.eks_client(region).describe_cluster(name=cl)
if 'cluster' not in cluster_description or 'tags' not in cluster_description['cluster']:
self.log_err("Unexpected cluster description: {}", cluster_description)
elif 'pcw_ignore' not in cluster_description['cluster']['tags']:
clusters[region].append(cl)
if len(clusters[region]) == 0:
del clusters[region]
return clusters

@staticmethod
Expand Down
4 changes: 2 additions & 2 deletions ocw/lib/cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def list_clusters():
for namespace in PCWConfig.get_namespaces_for('clusters'):
try:
clusters = EC2(namespace).all_clusters()
quantity = sum(len(c1) for c1 in clusters.keys())
logger.info("%d clusters found", quantity)
quantity = sum(len(clusters[c1]) for c1 in clusters)
logger.info("%d cluster(s) found", quantity)
if quantity > 0:
send_cluster_notification(namespace, clusters)
except Exception as e:
Expand Down
42 changes: 42 additions & 0 deletions tests/test_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,26 @@ def delete_vpc_endpoints(self, VpcEndpointIds):
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 Expand Up @@ -467,3 +487,25 @@ def mocked_get_boolean(config_path, field=None):
ec2_patch.cleanup_all()

assert called_stack == ['cleanup_images', 'cleanup_snapshots', 'cleanup_volumes', 'cleanup_uploader_vpcs']

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

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

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

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

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

0 comments on commit 77ef7e6

Please sign in to comment.