Skip to content

Commit 39fa37f

Browse files
l0rdAlexander Garagatyi
authored andcommitted
Improvements to set the right API endpoint URL (eclipse-che#1576)
Read env variable CHE_API_ENDPOINT if property machine.docker.che_api.endpoint is not set Set Che host IP to eth0 default IP when docker0 is not found on linux Fixing typos Signed-off-by: Mario Loriedo <mloriedo@redhat.com>
1 parent dfebc20 commit 39fa37f

9 files changed

Lines changed: 74 additions & 21 deletions

File tree

plugins/plugin-docker/che-plugin-docker-client/src/main/java/org/eclipse/che/plugin/docker/client/DockerConnectorConfiguration.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@
3737
*/
3838
public class DockerConnectorConfiguration {
3939
/**
40-
* Docker bridge name on Linux.
40+
* Default Docker bridge name on Linux.
4141
*/
4242
protected static final String BRIDGE_LINUX_INTERFACE_NAME = "docker0";
4343

44+
/**
45+
* Default network interface name (Linux system).
46+
*/
47+
protected static final String DEFAULT_LINUX_INTERFACE_NAME = "eth0";
48+
4449
/**
4550
* Default ip of docker host (Linux system).
4651
*/
@@ -249,9 +254,14 @@ protected static URI dockerDaemonUri(final boolean isLinux, @NotNull final Map<S
249254
protected String getDockerHostIp(final boolean isLinux, @NotNull final Map<String, String> env) {
250255
if (isLinux) {
251256
// search "docker0" bridge
252-
Optional<InetAddress> dockerHostInetAddress = networkFinder.getIPAddress(BRIDGE_LINUX_INTERFACE_NAME);
253-
if (dockerHostInetAddress.isPresent()) {
254-
return dockerHostInetAddress.get().getHostAddress();
257+
Optional<InetAddress> dockerBridgeInetAddress = networkFinder.getIPAddress(BRIDGE_LINUX_INTERFACE_NAME);
258+
if (dockerBridgeInetAddress.isPresent()) {
259+
return dockerBridgeInetAddress.get().getHostAddress();
260+
}
261+
// Che server is probably running in a Docker container: get its internal IP
262+
Optional<InetAddress> cheServerInetAddress = networkFinder.getIPAddress(DEFAULT_LINUX_INTERFACE_NAME);
263+
if (cheServerInetAddress.isPresent()) {
264+
return cheServerInetAddress.get().getHostAddress();
255265
}
256266
// return default Docker host ip address
257267
return DEFAULT_LINUX_DOCKER_HOST_IP;

plugins/plugin-docker/che-plugin-docker-client/src/main/java/org/eclipse/che/plugin/docker/client/connection/DockerConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ protected abstract DockerResponse request(String method,
9999
String path,
100100
String query,
101101
List<Pair<String, ?>> headers,
102-
Entity entity) throws IOException;
102+
Entity<?> entity) throws IOException;
103103

104104
public abstract void close();
105105

plugins/plugin-docker/che-plugin-docker-client/src/main/java/org/eclipse/che/plugin/docker/client/connection/TcpConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public TcpConnection(URI baseUri, DockerCertificates certificates, int connectio
5050
}
5151

5252
@Override
53-
protected DockerResponse request(String method, String path, String query, List<Pair<String, ?>> headers, Entity entity)
53+
protected DockerResponse request(String method, String path, String query, List<Pair<String, ?>> headers, Entity<?> entity)
5454
throws IOException {
5555
final String requestUri = path + (Strings.isNullOrEmpty(query) ? "" : "?" + query);
5656
final URL url = baseUri.resolve(requestUri).toURL();

plugins/plugin-docker/che-plugin-docker-client/src/main/java/org/eclipse/che/plugin/docker/client/connection/UnixSocketConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public UnixSocketConnection(String dockerSocketPath) {
4444
}
4545

4646
@Override
47-
protected DockerResponse request(String method, String path, String query, List<Pair<String, ?>> headers, Entity entity)
47+
protected DockerResponse request(String method, String path, String query, List<Pair<String, ?>> headers, Entity<?> entity)
4848
throws IOException {
4949
fd = connect();
5050
final OutputStream output = new BufferedOutputStream(openOutputStream(fd));

plugins/plugin-docker/che-plugin-docker-client/src/test/java/org/eclipse/che/plugin/docker/client/DockerConnectorConfigurationTest.java

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626

2727
import static java.util.Collections.emptyMap;
2828
import static org.eclipse.che.plugin.docker.client.DockerConnectorConfiguration.BRIDGE_LINUX_INTERFACE_NAME;
29+
import static org.eclipse.che.plugin.docker.client.DockerConnectorConfiguration.DEFAULT_LINUX_INTERFACE_NAME;
2930
import static org.eclipse.che.plugin.docker.client.DockerConnectorConfiguration.DEFAULT_DOCKER_MACHINE_DOCKER_HOST_IP;
3031
import static org.eclipse.che.plugin.docker.client.DockerConnectorConfiguration.DEFAULT_LINUX_DOCKER_HOST_IP;
3132
import static org.mockito.Matchers.anyString;
3233
import static org.mockito.Mockito.doReturn;
3334
import static org.mockito.Mockito.times;
35+
import static org.mockito.Mockito.never;
3436
import static org.mockito.Mockito.verify;
3537
import static org.testng.Assert.assertEquals;
3638

@@ -185,18 +187,42 @@ public void testDockerUriOnNonLinuxMissingProperties() throws Exception {
185187
}
186188

187189
/**
188-
* Check if docker host ip from container is DEFAULT_LINUX_DOCKER_HOST_IP when bridge is not defined
190+
* Check if Che server IP from container is DEFAULT_LINUX_DOCKER_HOST_IP
191+
* when bridge docker0 and interface eth0 are not defined
189192
*/
190193
@Test
191-
public void testLinuxDefaultDockerHostWithoutBridge() throws Exception {
194+
public void testLinuxDefaultDockerHostWithoutInterfaces() throws Exception {
192195
Map<String, String> env = new HashMap<>();
193196
NetworkFinder networkFinder = Mockito.mock(NetworkFinder.class);
194197
doReturn(Optional.empty()).when(networkFinder).getIPAddress(BRIDGE_LINUX_INTERFACE_NAME);
198+
doReturn(Optional.empty()).when(networkFinder).getIPAddress(DEFAULT_LINUX_INTERFACE_NAME);
195199
DockerConnectorConfiguration dockerConnectorConfiguration = new DockerConnectorConfiguration(null, null, null, networkFinder);
196200

197201
String ip = dockerConnectorConfiguration.getDockerHostIp(true, env);
198202
assertEquals(ip, DEFAULT_LINUX_DOCKER_HOST_IP);
199203
verify(networkFinder).getIPAddress(BRIDGE_LINUX_INTERFACE_NAME);
204+
verify(networkFinder).getIPAddress(DEFAULT_LINUX_INTERFACE_NAME);
205+
}
206+
207+
208+
/**
209+
* Check if Che server IP from container is eth0 default IP when bridge docker0 is not defined
210+
*/
211+
@Test
212+
public void testLinuxDefaultDockerHostWithEth0() throws Exception {
213+
Map<String, String> env = new HashMap<>();
214+
String eth0IpAddress = "172.17.0.2";
215+
InetAddress eth0InetAddress = Mockito.mock(InetAddress.class);
216+
doReturn(eth0IpAddress).when(eth0InetAddress).getHostAddress();
217+
NetworkFinder networkFinder = Mockito.mock(NetworkFinder.class);
218+
doReturn(Optional.empty()).when(networkFinder).getIPAddress(BRIDGE_LINUX_INTERFACE_NAME);
219+
doReturn(Optional.of(eth0InetAddress)).when(networkFinder).getIPAddress(DEFAULT_LINUX_INTERFACE_NAME);
220+
DockerConnectorConfiguration dockerConnectorConfiguration = new DockerConnectorConfiguration(null, null, null, networkFinder);
221+
222+
String ip = dockerConnectorConfiguration.getDockerHostIp(true, env);
223+
assertEquals(ip, eth0IpAddress);
224+
verify(networkFinder).getIPAddress(BRIDGE_LINUX_INTERFACE_NAME);
225+
verify(networkFinder).getIPAddress(DEFAULT_LINUX_INTERFACE_NAME);
200226
}
201227

202228

@@ -206,16 +232,23 @@ public void testLinuxDefaultDockerHostWithoutBridge() throws Exception {
206232
@Test
207233
public void testLinuxDefaultDockerHostWithBrige() throws Exception {
208234
Map<String, String> env = new HashMap<>();
209-
String myCustomIpAddress = "123.231.133.10";
210-
InetAddress inetAddress = Mockito.mock(InetAddress.class);
211-
doReturn(myCustomIpAddress).when(inetAddress).getHostAddress();
212235
NetworkFinder networkFinder = Mockito.mock(NetworkFinder.class);
213-
doReturn(Optional.of(inetAddress)).when(networkFinder).getIPAddress(BRIDGE_LINUX_INTERFACE_NAME);
236+
// eth0
237+
String eth0IpAddress = "172.17.0.2";
238+
InetAddress eth0InetAddress = Mockito.mock(InetAddress.class);
239+
doReturn(eth0IpAddress).when(eth0InetAddress).getHostAddress();
240+
doReturn(Optional.of(eth0InetAddress)).when(networkFinder).getIPAddress(DEFAULT_LINUX_INTERFACE_NAME);
241+
// docker0
242+
String docker0IpAddress = "123.231.133.10";
243+
InetAddress docker0InetAddress = Mockito.mock(InetAddress.class);
244+
doReturn(docker0IpAddress).when(docker0InetAddress).getHostAddress();
245+
doReturn(Optional.of(docker0InetAddress)).when(networkFinder).getIPAddress(BRIDGE_LINUX_INTERFACE_NAME);
214246
DockerConnectorConfiguration dockerConnectorConfiguration = new DockerConnectorConfiguration(null, null, null, networkFinder);
215247

216248
String ip = dockerConnectorConfiguration.getDockerHostIp(true, env);
217-
assertEquals(ip, myCustomIpAddress);
249+
assertEquals(ip, docker0IpAddress);
218250
verify(networkFinder).getIPAddress(BRIDGE_LINUX_INTERFACE_NAME);
251+
verify(networkFinder, never()).getIPAddress(DEFAULT_LINUX_INTERFACE_NAME);
219252
}
220253

221254

plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/DockerInstanceProvider.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
import java.io.IOException;
6262
import java.io.InputStream;
6363
import java.net.HttpURLConnection;
64+
import java.net.URI;
65+
import java.net.URISyntaxException;
6466
import java.net.URL;
6567
import java.nio.file.Files;
6668
import java.util.ArrayList;
@@ -193,12 +195,13 @@ public DockerInstanceProvider(DockerConnector docker,
193195
envVariablesForDevMachine.addAll(devMachineEnvVariables);
194196
this.devMachineEnvVariables = envVariablesForDevMachine;
195197

196-
// always add the docker host
197-
String dockerHost = DockerInstanceRuntimeInfo.CHE_HOST.concat(":").concat(dockerConnectorConfiguration.getDockerHostIp());
198+
// always add Che server to hosts list
199+
String cheHost = dockerConnectorConfiguration.getDockerHostIp();
200+
String cheHostAlias = DockerInstanceRuntimeInfo.CHE_HOST.concat(":").concat(cheHost);
198201
if (isNullOrEmpty(allMachinesExtraHosts)) {
199-
this.allMachinesExtraHosts = new String[] {dockerHost};
202+
this.allMachinesExtraHosts = new String[] {cheHostAlias};
200203
} else {
201-
this.allMachinesExtraHosts = ObjectArrays.concat(allMachinesExtraHosts.split(","), dockerHost);
204+
this.allMachinesExtraHosts = ObjectArrays.concat(allMachinesExtraHosts.split(","), cheHostAlias);
202205
}
203206
}
204207

plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/DockerMachineModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class DockerMachineModule extends AbstractModule {
2626
protected void configure() {
2727
bind(org.eclipse.che.plugin.docker.machine.cleaner.DockerContainerCleaner.class);
2828

29-
Multibinder<String> debMachineEnvVars = Multibinder.newSetBinder(binder(),
29+
Multibinder<String> devMachineEnvVars = Multibinder.newSetBinder(binder(),
3030
String.class,
3131
Names.named("machine.docker.dev_machine.machine_env"))
3232
.permitDuplicates();

plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/ext/provider/ApiEndpointEnvVariableProvider.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
import org.eclipse.che.plugin.docker.machine.DockerInstanceRuntimeInfo;
1414

15+
import com.google.common.base.Strings;
16+
1517
import javax.inject.Inject;
1618
import javax.inject.Named;
1719
import javax.inject.Provider;
@@ -30,6 +32,11 @@ public class ApiEndpointEnvVariableProvider implements Provider<String> {
3032

3133
@Override
3234
public String get() {
35+
String apiEndpointEnvVar = System.getenv(DockerInstanceRuntimeInfo.API_ENDPOINT_URL_VARIABLE);
36+
if (Strings.isNullOrEmpty(apiEndpoint) &&
37+
!Strings.isNullOrEmpty(apiEndpointEnvVar)) {
38+
apiEndpoint = apiEndpointEnvVar;
39+
}
3340
return DockerInstanceRuntimeInfo.API_ENDPOINT_URL_VARIABLE + '=' + apiEndpoint;
3441
}
3542
}

plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/local/LocalDockerModule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ protected void configure() {
6060

6161
bind(org.eclipse.che.plugin.docker.client.DockerRegistryChecker.class).asEagerSingleton();
6262

63-
Multibinder<String> debMachineEnvVars = Multibinder.newSetBinder(binder(),
63+
Multibinder<String> devMachineEnvVars = Multibinder.newSetBinder(binder(),
6464
String.class,
6565
Names.named("machine.docker.dev_machine.machine_env"))
6666
.permitDuplicates();
67-
debMachineEnvVars.addBinding()
67+
devMachineEnvVars.addBinding()
6868
.toProvider(org.eclipse.che.plugin.docker.machine.local.provider.DockerApiHostEnvVariableProvider.class);
6969

7070
install(new org.eclipse.che.plugin.docker.machine.DockerMachineModule());

0 commit comments

Comments
 (0)