Search in sources :

Example 1 with Pod

use of io.fabric8.kubernetes.api.model.Pod in project camel by apache.

the class KubernetesPodsProducer method doListPodsByLabel.

protected void doListPodsByLabel(Exchange exchange, String operation) {
    Map<String, String> labels = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_PODS_LABELS, Map.class);
    if (ObjectHelper.isEmpty(labels)) {
        LOG.error("Get pods by labels require specify a labels set");
        throw new IllegalArgumentException("Get pods by labels require specify a labels set");
    }
    MixedOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> pods = getEndpoint().getKubernetesClient().pods();
    for (Map.Entry<String, String> entry : labels.entrySet()) {
        pods.withLabel(entry.getKey(), entry.getValue());
    }
    PodList podList = pods.list();
    MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
    exchange.getOut().setBody(podList.getItems());
}
Also used : PodList(io.fabric8.kubernetes.api.model.PodList) PodResource(io.fabric8.kubernetes.client.dsl.PodResource) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) Pod(io.fabric8.kubernetes.api.model.Pod) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) Map(java.util.Map)

Example 2 with Pod

use of io.fabric8.kubernetes.api.model.Pod in project camel by apache.

the class KubernetesPodsProducerTest method getPodTest.

@Test
public void getPodTest() throws Exception {
    if (ObjectHelper.isEmpty(authToken)) {
        return;
    }
    Exchange ex = template.request("direct:getPod", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_POD_NAME, "elasticsearch-7015o");
        }
    });
    Pod result = ex.getOut().getBody(Pod.class);
    assertNull(result);
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Pod(io.fabric8.kubernetes.api.model.Pod) Test(org.junit.Test)

Example 3 with Pod

use of io.fabric8.kubernetes.api.model.Pod in project camel by apache.

the class KubernetesPodsProducer method doGetPod.

protected void doGetPod(Exchange exchange, String operation) throws Exception {
    Pod pod = null;
    String podName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_POD_NAME, String.class);
    String namespaceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class);
    if (ObjectHelper.isEmpty(podName)) {
        LOG.error("Get a specific pod require specify a pod name");
        throw new IllegalArgumentException("Get a specific pod require specify a pod name");
    }
    if (ObjectHelper.isEmpty(namespaceName)) {
        LOG.error("Get a specific pod require specify a namespace name");
        throw new IllegalArgumentException("Get a specific pod require specify a namespace name");
    }
    pod = getEndpoint().getKubernetesClient().pods().inNamespace(namespaceName).withName(podName).get();
    MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
    exchange.getOut().setBody(pod);
}
Also used : DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) Pod(io.fabric8.kubernetes.api.model.Pod)

Example 4 with Pod

use of io.fabric8.kubernetes.api.model.Pod 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 5 with Pod

use of io.fabric8.kubernetes.api.model.Pod 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)

Aggregations

Pod (io.fabric8.kubernetes.api.model.Pod)14 OpenShiftException (org.eclipse.che.plugin.openshift.client.exception.OpenShiftException)5 PodList (io.fabric8.kubernetes.api.model.PodList)4 Service (io.fabric8.kubernetes.api.model.Service)4 ImageStreamTag (io.fabric8.openshift.api.model.ImageStreamTag)4 KubernetesService (org.eclipse.che.plugin.openshift.client.kubernetes.KubernetesService)4 DoneablePod (io.fabric8.kubernetes.api.model.DoneablePod)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Test (org.junit.Test)3 Container (io.fabric8.kubernetes.api.model.Container)2 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)2 ServiceList (io.fabric8.kubernetes.api.model.ServiceList)2 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)2 ImageStream (io.fabric8.openshift.api.model.ImageStream)2 ArrayList (java.util.ArrayList)2 ImageInfo (org.eclipse.che.plugin.docker.client.json.ImageInfo)2 ContainerInNetwork (org.eclipse.che.plugin.docker.client.json.network.ContainerInNetwork)2 Ipam (org.eclipse.che.plugin.docker.client.json.network.Ipam)2