Search in sources :

Example 16 with KubernetesNamespace

use of org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespace 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 17 with KubernetesNamespace

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

the class PluginBrokerManager method getTooling.

/**
 * Deploys Che plugin brokers in a workspace, receives result of theirs execution and returns
 * resolved workspace tooling or error of plugins brokering execution.
 *
 * <p>This API is in <b>Beta</b> and is subject to changes or removal.
 */
@Beta
@Traced
public List<ChePlugin> getTooling(RuntimeIdentity identity, StartSynchronizer startSynchronizer, Collection<PluginFQN> pluginFQNs, boolean isEphemeral, boolean mergePlugins, Map<String, String> startOptions) throws InfrastructureException {
    String workspaceId = identity.getWorkspaceId();
    KubernetesNamespace kubernetesNamespace = factory.getOrCreate(identity);
    BrokersResult brokersResult = new BrokersResult();
    E brokerEnvironment = brokerEnvironmentFactory.createForMetadataBroker(pluginFQNs, identity, mergePlugins);
    if (isEphemeral) {
        EphemeralWorkspaceUtility.makeEphemeral(brokerEnvironment.getAttributes());
    }
    environmentProvisioner.provision(brokerEnvironment, identity);
    ListenBrokerEvents listenBrokerEvents = getListenEventPhase(workspaceId, brokersResult);
    PrepareStorage prepareStorage = getPrepareStoragePhase(identity, startSynchronizer, brokerEnvironment, startOptions);
    WaitBrokerResult waitBrokerResult = getWaitBrokerPhase(workspaceId, brokersResult);
    DeployBroker deployBroker = getDeployBrokerPhase(identity, kubernetesNamespace, brokerEnvironment, brokersResult, startOptions);
    LOG.debug("Entering plugin brokers deployment chain workspace '{}'", workspaceId);
    listenBrokerEvents.then(prepareStorage).then(deployBroker).then(waitBrokerResult);
    return listenBrokerEvents.execute();
}
Also used : ListenBrokerEvents(org.eclipse.che.workspace.infrastructure.kubernetes.wsplugins.brokerphases.ListenBrokerEvents) WaitBrokerResult(org.eclipse.che.workspace.infrastructure.kubernetes.wsplugins.brokerphases.WaitBrokerResult) DeployBroker(org.eclipse.che.workspace.infrastructure.kubernetes.wsplugins.brokerphases.DeployBroker) PrepareStorage(org.eclipse.che.workspace.infrastructure.kubernetes.wsplugins.brokerphases.PrepareStorage) KubernetesNamespace(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespace) Traced(org.eclipse.che.commons.annotation.Traced) Beta(com.google.common.annotations.Beta)

Example 18 with KubernetesNamespace

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

the class CommonPVCStrategy method prepare.

@Override
@Traced
public void prepare(KubernetesEnvironment k8sEnv, RuntimeIdentity identity, long timeoutMillis, Map<String, String> startOptions) throws InfrastructureException {
    String workspaceId = identity.getWorkspaceId();
    TracingTags.WORKSPACE_ID.set(workspaceId);
    if (EphemeralWorkspaceUtility.isEphemeral(k8sEnv.getAttributes())) {
        return;
    }
    log.debug("Preparing PVC started for workspace '{}'", workspaceId);
    Map<String, PersistentVolumeClaim> claims = k8sEnv.getPersistentVolumeClaims();
    if (claims.isEmpty()) {
        return;
    }
    if (claims.size() > 1) {
        throw new InfrastructureException(format("The only one PVC MUST be present in common strategy while it contains: %s.", claims.keySet().stream().collect(joining(", "))));
    }
    PersistentVolumeClaim commonPVC = claims.values().iterator().next();
    final KubernetesNamespace namespace = factory.getOrCreate(identity);
    final KubernetesPersistentVolumeClaims pvcs = namespace.persistentVolumeClaims();
    final Set<String> existing = pvcs.get().stream().map(p -> p.getMetadata().getName()).collect(toSet());
    if (!existing.contains(commonPVC.getMetadata().getName())) {
        log.debug("Creating PVC for workspace '{}'", workspaceId);
        pvcs.create(commonPVC);
        if (waitBound) {
            log.debug("Waiting for PVC for workspace '{}' to be bound", workspaceId);
            pvcs.waitBound(commonPVC.getMetadata().getName(), timeoutMillis);
        }
    }
    final String[] subpaths = (String[]) commonPVC.getAdditionalProperties().remove(format(SUBPATHS_PROPERTY_FMT, workspaceId));
    if (preCreateDirs && subpaths != null) {
        pvcSubPathHelper.createDirs(identity, workspaceId, commonPVC.getMetadata().getName(), startOptions, subpaths);
    }
    log.debug("Preparing PVC done for workspace '{}'", workspaceId);
}
Also used : VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) Workspace(org.eclipse.che.api.core.model.workspace.Workspace) PERSONAL_ACCOUNT(org.eclipse.che.api.user.server.UserManager.PERSONAL_ACCOUNT) Inject(com.google.inject.Inject) Page(org.eclipse.che.api.core.Page) LoggerFactory(org.slf4j.LoggerFactory) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) AccountImpl(org.eclipse.che.account.spi.AccountImpl) Traced(org.eclipse.che.commons.annotation.Traced) KubernetesNamespace(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespace) Map(java.util.Map) Named(javax.inject.Named) Collectors.toSet(java.util.stream.Collectors.toSet) KubernetesObjectUtil.newPVC(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.newPVC) WorkspaceManager(org.eclipse.che.api.workspace.server.WorkspaceManager) Logger(org.slf4j.Logger) Set(java.util.Set) KubernetesPersistentVolumeClaims(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesPersistentVolumeClaims) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) KubernetesNamespaceFactory(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespaceFactory) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) ServerException(org.eclipse.che.api.core.ServerException) TracingTags(org.eclipse.che.commons.tracing.TracingTags) RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) KubernetesPersistentVolumeClaims(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesPersistentVolumeClaims) KubernetesNamespace(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespace) Traced(org.eclipse.che.commons.annotation.Traced)

Example 19 with KubernetesNamespace

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

the class PreviewUrlCommandProvisioner method injectsPreviewUrlToCommands.

/**
 * Go through all commands, find matching service and exposed host. Then construct full preview
 * url from this data and set it as Command's parameter under `previewUrl` key.
 *
 * @param env environment to get commands
 * @param namespace current kubernetes namespace where we're looking for services and ingresses
 */
private void injectsPreviewUrlToCommands(E env, KubernetesNamespace namespace) throws InfrastructureException {
    if (env.getCommands() == null) {
        return;
    }
    List<T> exposureObjects = loadExposureObjects(namespace);
    List<Service> services = namespace.services().get();
    for (CommandImpl command : env.getCommands().stream().filter(c -> c.getPreviewUrl() != null).collect(Collectors.toList())) {
        Optional<Service> foundService = Services.findServiceWithPort(services, command.getPreviewUrl().getPort());
        if (!foundService.isPresent()) {
            String message = String.format("unable to find service for port '%s' for command '%s'", command.getPreviewUrl().getPort(), command.getName());
            LOG.warn(message);
            env.addWarning(new WarningImpl(NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL, String.format(NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL_MESSAGE, message)));
            continue;
        }
        Optional<String> foundHost = findHostForServicePort(exposureObjects, foundService.get(), command.getPreviewUrl().getPort());
        if (foundHost.isPresent()) {
            command.getAttributes().put(PREVIEW_URL_ATTRIBUTE, foundHost.get());
        } else {
            String message = String.format("unable to find ingress for service '%s' and port '%s'", foundService.get(), command.getPreviewUrl().getPort());
            LOG.warn(message);
            env.addWarning(new WarningImpl(NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL, String.format(NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL_MESSAGE, message)));
        }
    }
}
Also used : CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL(org.eclipse.che.workspace.infrastructure.kubernetes.Warnings.NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL) Logger(org.slf4j.Logger) WarningImpl(org.eclipse.che.api.workspace.server.model.impl.WarningImpl) CommandImpl(org.eclipse.che.api.workspace.server.model.impl.CommandImpl) LoggerFactory(org.slf4j.LoggerFactory) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Collectors(java.util.stream.Collectors) KubernetesNamespace(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespace) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) List(java.util.List) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) Services(org.eclipse.che.workspace.infrastructure.kubernetes.util.Services) NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL_MESSAGE(org.eclipse.che.workspace.infrastructure.kubernetes.Warnings.NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL_MESSAGE) Ingress(io.fabric8.kubernetes.api.model.networking.v1.Ingress) Optional(java.util.Optional) Service(io.fabric8.kubernetes.api.model.Service) PREVIEW_URL_ATTRIBUTE(org.eclipse.che.api.core.model.workspace.config.Command.PREVIEW_URL_ATTRIBUTE) WarningImpl(org.eclipse.che.api.workspace.server.model.impl.WarningImpl) Service(io.fabric8.kubernetes.api.model.Service)

Example 20 with KubernetesNamespace

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

the class OpenShiftPreviewUrlCommandProvisionerTest method throwsInfrastructureExceptionWhenK8sNamespaces.

@Test(expectedExceptions = InternalInfrastructureException.class)
public void throwsInfrastructureExceptionWhenK8sNamespaces() throws InfrastructureException {
    KubernetesNamespace namespace = Mockito.mock(KubernetesNamespace.class);
    previewUrlCommandProvisioner.provision(mockEnvironment, namespace);
}
Also used : KubernetesNamespace(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespace) Test(org.testng.annotations.Test)

Aggregations

KubernetesNamespace (org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespace)26 Test (org.testng.annotations.Test)18 AccountImpl (org.eclipse.che.account.spi.AccountImpl)10 Page (org.eclipse.che.api.core.Page)10 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)10 LabelSelector (io.fabric8.kubernetes.api.model.LabelSelector)8 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)8 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)8 Secret (io.fabric8.kubernetes.api.model.Secret)8 SecretBuilder (io.fabric8.kubernetes.api.model.SecretBuilder)8 PersonalAccessToken (org.eclipse.che.api.factory.server.scm.PersonalAccessToken)8 SubjectImpl (org.eclipse.che.commons.subject.SubjectImpl)8 KubernetesNamespaceMetaImpl (org.eclipse.che.workspace.infrastructure.kubernetes.api.server.impls.KubernetesNamespaceMetaImpl)8 KubernetesNamespaceMeta (org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta)8 KubernetesSecrets (org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesSecrets)8 HashMap (java.util.HashMap)6 WorkspaceConfigImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl)6 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 KubernetesPersistentVolumeClaims (org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesPersistentVolumeClaims)5