Search in sources :

Example 1 with OpenShiftException

use of org.eclipse.che.plugin.openshift.client.exception.OpenShiftException in project che by eclipse.

the class OpenShiftConnector method getImageStreamTagFromRepo.

/**
     * Gets the ImageStreamTag corresponding to a given tag name (i.e. without the repository)
     * @param imageStreamTagName the tag name to search for
     * @return
     * @throws IOException if either no matching tag is found, or there are multiple matches.
     */
private ImageStreamTag getImageStreamTagFromRepo(String imageStreamTagName) throws IOException {
    // Since repository + tag are limited to 63 chars, it's possible that the entire
    // tag name did not fit, so we have to match a substring.
    String imageTagTrimmed = imageStreamTagName.length() > 30 ? imageStreamTagName.substring(0, 30) : imageStreamTagName;
    // Note: ideally, ImageStreamTags could be identified with a label, but it seems like
    // ImageStreamTags do not support labels.
    List<ImageStreamTag> imageStreams = openShiftClient.imageStreamTags().inNamespace(openShiftCheProjectName).list().getItems();
    // We only get ImageStreamTag names here, since these ImageStreamTags do not include
    // Docker metadata, for some reason.
    List<String> imageStreamTags = imageStreams.stream().filter(e -> e.getMetadata().getName().contains(imageTagTrimmed)).map(e -> e.getMetadata().getName()).collect(Collectors.toList());
    if (imageStreamTags.size() < 1) {
        throw new OpenShiftException(String.format("ImageStreamTag %s not found!", imageStreamTagName));
    } else if (imageStreamTags.size() > 1) {
        throw new OpenShiftException(String.format("Multiple ImageStreamTags found for name %s", imageStreamTagName));
    }
    String imageStreamTag = imageStreamTags.get(0);
    // Finally, get the ImageStreamTag, with Docker metadata.
    return openShiftClient.imageStreamTags().inNamespace(openShiftCheProjectName).withName(imageStreamTag).get();
}
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) OpenShiftException(org.eclipse.che.plugin.openshift.client.exception.OpenShiftException) ImageStreamTag(io.fabric8.openshift.api.model.ImageStreamTag)

Example 2 with OpenShiftException

use of org.eclipse.che.plugin.openshift.client.exception.OpenShiftException 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 3 with OpenShiftException

use of org.eclipse.che.plugin.openshift.client.exception.OpenShiftException in project che by eclipse.

the class OpenShiftConnector method cleanUpWorkspaceResources.

private void cleanUpWorkspaceResources(String deploymentName) throws IOException {
    Deployment deployment = getDeploymentByName(deploymentName);
    Service service = getCheServiceBySelector(OPENSHIFT_DEPLOYMENT_LABEL, deploymentName);
    if (service != null) {
        LOG.info("Removing OpenShift Service {}", service.getMetadata().getName());
        openShiftClient.resource(service).delete();
    }
    if (deployment != null) {
        LOG.info("Removing OpenShift Deployment {}", deployment.getMetadata().getName());
        openShiftClient.resource(deployment).delete();
    }
    // Wait for all pods to terminate before returning.
    try {
        for (int waitCount = 0; waitCount < OPENSHIFT_WAIT_POD_TIMEOUT; waitCount++) {
            List<Pod> pods = openShiftClient.pods().inNamespace(openShiftCheProjectName).withLabel(OPENSHIFT_DEPLOYMENT_LABEL, deploymentName).list().getItems();
            if (pods.size() == 0) {
                return;
            }
            Thread.sleep(OPENSHIFT_WAIT_POD_DELAY);
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        LOG.info("Thread interrupted while cleaning up workspace");
    }
    throw new OpenShiftException("Timeout while waiting for pods to terminate");
}
Also used : Pod(io.fabric8.kubernetes.api.model.Pod) OpenShiftException(org.eclipse.che.plugin.openshift.client.exception.OpenShiftException) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) KubernetesService(org.eclipse.che.plugin.openshift.client.kubernetes.KubernetesService) Service(io.fabric8.kubernetes.api.model.Service)

Example 4 with OpenShiftException

use of org.eclipse.che.plugin.openshift.client.exception.OpenShiftException in project che by eclipse.

the class OpenShiftConnector method pull.

/**
     * Creates an ImageStream that tracks the repository.
     *
     * <p>Note: This method does not cause the relevant image to actually be pulled to the local
     * repository, but creating the ImageStream is necessary as it is used to obtain
     * the address of the internal Docker registry later.
     *
     * @see DockerConnector#pull(PullParams, ProgressMonitor)
     */
@Override
public void pull(final PullParams params, final ProgressMonitor progressMonitor) throws IOException {
    // image to be pulled
    String repo = params.getFullRepo();
    // e.g. latest, usually
    String tag = params.getTag();
    String imageStreamName = KubernetesStringUtils.convertPullSpecToImageStreamName(repo);
    ImageStream existingImageStream = openShiftClient.imageStreams().inNamespace(openShiftCheProjectName).withName(imageStreamName).get();
    if (existingImageStream == null) {
        openShiftClient.imageStreams().inNamespace(openShiftCheProjectName).createNew().withNewMetadata().withName(// imagestream id
        imageStreamName).endMetadata().withNewSpec().addNewTag().withName(tag).endTag().withDockerImageRepository(// tracking repo
        repo).endSpec().withNewStatus().withDockerImageRepository("").endStatus().done();
    }
    // Wait for Image metadata to be obtained.
    ImageStream createdImageStream;
    for (int waitCount = 0; waitCount < OPENSHIFT_IMAGESTREAM_MAX_WAIT_COUNT; waitCount++) {
        try {
            Thread.sleep(OPENSHIFT_IMAGESTREAM_WAIT_DELAY);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        createdImageStream = openShiftClient.imageStreams().inNamespace(openShiftCheProjectName).withName(imageStreamName).get();
        if (createdImageStream != null && createdImageStream.getStatus().getDockerImageRepository() != null) {
            LOG.info(String.format("Created ImageStream %s.", imageStreamName));
            return;
        }
    }
    throw new OpenShiftException(String.format("Failed to create ImageStream %s.", imageStreamName));
}
Also used : OpenShiftException(org.eclipse.che.plugin.openshift.client.exception.OpenShiftException) ImageStream(io.fabric8.openshift.api.model.ImageStream)

Example 5 with OpenShiftException

use of org.eclipse.che.plugin.openshift.client.exception.OpenShiftException in project che by eclipse.

the class OpenShiftConnector method waitAndRetrieveContainerID.

private String waitAndRetrieveContainerID(String deploymentName) throws IOException {
    for (int i = 0; i < OPENSHIFT_WAIT_POD_TIMEOUT; i++) {
        try {
            Thread.sleep(OPENSHIFT_WAIT_POD_DELAY);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
        List<Pod> pods = openShiftClient.pods().inNamespace(this.openShiftCheProjectName).withLabel(OPENSHIFT_DEPLOYMENT_LABEL, deploymentName).list().getItems();
        if (pods.size() < 1) {
            throw new OpenShiftException(String.format("Pod with deployment name %s not found", deploymentName));
        } else if (pods.size() > 1) {
            throw new OpenShiftException(String.format("Multiple pods with deployment name %s found", deploymentName));
        }
        Pod pod = pods.get(0);
        String status = pod.getStatus().getPhase();
        if (OPENSHIFT_POD_STATUS_RUNNING.equals(status)) {
            String containerID = pod.getStatus().getContainerStatuses().get(0).getContainerID();
            String normalizedID = KubernetesStringUtils.normalizeContainerID(containerID);
            openShiftClient.pods().inNamespace(openShiftCheProjectName).withName(pod.getMetadata().getName()).edit().editMetadata().addToLabels(CHE_CONTAINER_IDENTIFIER_LABEL_KEY, KubernetesStringUtils.getLabelFromContainerID(normalizedID)).endMetadata().done();
            return normalizedID;
        }
    }
    return null;
}
Also used : Pod(io.fabric8.kubernetes.api.model.Pod) OpenShiftException(org.eclipse.che.plugin.openshift.client.exception.OpenShiftException)

Aggregations

OpenShiftException (org.eclipse.che.plugin.openshift.client.exception.OpenShiftException)6 Pod (io.fabric8.kubernetes.api.model.Pod)4 Service (io.fabric8.kubernetes.api.model.Service)3 ImageStream (io.fabric8.openshift.api.model.ImageStream)3 ImageStreamTag (io.fabric8.openshift.api.model.ImageStreamTag)3 KubernetesService (org.eclipse.che.plugin.openshift.client.kubernetes.KubernetesService)3 Container (io.fabric8.kubernetes.api.model.Container)2 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)2 IOException (java.io.IOException)2 ContainerCreated (org.eclipse.che.plugin.docker.client.json.ContainerCreated)2 Strings.isNullOrEmpty (com.google.common.base.Strings.isNullOrEmpty)1 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)1 PodList (io.fabric8.kubernetes.api.model.PodList)1 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)1 PodSpecBuilder (io.fabric8.kubernetes.api.model.PodSpecBuilder)1 Probe (io.fabric8.kubernetes.api.model.Probe)1 ProbeBuilder (io.fabric8.kubernetes.api.model.ProbeBuilder)1 ServiceList (io.fabric8.kubernetes.api.model.ServiceList)1 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)1 Volume (io.fabric8.kubernetes.api.model.Volume)1