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

Added support for Core team metrics #25

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions aws_quota/check/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .cloudformation import *
from .dynamodb import *
from .ebs import *
from .ecr import *
from .ec2 import *
from .ecs import *
from .eks import *
Expand Down
28 changes: 27 additions & 1 deletion aws_quota/check/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,30 @@ class LaunchTemplatesCount(QuotaCheck):

@property
def current(self):
return self.count_paginated_results("ec2", "describe_launch_templates", "LaunchTemplates")
return self.count_paginated_results("ec2", "describe_launch_templates", "LaunchTemplates")

class AmiCount(QuotaCheck):
key = "ec2_ami_count"
scope = QuotaScope.REGION
service_code = 'ec2'
quota_code = 'L-B665C33B'
description = "The maximum number of public and private AMIs allowed in this Region. These include available, disabled, and pending AMIs, and AMIs in the Recycle Bin."

@property
def current(self):
return self.count_paginated_results("ec2", "describe_images", "Images",
{"Owners": ["self"], "IncludeDeprecated": True, "IncludeDisabled": True}) + \
self.count_paginated_results("ec2", "list_images_in_recycle_bin", "Images")

class PublicAmiCount(QuotaCheck):
key = "ec2_public_ami_count"
scope = QuotaScope.REGION
service_code = 'ec2'
quota_code = 'L-0E3CBAB9'
description = "The maximum number of public AMIs, including public AMIs in the Recycle Bin, allowed in this Region."

@property
def current(self):
return self.count_paginated_results("ec2", "describe_images", "Images",
{"Owners": ["self"], "IncludeDeprecated": True, "IncludeDisabled": True,
"Filters":[{"Name": "is-public", "Values": ["true"]}]})
43 changes: 43 additions & 0 deletions aws_quota/check/ecr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from typing import List
from .quota_check import InstanceQuotaCheck, QuotaScope
from aws_quota.utils import get_paginated_results

import boto3
import cachetools


@cachetools.cached(cache=cachetools.TTLCache(maxsize=1, ttl=60))
def get_all_repositories(session: boto3.Session) -> List[str]:
services = ['ecr']
# ECR is available in all regions, but ecr-public is only available in us-east-1
if session.region_name == 'us-east-1':
services.append('ecr-public')

return [
repository['repositoryArn']
for service in services
for repository in get_paginated_results(session, service, 'describe_repositories', 'repositories')
]

@cachetools.cached(cache=cachetools.TTLCache(10000, 60)) # 10k = default number of max registries per account
def get_repository_images(session: boto3.Session, repository_arn: str) -> List[str]:
arn_parts = repository_arn.split(':')
service = arn_parts[2]
repository_name = arn_parts[5].removeprefix("repository/")
return get_paginated_results(session, service, "describe_images", "imageDetails", {'repositoryName': repository_name})
Copy link

@taraspos taraspos Mar 20, 2024

Choose a reason for hiding this comment

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

@fheinecke did you try by chance list-images call, is it any faster?

Copy link
Author

Choose a reason for hiding this comment

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

Just tested this. The decrease is negligible and within the margin of error for the tests I ran - around 100us per image. In addition the public ECR endpoint doesn't support list_images, so I'd need to add separate logic for both public and private ECR.


class ImagesPerRepository(InstanceQuotaCheck):
key = "ecr_images_per_repository"
scope = QuotaScope.INSTANCE
service_code = 'ecr'
quota_code = 'L-03A36CE1'
description = "The maximum number of images per repository."
instance_id = 'Repository ARN'

@staticmethod
def get_all_identifiers(session: boto3.Session) -> List[str]:
return get_all_repositories(session)

@property
def current(self):
return len(get_repository_images(self.boto_session, self.instance_id))
Loading