Skip to content

Commit

Permalink
revert back cleanup of all container blobs
Browse files Browse the repository at this point in the history
  • Loading branch information
asmorodskyi committed Nov 30, 2022
1 parent 70c4590 commit 6d4c671
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
8 changes: 4 additions & 4 deletions ocw/lib/EC2.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def cleanup_volumes(self, valid_period_days):
for volume in response['Volumes']:
if EC2.is_outdated(volume['CreateTime'], valid_period_days):
if self.volume_protected(volume):
self.log_info('Volume {} has tag DO_NOT_DELETE so protected from deletion',
self.log_info('Volume {} has tag pcw_ignore so protected from deletion',
volume['VolumeId'])
elif self.dry_run:
self.log_info("Volume deletion of {} skipped due to dry run mode", volume['VolumeId'])
Expand All @@ -134,7 +134,7 @@ def cleanup_volumes(self, valid_period_days):
def volume_protected(self, volume):
if 'Tags' in volume:
for tag in volume['Tags']:
if tag['Key'] == 'DO_NOT_DELETE':
if tag['Key'] == 'pcw_ignore':
return True
return False

Expand Down Expand Up @@ -199,10 +199,10 @@ def delete_all_clusters(self):
def cleanup_all(self):
valid_period_days = PCWConfig.get_feature_property('cleanup', 'ec2-max-age-days', self._namespace)

if valid_period_days >= 0:
if valid_period_days > 0:
self.cleanup_images(valid_period_days)
self.cleanup_volumes(valid_period_days)
self.cleanup_snapshots(valid_period_days)
self.cleanup_volumes(valid_period_days)
if PCWConfig.getBoolean('cleanup/vpc_cleanup', self._namespace):
self.cleanup_uploader_vpcs()

Expand Down
25 changes: 23 additions & 2 deletions ocw/lib/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from azure.mgmt.storage import StorageManagementClient
from azure.storage.blob import BlobServiceClient
from msrest.exceptions import AuthenticationError
import re
import time
from typing import Dict

Expand Down Expand Up @@ -102,14 +103,34 @@ def list_by_resource_group(self, resource_group, filters=None):
resource_group, filter=filters, expand="changedTime")]

def cleanup_all(self):
self.cleanup_blob_containers()
self.cleanup_images_from_rg()
self.cleanup_disks_from_rg()
self.cleanup_blob_containers()


@staticmethod
def container_valid_for_cleanup(container):
'''
under term "container" we meant Azure Blob Storage Container.
See https://learn.microsoft.com/en-us/azure/storage/blobs/storage-blobs-introduction
for more details
Container is valid for cleanup if it met 2 conditions :
1. "metadata" of container does not contain special tag (pcw_ignore)
2. Container name or contains "bootdiagnostics-" in its name or its name is "sle-images"
'''
if 'pcw_ignore' in container['metadata']:
return False
if re.match('^bootdiagnostics-', container.name):
return True
if container.name == 'sle-images':
return True
return False


def cleanup_blob_containers(self):
containers = self.bs_client().list_containers(include_metadata=True)
for c in containers:
if 'pcw_ignore' not in c['metadata']:
if Azure.container_valid_for_cleanup(c):
self.log_dbg('Found container {}', c.name)
container_blobs = self.container_client(c.name).list_blobs()
for blob in container_blobs:
Expand Down
15 changes: 13 additions & 2 deletions tests/test_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ def __init__(self, managed_by = None):

class FakeBlobContainer:

def __init__(self, metadata = []):
self.name = Faker().uuid4()
def __init__(self, metadata = [], name = None):
if name is None:
self.name = "sle-images"
else:
self.name = name
self.metadata = metadata

def __getitem__(self, i):
Expand Down Expand Up @@ -269,3 +272,11 @@ def mock_list_resource_groups(self):
failed_list_resource_groups = 5
with pytest.raises(AuthenticationError):
az = Azure('fake')

def test_container_valid_for_cleanup():

assert Azure.container_valid_for_cleanup(FakeBlobContainer({},"random name")) == False
assert Azure.container_valid_for_cleanup(FakeBlobContainer({}, "sle-images")) == True
assert Azure.container_valid_for_cleanup(FakeBlobContainer({}, "bootdiagnostics-dsfsdfsdf")) == True
assert Azure.container_valid_for_cleanup(FakeBlobContainer({"pcw_ignore": "1"}, "bootdiagnostics-sdafsdfs")) == False
assert Azure.container_valid_for_cleanup(FakeBlobContainer({"pcw_ignore": "1"}, "sle-images")) == False
4 changes: 2 additions & 2 deletions tests/test_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def test_cleanup_volumes_cleanupcheck(ec2_patch):
'Volumes': [{'VolumeId': MockedEC2Client.volumeid_to_delete, 'CreateTime': older_than_max_age_date},
{'VolumeId': 'too_young_to_die', 'CreateTime': now_age_date},
{'VolumeId': MockedEC2Client.volumeid_to_delete, 'CreateTime': older_than_max_age_date,
'Tags': [{'Key': 'DO_NOT_DELETE', 'Value': '1'}]}, ]
'Tags': [{'Key': 'pcw_ignore', 'Value': '1'}]}, ]
}
ec2_patch.cleanup_volumes(ec2_max_age_days)
assert len(MockedEC2Client.deleted_volumes) == 1
Expand Down Expand Up @@ -442,7 +442,7 @@ def mocked_get_boolean(config_path, field=None):

ec2_patch.cleanup_all()

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

def test_list_clusters(ec2_patch, monkeypatch):
mocked_eks = MockedEKSClient()
Expand Down

0 comments on commit 6d4c671

Please sign in to comment.