Search in sources :

Example 1 with ContainerConfig

use of org.eclipse.che.plugin.docker.client.json.ContainerConfig in project che by eclipse.

the class DockerProcessTest method setUp.

@BeforeMethod
public void setUp() throws Exception {
    dockerConnectorConfiguration = new DockerConnectorConfiguration(new InitialAuthConfig(), new DefaultNetworkFinder());
    docker = new DockerConnector(dockerConnectorConfiguration, new DockerConnectionFactory(dockerConnectorConfiguration), new DockerRegistryAuthResolver(null, null), new DockerApiVersionPathPrefixProvider("1.18"));
    final ContainerCreated containerCreated = docker.createContainer(CreateContainerParams.create(new ContainerConfig().withImage("ubuntu").withCmd("tail", "-f", "/dev/null")));
    container = containerCreated.getId();
    docker.startContainer(StartContainerParams.create(containerCreated.getId()));
    when(dockerConnectorProvider.get()).thenReturn(docker);
}
Also used : ContainerConfig(org.eclipse.che.plugin.docker.client.json.ContainerConfig) DockerRegistryAuthResolver(org.eclipse.che.plugin.docker.client.DockerRegistryAuthResolver) ContainerCreated(org.eclipse.che.plugin.docker.client.json.ContainerCreated) DockerConnector(org.eclipse.che.plugin.docker.client.DockerConnector) DockerConnectorConfiguration(org.eclipse.che.plugin.docker.client.DockerConnectorConfiguration) InitialAuthConfig(org.eclipse.che.plugin.docker.client.InitialAuthConfig) DockerApiVersionPathPrefixProvider(org.eclipse.che.plugin.docker.client.DockerApiVersionPathPrefixProvider) DefaultNetworkFinder(org.eclipse.che.plugin.docker.client.helper.DefaultNetworkFinder) DockerConnectionFactory(org.eclipse.che.plugin.docker.client.connection.DockerConnectionFactory) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 2 with ContainerConfig

use of org.eclipse.che.plugin.docker.client.json.ContainerConfig in project che by eclipse.

the class OpenShiftConnectorTest method shouldGetWorkspaceIDWhenAValidOneIsProvidedInCreateContainerParams.

@Test
public void shouldGetWorkspaceIDWhenAValidOneIsProvidedInCreateContainerParams() throws IOException {
    //Given
    String expectedWorkspaceID = "abcd1234";
    ContainerConfig containerConfig = mock(ContainerConfig.class);
    CreateContainerParams createContainerParams = CreateContainerParams.create(containerConfig);
    when(containerConfig.getEnv()).thenReturn(CONTAINER_ENV_VARIABLES);
    //When
    openShiftConnector = new OpenShiftConnector(dockerConnectorConfiguration, dockerConnectionFactory, authManager, dockerApiVersionPathPrefixProvider, CHE_DEFAULT_OPENSHIFT_PROJECT_NAME, CHE_DEFAULT_OPENSHIFT_SERVICEACCOUNT, OPENSHIFT_LIVENESS_PROBE_DELAY, OPENSHIFT_LIVENESS_PROBE_TIMEOUT);
    String workspaceID = openShiftConnector.getCheWorkspaceId(createContainerParams);
    //Then
    assertEquals(workspaceID, expectedWorkspaceID);
}
Also used : ContainerConfig(org.eclipse.che.plugin.docker.client.json.ContainerConfig) CreateContainerParams(org.eclipse.che.plugin.docker.client.params.CreateContainerParams) Test(org.testng.annotations.Test)

Example 3 with ContainerConfig

use of org.eclipse.che.plugin.docker.client.json.ContainerConfig in project che by eclipse.

the class DockerConnectorTest method shouldBeAbleToCreateContainer.

@Test
public void shouldBeAbleToCreateContainer() throws IOException, JsonParseException {
    CreateContainerParams createContainerParams = CreateContainerParams.create(new ContainerConfig());
    ContainerCreated containerCreated = mock(ContainerCreated.class);
    when(dockerResponse.getStatus()).thenReturn(RESPONSE_CREATED_CODE);
    doReturn(containerCreated).when(dockerConnector).parseResponseStreamAndClose(inputStream, ContainerCreated.class);
    ContainerCreated returnedContainerCreated = dockerConnector.createContainer(createContainerParams);
    verify(dockerConnectionFactory).openConnection(any(URI.class));
    verify(dockerConnection).method(REQUEST_METHOD_POST);
    verify(dockerConnection).path("/containers/create");
    verify(dockerConnection).header("Content-Type", MediaType.APPLICATION_JSON);
    verify(dockerConnection).header(eq("Content-Length"), anyInt());
    verify(dockerConnection).entity(any(byte[].class));
    verify(dockerConnection).request();
    verify(dockerResponse).getStatus();
    verify(dockerResponse).getInputStream();
    assertEquals(returnedContainerCreated, containerCreated);
}
Also used : ContainerConfig(org.eclipse.che.plugin.docker.client.json.ContainerConfig) ContainerCreated(org.eclipse.che.plugin.docker.client.json.ContainerCreated) CreateContainerParams(org.eclipse.che.plugin.docker.client.params.CreateContainerParams) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 4 with ContainerConfig

use of org.eclipse.che.plugin.docker.client.json.ContainerConfig in project che by eclipse.

the class OpenShiftConnector method createContainerInfo.

/**
     * Collects the relevant information from a Service, an ImageInfo, and a Pod into
     * a docker ContainerInfo JSON object. The returned object is what would be returned
     * by executing {@code docker inspect <container>}, with fields filled as available.
     * @param svc
     * @param imageInfo
     * @param pod
     * @param containerId
     * @return
     */
private ContainerInfo createContainerInfo(Service svc, ImageInfo imageInfo, Pod pod, String containerId) {
    // In Che on OpenShift, we only have one container per pod.
    Container container = pod.getSpec().getContainers().get(0);
    ContainerConfig imageContainerConfig = imageInfo.getContainerConfig();
    // HostConfig
    HostConfig hostConfig = new HostConfig();
    hostConfig.setBinds(new String[0]);
    hostConfig.setMemory(imageInfo.getConfig().getMemory());
    // Env vars
    List<String> imageEnv = Arrays.asList(imageContainerConfig.getEnv());
    List<String> containerEnv = container.getEnv().stream().map(e -> String.format("%s=%s", e.getName(), e.getValue())).collect(Collectors.toList());
    String[] env = Stream.concat(imageEnv.stream(), containerEnv.stream()).toArray(String[]::new);
    // Exposed Ports
    Map<String, List<PortBinding>> ports = getCheServicePorts(svc);
    Map<String, Map<String, String>> exposedPorts = new HashMap<>();
    for (String key : ports.keySet()) {
        exposedPorts.put(key, Collections.emptyMap());
    }
    // Labels
    Map<String, String> annotations = KubernetesLabelConverter.namesToLabels(svc.getMetadata().getAnnotations());
    Map<String, String> containerLabels = imageInfo.getConfig().getLabels();
    Map<String, String> labels = Stream.concat(annotations.entrySet().stream(), containerLabels.entrySet().stream()).filter(e -> e.getKey().startsWith(KubernetesLabelConverter.getCheServerLabelPrefix())).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
    // ContainerConfig
    ContainerConfig config = imageContainerConfig;
    config.setHostname(svc.getMetadata().getName());
    config.setEnv(env);
    config.setExposedPorts(exposedPorts);
    config.setLabels(labels);
    config.setImage(container.getImage());
    // NetworkSettings
    NetworkSettings networkSettings = new NetworkSettings();
    networkSettings.setIpAddress(svc.getSpec().getClusterIP());
    networkSettings.setGateway(svc.getSpec().getClusterIP());
    networkSettings.setPorts(ports);
    // Make final ContainerInfo
    ContainerInfo info = new ContainerInfo();
    info.setId(containerId);
    info.setConfig(config);
    info.setNetworkSettings(networkSettings);
    info.setHostConfig(hostConfig);
    info.setImage(imageInfo.getConfig().getImage());
    return info;
}
Also used : VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) Arrays(java.util.Arrays) RemoveContainerParams(org.eclipse.che.plugin.docker.client.params.RemoveContainerParams) HostConfig(org.eclipse.che.plugin.docker.client.json.HostConfig) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) LoggerFactory(org.slf4j.LoggerFactory) MessageProcessor(org.eclipse.che.plugin.docker.client.MessageProcessor) IpamConfig(org.eclipse.che.plugin.docker.client.json.network.IpamConfig) KubernetesContainer(org.eclipse.che.plugin.openshift.client.kubernetes.KubernetesContainer) ServicePort(io.fabric8.kubernetes.api.model.ServicePort) ContainerListEntry(org.eclipse.che.plugin.docker.client.json.ContainerListEntry) DefaultOpenShiftClient(io.fabric8.openshift.client.DefaultOpenShiftClient) VolumeMountBuilder(io.fabric8.kubernetes.api.model.VolumeMountBuilder) DockerRegistryAuthResolver(org.eclipse.che.plugin.docker.client.DockerRegistryAuthResolver) Filters(org.eclipse.che.plugin.docker.client.json.Filters) Map(java.util.Map) NetworkSettings(org.eclipse.che.plugin.docker.client.json.NetworkSettings) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) DockerConnectionFactory(org.eclipse.che.plugin.docker.client.connection.DockerConnectionFactory) ImageStreamTag(io.fabric8.openshift.api.model.ImageStreamTag) ImageInfo(org.eclipse.che.plugin.docker.client.json.ImageInfo) InspectImageParams(org.eclipse.che.plugin.docker.client.params.InspectImageParams) Set(java.util.Set) OpenShiftException(org.eclipse.che.plugin.openshift.client.exception.OpenShiftException) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) Collectors(java.util.stream.Collectors) GetNetworksParams(org.eclipse.che.plugin.docker.client.params.network.GetNetworksParams) KubernetesService(org.eclipse.che.plugin.openshift.client.kubernetes.KubernetesService) ConnectContainerToNetworkParams(org.eclipse.che.plugin.docker.client.params.network.ConnectContainerToNetworkParams) List(java.util.List) Network(org.eclipse.che.plugin.docker.client.json.network.Network) Stream(java.util.stream.Stream) CommitParams(org.eclipse.che.plugin.docker.client.params.CommitParams) ContainerConfig(org.eclipse.che.plugin.docker.client.json.ContainerConfig) GetEventsParams(org.eclipse.che.plugin.docker.client.params.GetEventsParams) DeploymentBuilder(io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder) ImageConfig(org.eclipse.che.plugin.docker.client.json.ImageConfig) TagParams(org.eclipse.che.plugin.docker.client.params.TagParams) CreateContainerParams(org.eclipse.che.plugin.docker.client.params.CreateContainerParams) ServiceList(io.fabric8.kubernetes.api.model.ServiceList) DockerApiVersionPathPrefixProvider(org.eclipse.che.plugin.docker.client.DockerApiVersionPathPrefixProvider) ProbeBuilder(io.fabric8.kubernetes.api.model.ProbeBuilder) Ipam(org.eclipse.che.plugin.docker.client.json.network.Ipam) PullParams(org.eclipse.che.plugin.docker.client.params.PullParams) DockerConnectorConfiguration(org.eclipse.che.plugin.docker.client.DockerConnectorConfiguration) ImageNotFoundException(org.eclipse.che.plugin.docker.client.exception.ImageNotFoundException) Container(io.fabric8.kubernetes.api.model.Container) PutResourceParams(org.eclipse.che.plugin.docker.client.params.PutResourceParams) StopContainerParams(org.eclipse.che.plugin.docker.client.params.StopContainerParams) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) HashMap(java.util.HashMap) ContainerCreated(org.eclipse.che.plugin.docker.client.json.ContainerCreated) Singleton(javax.inject.Singleton) StartContainerParams(org.eclipse.che.plugin.docker.client.params.StartContainerParams) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) RemoveImageParams(org.eclipse.che.plugin.docker.client.params.RemoveImageParams) InspectNetworkParams(org.eclipse.che.plugin.docker.client.params.network.InspectNetworkParams) ProgressMonitor(org.eclipse.che.plugin.docker.client.ProgressMonitor) KubernetesEnvVar(org.eclipse.che.plugin.openshift.client.kubernetes.KubernetesEnvVar) CreateNetworkParams(org.eclipse.che.plugin.docker.client.params.network.CreateNetworkParams) PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) PortBinding(org.eclipse.che.plugin.docker.client.json.PortBinding) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) Named(javax.inject.Named) ContainerInfo(org.eclipse.che.plugin.docker.client.json.ContainerInfo) Service(io.fabric8.kubernetes.api.model.Service) DisconnectContainerFromNetworkParams(org.eclipse.che.plugin.docker.client.params.network.DisconnectContainerFromNetworkParams) KillContainerParams(org.eclipse.che.plugin.docker.client.params.KillContainerParams) Volume(io.fabric8.kubernetes.api.model.Volume) ImageStream(io.fabric8.openshift.api.model.ImageStream) Probe(io.fabric8.kubernetes.api.model.Probe) GetResourceParams(org.eclipse.che.plugin.docker.client.params.GetResourceParams) Logger(org.slf4j.Logger) Pod(io.fabric8.kubernetes.api.model.Pod) IOException(java.io.IOException) Event(org.eclipse.che.plugin.docker.client.json.Event) KubernetesLabelConverter(org.eclipse.che.plugin.openshift.client.kubernetes.KubernetesLabelConverter) RemoveNetworkParams(org.eclipse.che.plugin.docker.client.params.RemoveNetworkParams) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) NetworkCreated(org.eclipse.che.plugin.docker.client.json.NetworkCreated) ContainerInNetwork(org.eclipse.che.plugin.docker.client.json.network.ContainerInNetwork) PodList(io.fabric8.kubernetes.api.model.PodList) DockerConnector(org.eclipse.che.plugin.docker.client.DockerConnector) KubernetesStringUtils(org.eclipse.che.plugin.openshift.client.kubernetes.KubernetesStringUtils) Collections(java.util.Collections) InputStream(java.io.InputStream) HashMap(java.util.HashMap) ContainerConfig(org.eclipse.che.plugin.docker.client.json.ContainerConfig) KubernetesContainer(org.eclipse.che.plugin.openshift.client.kubernetes.KubernetesContainer) Container(io.fabric8.kubernetes.api.model.Container) NetworkSettings(org.eclipse.che.plugin.docker.client.json.NetworkSettings) HostConfig(org.eclipse.che.plugin.docker.client.json.HostConfig) ContainerInfo(org.eclipse.che.plugin.docker.client.json.ContainerInfo) List(java.util.List) ServiceList(io.fabric8.kubernetes.api.model.ServiceList) ArrayList(java.util.ArrayList) PodList(io.fabric8.kubernetes.api.model.PodList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with ContainerConfig

use of org.eclipse.che.plugin.docker.client.json.ContainerConfig in project che by eclipse.

the class MachineProviderImplTest method shouldAddLinksToContainerOnCreation.

@Test
public void shouldAddLinksToContainerOnCreation() throws Exception {
    // given
    String[] links = new String[] { "container1", "container2:alias" };
    CheServiceImpl service = createService();
    service.setLinks(asList(links));
    // when
    createInstanceFromRecipe(service, true);
    // then
    ArgumentCaptor<CreateContainerParams> argumentCaptor = ArgumentCaptor.forClass(CreateContainerParams.class);
    verify(dockerConnector).createContainer(argumentCaptor.capture());
    ContainerConfig containerConfig = argumentCaptor.getValue().getContainerConfig();
    assertEquals(containerConfig.getHostConfig().getLinks(), links);
    assertEquals(containerConfig.getNetworkingConfig().getEndpointsConfig().get(NETWORK_NAME).getLinks(), links);
}
Also used : ContainerConfig(org.eclipse.che.plugin.docker.client.json.ContainerConfig) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) CreateContainerParams(org.eclipse.che.plugin.docker.client.params.CreateContainerParams) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Aggregations

ContainerConfig (org.eclipse.che.plugin.docker.client.json.ContainerConfig)8 CreateContainerParams (org.eclipse.che.plugin.docker.client.params.CreateContainerParams)6 Test (org.testng.annotations.Test)4 DockerConnector (org.eclipse.che.plugin.docker.client.DockerConnector)3 HostConfig (org.eclipse.che.plugin.docker.client.json.HostConfig)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 Inject (javax.inject.Inject)2 Named (javax.inject.Named)2 CheServiceImpl (org.eclipse.che.api.environment.server.model.CheServiceImpl)2 ProgressMonitor (org.eclipse.che.plugin.docker.client.ProgressMonitor)2 ImageNotFoundException (org.eclipse.che.plugin.docker.client.exception.ImageNotFoundException)2 ContainerCreated (org.eclipse.che.plugin.docker.client.json.ContainerCreated)2 NetworkSettings (org.eclipse.che.plugin.docker.client.json.NetworkSettings)2 PortBinding (org.eclipse.che.plugin.docker.client.json.PortBinding)2