@@ -107,17 +107,33 @@ def discoverClusters(Map params = [:]) {
107107 // Step 3: Consolidate data
108108 def consolidatedClusters = []
109109
110- // Process all unique cluster names
111- def allClusterNames = (s3Clusters. keySet() + awsClusters. keySet()) as Set
112-
113- allClusterNames. each { clusterName ->
114- def s3Cluster = s3Clusters[clusterName]
115- def awsCluster = awsClusters[clusterName]
110+ // Track which S3 clusters have been matched to AWS clusters
111+ def matchedS3Clusters = [] as Set
112+ def matchedAWSClusters = [] as Set
113+
114+ // First pass: Try to match AWS clusters with S3 clusters
115+ // AWS cluster names often have random suffixes (e.g., test-cluster-7-qc8lc)
116+ // while S3 stores them with base names (e.g., test-cluster-7)
117+ awsClusters. each { awsClusterName , awsCluster ->
118+ def matchedS3Name = null
119+
120+ // Check if this AWS cluster name starts with any S3 cluster name
121+ s3Clusters. each { s3ClusterName , s3Cluster ->
122+ // Match if AWS cluster name starts with S3 cluster name followed by a hyphen
123+ // This handles the pattern: base-name-randomsuffix
124+ if (awsClusterName == s3ClusterName ||
125+ awsClusterName. startsWith(" ${ s3ClusterName} -" )) {
126+ matchedS3Name = s3ClusterName
127+ return true // Break out of the inner loop
128+ }
129+ }
116130
117- if (s3Cluster && awsCluster) {
118- // Merge data from both sources
131+ if (matchedS3Name) {
132+ // Found matching S3 backup
133+ def s3Cluster = s3Clusters[matchedS3Name]
119134 def merged = [
120- name : clusterName,
135+ name : awsClusterName, // Use the AWS cluster name (with suffix) as the primary name
136+ baseName : matchedS3Name, // Store the base name for reference
121137 source : ' combined' ,
122138 status : s3Cluster. status ?: awsCluster. status,
123139 metadata : [:],
@@ -130,16 +146,26 @@ def discoverClusters(Map params = [:]) {
130146 merged. metadata. putAll(s3Cluster. metadata ?: [:])
131147
132148 consolidatedClusters << merged
133- } else if (s3Cluster) {
134- // S3 only - cluster might be deleted but state preserved
135- consolidatedClusters << s3Cluster
136- } else if (awsCluster) {
137- // AWS only - active cluster without S3 backup
149+ matchedS3Clusters << matchedS3Name
150+ matchedAWSClusters << awsClusterName
151+ }
152+ }
153+
154+ // Second pass: Add unmatched AWS clusters (no S3 backup)
155+ awsClusters. each { awsClusterName , awsCluster ->
156+ if (! matchedAWSClusters. contains(awsClusterName)) {
138157 awsCluster. s3State = [hasBackup : false ]
139158 consolidatedClusters << awsCluster
140159 }
141160 }
142161
162+ // Third pass: Add unmatched S3 clusters (deleted or no AWS resources)
163+ s3Clusters. each { s3ClusterName , s3Cluster ->
164+ if (! matchedS3Clusters. contains(s3ClusterName)) {
165+ consolidatedClusters << s3Cluster
166+ }
167+ }
168+
143169 // Sort by cluster name for consistent output
144170 consolidatedClusters. sort { it. name }
145171
0 commit comments