|
| 1 | +from os import getenv |
| 2 | +from typing import Optional |
| 3 | + |
1 | 4 | import boto3
|
2 | 5 | from collectors import gc
|
3 | 6 | from collections import defaultdict
|
|
9 | 12 |
|
10 | 13 |
|
11 | 14 | class AMIGarbageCollector(gc.GarbageCollector):
|
12 |
| - def __init__(self, current_images: list[str], region: str, dry_run: bool): |
| 15 | + def __init__( |
| 16 | + self, |
| 17 | + current_images: list[str], |
| 18 | + current_branches: list[str], |
| 19 | + region: str, |
| 20 | + dry_run: bool, |
| 21 | + ): |
13 | 22 | super().__init__("AMI Collector", region, dry_run)
|
14 | 23 | self.ec2_client = boto3.client("ec2", region_name=region)
|
15 | 24 | self.current_images = current_images
|
| 25 | + self.current_branches = current_branches |
16 | 26 | self.search_tag_name = "vm-images"
|
17 | 27 | self.search_tag_value = "true"
|
18 | 28 |
|
@@ -44,29 +54,46 @@ def _get_amis(self) -> list[ImageTypeDef]:
|
44 | 54 |
|
45 | 55 | def _find_expired_amis(self, amis: list[ImageTypeDef]) -> list[ImageTypeDef]:
|
46 | 56 | expired_amis = []
|
47 |
| - ami_groups = defaultdict(list) |
| 57 | + ami_groups: dict[str, dict[str, list[ImageTypeDef]]] = defaultdict( |
| 58 | + lambda: defaultdict(list) |
| 59 | + ) |
48 | 60 |
|
49 | 61 | # Group AMIs by "image-name" tag
|
50 | 62 | for ami in amis:
|
51 | 63 | img_tags = ami["Tags"]
|
52 | 64 | if img_tags:
|
| 65 | + image_name: Optional[str] = None |
| 66 | + branch_name: Optional[str] = None |
53 | 67 | for tag in img_tags:
|
54 | 68 | if tag["Key"] == "image-name":
|
55 |
| - img_name = tag["Value"] |
56 |
| - ami_groups[img_name].append(ami) |
| 69 | + image_name = tag["Value"] |
| 70 | + if tag["Key"] == "branch-name": |
| 71 | + branch_name = tag["Value"] |
| 72 | + if image_name: |
| 73 | + if branch_name: |
| 74 | + ami_groups[branch_name][image_name].append(ami) |
57 | 75 | break
|
| 76 | + else: |
| 77 | + expired_amis.append(ami) |
58 | 78 |
|
59 | 79 | # Sort AMIs by creation date.
|
60 | 80 | # If image is currently supported, keep only the newest AMI. Expire the rest.
|
61 | 81 | # If image is not currently supported, expire all AMIs.
|
62 |
| - for img_name, amis in ami_groups.items(): |
63 |
| - amis = sorted( |
64 |
| - amis, key=lambda x: parser.parse(x["CreationDate"]), reverse=True |
65 |
| - ) |
66 |
| - if img_name in self.current_images: |
67 |
| - expired_amis.extend(amis[1:]) |
68 |
| - else: |
69 |
| - expired_amis.extend(amis) |
| 82 | + for branch_name, images in ami_groups.items(): |
| 83 | + if branch_name == gc.DEFAULT_BRANCH_NAME: |
| 84 | + for image_name, amis in images.items(): |
| 85 | + if image_name in self.current_images: |
| 86 | + amis = sorted( |
| 87 | + amis, |
| 88 | + key=lambda x: parser.parse(x["CreationDate"]), |
| 89 | + reverse=True, |
| 90 | + ) |
| 91 | + expired_amis.extend(amis[1:]) |
| 92 | + else: |
| 93 | + expired_amis.extend(amis) |
| 94 | + elif branch_name not in self.current_branches: |
| 95 | + for image_name, amis in images.items(): |
| 96 | + expired_amis.extend(amis) |
70 | 97 |
|
71 | 98 | return expired_amis
|
72 | 99 |
|
|
0 commit comments