|
23 | 23 | from hyperpod_cli.utils import ( |
24 | 24 | get_eks_cluster_name, |
25 | 25 | get_hyperpod_cluster_region, |
| 26 | + validate_region_and_cluster_name, |
26 | 27 | ) |
27 | 28 | from kubernetes.client.rest import ApiException |
28 | 29 | from kubernetes.client import V1ResourceAttributes |
| 30 | +import re |
29 | 31 |
|
30 | 32 | AMAZON_ClOUDWATCH_OBSERVABILITY = "amazon-cloudwatch-observability" |
31 | 33 |
|
@@ -103,18 +105,29 @@ def generate_cloudwatch_link( |
103 | 105 | container_id = None |
104 | 106 |
|
105 | 107 | # Cloudwatch container insight log groups should have the same pod log as API response |
106 | | - if node_name and pod_name and namespace and container_name and container_id: |
107 | | - region = get_hyperpod_cluster_region() |
| 108 | + region = get_hyperpod_cluster_region() |
| 109 | + cloudwatch_url = self.get_log_url(eks_cluster_name, region, node_name, pod_name, namespace, container_name, container_id) |
108 | 110 |
|
109 | | - cloudwatch_url = self.get_log_url(eks_cluster_name, region, node_name, pod_name, namespace, container_name, container_id) |
110 | | - cloudwatch_link = f'The pod cloudwatch log stream link is {cloudwatch_url}' |
111 | | - else: |
112 | | - cloudwatch_link = 'Failed to load container insights CloudWatch Link!' |
| 111 | + if not validate_region_and_cluster_name(region, eks_cluster_name): |
| 112 | + return 'Failed to validate Eks cluster name and region' |
| 113 | + |
| 114 | + if not self._validate_log_url(cloudwatch_url): |
| 115 | + return 'Failed to validate cloudwatch log url. Please verify node name, pod container name and container id are valid' |
| 116 | + |
| 117 | + cloudwatch_link = f'The pod cloudwatch log stream link is {cloudwatch_url}' |
113 | 118 | else: |
114 | 119 | cloudwatch_link = None |
115 | 120 |
|
116 | 121 | return cloudwatch_link |
117 | 122 |
|
| 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 | + |
118 | 131 | def get_log_url(self, eks_cluster_name, region, node_name, pod_name, namespace, container_name, container_id): |
119 | 132 | console_prefix = f'https://{region}.console.aws.amazon.com/cloudwatch/home?region={region}#' |
120 | 133 | log_group_prefix = f'logsV2:log-groups/log-group/$252Faws$252Fcontainerinsights$252F{eks_cluster_name}$252Fapplication/log-events/' |
|
0 commit comments