3030import re
3131
3232AMAZON_ClOUDWATCH_OBSERVABILITY = "amazon-cloudwatch-observability"
33+ CONTAINER_INSIGHTS_LOG_REGEX_PATTERN = "https:\/\/([a-z0-9-]+).console.aws.amazon.com\/cloudwatch\/home\?region=([a-z0-9-]+)#logsV2:log-groups\/log-group\/\$252Faws\$252Fcontainerinsights\$252F([a-zA-Z0-9-]+)\$252Fapplication\/log-events\/([a-z0-9-]+)-application.var.log.containers.([a-z0-9-]+)_([a-z0-9-]+)_([a-z0-9-]+)-([a-z0-9-]+).log"
3334
3435class GetLogs :
3536 def __init__ (self ):
@@ -76,6 +77,7 @@ def generate_cloudwatch_link(
7677 namespace : Optional [str ],
7778 ):
7879 eks_cluster_name = get_eks_cluster_name ()
80+ region = get_hyperpod_cluster_region ()
7981
8082 if self .is_container_insights_addon_enabled (eks_cluster_name ):
8183 k8s_client = KubernetesClient ()
@@ -105,13 +107,12 @@ def generate_cloudwatch_link(
105107 container_id = None
106108
107109 # Cloudwatch container insight log groups should have the same pod log as API response
108- region = get_hyperpod_cluster_region ()
109110 cloudwatch_url = self .get_log_url (eks_cluster_name , region , node_name , pod_name , namespace , container_name , container_id )
110111
111112 if not validate_region_and_cluster_name (region , eks_cluster_name ):
112113 return 'Failed to validate Eks cluster name and region'
113114
114- if not self . _validate_log_url ( cloudwatch_url ):
115+ if not re . match ( CONTAINER_INSIGHTS_LOG_REGEX_PATTERN , cloudwatch_url ):
115116 return 'Failed to validate cloudwatch log url. Please verify node name, pod container name and container id are valid'
116117
117118 cloudwatch_link = f'The pod cloudwatch log stream link is { cloudwatch_url } '
@@ -120,14 +121,6 @@ def generate_cloudwatch_link(
120121
121122 return cloudwatch_link
122123
123- def _validate_log_url (self , log_url ):
124- pattern = "https:\/\/([a-z0-9-]+).console.aws.amazon.com\/cloudwatch\/home\?region=([a-z0-9-]+)#logsV2:log-groups\/log-group\/\$252Faws\$252Fcontainerinsights\$252F([a-zA-Z0-9-]+)\$252Fapplication\/log-events\/([a-z0-9-]+)-application.var.log.containers.([a-z0-9-]+)_([a-z0-9-]+)_([a-z0-9-]+)-([a-z0-9-]+).log"
125- match = re .match (pattern , log_url )
126- if match :
127- return True
128- else :
129- return False
130-
131124 def get_log_url (self , eks_cluster_name , region , node_name , pod_name , namespace , container_name , container_id ):
132125 console_prefix = f'https://{ region } .console.aws.amazon.com/cloudwatch/home?region={ region } #'
133126 log_group_prefix = f'logsV2:log-groups/log-group/$252Faws$252Fcontainerinsights$252F{ eks_cluster_name } $252Fapplication/log-events/'
0 commit comments