use of org.eclipse.che.plugin.docker.client.json.network.Network in project che by eclipse.
the class DockerConnectorTest method shouldBeAbleToGetNetworksWithParams.
@Test
public void shouldBeAbleToGetNetworksWithParams() throws Exception {
// given
Network network = createNetwork();
List<Network> originNetworks = singletonList(network);
ByteArrayInputStream inputStream = new ByteArrayInputStream(GSON.toJson(originNetworks).getBytes());
doReturn(inputStream).when(dockerResponse).getInputStream();
GetNetworksParams getNetworksParams = GetNetworksParams.create().withFilters(new Filters().withFilter("key", "value1", "value2"));
// when
List<Network> actual = dockerConnector.getNetworks(getNetworksParams);
// then
assertEquals(actual, originNetworks);
verify(dockerConnectionFactory).openConnection(any(URI.class));
verify(dockerConnection).method(REQUEST_METHOD_GET);
verify(dockerConnection).path("/networks");
verify(dockerConnection).query(eq("filters"), anyObject());
verify(dockerConnection).request();
verify(dockerResponse).getStatus();
verify(dockerResponse).getInputStream();
}
use of org.eclipse.che.plugin.docker.client.json.network.Network in project che by eclipse.
the class DockerConnectorTest method shouldBeAbleToInspectNetwork.
@Test
public void shouldBeAbleToInspectNetwork() throws Exception {
// given
Network network = createNetwork();
doReturn(network).when(dockerConnector).inspectNetwork(any(InspectNetworkParams.class));
// when
dockerConnector.inspectNetwork(network.getId());
// then
verify(dockerConnector).inspectNetwork(InspectNetworkParams.create(network.getId()));
}
use of org.eclipse.che.plugin.docker.client.json.network.Network in project che by eclipse.
the class OpenShiftConnector method getNetworks.
/**
* In OpenShift, there is only one network in the Docker sense, and it is similar
* to the default bridge network. Rather than implementing all of the filters
* available in the Docker API, we only implement {@code type=["custom"|"builtin"]}.
*
* <p> If type is "custom", null is returned. Otherwise, the default network is returned,
* and the result is effectively the same as {@link DockerConnector#inspectNetwork(String)}
* where the network is "bridge".
*
* @see DockerConnector#getNetworks()
*/
@Override
public List<Network> getNetworks(GetNetworksParams params) throws IOException {
Filters filters = params.getFilters();
List<Network> networks = new ArrayList<>();
List<String> typeFilters = filters.getFilter("type");
if (typeFilters == null || !typeFilters.contains("custom")) {
Network network = inspectNetwork("openshift");
networks.add(network);
}
return networks;
}
use of org.eclipse.che.plugin.docker.client.json.network.Network in project che by eclipse.
the class OpenShiftConnector method inspectNetwork.
@Override
public Network inspectNetwork(InspectNetworkParams params) throws IOException {
String netId = params.getNetworkId();
ServiceList services = openShiftClient.services().inNamespace(this.openShiftCheProjectName).list();
Map<String, ContainerInNetwork> containers = new HashMap<>();
for (Service svc : services.getItems()) {
String selector = svc.getSpec().getSelector().get(OPENSHIFT_DEPLOYMENT_LABEL);
if (selector == null || !selector.startsWith(CHE_OPENSHIFT_RESOURCES_PREFIX)) {
continue;
}
PodList pods = openShiftClient.pods().inNamespace(openShiftCheProjectName).withLabel(OPENSHIFT_DEPLOYMENT_LABEL, selector).list();
for (Pod pod : pods.getItems()) {
String podName = pod.getMetadata().getName();
ContainerInNetwork container = new ContainerInNetwork().withName(podName).withIPv4Address(svc.getSpec().getClusterIP());
String podId = KubernetesStringUtils.getLabelFromContainerID(pod.getMetadata().getLabels().get(CHE_CONTAINER_IDENTIFIER_LABEL_KEY));
if (podId == null) {
continue;
}
containers.put(podId, container);
}
}
List<IpamConfig> ipamConfig = new ArrayList<>();
Ipam ipam = new Ipam().withDriver("bridge").withOptions(Collections.emptyMap()).withConfig(ipamConfig);
return new Network().withName("OpenShift").withId(netId).withContainers(containers).withLabels(Collections.emptyMap()).withOptions(Collections.emptyMap()).withDriver("default").withIPAM(ipam).withScope("local").withInternal(false).withEnableIPv6(false);
}
use of org.eclipse.che.plugin.docker.client.json.network.Network in project che by eclipse.
the class DockerAbandonedResourcesCleanerTest method shouldBeAbleToRemoveSeveralAbandonedNetworks.
@Test
public void shouldBeAbleToRemoveSeveralAbandonedNetworks() throws IOException {
// given
final Network abandonedNetwork2 = mock(Network.class);
final String abandonedNetwork2Id = "network2";
when(abandonedNetwork2.getId()).thenReturn(abandonedNetwork2Id);
when(abandonedNetwork2.getName()).thenReturn("workspace0w5kg95j93kd9a1l_cjmd8rbnf9j9dnso");
when(abandonedNetwork2.getContainers()).thenReturn(new HashMap<>());
final Network userNetwork = mock(Network.class);
final String userNetworkId = "network4";
when(userNetwork.getId()).thenReturn(userNetworkId);
when(userNetwork.getName()).thenReturn("userNetwork");
when(userNetwork.getContainers()).thenReturn(new HashMap<>());
usedNetworkContainers.put(containerId1, containerInNetwork1);
networks.add(usedNetwork);
networks.add(abandonedNetwork);
networks.add(abandonedNetwork2);
networks.add(userNetwork);
// when
cleaner.cleanNetworks();
// then
verify(dockerConnector, never()).removeNetwork(usedNetworkId);
verify(dockerConnector, never()).removeNetwork(userNetworkId);
verify(dockerConnector).removeNetwork(abandonedNetworkId);
verify(dockerConnector).removeNetwork(abandonedNetworkId);
}
Aggregations