Search in sources :

Example 1 with ImageInfo

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

the class OpenShiftConnector method getImageInfoFromTag.

/**
     * Extracts the ImageInfo stored in an ImageStreamTag. The returned object is the JSON
     * that would be returned by executing {@code docker inspect <image>}, except, due to a quirk
     * in OpenShift's handling of this data, fields except for {@code Config} and {@code ContainerConfig}
     * are null.
     * @param imageStreamTag
     * @return
     */
private ImageInfo getImageInfoFromTag(ImageStreamTag imageStreamTag) {
    // The DockerImageConfig string here is the JSON that would be returned by a docker inspect image,
    // except that the capitalization is inconsistent, breaking deserialization. Top level elements
    // are lowercased with underscores, while nested elements conform to FieldNamingPolicy.UPPER_CAMEL_CASE.
    // We're only converting the config fields for brevity; this means that other fields are null.
    String dockerImageConfig = imageStreamTag.getImage().getDockerImageConfig();
    ImageInfo info = GSON.fromJson(dockerImageConfig.replaceFirst("config", "Config").replaceFirst("container_config", "ContainerConfig"), ImageInfo.class);
    return info;
}
Also used : ImageInfo(org.eclipse.che.plugin.docker.client.json.ImageInfo)

Example 2 with ImageInfo

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

the class DockerConnectorTest method shouldBeAbleToInspectImage.

@Test
public void shouldBeAbleToInspectImage() throws IOException, JsonParseException {
    InspectImageParams inspectImageParams = InspectImageParams.create(IMAGE);
    ImageInfo imageInfo = mock(ImageInfo.class);
    doReturn(imageInfo).when(dockerConnector).parseResponseStreamAndClose(inputStream, ImageInfo.class);
    ImageInfo returnedImageInfo = dockerConnector.inspectImage(inspectImageParams);
    verify(dockerConnectionFactory).openConnection(any(URI.class));
    verify(dockerConnection).method(REQUEST_METHOD_GET);
    verify(dockerConnection).path("/images/" + inspectImageParams.getImage() + "/json");
    verify(dockerConnection).request();
    verify(dockerResponse).getStatus();
    verify(dockerResponse).getInputStream();
    assertEquals(returnedImageInfo, imageInfo);
}
Also used : InspectImageParams(org.eclipse.che.plugin.docker.client.params.InspectImageParams) ImageInfo(org.eclipse.che.plugin.docker.client.json.ImageInfo) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 3 with ImageInfo

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

the class DockerConnectorTest method shouldCallInspectImageWithParametersObject.

@Test
public void shouldCallInspectImageWithParametersObject() throws IOException {
    InspectImageParams inspectImageParams = InspectImageParams.create(IMAGE);
    ImageInfo imageInfo = mock(ImageInfo.class);
    doReturn(imageInfo).when(dockerConnector).inspectImage(any(InspectImageParams.class));
    ImageInfo returnedImageInfo = dockerConnector.inspectImage(IMAGE);
    verify(dockerConnector).inspectImage((InspectImageParams) captor.capture());
    assertEquals(captor.getValue(), inspectImageParams);
    assertEquals(returnedImageInfo, imageInfo);
}
Also used : InspectImageParams(org.eclipse.che.plugin.docker.client.params.InspectImageParams) ImageInfo(org.eclipse.che.plugin.docker.client.json.ImageInfo) Test(org.testng.annotations.Test)

Example 4 with ImageInfo

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

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

the class OpenShiftConnector method inspectContainer.

@Override
public ContainerInfo inspectContainer(String containerId) throws IOException {
    Pod pod = getChePodByContainerId(containerId);
    if (pod == null) {
        LOG.warn("No Pod found by container ID {}", containerId);
        return null;
    }
    List<Container> podContainers = pod.getSpec().getContainers();
    if (podContainers.size() > 1) {
        throw new OpenShiftException("Multiple Containers found in Pod.");
    } else if (podContainers.size() < 1 || isNullOrEmpty(podContainers.get(0).getImage())) {
        throw new OpenShiftException(String.format("Container %s not found", containerId));
    }
    String podPullSpec = podContainers.get(0).getImage();
    String tagName = KubernetesStringUtils.getTagNameFromPullSpec(podPullSpec);
    ImageStreamTag tag = getImageStreamTagFromRepo(tagName);
    ImageInfo imageInfo = getImageInfoFromTag(tag);
    String deploymentName = pod.getMetadata().getLabels().get(OPENSHIFT_DEPLOYMENT_LABEL);
    if (deploymentName == null) {
        LOG.warn("No label {} found for Pod {}", OPENSHIFT_DEPLOYMENT_LABEL, pod.getMetadata().getName());
        return null;
    }
    Service svc = getCheServiceBySelector(OPENSHIFT_DEPLOYMENT_LABEL, deploymentName);
    if (svc == null) {
        LOG.warn("No Service found by selector {}={}", OPENSHIFT_DEPLOYMENT_LABEL, deploymentName);
        return null;
    }
    return createContainerInfo(svc, imageInfo, pod, containerId);
}
Also used : KubernetesContainer(org.eclipse.che.plugin.openshift.client.kubernetes.KubernetesContainer) Container(io.fabric8.kubernetes.api.model.Container) Pod(io.fabric8.kubernetes.api.model.Pod) OpenShiftException(org.eclipse.che.plugin.openshift.client.exception.OpenShiftException) ImageStreamTag(io.fabric8.openshift.api.model.ImageStreamTag) KubernetesService(org.eclipse.che.plugin.openshift.client.kubernetes.KubernetesService) Service(io.fabric8.kubernetes.api.model.Service) ImageInfo(org.eclipse.che.plugin.docker.client.json.ImageInfo)

Aggregations

ImageInfo (org.eclipse.che.plugin.docker.client.json.ImageInfo)5 InspectImageParams (org.eclipse.che.plugin.docker.client.params.InspectImageParams)3 Container (io.fabric8.kubernetes.api.model.Container)2 Pod (io.fabric8.kubernetes.api.model.Pod)2 Service (io.fabric8.kubernetes.api.model.Service)2 ImageStreamTag (io.fabric8.openshift.api.model.ImageStreamTag)2 OpenShiftException (org.eclipse.che.plugin.openshift.client.exception.OpenShiftException)2 KubernetesContainer (org.eclipse.che.plugin.openshift.client.kubernetes.KubernetesContainer)2 KubernetesService (org.eclipse.che.plugin.openshift.client.kubernetes.KubernetesService)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 VolumeBuilder (io.fabric8.kubernetes.api.model.VolumeBuilder)1