Hostname configuration of workspace routes (#19148)

* Hostname configuration of workspace routes

Signed-off-by: Sergii Kabashniuk <skabashniuk@redhat.com>
7.28.x
Sergii Kabashniuk 2021-03-02 16:38:13 +02:00 committed by GitHub
parent 2263afd453
commit a01e371268
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 58 additions and 3 deletions

View File

@ -539,6 +539,11 @@ che.infra.openshift.trusted_ca.dest_configmap_labels=config.openshift.io/inject-
# to allow clear identification.
che.infra.openshift.route.labels=NULL
# The hostname that should be used as a suffix for the workspace routes.
# For example host=open.che.org then the route will look like routed3qrtk.open.che.org
# It has to be a valid DNS name.
che.infra.openshift.route.host.domain_suffix=NULL
### Experimental properties
# Next properties are subject to changes and removal, so do not rely on them in a stable Che assembly

View File

@ -94,6 +94,10 @@
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-commons-inject</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-commons-lang</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-commons-tracing</artifactId>

View File

@ -26,6 +26,7 @@ import javax.inject.Named;
import javax.inject.Singleton;
import org.eclipse.che.api.core.model.workspace.config.ServerConfig;
import org.eclipse.che.commons.annotation.Nullable;
import org.eclipse.che.commons.lang.NameGenerator;
import org.eclipse.che.workspace.infrastructure.kubernetes.Annotations;
import org.eclipse.che.workspace.infrastructure.kubernetes.Names;
import org.eclipse.che.workspace.infrastructure.kubernetes.server.external.ExternalServerExposer;
@ -99,14 +100,17 @@ import org.eclipse.che.workspace.infrastructure.openshift.environment.OpenShiftE
public class RouteServerExposer implements ExternalServerExposer<OpenShiftEnvironment> {
private final Map<String, String> labels;
private final String domainSuffix;
@Inject
public RouteServerExposer(
@Nullable @Named("che.infra.openshift.route.labels") String labelsProperty) {
@Nullable @Named("che.infra.openshift.route.labels") String labelsProperty,
@Nullable @Named("che.infra.openshift.route.host.domain_suffix") String domainSuffix) {
this.labels =
labelsProperty != null
? Splitter.on(",").withKeyValueSeparator("=").split(labelsProperty)
: emptyMap();
this.domainSuffix = domainSuffix;
}
@Override
@ -124,6 +128,10 @@ public class RouteServerExposer implements ExternalServerExposer<OpenShiftEnviro
.withTargetPort(servicePort.getName())
.withServers(externalServers)
.withLabels(labels)
.withHost(
domainSuffix != null
? NameGenerator.generate("route", "." + domainSuffix, 6)
: null)
.withTo(serviceName)
.build();
env.getRoutes().put(commonRoute.getMetadata().getName(), commonRoute);
@ -132,6 +140,7 @@ public class RouteServerExposer implements ExternalServerExposer<OpenShiftEnviro
private static class RouteBuilder {
private String name;
private String host;
private String serviceName;
private IntOrString targetPort;
private Map<String, ServerConfig> servers;
@ -148,6 +157,11 @@ public class RouteServerExposer implements ExternalServerExposer<OpenShiftEnviro
return this;
}
private RouteBuilder withHost(String host) {
this.host = host;
return this;
}
private RouteBuilder withTargetPort(String targetPortName) {
this.targetPort = new IntOrString(targetPortName);
return this;
@ -189,6 +203,7 @@ public class RouteServerExposer implements ExternalServerExposer<OpenShiftEnviro
.withLabels(labels)
.endMetadata()
.withNewSpec()
.withHost(host)
.withNewTo()
.withName(serviceName)
.endTo()

View File

@ -13,6 +13,8 @@ package org.eclipse.che.workspace.infrastructure.openshift.server;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import io.fabric8.kubernetes.api.model.ServicePort;
import io.fabric8.openshift.api.model.Route;
@ -33,7 +35,7 @@ import org.testng.annotations.Test;
public class OpenShiftExternalServerExposerTest {
private static final String LABELS = "foo=bar";
private RouteServerExposer osExternalServerExposer = new RouteServerExposer(LABELS);
private RouteServerExposer osExternalServerExposer = new RouteServerExposer(LABELS, null);
@Test
public void shouldAddRouteToEnvForExposingSpecifiedServer() {
@ -63,5 +65,34 @@ public class OpenShiftExternalServerExposerTest {
assertEquals(annotations.machineName(), "machine123");
assertEquals(annotations.servers(), servers);
assertEquals(route.getMetadata().getLabels().get("foo"), "bar");
assertNull(route.getSpec().getHost());
}
@Test
public void shouldAddRouteToEnvForExposingSpecifiedServerWithSpecificHost() {
// given
RouteServerExposer osExternalServerExposer = new RouteServerExposer(LABELS, "open.che.org");
OpenShiftEnvironment osEnv = OpenShiftEnvironment.builder().build();
Map<String, ServerConfig> servers = new HashMap<>();
servers.put("server", new ServerConfigImpl());
// when
osExternalServerExposer.expose(
osEnv,
"machine123",
"service123",
null,
new ServicePort("servicePort", null, null, "TCP", null),
servers);
// then
assertEquals(1, osEnv.getRoutes().size());
Route route = osEnv.getRoutes().values().iterator().next();
assertNotNull(route);
assertEquals(route.getSpec().getTo().getName(), "service123");
assertEquals(route.getSpec().getPort().getTargetPort().getStrVal(), "servicePort");
assertTrue(route.getSpec().getHost().endsWith(".open.che.org"));
assertTrue(route.getSpec().getHost().startsWith("route"));
}
}

View File

@ -43,7 +43,7 @@ public class OpenShiftPreviewUrlExposerTest {
@BeforeMethod
public void setUp() {
RouteServerExposer externalServerExposer = new RouteServerExposer("a=b");
RouteServerExposer externalServerExposer = new RouteServerExposer("a=b", null);
previewUrlEndpointsProvisioner = new OpenShiftPreviewUrlExposer(externalServerExposer);
}