From f8bfe2468ebf4edb191011edcf92816a708fdb55 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 15 May 2024 14:49:18 +0000 Subject: [PATCH] fix(ClusterController): Fix GetClusters returning only the last 2 providers clusterNames of application (#6210) (#6213) (cherry picked from commit c9bba669c24d1dd5d2e6d17a58fa4bd99aa20881) Co-authored-by: Christos Arvanitis --- .../controllers/ClusterController.groovy | 15 +++------------ .../controllers/ClusterControllerSpec.groovy | 12 ++++++++++-- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/clouddriver-web/src/main/groovy/com/netflix/spinnaker/clouddriver/controllers/ClusterController.groovy b/clouddriver-web/src/main/groovy/com/netflix/spinnaker/clouddriver/controllers/ClusterController.groovy index 85c9c36958d..f9244bd3adb 100644 --- a/clouddriver-web/src/main/groovy/com/netflix/spinnaker/clouddriver/controllers/ClusterController.groovy +++ b/clouddriver-web/src/main/groovy/com/netflix/spinnaker/clouddriver/controllers/ClusterController.groovy @@ -79,23 +79,14 @@ class ClusterController { .sorted(Comparator.comparing({ Application it -> it.getName().toLowerCase() })) .collect(Collectors.toList()) - Map> clusterNames = Map.of() - def lastApp = null - for (app in apps) { - if (!lastApp) { - clusterNames = app.getClusterNames() - } else { - clusterNames = mergeClusters(lastApp, app) - } - lastApp = app - } + Map> clusterNames = mergeClusters(apps) return clusterNames } - private Map> mergeClusters(Application a, Application b) { + private Map> mergeClusters(List a) { Map> map = new HashMap<>() - Stream.of(a, b) + a.stream() .flatMap({ it.getClusterNames().entrySet().stream() }) .forEach({ entry -> map.computeIfAbsent(entry.getKey(), { new HashSet<>() }).addAll(entry.getValue()) diff --git a/clouddriver-web/src/test/groovy/com/netflix/spinnaker/clouddriver/controllers/ClusterControllerSpec.groovy b/clouddriver-web/src/test/groovy/com/netflix/spinnaker/clouddriver/controllers/ClusterControllerSpec.groovy index 6a76b5f42f9..bb9480f7e64 100644 --- a/clouddriver-web/src/test/groovy/com/netflix/spinnaker/clouddriver/controllers/ClusterControllerSpec.groovy +++ b/clouddriver-web/src/test/groovy/com/netflix/spinnaker/clouddriver/controllers/ClusterControllerSpec.groovy @@ -56,13 +56,21 @@ class ClusterControllerSpec extends Specification { getApplication("app") >> app2 } - clusterController.applicationProviders = [appProvider1, appProvider2] + def app3 = Stub(Application) { + getName() >> "app" + getClusterNames() >> ["stage": ["we-need-all-clusters-to-be-returned"] as Set] + } + def appProvider3 = Stub(ApplicationProvider) { + getApplication("app") >> app3 + } + + clusterController.applicationProviders = [appProvider1, appProvider2, appProvider3] when: def result = clusterController.listByAccount("app") then: - result == [test: ["foo", "bar"] as Set, prod: ["baz"] as Set] + result == [test: ["foo", "bar"] as Set, prod: ["baz"] as Set, stage: ["we-need-all-clusters-to-be-returned"] as Set] } void "should throw exception when looking for specific cluster that doesnt exist"() {