CHE-6216 Ignore Routes that are specified in OpenShift recipe (#6259)

Estimated environment will contain warning when user specify
Route in OpenShift recipe
6.19.x
Sergii Leshchenko 2017-09-14 16:38:51 +03:00 committed by GitHub
parent 27667154a0
commit 36f6ce9aaf
4 changed files with 102 additions and 65 deletions

View File

@ -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<String, Pod> pods;
private Map<String, Service> services;
private Map<String, Route> routes;
private Map<String, PersistentVolumeClaim> persistentVolumeClaims;
public OpenShiftEnvironment() {}
private final Map<String, Pod> pods;
private final Map<String, Service> services;
private final Map<String, Route> routes;
private final Map<String, PersistentVolumeClaim> persistentVolumeClaims;
public static Builder builder() {
return new Builder();
}
private OpenShiftEnvironment(
Map<String, Pod> pods,
Map<String, Service> services,
Map<String, Route> routes,
Map<String, PersistentVolumeClaim> persistentVolumeClaims) {
this.pods = pods;
this.services = services;
this.routes = routes;
this.persistentVolumeClaims = persistentVolumeClaims;
}
/** Returns pods that should be created when environment starts. */
public Map<String, Pod> getPods() {
if (pods == null) {
pods = new HashMap<>();
}
return pods;
}
public void setPods(Map<String, Pod> pods) {
this.pods = pods;
}
public OpenShiftEnvironment withPods(Map<String, Pod> pods) {
this.pods = pods;
return this;
}
/** Returns services that should be created when environment starts. */
public Map<String, Service> getServices() {
if (services == null) {
services = new HashMap<>();
}
return services;
}
public void setServices(Map<String, Service> services) {
this.services = services;
}
public OpenShiftEnvironment withServices(Map<String, Service> services) {
this.services = services;
return this;
}
/** Returns services that should be created when environment starts. */
public Map<String, Route> getRoutes() {
if (routes == null) {
routes = new HashMap<>();
}
return routes;
}
public void setRoutes(Map<String, Route> routes) {
this.routes = routes;
}
public OpenShiftEnvironment withRoutes(Map<String, Route> routes) {
this.routes = routes;
return this;
}
/** Returns PVCs that should be created when environment starts. */
public Map<String, PersistentVolumeClaim> getPersistentVolumeClaims() {
if (persistentVolumeClaims == null) {
persistentVolumeClaims = new HashMap<>();
}
return persistentVolumeClaims;
}
public void setPersistentVolumeClaims(Map<String, PersistentVolumeClaim> persistentVolumeClaims) {
this.persistentVolumeClaims = persistentVolumeClaims;
}
public static class Builder {
private final Map<String, Pod> pods = new HashMap<>();
private final Map<String, Service> services = new HashMap<>();
private final Map<String, Route> routes = new HashMap<>();
private final Map<String, PersistentVolumeClaim> persistentVolumeClaims = new HashMap<>();
public OpenShiftEnvironment withPersistentVolumeClaims(
Map<String, PersistentVolumeClaim> persistentVolumeClaims) {
this.persistentVolumeClaims = persistentVolumeClaims;
return this;
private Builder() {}
public Builder setPods(Map<String, Pod> pods) {
this.pods.putAll(pods);
return this;
}
public Builder setServices(Map<String, Service> services) {
this.services.putAll(services);
return this;
}
public Builder setRoutes(Map<String, Route> route) {
this.routes.putAll(route);
return this;
}
public Builder setPersistentVolumeClaims(Map<String, PersistentVolumeClaim> pvcs) {
this.persistentVolumeClaims.putAll(pvcs);
return this;
}
public OpenShiftEnvironment build() {
return new OpenShiftEnvironment(pods, services, routes, persistentVolumeClaims);
}
}
}

View File

@ -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<String, Pod> pods = new HashMap<>();
Map<String, Service> services = new HashMap<>();
Map<String, Route> routes = new HashMap<>();
Map<String, PersistentVolumeClaim> 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(

View File

@ -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);
}

View File

@ -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<HasMetadata> 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<Warning> actualWarnings = captureWarnings().iterator();
for (Warning expected : expectedWarnings) {