From 36f6ce9aaff5f9dbcd9f58a0233de3e47c38d8db Mon Sep 17 00:00:00 2001 From: Sergii Leshchenko Date: Thu, 14 Sep 2017 16:38:51 +0300 Subject: [PATCH] CHE-6216 Ignore Routes that are specified in OpenShift recipe (#6259) Estimated environment will contain warning when user specify Route in OpenShift recipe --- .../environment/OpenShiftEnvironment.java | 110 +++++++++--------- .../OpenShiftEnvironmentParser.java | 36 ++++-- .../openshift/ServerExposerTest.java | 4 +- .../OpenShiftEnvironmentParserTest.java | 17 +++ 4 files changed, 102 insertions(+), 65 deletions(-) diff --git a/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/environment/OpenShiftEnvironment.java b/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/environment/OpenShiftEnvironment.java index c851644b42..7abae0ceab 100644 --- a/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/environment/OpenShiftEnvironment.java +++ b/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/environment/OpenShiftEnvironment.java @@ -17,77 +17,83 @@ import io.fabric8.openshift.api.model.Route; import java.util.HashMap; import java.util.Map; -/** @author Sergii Leshchenko */ +/** + * Holds objects of OpenShift environment. + * + * @author Sergii Leshchenko + */ public class OpenShiftEnvironment { - private Map pods; - private Map services; - private Map routes; - private Map persistentVolumeClaims; - public OpenShiftEnvironment() {} + private final Map pods; + private final Map services; + private final Map routes; + private final Map persistentVolumeClaims; + public static Builder builder() { + return new Builder(); + } + + private OpenShiftEnvironment( + Map pods, + Map services, + Map routes, + Map persistentVolumeClaims) { + this.pods = pods; + this.services = services; + this.routes = routes; + this.persistentVolumeClaims = persistentVolumeClaims; + } + + /** Returns pods that should be created when environment starts. */ public Map getPods() { - if (pods == null) { - pods = new HashMap<>(); - } return pods; } - public void setPods(Map pods) { - this.pods = pods; - } - - public OpenShiftEnvironment withPods(Map pods) { - this.pods = pods; - return this; - } - + /** Returns services that should be created when environment starts. */ public Map getServices() { - if (services == null) { - services = new HashMap<>(); - } return services; } - public void setServices(Map services) { - this.services = services; - } - - public OpenShiftEnvironment withServices(Map services) { - this.services = services; - return this; - } - + /** Returns services that should be created when environment starts. */ public Map getRoutes() { - if (routes == null) { - routes = new HashMap<>(); - } return routes; } - public void setRoutes(Map routes) { - this.routes = routes; - } - - public OpenShiftEnvironment withRoutes(Map routes) { - this.routes = routes; - return this; - } - + /** Returns PVCs that should be created when environment starts. */ public Map getPersistentVolumeClaims() { - if (persistentVolumeClaims == null) { - persistentVolumeClaims = new HashMap<>(); - } return persistentVolumeClaims; } - public void setPersistentVolumeClaims(Map persistentVolumeClaims) { - this.persistentVolumeClaims = persistentVolumeClaims; - } + public static class Builder { + private final Map pods = new HashMap<>(); + private final Map services = new HashMap<>(); + private final Map routes = new HashMap<>(); + private final Map persistentVolumeClaims = new HashMap<>(); - public OpenShiftEnvironment withPersistentVolumeClaims( - Map persistentVolumeClaims) { - this.persistentVolumeClaims = persistentVolumeClaims; - return this; + private Builder() {} + + public Builder setPods(Map pods) { + this.pods.putAll(pods); + return this; + } + + public Builder setServices(Map services) { + this.services.putAll(services); + return this; + } + + public Builder setRoutes(Map route) { + this.routes.putAll(route); + return this; + } + + public Builder setPersistentVolumeClaims(Map pvcs) { + this.persistentVolumeClaims.putAll(pvcs); + return this; + } + + public OpenShiftEnvironment build() { + return new OpenShiftEnvironment(pods, services, routes, persistentVolumeClaims); + } } } diff --git a/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/environment/OpenShiftEnvironmentParser.java b/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/environment/OpenShiftEnvironmentParser.java index 445e4679e5..b36b68fa1d 100644 --- a/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/environment/OpenShiftEnvironmentParser.java +++ b/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/environment/OpenShiftEnvironmentParser.java @@ -36,6 +36,7 @@ import org.eclipse.che.api.workspace.server.spi.InternalEnvironment.InternalReci import org.eclipse.che.api.workspace.server.spi.InternalMachineConfig; import org.eclipse.che.workspace.infrastructure.openshift.OpenShiftClientFactory; import org.eclipse.che.workspace.infrastructure.openshift.ServerExposer; +import org.eclipse.che.workspace.infrastructure.openshift.environment.OpenShiftEnvironment.Builder; /** * Parses {@link InternalEnvironment} into {@link OpenShiftEnvironment}. @@ -51,6 +52,11 @@ import org.eclipse.che.workspace.infrastructure.openshift.ServerExposer; */ public class OpenShiftEnvironmentParser { + static final int ROUTE_IGNORED_WARNING_CODE = 4100; + static final String ROUTES_IGNORED_WARNING_MESSAGE = + "Routes specified in OpenShift recipe are ignored. " + + "To expose ports please define servers in machine configuration."; + static final String DEFAULT_RESTART_POLICY = "Never"; private final OpenShiftClientFactory clientFactory; @@ -93,8 +99,8 @@ public class OpenShiftEnvironmentParser { Map pods = new HashMap<>(); Map services = new HashMap<>(); - Map routes = new HashMap<>(); Map pvcs = new HashMap<>(); + boolean isAnyRoutePresent = false; for (HasMetadata object : list.getItems()) { if (object instanceof DeploymentConfig) { throw new ValidationException("Supporting of deployment configs is not implemented yet."); @@ -105,26 +111,32 @@ public class OpenShiftEnvironmentParser { Service service = (Service) object; services.put(service.getMetadata().getName(), service); } else if (object instanceof Route) { - Route route = (Route) object; - routes.put(route.getMetadata().getName(), route); + isAnyRoutePresent = true; } else if (object instanceof PersistentVolumeClaim) { PersistentVolumeClaim pvc = (PersistentVolumeClaim) object; pvcs.put(pvc.getMetadata().getName(), pvc); } else { throw new ValidationException( - String.format("Found unknown object type '%s'", object.getMetadata())); + format("Found unknown object type '%s'", object.getMetadata())); } } - OpenShiftEnvironment openShiftEnvironment = - new OpenShiftEnvironment() - .withPods(pods) - .withServices(services) - .withRoutes(routes) - .withPersistentVolumeClaims(pvcs); - normalizeEnvironment(openShiftEnvironment, environment); + Builder openShiftEnvBuilder = + OpenShiftEnvironment.builder() + .setPods(pods) + .setServices(services) + .setPersistentVolumeClaims(pvcs); - return openShiftEnvironment; + if (isAnyRoutePresent) { + environment.addWarning( + new WarningImpl(ROUTE_IGNORED_WARNING_CODE, ROUTES_IGNORED_WARNING_MESSAGE)); + } + + OpenShiftEnvironment openShiftEnv = openShiftEnvBuilder.build(); + + normalizeEnvironment(openShiftEnv, environment); + + return openShiftEnv; } private void normalizeEnvironment( diff --git a/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/ServerExposerTest.java b/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/ServerExposerTest.java index 767138ca22..20465ca555 100644 --- a/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/ServerExposerTest.java +++ b/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/ServerExposerTest.java @@ -39,6 +39,7 @@ import org.testng.annotations.Test; * @author Sergii Leshchenko */ public class ServerExposerTest { + private static final String SERVER_PREFIX = "server"; private ServerExposer serverExposer; @@ -58,7 +59,8 @@ public class ServerExposerTest { .endSpec() .build(); - openShiftEnvironment = new OpenShiftEnvironment().withPods(ImmutableMap.of("pod", pod)); + openShiftEnvironment = + OpenShiftEnvironment.builder().setPods(ImmutableMap.of("pod", pod)).build(); this.serverExposer = new ServerExposer("pod/main", container, openShiftEnvironment); } diff --git a/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/environment/OpenShiftEnvironmentParserTest.java b/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/environment/OpenShiftEnvironmentParserTest.java index 3475eaa1b0..e12aaf346c 100644 --- a/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/environment/OpenShiftEnvironmentParserTest.java +++ b/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/environment/OpenShiftEnvironmentParserTest.java @@ -11,6 +11,7 @@ package org.eclipse.che.workspace.infrastructure.openshift.environment; import static java.lang.String.format; +import static java.util.Arrays.*; import static java.util.Collections.singletonList; import static org.eclipse.che.workspace.infrastructure.openshift.environment.OpenShiftEnvironmentParser.DEFAULT_RESTART_POLICY; import static org.mockito.Matchers.any; @@ -18,6 +19,7 @@ import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; import io.fabric8.kubernetes.api.model.Container; @@ -32,6 +34,7 @@ import io.fabric8.kubernetes.api.model.PodSpec; import io.fabric8.kubernetes.api.model.PodSpecBuilder; import io.fabric8.kubernetes.client.dsl.KubernetesListMixedOperation; import io.fabric8.kubernetes.client.dsl.RecreateFromServerGettable; +import io.fabric8.openshift.api.model.Route; import io.fabric8.openshift.client.OpenShiftClient; import java.io.InputStream; import java.util.Iterator; @@ -104,6 +107,20 @@ public class OpenShiftEnvironmentParserTest { ALWAYS_RESTART_POLICY, TEST_POD_NAME, DEFAULT_RESTART_POLICY))); } + @Test + public void ignoreRoutesWhenRecipeContainsThem() throws Exception { + final List objects = asList(new Route(), new Route()); + when(validatedObjects.getItems()).thenReturn(objects); + + final OpenShiftEnvironment parsed = osEnvironmentParser.parse(internalEnvironment); + + assertTrue(parsed.getRoutes().isEmpty()); + verifyWarnings( + new WarningImpl( + OpenShiftEnvironmentParser.ROUTE_IGNORED_WARNING_CODE, + OpenShiftEnvironmentParser.ROUTES_IGNORED_WARNING_MESSAGE)); + } + private void verifyWarnings(Warning... expectedWarnings) { final Iterator actualWarnings = captureWarnings().iterator(); for (Warning expected : expectedWarnings) {