Skip to content

Commit

Permalink
Fixed the cluster_id tag (#801)
Browse files Browse the repository at this point in the history
  • Loading branch information
athiruma authored Jun 10, 2024
1 parent acaf2d6 commit d9c6053
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class TagClusterOperations:
This class tags AWS resources
"""

CLUSTER_ID_COST_ALLOCATION_TAG = 'cluster_id'

def __init__(self, region: str, input_tags: dict = None, cluster_name: str = None, cluster_prefix: str = None, dry_run: str = None, cluster_only: bool = None):
self.cluster_only = cluster_only
self.cluster_prefix = cluster_prefix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class TagClusterResources(TagClusterOperations):

SHORT_ID = 5
NA_VALUE = 'NA'
CLUSTER_ID_COST_ALLOCATION_TAG = 'cluster_id'

def __init__(self, cluster_name: str = None, cluster_prefix: str = None, input_tags: dict = None,
region: str = 'us-east-2', dry_run: str = 'yes', cluster_only: bool = False):
Expand Down Expand Up @@ -61,13 +60,16 @@ def __add_cluster_id_tag(self, tags: list):
if not cluster_id_tag:
cluster_name = ''
for tag in tags:
if self.cluster_prefix in tag.get('Key'):
cluster_name = tag['Key']
break
if tag.get('Key') == 'api.openshift.com/name':
cluster_name = tag['Value']
else:
if not cluster_name and self.cluster_prefix in tag.get('Key'):
cluster_name = tag['Key']
if cluster_name:
# cluster_name = cluster_id
cluster_name = cluster_name.split('/')[-1]
tags.append({'Key': self.CLUSTER_ID_COST_ALLOCATION_TAG, 'Value': cluster_name})
return tags

def __check_name_in_tags(self, tags: list, resource_id: str):
"""
Expand Down Expand Up @@ -155,7 +157,6 @@ def __generate_cluster_resources_list_by_tag(self, resources_list: list, input_r
instance_tags = self.__get_cluster_tags_by_instance_cluster(cluster_name=tag.get('Key'))
add_tags.extend(instance_tags)
add_tags = self.__check_name_in_tags(tags=add_tags, resource_id=resource_id)
self.__add_cluster_id_tag(tags=add_tags)
add_tags = self.__remove_tags_start_with_aws(add_tags)
add_tags = self.__filter_resource_tags_by_add_tags(resource.get(tags), add_tags)
if add_tags:
Expand Down Expand Up @@ -256,7 +257,7 @@ def __validate_existing_tag(self, tags: list):
@param tags:
@return:
"""
check_tags = ['User', 'Project', 'Manager', 'Owner', 'Email']
check_tags = ['User', 'Project', 'Manager', 'Owner', 'Email', self.CLUSTER_ID_COST_ALLOCATION_TAG]
tag_count = 0
for tag in tags:
if tag.get('Key') in check_tags:
Expand Down Expand Up @@ -285,6 +286,7 @@ def update_cluster_tags(self, resources: list):
for tag in tags:
if self.cluster_prefix in tag.get('Key'):
add_tags = self.__append_input_tags()
add_tags.append(tag)
cluster_name = tag.get('Key').split('/')[-1]
user = self.ec2_operations.get_tag_value_from_tags(tags=tags, tag_name='User')
if cluster_name in cluster_instances and user and user != 'NA':
Expand Down Expand Up @@ -328,6 +330,7 @@ def update_cluster_tags(self, resources: list):
add_tags.append({'Key': 'LaunchTime',
'Value': self.get_date_from_date_time(item.get('LaunchTime'))})
add_tags = self.remove_creation_date(add_tags)
add_tags = self.__add_cluster_id_tag(tags=add_tags)
add_tags = self.__filter_resource_tags_by_add_tags(tags=item.get('Tags'),
search_tags=add_tags)
if add_tags:
Expand Down
2 changes: 1 addition & 1 deletion jenkins/tenant/aws/common/run_cost_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


cost_tags = ['PurchaseType', 'ChargeType', 'User', 'Budget', 'Project', 'Manager', 'Owner',
'LaunchTime', 'Name', 'Email', 'Environment', 'User:Spot']
'LaunchTime', 'Name', 'Email', 'Environment', 'User:Spot', 'cluster_id']
cost_metric = 'UnblendedCost' # UnblendedCost/BlendedCost
granularity = 'DAILY' # DAILY/MONTHLY/HOURLY
cost_explorer_index = 'cloud-governance-haim-cost-explorer-global-index'
Expand Down

0 comments on commit d9c6053

Please sign in to comment.