Search in sources :

Example 1 with ContainerCreated

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

the class OpenShiftConnector method createContainer.

/**
     * @param createContainerParams
     * @return
     * @throws IOException
     */
@Override
public ContainerCreated createContainer(CreateContainerParams createContainerParams) throws IOException {
    String containerName = KubernetesStringUtils.convertToContainerName(createContainerParams.getContainerName());
    String workspaceID = getCheWorkspaceId(createContainerParams);
    // Generate workspaceID if CHE_WORKSPACE_ID env var does not exist
    workspaceID = workspaceID.isEmpty() ? KubernetesStringUtils.generateWorkspaceID() : workspaceID;
    // imageForDocker is the docker version of the image repository. It's needed for other
    // OpenShiftConnector API methods, but is not acceptable as an OpenShift name
    String imageForDocker = createContainerParams.getContainerConfig().getImage();
    // imageStreamTagName is imageForDocker converted into a form that can be used
    // in OpenShift
    String imageStreamTagName = KubernetesStringUtils.convertPullSpecToTagName(imageForDocker);
    // imageStreamTagName is not enough to fill out a pull spec; it is only the tag, so we
    // have to get the ImageStreamTag from the tag, and then get the full ImageStreamTag name
    // from that tag. This works because the tags used in Che are unique.
    ImageStreamTag imageStreamTag = getImageStreamTagFromRepo(imageStreamTagName);
    String imageStreamTagPullSpec = imageStreamTag.getMetadata().getName();
    // Next we need to get the address of the registry where the ImageStreamTag is stored
    String imageStreamName = KubernetesStringUtils.getImageStreamNameFromPullSpec(imageStreamTagPullSpec);
    ImageStream imageStream = openShiftClient.imageStreams().inNamespace(openShiftCheProjectName).withName(imageStreamName).get();
    if (imageStream == null) {
        throw new OpenShiftException("ImageStream not found");
    }
    String registryAddress = imageStream.getStatus().getDockerImageRepository().split("/")[0];
    // The above needs to be combined to form a pull spec that will work when defining a container.
    String dockerPullSpec = String.format("%s/%s/%s", registryAddress, openShiftCheProjectName, imageStreamTagPullSpec);
    Set<String> containerExposedPorts = createContainerParams.getContainerConfig().getExposedPorts().keySet();
    Set<String> imageExposedPorts = inspectImage(InspectImageParams.create(imageForDocker)).getConfig().getExposedPorts().keySet();
    Set<String> exposedPorts = getExposedPorts(containerExposedPorts, imageExposedPorts);
    boolean runContainerAsRoot = runContainerAsRoot(imageForDocker);
    String[] envVariables = createContainerParams.getContainerConfig().getEnv();
    String[] volumes = createContainerParams.getContainerConfig().getHostConfig().getBinds();
    Map<String, String> additionalLabels = createContainerParams.getContainerConfig().getLabels();
    String containerID;
    try {
        createOpenShiftService(workspaceID, exposedPorts, additionalLabels);
        String deploymentName = createOpenShiftDeployment(workspaceID, dockerPullSpec, containerName, exposedPorts, envVariables, volumes, runContainerAsRoot);
        containerID = waitAndRetrieveContainerID(deploymentName);
        if (containerID == null) {
            throw new OpenShiftException("Failed to get the ID of the container running in the OpenShift pod");
        }
    } catch (IOException e) {
        // Make sure we clean up deployment and service in case of an error -- otherwise Che can end up
        // in an inconsistent state.
        LOG.info("Error while creating Pod, removing deployment");
        String deploymentName = CHE_OPENSHIFT_RESOURCES_PREFIX + workspaceID;
        cleanUpWorkspaceResources(deploymentName);
        openShiftClient.resource(imageStreamTag).delete();
        throw e;
    }
    return new ContainerCreated(containerID, null);
}
Also used : ContainerCreated(org.eclipse.che.plugin.docker.client.json.ContainerCreated) OpenShiftException(org.eclipse.che.plugin.openshift.client.exception.OpenShiftException) ImageStreamTag(io.fabric8.openshift.api.model.ImageStreamTag) ImageStream(io.fabric8.openshift.api.model.ImageStream) IOException(java.io.IOException)

Example 2 with ContainerCreated

use of org.eclipse.che.plugin.docker.client.json.ContainerCreated 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 3 with ContainerCreated

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

the class MachineProviderImplTest method setUp.

@BeforeMethod
public void setUp() throws Exception {
    when(dockerConnectorConfiguration.getDockerHostIp()).thenReturn("123.123.123.123");
    provider = spy(new MachineProviderBuilder().build());
    EnvironmentContext envCont = new EnvironmentContext();
    envCont.setSubject(new SubjectImpl(USER_NAME, "userId", USER_TOKEN, false));
    EnvironmentContext.setCurrent(envCont);
    when(recipeRetriever.getRecipe(any(MachineConfig.class))).thenReturn(new RecipeImpl().withType(DOCKER_FILE_TYPE).withScript("FROM codenvy"));
    when(dockerMachineFactory.createNode(anyString(), anyString())).thenReturn(dockerNode);
    when(dockerConnector.createContainer(any(CreateContainerParams.class))).thenReturn(new ContainerCreated(CONTAINER_ID, new String[0]));
    when(dockerConnector.inspectContainer(any(InspectContainerParams.class))).thenReturn(containerInfo);
    when(containerInfo.getState()).thenReturn(containerState);
    when(containerState.isRunning()).thenReturn(false);
}
Also used : EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) ContainerCreated(org.eclipse.che.plugin.docker.client.json.ContainerCreated) InspectContainerParams(org.eclipse.che.plugin.docker.client.params.InspectContainerParams) MachineConfig(org.eclipse.che.api.core.model.machine.MachineConfig) RecipeImpl(org.eclipse.che.api.machine.server.recipe.RecipeImpl) CreateContainerParams(org.eclipse.che.plugin.docker.client.params.CreateContainerParams) Matchers.anyString(org.mockito.Matchers.anyString) SubjectImpl(org.eclipse.che.commons.subject.SubjectImpl) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 4 with ContainerCreated

use of org.eclipse.che.plugin.docker.client.json.ContainerCreated 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)

Aggregations

ContainerCreated (org.eclipse.che.plugin.docker.client.json.ContainerCreated)4 ContainerConfig (org.eclipse.che.plugin.docker.client.json.ContainerConfig)2 CreateContainerParams (org.eclipse.che.plugin.docker.client.params.CreateContainerParams)2 BeforeMethod (org.testng.annotations.BeforeMethod)2 ImageStream (io.fabric8.openshift.api.model.ImageStream)1 ImageStreamTag (io.fabric8.openshift.api.model.ImageStreamTag)1 IOException (java.io.IOException)1 URI (java.net.URI)1 MachineConfig (org.eclipse.che.api.core.model.machine.MachineConfig)1 RecipeImpl (org.eclipse.che.api.machine.server.recipe.RecipeImpl)1 EnvironmentContext (org.eclipse.che.commons.env.EnvironmentContext)1 SubjectImpl (org.eclipse.che.commons.subject.SubjectImpl)1 DockerApiVersionPathPrefixProvider (org.eclipse.che.plugin.docker.client.DockerApiVersionPathPrefixProvider)1 DockerConnector (org.eclipse.che.plugin.docker.client.DockerConnector)1 DockerConnectorConfiguration (org.eclipse.che.plugin.docker.client.DockerConnectorConfiguration)1 DockerRegistryAuthResolver (org.eclipse.che.plugin.docker.client.DockerRegistryAuthResolver)1 InitialAuthConfig (org.eclipse.che.plugin.docker.client.InitialAuthConfig)1 DockerConnectionFactory (org.eclipse.che.plugin.docker.client.connection.DockerConnectionFactory)1 DefaultNetworkFinder (org.eclipse.che.plugin.docker.client.helper.DefaultNetworkFinder)1 InspectContainerParams (org.eclipse.che.plugin.docker.client.params.InspectContainerParams)1