From e7f8a859652089e6774fbc9443ec1f16252eb68f Mon Sep 17 00:00:00 2001 From: Øyvind Grønnesby Date: Mon, 8 Jul 2019 16:53:04 +0200 Subject: Default to all declared prod zones for endpoints without regions - Extract all production zones in the DeploymentSpec constructor and update all Endpoints instances without regions to these zones - Add more tests to validate that this does what we want it to. --- .../config/application/api/DeploymentSpec.java | 27 ++++++++++++++++++- .../com/yahoo/config/application/api/Endpoint.java | 3 +++ .../config/application/api/DeploymentSpecTest.java | 31 ++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) (limited to 'config-model-api') diff --git a/config-model-api/src/main/java/com/yahoo/config/application/api/DeploymentSpec.java b/config-model-api/src/main/java/com/yahoo/config/application/api/DeploymentSpec.java index ada5ee23a7c..5b5f89fd8d1 100644 --- a/config-model-api/src/main/java/com/yahoo/config/application/api/DeploymentSpec.java +++ b/config-model-api/src/main/java/com/yahoo/config/application/api/DeploymentSpec.java @@ -74,11 +74,36 @@ public class DeploymentSpec { this.athenzDomain = athenzDomain; this.athenzService = athenzService; this.notifications = notifications; - this.endpoints = List.copyOf(Objects.requireNonNull(endpoints, "Missing endpoints parameter")); + this.endpoints = ImmutableList.copyOf(validateEndpoints(endpoints, this.steps)); validateZones(this.steps); validateAthenz(); validateEndpoints(this.steps, globalServiceId, this.endpoints); } + + /** Validates the endpoints and makes sure default values are respected */ + private List validateEndpoints(List endpoints, List steps) { + Objects.requireNonNull(endpoints, "Missing endpoints parameter"); + + var productionRegions = steps.stream() + .filter(step -> step.deploysTo(Environment.prod)) + .flatMap(step -> step.zones().stream()) + .flatMap(zone -> zone.region().stream()) + .map(RegionName::value) + .collect(Collectors.toSet()); + + var rebuiltEndpointsList = new ArrayList(); + + for (var endpoint : endpoints) { + if (endpoint.regions().isEmpty()) { + var rebuiltEndpoint = endpoint.withRegions(productionRegions); + rebuiltEndpointsList.add(rebuiltEndpoint); + } else { + rebuiltEndpointsList.add(endpoint); + } + } + + return ImmutableList.copyOf(rebuiltEndpointsList); + } /** Throw an IllegalArgumentException if the total delay exceeds 24 hours */ private void validateTotalDelay(List steps) { diff --git a/config-model-api/src/main/java/com/yahoo/config/application/api/Endpoint.java b/config-model-api/src/main/java/com/yahoo/config/application/api/Endpoint.java index 158fbfb175f..e47dcd78219 100644 --- a/config-model-api/src/main/java/com/yahoo/config/application/api/Endpoint.java +++ b/config-model-api/src/main/java/com/yahoo/config/application/api/Endpoint.java @@ -75,4 +75,7 @@ public class Endpoint { return Objects.hash(endpointId, containerId, regions); } + public Endpoint withRegions(Set regions) { + return new Endpoint(endpointId, containerId, regions); + } } diff --git a/config-model-api/src/test/java/com/yahoo/config/application/api/DeploymentSpecTest.java b/config-model-api/src/test/java/com/yahoo/config/application/api/DeploymentSpecTest.java index 8120c82e8f4..47eaf7a515a 100644 --- a/config-model-api/src/test/java/com/yahoo/config/application/api/DeploymentSpecTest.java +++ b/config-model-api/src/test/java/com/yahoo/config/application/api/DeploymentSpecTest.java @@ -14,6 +14,7 @@ import java.util.List; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; import static com.yahoo.config.application.api.Notifications.Role.author; import static com.yahoo.config.application.api.Notifications.When.failing; @@ -516,6 +517,28 @@ public class DeploymentSpecTest { assertEquals(List.of("fooooooooooo"), endpointIds("")); } + @Test + public void endpointDefaultRegions() { + var spec = DeploymentSpec.fromXml("" + + "" + + " " + + " us-east" + + " us-west" + + " " + + " " + + " " + + " us-east" + + " " + + " " + + " " + + " " + + ""); + + assertEquals(Set.of("us-east"), endpointRegions("foo", spec)); + assertEquals(Set.of("us-east", "us-west"), endpointRegions("nalle", spec)); + assertEquals(Set.of("us-east", "us-west"), endpointRegions("default", spec)); + } + private static void assertInvalid(String endpointTag) { try { endpointIds(endpointTag); @@ -523,6 +546,14 @@ public class DeploymentSpecTest { } catch (IllegalArgumentException ignored) {} } + private static Set endpointRegions(String endpointId, DeploymentSpec spec) { + return spec.endpoints().stream() + .filter(endpoint -> endpoint.endpointId().equals(endpointId)) + .flatMap(endpoint -> endpoint.regions().stream()) + .map(RegionName::value) + .collect(Collectors.toSet()); + } + private static List endpointIds(String endpointTag) { var xml = "" + " " + -- cgit v1.2.3