-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IDEX-3244 Allow to detect the ip to use for the container host. And t…
…hen add this container host as an extra host when starting a container Change-Id: I953539a7a29e257d89a6604bfe9efb885965e508 Signed-off-by: Florent BENOIT <fbenoit@codenvy.com>
- Loading branch information
Showing
9 changed files
with
455 additions
and
12 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
99 changes: 99 additions & 0 deletions
99
...lient/src/main/java/org/eclipse/che/plugin/docker/client/helper/DefaultNetworkFinder.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,99 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2012-2015 Codenvy, S.A. | ||
* 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: | ||
* Codenvy, S.A. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.che.plugin.docker.client.helper; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.inject.Singleton; | ||
import java.net.Inet4Address; | ||
import java.net.InetAddress; | ||
import java.net.NetworkInterface; | ||
import java.net.SocketException; | ||
import java.util.Enumeration; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Default Implementation of the {@link NetworkFinder} | ||
* | ||
* @author Florent Benoit | ||
*/ | ||
@Singleton | ||
public class DefaultNetworkFinder implements NetworkFinder { | ||
|
||
/** | ||
* Logger. | ||
*/ | ||
private static final Logger LOG = LoggerFactory.getLogger(DefaultNetworkFinder.class); | ||
|
||
/** | ||
* Gets the first inet address of a given network interface if it's found | ||
* | ||
* @param bridgeName | ||
* name of the network interface | ||
* @return only ipv4 ip of the given bridge | ||
*/ | ||
@Override | ||
public Optional<InetAddress> getIPAddress(String bridgeName) { | ||
|
||
NetworkInterface docker0 = null; | ||
try { | ||
docker0 = NetworkInterface.getByName(bridgeName); | ||
} catch (SocketException e) { | ||
LOG.error("Unable to list the network interfaces", e); | ||
} | ||
|
||
if (docker0 != null) { | ||
// first ipv4 ip | ||
Enumeration<InetAddress> addresses = docker0.getInetAddresses(); | ||
while (addresses.hasMoreElements()) { | ||
InetAddress inetAddress = addresses.nextElement(); | ||
if (inetAddress instanceof Inet4Address) { | ||
return Optional.of(inetAddress); | ||
} | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
/** | ||
* Search if a given network interface is matching the given subnet | ||
* | ||
* @param subnet | ||
* the first digits of an ip address. Like 123.123.123 | ||
* @return optional ipv4 internet address if there was a matching one | ||
*/ | ||
@Override | ||
public Optional<InetAddress> foundInetAddressMatching(String subnet) { | ||
|
||
Enumeration<NetworkInterface> interfacesEnum = null; | ||
try { | ||
interfacesEnum = NetworkInterface.getNetworkInterfaces(); | ||
} catch (SocketException e) { | ||
LOG.error("Unable to list the network interfaces", e); | ||
return Optional.empty(); | ||
} | ||
|
||
while (interfacesEnum.hasMoreElements()) { | ||
NetworkInterface networkInterface = interfacesEnum.nextElement(); | ||
Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses(); | ||
while (inetAddressEnumeration.hasMoreElements()) { | ||
InetAddress inetAddress = inetAddressEnumeration.nextElement(); | ||
if (inetAddress instanceof Inet4Address) { | ||
if (inetAddress.getHostAddress().startsWith(subnet)) { | ||
return Optional.of(inetAddress); | ||
} | ||
} | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...ocker-client/src/main/java/org/eclipse/che/plugin/docker/client/helper/NetworkFinder.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,43 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2012-2015 Codenvy, S.A. | ||
* 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: | ||
* Codenvy, S.A. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.che.plugin.docker.client.helper; | ||
|
||
import com.google.inject.ImplementedBy; | ||
|
||
import java.net.InetAddress; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Provides helper method for the network | ||
* | ||
* @author Florent Benoit | ||
*/ | ||
@ImplementedBy(DefaultNetworkFinder.class) | ||
public interface NetworkFinder { | ||
|
||
/** | ||
* Gets the first inet address of a given network interface | ||
* | ||
* @param bridgeName | ||
* name of the network interface | ||
* @return only ipv4 ip of the given bridge if found | ||
*/ | ||
Optional<InetAddress> getIPAddress(String bridgeName); | ||
|
||
/** | ||
* Search if a given network interface is matching the given subnet | ||
* | ||
* @param subnet | ||
* the first digits of an ip address. Like 123.123.123 | ||
* @return optional ipv4 internet address if there was a matching one | ||
*/ | ||
Optional<InetAddress> foundInetAddressMatching(String subnet); | ||
} |
Oops, something went wrong.