Search in sources :

Example 1 with Image

use of io.fabric8.docker.api.model.Image 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 Image

use of io.fabric8.docker.api.model.Image in project che by eclipse.

the class OpenShiftConnector method commit.

/**
     * OpenShift does not support taking image snapshots since the underlying assumption
     * is that Pods are largely immutable (and so any snapshot would be identical to the image
     * used to create the pod). Che uses docker commit to create machine snapshots, which are
     * used to restore workspaces. To emulate this functionality in OpenShift, commit
     * actually creates a new ImageStreamTag by calling {@link OpenShiftConnector#tag(TagParams)}
     * named for the snapshot that would be created.
     *
     * @see DockerConnector#commit(CommitParams)
     */
@Override
public String commit(final CommitParams params) throws IOException {
    // e.g. machine_snapshot_mdkfmksdfm
    String repo = params.getRepository();
    // container ID
    String container = params.getContainer();
    Pod pod = getChePodByContainerId(container);
    String image = pod.getSpec().getContainers().get(0).getImage();
    String imageStreamTagName = KubernetesStringUtils.getTagNameFromPullSpec(image);
    ImageStreamTag imageStreamTag = getImageStreamTagFromRepo(imageStreamTagName);
    String sourcePullSpec = imageStreamTag.getTag().getFrom().getName();
    String trackingRepo = KubernetesStringUtils.stripTagFromPullSpec(sourcePullSpec);
    String tag = KubernetesStringUtils.getTagNameFromPullSpec(sourcePullSpec);
    tag(TagParams.create(trackingRepo, repo).withTag(tag));
    // Return value not used.
    return repo;
}
Also used : Pod(io.fabric8.kubernetes.api.model.Pod) ImageStreamTag(io.fabric8.openshift.api.model.ImageStreamTag)

Example 3 with Image

use of io.fabric8.docker.api.model.Image in project che by eclipse.

the class OpenShiftConnector method inspectImage.

@Override
public ImageInfo inspectImage(InspectImageParams params) throws IOException {
    String image = KubernetesStringUtils.getImageStreamNameFromPullSpec(params.getImage());
    String imageStreamTagName = KubernetesStringUtils.convertPullSpecToTagName(image);
    ImageStreamTag imageStreamTag = getImageStreamTagFromRepo(imageStreamTagName);
    return getImageInfoFromTag(imageStreamTag);
}
Also used : ImageStreamTag(io.fabric8.openshift.api.model.ImageStreamTag)

Example 4 with Image

use of io.fabric8.docker.api.model.Image in project che by eclipse.

the class OpenShiftConnector method tag.

/**
     * Creates an ImageStreamTag that tracks a given image.
     *
     * <p> Docker tags are used extensively in Che: all workspaces run on tagged images
     * tracking built stacks. For new workspaces, or when snapshots are not used, the
     * tracked image is e.g. {@code eclipse/ubuntu_jdk8}, whereas for snapshotted workspaces,
     * the tracked image is the snapshot (e.g. {@code machine_snapshot-<identifier>}.
     *
     * <p> Since OpenShift does not support the same tagging functionality as Docker,
     * tags are implemented as ImageStreamTags, where the {@code From} field is always
     * the original image, and the ImageStreamTag name is derived from both the source
     * image and the target image. This replicates functionality for Che in Docker,
     * while working differently under the hood. The ImageStream name is derived from
     * the image that is being tracked (e.g. {@code eclipse/ubuntu_jdk8}), while the tag
     * name is derived from the target image (e.g. {@code eclipse-che/che_workspace<identifier>}).
     *
     * @see DockerConnector#tag(TagParams)
     */
@Override
public void tag(final TagParams params) throws IOException {
    // E.g. `docker tag sourceImage targetImage`
    // e.g. eclipse/ubuntu_jdk8
    String paramsSourceImage = params.getImage();
    // e.g. eclipse-che/<identifier>
    String targetImage = params.getRepository();
    String paramsTag = params.getTag();
    String sourceImage = KubernetesStringUtils.stripTagFromPullSpec(paramsSourceImage);
    String tag = KubernetesStringUtils.getTagNameFromPullSpec(paramsSourceImage);
    if (isNullOrEmpty(tag)) {
        tag = !isNullOrEmpty(paramsTag) ? paramsTag : "latest";
    }
    String sourceImageWithTag;
    // Check if sourceImage matches existing imageStreamTag (e.g. when tagging a snapshot)
    try {
        String sourceImageTagName = KubernetesStringUtils.convertPullSpecToTagName(sourceImage);
        ImageStreamTag existingTag = getImageStreamTagFromRepo(sourceImageTagName);
        sourceImageWithTag = existingTag.getTag().getFrom().getName();
    } catch (IOException e) {
        // Image not found.
        sourceImageWithTag = String.format("%s:%s", sourceImage, tag);
    }
    String imageStreamTagName = KubernetesStringUtils.createImageStreamTagName(sourceImageWithTag, targetImage);
    createImageStreamTag(sourceImageWithTag, imageStreamTagName);
}
Also used : ImageStreamTag(io.fabric8.openshift.api.model.ImageStreamTag) IOException(java.io.IOException)

Example 5 with Image

use of io.fabric8.docker.api.model.Image in project che by eclipse.

the class OpenShiftConnector method removeImage.

@Override
public void removeImage(final RemoveImageParams params) throws IOException {
    String image = KubernetesStringUtils.getImageStreamNameFromPullSpec(params.getImage());
    String imageStreamTagName = KubernetesStringUtils.convertPullSpecToTagName(image);
    ImageStreamTag imageStreamTag = getImageStreamTagFromRepo(imageStreamTagName);
    openShiftClient.resource(imageStreamTag).delete();
}
Also used : ImageStreamTag(io.fabric8.openshift.api.model.ImageStreamTag)

Aggregations

Test (org.junit.Test)35 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)28 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)20 IOException (java.io.IOException)16 AbstractConfigHandlerTest (io.fabric8.maven.docker.config.handler.AbstractConfigHandlerTest)15 File (java.io.File)13 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)12 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)12 ImageStream (io.fabric8.openshift.api.model.ImageStream)10 Arguments (io.fabric8.maven.docker.config.Arguments)8 Service (io.fabric8.kubernetes.api.model.Service)7 Fabric8ServiceException (io.fabric8.maven.core.service.Fabric8ServiceException)7 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)6 BuildService (io.fabric8.maven.core.service.BuildService)6 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)6 HashMap (java.util.HashMap)6 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)5 DockerAccessException (io.fabric8.maven.docker.access.DockerAccessException)5 ImageName (io.fabric8.maven.docker.util.ImageName)5 ImageStreamTag (io.fabric8.openshift.api.model.ImageStreamTag)5