-
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.
Merge pull request #3282 from amisevsk/CHE-2030-refactor
Refactor DockerInstanceRuntimeInfo#getServers() (#2030)
- Loading branch information
Showing
16 changed files
with
1,506 additions
and
509 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
96 changes: 96 additions & 0 deletions
96
.../src/main/java/org/eclipse/che/plugin/docker/machine/DefaultServerEvaluationStrategy.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,96 @@ | ||
/******************************************************************************* | ||
* 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 java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
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 com.google.inject.Inject; | ||
import com.google.inject.name.Named; | ||
import static com.google.common.base.Strings.isNullOrEmpty; | ||
|
||
|
||
/** | ||
* Represents the default server evaluation strategy. By default, calling | ||
* {@link ServerEvaluationStrategy#getServers(ContainerInfo, String, Map)} will return a completed | ||
* {@link ServerImpl} with internal and external address set to the address of the Docker host. | ||
* | ||
* <p>The addresses used for internal and external address can be overridden via the properties | ||
* {@code che.docker.ip} and {@code che.docker.ip.external}, respectively. | ||
* | ||
* @author Angel Misevski <[email protected]> | ||
* @see ServerEvaluationStrategy | ||
*/ | ||
public class DefaultServerEvaluationStrategy extends ServerEvaluationStrategy { | ||
|
||
/** | ||
* Used to store the address set by property {@code che.docker.ip}, if applicable. | ||
*/ | ||
protected String internalAddressProperty; | ||
|
||
/** | ||
* Used to store the address set by property {@code che.docker.ip.external}. if applicable. | ||
*/ | ||
protected String externalAddressProperty; | ||
|
||
@Inject | ||
public DefaultServerEvaluationStrategy (@Nullable @Named("che.docker.ip") String internalAddress, | ||
@Nullable @Named("che.docker.ip.external") String externalAddress) { | ||
this.internalAddressProperty = internalAddress; | ||
this.externalAddressProperty = externalAddress; | ||
} | ||
|
||
@Override | ||
protected Map<String, String> getInternalAddressesAndPorts(ContainerInfo containerInfo, String internalHost) { | ||
String internalAddressContainer = containerInfo.getNetworkSettings().getGateway(); | ||
|
||
String internalAddress = internalAddressProperty != null ? | ||
internalAddressProperty : | ||
!isNullOrEmpty(internalAddressContainer) ? | ||
internalAddressContainer : | ||
internalHost; | ||
|
||
Map<String, List<PortBinding>> portBindings = containerInfo.getNetworkSettings().getPorts(); | ||
|
||
return getAddressesAndPorts(internalAddress, portBindings); | ||
} | ||
|
||
@Override | ||
protected Map<String, String> getExternalAddressesAndPorts(ContainerInfo containerInfo, String internalHost) { | ||
String externalAddressContainer = containerInfo.getNetworkSettings().getGateway(); | ||
|
||
String externalAddress = externalAddressProperty != null ? | ||
externalAddressProperty : | ||
!isNullOrEmpty(externalAddressContainer) ? | ||
externalAddressContainer : | ||
internalHost; | ||
|
||
Map<String, List<PortBinding>> portBindings = containerInfo.getNetworkSettings().getPorts(); | ||
|
||
return getAddressesAndPorts(externalAddress, portBindings); | ||
} | ||
|
||
private Map<String, String> getAddressesAndPorts(String address, Map<String, List<PortBinding>> ports) { | ||
Map<String, String> addressesAndPorts = new HashMap<>(); | ||
for (String portKey : ports.keySet()) { | ||
String port = ports.get(portKey).get(0).getHostPort(); | ||
addressesAndPorts.put(portKey, address + ":" + port); | ||
} | ||
return addressesAndPorts; | ||
} | ||
} |
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
Oops, something went wrong.