Search in sources :

Example 1 with KubernetesDeployments

use of org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesDeployments in project che-server by eclipse-che.

the class PVCSubPathHelper method execute.

private void execute(RuntimeIdentity identity, String workspaceId, String namespace, String pvcName, String[] commandBase, Map<String, String> startOptions, boolean watchFailureEvents, String... arguments) {
    final String jobName = commandBase[0];
    final String podName = jobName + '-' + workspaceId;
    final String[] command = buildCommand(commandBase, arguments);
    final Pod pod = newPod(podName, pvcName, command);
    securityContextProvisioner.provision(pod.getSpec());
    nodeSelectorProvisioner.provision(pod.getSpec());
    tolerationsProvisioner.provision(pod.getSpec());
    KubernetesDeployments deployments = null;
    try {
        KubernetesNamespace ns = factory.access(workspaceId, namespace);
        if (!checkPVCExistsAndNotTerminating(ns, pvcName)) {
            return;
        }
        deployments = ns.deployments();
        deployments.create(pod);
        watchLogsIfDebugEnabled(deployments, pod, identity, startOptions);
        PodStatus finishedStatus = waitPodStatus(podName, deployments, watchFailureEvents);
        if (POD_PHASE_FAILED.equals(finishedStatus.getPhase())) {
            String logs = deployments.getPodLogs(podName);
            LOG.error("Job command '{}' execution is failed. Logs: {}", Arrays.toString(command), // Force logs onto one line
            Strings.nullToEmpty(logs).replace("\n", " \\n"));
        }
    } catch (InfrastructureException ex) {
        LOG.error("Unable to perform '{}' command for the workspace '{}' cause: '{}'", Arrays.toString(command), workspaceId, ex.getMessage());
        deployments.stopWatch(true);
    } finally {
        if (deployments != null) {
            deployments.stopWatch();
            try {
                deployments.delete(podName);
            } catch (InfrastructureException ignored) {
            }
        }
    }
}
Also used : PodStatus(io.fabric8.kubernetes.api.model.PodStatus) Pod(io.fabric8.kubernetes.api.model.Pod) KubernetesDeployments(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesDeployments) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) KubernetesNamespace(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespace)

Example 2 with KubernetesDeployments

use of org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesDeployments in project che-server by eclipse-che.

the class DeployBroker method execute.

@Override
public List<ChePlugin> execute() throws InfrastructureException {
    LOG.debug("Starting brokers pod for workspace '{}'", runtimeId.getWorkspaceId());
    Span tracingSpan = tracer.buildSpan(DEPLOY_BROKER_PHASE).start();
    TracingTags.WORKSPACE_ID.set(tracingSpan, runtimeId.getWorkspaceId());
    KubernetesDeployments deployments = namespace.deployments();
    try {
        // broker in a workspace.
        for (ConfigMap configMap : brokerEnvironment.getConfigMaps().values()) {
            namespace.configMaps().create(configMap);
        }
        for (Secret secret : brokerEnvironment.getSecrets().values()) {
            namespace.secrets().create(secret);
        }
        Pod pluginBrokerPod = getPluginBrokerPod(brokerEnvironment.getPodsCopy());
        if (factory.isConfigured()) {
            UnrecoverablePodEventListener unrecoverableEventListener = factory.create(ImmutableSet.of(pluginBrokerPod.getMetadata().getName()), this::handleUnrecoverableEvent);
            namespace.deployments().watchEvents(unrecoverableEventListener);
        }
        namespace.deployments().watchEvents(new RuntimeLogsPublisher(runtimeEventsPublisher, runtimeId, ImmutableSet.of(pluginBrokerPod.getMetadata().getName())));
        deployments.create(pluginBrokerPod);
        watchLogsIfDebugEnabled(startOptions, pluginBrokerPod);
        LOG.debug("Brokers pod is created for workspace '{}'", runtimeId.getWorkspaceId());
        tracingSpan.finish();
        return nextPhase.execute();
    } catch (InfrastructureException e) {
        namespace.deployments().stopWatch(true);
        // Ensure span is finished with exception message
        TracingTags.setErrorStatus(tracingSpan, e);
        tracingSpan.finish();
        throw e;
    } finally {
        namespace.deployments().stopWatch();
        try {
            deployments.delete();
        } catch (InfrastructureException e) {
            LOG.error("Brokers pod removal failed. Error: " + e.getLocalizedMessage(), e);
        }
        try {
            namespace.secrets().delete();
        } catch (InfrastructureException ex) {
            LOG.error("Brokers secret removal failed. Error: " + ex.getLocalizedMessage(), ex);
        }
        try {
            namespace.configMaps().delete();
        } catch (InfrastructureException ex) {
            LOG.error("Brokers config map removal failed. Error: " + ex.getLocalizedMessage(), ex);
        }
    }
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) RuntimeLogsPublisher(org.eclipse.che.workspace.infrastructure.kubernetes.RuntimeLogsPublisher) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Pod(io.fabric8.kubernetes.api.model.Pod) UnrecoverablePodEventListener(org.eclipse.che.workspace.infrastructure.kubernetes.util.UnrecoverablePodEventListener) Span(io.opentracing.Span) KubernetesDeployments(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesDeployments) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Example 3 with KubernetesDeployments

use of org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesDeployments in project devspaces-images by redhat-developer.

the class PVCSubPathHelper method execute.

private void execute(RuntimeIdentity identity, String workspaceId, String namespace, String pvcName, String[] commandBase, Map<String, String> startOptions, boolean watchFailureEvents, String... arguments) {
    final String jobName = commandBase[0];
    final String podName = jobName + '-' + workspaceId;
    final String[] command = buildCommand(commandBase, arguments);
    final Pod pod = newPod(podName, pvcName, command);
    securityContextProvisioner.provision(pod.getSpec());
    nodeSelectorProvisioner.provision(pod.getSpec());
    tolerationsProvisioner.provision(pod.getSpec());
    KubernetesDeployments deployments = null;
    try {
        KubernetesNamespace ns = factory.access(workspaceId, namespace);
        if (!checkPVCExistsAndNotTerminating(ns, pvcName)) {
            return;
        }
        deployments = ns.deployments();
        deployments.create(pod);
        watchLogsIfDebugEnabled(deployments, pod, identity, startOptions);
        PodStatus finishedStatus = waitPodStatus(podName, deployments, watchFailureEvents);
        if (POD_PHASE_FAILED.equals(finishedStatus.getPhase())) {
            String logs = deployments.getPodLogs(podName);
            LOG.error("Job command '{}' execution is failed. Logs: {}", Arrays.toString(command), // Force logs onto one line
            Strings.nullToEmpty(logs).replace("\n", " \\n"));
        }
    } catch (InfrastructureException ex) {
        LOG.error("Unable to perform '{}' command for the workspace '{}' cause: '{}'", Arrays.toString(command), workspaceId, ex.getMessage());
        deployments.stopWatch(true);
    } finally {
        if (deployments != null) {
            deployments.stopWatch();
            try {
                deployments.delete(podName);
            } catch (InfrastructureException ignored) {
            }
        }
    }
}
Also used : PodStatus(io.fabric8.kubernetes.api.model.PodStatus) Pod(io.fabric8.kubernetes.api.model.Pod) KubernetesDeployments(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesDeployments) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) KubernetesNamespace(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespace)

Example 4 with KubernetesDeployments

use of org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesDeployments in project devspaces-images by redhat-developer.

the class DeployBroker method execute.

@Override
public List<ChePlugin> execute() throws InfrastructureException {
    LOG.debug("Starting brokers pod for workspace '{}'", runtimeId.getWorkspaceId());
    Span tracingSpan = tracer.buildSpan(DEPLOY_BROKER_PHASE).start();
    TracingTags.WORKSPACE_ID.set(tracingSpan, runtimeId.getWorkspaceId());
    KubernetesDeployments deployments = namespace.deployments();
    try {
        // broker in a workspace.
        for (ConfigMap configMap : brokerEnvironment.getConfigMaps().values()) {
            namespace.configMaps().create(configMap);
        }
        for (Secret secret : brokerEnvironment.getSecrets().values()) {
            namespace.secrets().create(secret);
        }
        Pod pluginBrokerPod = getPluginBrokerPod(brokerEnvironment.getPodsCopy());
        if (factory.isConfigured()) {
            UnrecoverablePodEventListener unrecoverableEventListener = factory.create(ImmutableSet.of(pluginBrokerPod.getMetadata().getName()), this::handleUnrecoverableEvent);
            namespace.deployments().watchEvents(unrecoverableEventListener);
        }
        namespace.deployments().watchEvents(new RuntimeLogsPublisher(runtimeEventsPublisher, runtimeId, ImmutableSet.of(pluginBrokerPod.getMetadata().getName())));
        deployments.create(pluginBrokerPod);
        watchLogsIfDebugEnabled(startOptions, pluginBrokerPod);
        LOG.debug("Brokers pod is created for workspace '{}'", runtimeId.getWorkspaceId());
        tracingSpan.finish();
        return nextPhase.execute();
    } catch (InfrastructureException e) {
        namespace.deployments().stopWatch(true);
        // Ensure span is finished with exception message
        TracingTags.setErrorStatus(tracingSpan, e);
        tracingSpan.finish();
        throw e;
    } finally {
        namespace.deployments().stopWatch();
        try {
            deployments.delete();
        } catch (InfrastructureException e) {
            LOG.error("Brokers pod removal failed. Error: " + e.getLocalizedMessage(), e);
        }
        try {
            namespace.secrets().delete();
        } catch (InfrastructureException ex) {
            LOG.error("Brokers secret removal failed. Error: " + ex.getLocalizedMessage(), ex);
        }
        try {
            namespace.configMaps().delete();
        } catch (InfrastructureException ex) {
            LOG.error("Brokers config map removal failed. Error: " + ex.getLocalizedMessage(), ex);
        }
    }
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) RuntimeLogsPublisher(org.eclipse.che.workspace.infrastructure.kubernetes.RuntimeLogsPublisher) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Pod(io.fabric8.kubernetes.api.model.Pod) UnrecoverablePodEventListener(org.eclipse.che.workspace.infrastructure.kubernetes.util.UnrecoverablePodEventListener) Span(io.opentracing.Span) KubernetesDeployments(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesDeployments) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Aggregations

Pod (io.fabric8.kubernetes.api.model.Pod)4 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)4 KubernetesDeployments (org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesDeployments)4 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)2 PodStatus (io.fabric8.kubernetes.api.model.PodStatus)2 Secret (io.fabric8.kubernetes.api.model.Secret)2 Span (io.opentracing.Span)2 InternalInfrastructureException (org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException)2 RuntimeLogsPublisher (org.eclipse.che.workspace.infrastructure.kubernetes.RuntimeLogsPublisher)2 KubernetesNamespace (org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespace)2 UnrecoverablePodEventListener (org.eclipse.che.workspace.infrastructure.kubernetes.util.UnrecoverablePodEventListener)2