Search in sources :

Example 1 with Network

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();
}
Also used : Filters(org.eclipse.che.plugin.docker.client.json.Filters) ByteArrayInputStream(java.io.ByteArrayInputStream) GetNetworksParams(org.eclipse.che.plugin.docker.client.params.network.GetNetworksParams) Network(org.eclipse.che.plugin.docker.client.json.network.Network) ContainerInNetwork(org.eclipse.che.plugin.docker.client.json.network.ContainerInNetwork) NewNetwork(org.eclipse.che.plugin.docker.client.json.network.NewNetwork) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 2 with Network

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()));
}
Also used : Network(org.eclipse.che.plugin.docker.client.json.network.Network) ContainerInNetwork(org.eclipse.che.plugin.docker.client.json.network.ContainerInNetwork) NewNetwork(org.eclipse.che.plugin.docker.client.json.network.NewNetwork) InspectNetworkParams(org.eclipse.che.plugin.docker.client.params.network.InspectNetworkParams) Test(org.testng.annotations.Test)

Example 3 with Network

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;
}
Also used : Filters(org.eclipse.che.plugin.docker.client.json.Filters) Network(org.eclipse.che.plugin.docker.client.json.network.Network) ContainerInNetwork(org.eclipse.che.plugin.docker.client.json.network.ContainerInNetwork) ArrayList(java.util.ArrayList)

Example 4 with Network

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);
}
Also used : PodList(io.fabric8.kubernetes.api.model.PodList) Pod(io.fabric8.kubernetes.api.model.Pod) HashMap(java.util.HashMap) ServiceList(io.fabric8.kubernetes.api.model.ServiceList) ArrayList(java.util.ArrayList) KubernetesService(org.eclipse.che.plugin.openshift.client.kubernetes.KubernetesService) Service(io.fabric8.kubernetes.api.model.Service) IpamConfig(org.eclipse.che.plugin.docker.client.json.network.IpamConfig) ContainerInNetwork(org.eclipse.che.plugin.docker.client.json.network.ContainerInNetwork) Ipam(org.eclipse.che.plugin.docker.client.json.network.Ipam) Network(org.eclipse.che.plugin.docker.client.json.network.Network) ContainerInNetwork(org.eclipse.che.plugin.docker.client.json.network.ContainerInNetwork)

Example 5 with Network

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);
}
Also used : Network(org.eclipse.che.plugin.docker.client.json.network.Network) ContainerInNetwork(org.eclipse.che.plugin.docker.client.json.network.ContainerInNetwork) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Aggregations

ContainerInNetwork (org.eclipse.che.plugin.docker.client.json.network.ContainerInNetwork)6 Network (org.eclipse.che.plugin.docker.client.json.network.Network)6 Test (org.testng.annotations.Test)4 NewNetwork (org.eclipse.che.plugin.docker.client.json.network.NewNetwork)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 Filters (org.eclipse.che.plugin.docker.client.json.Filters)2 Pod (io.fabric8.kubernetes.api.model.Pod)1 PodList (io.fabric8.kubernetes.api.model.PodList)1 Service (io.fabric8.kubernetes.api.model.Service)1 ServiceList (io.fabric8.kubernetes.api.model.ServiceList)1 HashMap (java.util.HashMap)1 Ipam (org.eclipse.che.plugin.docker.client.json.network.Ipam)1 IpamConfig (org.eclipse.che.plugin.docker.client.json.network.IpamConfig)1 GetNetworksParams (org.eclipse.che.plugin.docker.client.params.network.GetNetworksParams)1 InspectNetworkParams (org.eclipse.che.plugin.docker.client.params.network.InspectNetworkParams)1 KubernetesService (org.eclipse.che.plugin.openshift.client.kubernetes.KubernetesService)1 Matchers.anyString (org.mockito.Matchers.anyString)1