-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Che server and workpaces exposed on the same single TCP port (#4351)
Signed-off-by: Mario Loriedo <mloriedo@redhat.com>
- Loading branch information
Showing
10 changed files
with
334 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
.../org/eclipse/che/plugin/docker/machine/LocalDockerSinglePortServerEvaluationStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2016-2017 Red Hat Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.che.plugin.docker.machine; | ||
|
||
import com.google.inject.Inject; | ||
import com.google.inject.name.Named; | ||
import org.eclipse.che.api.machine.server.model.impl.ServerConfImpl; | ||
import org.eclipse.che.api.machine.server.model.impl.ServerImpl; | ||
import org.eclipse.che.commons.annotation.Nullable; | ||
import org.eclipse.che.plugin.docker.client.json.ContainerInfo; | ||
import org.eclipse.che.plugin.docker.client.json.PortBinding; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
|
||
/** | ||
* Represents a server evaluation strategy for the configuration where the workspace server and workspace | ||
* containers are running on the same Docker network and are exposed through the same single port. | ||
* | ||
* This server evaluation strategy will return a completed {@link ServerImpl} with internal addresses set | ||
* as {@link LocalDockerServerEvaluationStrategy} does. Contrarily external addresses will have the following format: | ||
* | ||
* serverName.workspaceID.cheExternalAddress (e.g. terminal.79rfwhqaztq2ru2k.che.local) | ||
* | ||
* <p>cheExternalAddress can be set using property {@code che.docker.ip.external}. | ||
* This strategy is useful when Che and the workspace servers need to be exposed on the same single TCP port | ||
* | ||
* @author Mario Loriedo <mloriedo@redhat.com> | ||
* @see ServerEvaluationStrategy | ||
*/ | ||
public class LocalDockerSinglePortServerEvaluationStrategy extends LocalDockerServerEvaluationStrategy { | ||
|
||
private static final String CHE_WORKSPACE_ID_ENV_VAR = "CHE_WORKSPACE_ID"; | ||
|
||
@Inject | ||
public LocalDockerSinglePortServerEvaluationStrategy(@Nullable @Named("che.docker.ip") String internalAddress, | ||
@Nullable @Named("che.docker.ip.external") String externalAddress) { | ||
super(internalAddress, externalAddress); | ||
} | ||
|
||
@Override | ||
protected Map<String, String> getExternalAddressesAndPorts(ContainerInfo containerInfo, | ||
String internalHost) { | ||
String cheExternalAddress = getCheExternalAddress(containerInfo, internalHost); | ||
String workspaceID = getWorkspaceID(containerInfo.getConfig().getEnv()); | ||
Map<String, String> labels= containerInfo.getConfig().getLabels(); | ||
|
||
Map<String, List<PortBinding>> portBindings = containerInfo.getNetworkSettings().getPorts(); | ||
Map<String, String> addressesAndPorts = new HashMap<>(); | ||
for (String serverKey : portBindings.keySet()) { | ||
String serverName = getWorkspaceServerName(labels, serverKey); | ||
String serverURL = serverName + "." + workspaceID + "." + cheExternalAddress; | ||
addressesAndPorts.put(serverKey, serverURL); | ||
} | ||
|
||
return addressesAndPorts; | ||
} | ||
|
||
private String getWorkspaceServerName(Map<String, String> labels, String portKey) { | ||
ServerConfImpl serverConf = getServerConfImpl(portKey, labels, new HashMap<>()); | ||
if (serverConf == null) { | ||
return "server-" + portKey.split("/",2)[0]; | ||
} | ||
return serverConf.getRef(); | ||
} | ||
|
||
private String getWorkspaceID(String[] env) { | ||
Stream<String> envStream = Arrays.stream(env); | ||
return envStream.filter(v -> v.startsWith(CHE_WORKSPACE_ID_ENV_VAR) && v.contains("=")). | ||
map(v -> v.split("=",2)[1]). | ||
findFirst(). | ||
orElse("unknown-ws"). | ||
replaceFirst("workspace",""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
.../eclipse/che/plugin/docker/machine/LocalDockerSinglePortServerEvaluationStrategyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2016-2017 Red Hat Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.che.plugin.docker.machine; | ||
|
||
import org.eclipse.che.api.core.model.machine.MachineConfig; | ||
import org.eclipse.che.api.machine.server.model.impl.ServerConfImpl; | ||
import org.eclipse.che.api.machine.server.model.impl.ServerImpl; | ||
import org.eclipse.che.api.machine.server.model.impl.ServerPropertiesImpl; | ||
import org.eclipse.che.plugin.docker.client.json.ContainerConfig; | ||
import org.eclipse.che.plugin.docker.client.json.ContainerInfo; | ||
import org.eclipse.che.plugin.docker.client.json.NetworkSettings; | ||
import org.eclipse.che.plugin.docker.client.json.PortBinding; | ||
import org.mockito.Mock; | ||
import org.mockito.testng.MockitoTestNGListener; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Listeners; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.mockito.Mockito.when; | ||
import static org.testng.Assert.assertEquals; | ||
|
||
@Listeners(MockitoTestNGListener.class) | ||
public class LocalDockerSinglePortServerEvaluationStrategyTest { | ||
|
||
private static final String CHE_DOCKER_IP_EXTERNAL = "container-host-ext.com"; | ||
private static final String ALL_IP_ADDRESS = "0.0.0.0"; | ||
private static final String CONTAINERCONFIG_HOSTNAME = "che-ws-y6jwknht0efzczit-4086112300-fm0aj"; | ||
private static final String WORKSPACE_ID = "79rfwhqaztq2ru2k"; | ||
|
||
@Mock | ||
private ContainerInfo containerInfo; | ||
@Mock | ||
private ContainerConfig containerConfig; | ||
@Mock | ||
private NetworkSettings networkSettings; | ||
|
||
private ServerEvaluationStrategy strategy; | ||
|
||
private Map<String, ServerConfImpl> serverConfs; | ||
|
||
private Map<String, List<PortBinding>> ports; | ||
|
||
private Map<String, String> labels; | ||
|
||
private String[] env; | ||
|
||
@BeforeMethod | ||
public void setUp() { | ||
|
||
serverConfs = new HashMap<>(); | ||
serverConfs.put("4301/tcp", new ServerConfImpl("sysServer1-tcp", "4301/tcp", "http", "/some/path1")); | ||
serverConfs.put("4305/udp", new ServerConfImpl("devSysServer1-udp", "4305/udp", null, "some/path4")); | ||
|
||
ports = new HashMap<>(); | ||
ports.put("4301/tcp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) | ||
.withHostPort("32100"))); | ||
ports.put("4305/udp", Collections.singletonList(new PortBinding().withHostIp(ALL_IP_ADDRESS ) | ||
.withHostPort("32103"))); | ||
|
||
labels = new HashMap<>(); | ||
labels.put("che:server:4301/tcp:ref", "sysServer1-tcp"); | ||
labels.put("che:server:4305/udp:ref", "devSysServer1-udp"); | ||
|
||
env = new String[]{"CHE_WORKSPACE_ID="+ WORKSPACE_ID}; | ||
|
||
when(containerInfo.getNetworkSettings()).thenReturn(networkSettings); | ||
when(networkSettings.getIpAddress()).thenReturn(CONTAINERCONFIG_HOSTNAME); | ||
when(networkSettings.getPorts()).thenReturn(ports); | ||
when(containerInfo.getConfig()).thenReturn(containerConfig); | ||
when(containerConfig.getHostname()).thenReturn(CONTAINERCONFIG_HOSTNAME); | ||
when(containerConfig.getEnv()).thenReturn(env); | ||
when(containerConfig.getLabels()).thenReturn(labels); | ||
} | ||
|
||
/** | ||
* Test: single port strategy should use . | ||
* @throws Exception | ||
*/ | ||
@Test | ||
public void shouldUseServerRefToBuildAddressWhenAvailable() throws Exception { | ||
// given | ||
strategy = new LocalDockerSinglePortServerEvaluationStrategy(null, null); | ||
|
||
final Map<String, ServerImpl> expectedServers = getExpectedServers(CHE_DOCKER_IP_EXTERNAL, | ||
CONTAINERCONFIG_HOSTNAME, | ||
true); | ||
|
||
// when | ||
final Map<String, ServerImpl> servers = strategy.getServers(containerInfo, | ||
CHE_DOCKER_IP_EXTERNAL, | ||
serverConfs); | ||
|
||
// then | ||
assertEquals(servers, expectedServers); | ||
} | ||
|
||
private Map<String, ServerImpl> getExpectedServers(String externalAddress, | ||
String internalAddress, | ||
boolean useExposedPorts) { | ||
String port1; | ||
String port2; | ||
if (useExposedPorts) { | ||
port1 = ":4301"; | ||
port2 = ":4305"; | ||
} else { | ||
port1 = ":32100"; | ||
port2 = ":32103"; | ||
} | ||
Map<String, ServerImpl> expectedServers = new HashMap<>(); | ||
expectedServers.put("4301/tcp", new ServerImpl("sysServer1-tcp", | ||
"http", | ||
"sysServer1-tcp." + WORKSPACE_ID + "." + externalAddress, | ||
"http://" + "sysServer1-tcp." + WORKSPACE_ID + "." + externalAddress + "/some/path1", | ||
new ServerPropertiesImpl("/some/path1", | ||
internalAddress + port1, | ||
"http://" + internalAddress + port1 + "/some/path1"))); | ||
expectedServers.put("4305/udp", new ServerImpl("devSysServer1-udp", | ||
null, | ||
"devSysServer1-udp." + WORKSPACE_ID + "." + externalAddress, | ||
null, | ||
new ServerPropertiesImpl("some/path4", | ||
internalAddress + port2, | ||
null))); | ||
return expectedServers; | ||
} | ||
|
||
} |
Oops, something went wrong.