Skip to content

Commit

Permalink
Added the graviton cost
Browse files Browse the repository at this point in the history
  • Loading branch information
athiruma committed Oct 9, 2024
1 parent 50814fc commit 32692de
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
29 changes: 29 additions & 0 deletions cloud_governance/common/utils/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,32 @@
EC2_NAMESPACE = 'AWS/EC2'
CLOUDWATCH_METRICS_AVAILABLE_DAYS = 14
AWS_DEFAULT_GLOBAL_REGION = 'us-east-1'

# X86 to Graviton
GRAVITON_MAPPINGS = {
'c6a': 'c6g',
'c6i': 'c6g',
'c6in': 'c6gn',
'c7': 'c7g',
'c7i': 'c7g',
'c7a': 'c7g',
'c7i-flex': 'c7g',
'g5': 'g5g',
'hpc7a': 'hpc7g',
'i4i': 'i4g',
'm5': 'm6g',
'm5a': 'm6g',
'm5n': 'm6g',
'm5zn': 'm6g',
'm6i': 'm6g',
'm6in': 'm6g',
'm6a': 'm6g',
'm7i': 'm7g',
'm7a': 'm7g',
'm7i-flex': 'm7g',
'r7i': 'r8g',
'r7a': 'r7g',
'r6i': 'r6g',
't3': 't4g',
't2': 'a1'
}
24 changes: 23 additions & 1 deletion cloud_governance/policy/aws/monitor/cluster_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re

from cloud_governance.cloud_resource_orchestration.utils.common_operations import string_equal_ignore_case
from cloud_governance.common.utils.configs import GRAVITON_MAPPINGS, DEFAULT_ROUND_DIGITS
from cloud_governance.policy.helpers.aws.aws_policy_operations import AWSPolicyOperations


Expand Down Expand Up @@ -41,6 +42,7 @@ def run_policy_operations(self):
launch_time = instance.get('LaunchTime')
running_instances = stopped_instances = 0
running_days = self.calculate_days(instance.get('LaunchTime'))
running_hours = self.calculate_hours(instance.get('LaunchTime'))
stopped_date_time = ''
if string_equal_ignore_case(instance_state, 'stopped'):
stopped_instances = 1
Expand All @@ -55,7 +57,7 @@ def run_policy_operations(self):
running_instances = 1
creation_date = ''
rosa_cluster = True if self.get_tag_name_from_tags(tags=tags, tag_name='red-hat-managed') else False
using_graviton = True if '.g' in instance.get('InstanceType') else False
using_graviton = True if 'g' or 'a1' in instance.get('InstanceType') else False
if 'master' in name_tag.lower():
creation_date = self.__get_creation_date(instance.get('BlockDeviceMappings', []))
instance_data = f"{instance.get('InstanceId')}, {self.get_tag_name_from_tags(tags=tags, tag_name='Name')}, {instance.get('InstanceType')}, {instance_state}, {running_days}, {launch_time}"
Expand All @@ -78,6 +80,7 @@ def run_policy_operations(self):
'InstanceTypes': [instance.get('InstanceType')],
'User': self.get_tag_name_from_tags(tags=tags, tag_name='User'),
'RunningDays': running_days,
'RunningHours': running_hours,
'RegionName': self._region,
'PublicCloud': self._cloud_name,
'Instances': [instance_data],
Expand All @@ -94,5 +97,24 @@ def run_policy_operations(self):
cluster_data[cluster_tag]['StoppedDate'] = stopped_date_time
for cluster in cluster_data.values():
instance_types = cluster['InstanceTypes']
total_cost = 0
graviton_instance_cost = 0
cluster['GravitonInstanceTypes'] = []
running_hours = 0 if cluster['RunningHours'] == 0 else cluster['RunningHours']
for instance_type in set(instance_types):
instance_types_count = instance_types.count(instance_type)
unit_price = self._resource_pricing.get_ec2_price(region_name=self._region,
instance_type=instance_type)
total_cost += unit_price * 24 * running_hours * instance_types_count
if not cluster['Graviton']:
instance_family, instance_size = instance_type.split('.')
graviton_instance = f'{GRAVITON_MAPPINGS[instance_family]}.{instance_size}'
graviton_unit_price = self._resource_pricing.get_ec2_price(region_name=self._region,
instance_type=graviton_instance)
graviton_instance_cost += graviton_unit_price * 24 * running_hours * instance_types_count
cluster['GravitonInstanceTypes'].append(f"{graviton_instance}: {instance_types_count}")

cluster['TotalGravitonInstanceCost'] = round(graviton_instance_cost, DEFAULT_ROUND_DIGITS)
cluster['TotalCost'] = round(total_cost, DEFAULT_ROUND_DIGITS)
cluster['InstanceTypes'] = [f"{x}: {instance_types.count(x)}" for x in set(instance_types)]
return list(cluster_data.values())
19 changes: 18 additions & 1 deletion cloud_governance/policy/helpers/abstract_policy_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


class AbstractPolicyOperations(ABC):

DAYS_TO_NOTIFY_ADMINS = 2
DAYS_TO_TRIGGER_RESOURCE_MAIL = 4
DAILY_HOURS = 24
Expand Down Expand Up @@ -46,6 +45,24 @@ def calculate_days(self, create_date: Union[datetime, str], start_date: Union[da
days = start_date - create_date.date()
return days.days

def calculate_hours(self, create_date: Union[datetime, str],
start_date: Union[datetime, str] = datetime.now(timezone.utc)):
"""
This method returns the hours
:param start_date:
:type start_date:
:param create_date:
:type create_date:
:return:
:rtype:
"""
if isinstance(create_date, str):
create_date = datetime.strptime(create_date, "%Y-%M-%d %H:%M:%S").replace(tzinfo=timezone.utc)
if isinstance(start_date, str):
start_date = datetime.strptime(start_date, "%Y-%m-%d %H:%M:%S").replace(tzinfo=timezone.utc)
days = start_date - create_date
return days.seconds / 3600

def get_clean_up_days_count(self, tags: Union[list, dict]):
"""
This method returns the cleanup days count
Expand Down

0 comments on commit 32692de

Please sign in to comment.