Search in sources :

Example 6 with KubernetesMachineImpl

use of org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesMachineImpl in project che-server by eclipse-che.

the class KubernetesInternalRuntime method watchLogsIfDebugEnabled.

private void watchLogsIfDebugEnabled(Map<String, String> startOptions) throws InfrastructureException {
    if (LogWatcher.shouldWatchLogs(startOptions)) {
        LOG.info("Debug workspace startup. Will watch the logs of '{}'", getContext().getIdentity().getWorkspaceId());
        // get all the pods we care about
        Set<String> podNames = machines.getMachines(getContext().getIdentity()).values().stream().filter(Objects::nonNull).map(KubernetesMachineImpl::getPodName).filter(Objects::nonNull).collect(Collectors.toSet());
        LOG.debug("Watch '{}' pods in workspace '{}'", podNames, getContext().getIdentity().getWorkspaceId());
        namespace.deployments().watchLogs(new PodLogToEventPublisher(this.eventPublisher, this.getContext().getIdentity()), this.eventPublisher, LogWatchTimeouts.DEFAULT, podNames, LogWatcher.getLogLimitBytes(startOptions));
    }
}
Also used : KubernetesMachineImpl(org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesMachineImpl) PodLogToEventPublisher(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.log.PodLogToEventPublisher)

Example 7 with KubernetesMachineImpl

use of org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesMachineImpl in project che-server by eclipse-che.

the class KubernetesInternalRuntime method checkServers.

/**
 * Returns a function, the result of which the completable stage that performs servers checks and
 * start of servers probes.
 */
private Function<Void, CompletionStage<Void>> checkServers(List<CompletableFuture<?>> toCancelFutures, KubernetesMachineImpl machine) {
    // Need to get active span here to allow use in returned function;
    final Span activeSpan = tracer.activeSpan();
    return ignored -> {
        // Span must be created within this lambda block, otherwise the span begins as soon as
        // this function is called (i.e. before the previous steps in the machine boot chain
        // are complete
        final Span tracingSpan = tracer.buildSpan(CHECK_SERVERS).asChildOf(activeSpan).start();
        TracingTags.WORKSPACE_ID.set(tracingSpan, getContext().getIdentity().getWorkspaceId());
        TracingTags.MACHINE_NAME.set(tracingSpan, machine.getName());
        // This completable future is used to unity the servers checks and start of probes
        final CompletableFuture<Void> serversAndProbesFuture = new CompletableFuture<>();
        final String machineName = machine.getName();
        final RuntimeIdentity runtimeId = getContext().getIdentity();
        final ServersChecker serverCheck = serverCheckerFactory.create(runtimeId, machineName, machine.getServers());
        final CompletableFuture<?> serversReadyFuture;
        LOG.debug("Performing servers check for machine '{}' in workspace '{}'", machineName, runtimeId.getWorkspaceId());
        try {
            serversReadyFuture = serverCheck.startAsync(new ServerReadinessHandler(machineName));
            toCancelFutures.add(serversReadyFuture);
            serversAndProbesFuture.whenComplete((ok, ex) -> {
                LOG.debug("Servers checks done for machine '{}' in workspace '{}'", machineName, runtimeId.getWorkspaceId());
                serversReadyFuture.cancel(true);
            });
        } catch (InfrastructureException ex) {
            serversAndProbesFuture.completeExceptionally(ex);
            TracingTags.setErrorStatus(tracingSpan, ex);
            tracingSpan.finish();
            return serversAndProbesFuture;
        }
        serversReadyFuture.whenComplete((BiConsumer<Object, Throwable>) (ok, ex) -> {
            if (ex != null) {
                serversAndProbesFuture.completeExceptionally(ex);
                TracingTags.setErrorStatus(tracingSpan, ex);
                tracingSpan.finish();
                return;
            }
            try {
                probeScheduler.schedule(probesFactory.getProbes(runtimeId, machineName, machine.getServers()), new ServerLivenessHandler());
            } catch (InfrastructureException iex) {
                serversAndProbesFuture.completeExceptionally(iex);
            }
            serversAndProbesFuture.complete(null);
            tracingSpan.finish();
        });
        return serversAndProbesFuture;
    };
}
Also used : ProbeResult(org.eclipse.che.api.workspace.server.hc.probe.ProbeResult) ServerStatus(org.eclipse.che.api.core.model.workspace.runtime.ServerStatus) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) Assisted(com.google.inject.assistedinject.Assisted) KubernetesObjectUtil.putLabels(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.putLabels) KubernetesRuntimeState(org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesRuntimeState) Collectors.toMap(java.util.stream.Collectors.toMap) PodMerger(org.eclipse.che.workspace.infrastructure.kubernetes.environment.PodMerger) Map(java.util.Map) StateException(org.eclipse.che.api.workspace.server.spi.StateException) KubernetesObjectUtil.putAnnotations(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.putAnnotations) CHECK_SERVERS(org.eclipse.che.workspace.infrastructure.kubernetes.util.TracingSpanConstants.CHECK_SERVERS) Command(org.eclipse.che.api.core.model.workspace.config.Command) WorkspaceProbes(org.eclipse.che.api.workspace.server.hc.probe.WorkspaceProbes) ImmutableMap(com.google.common.collect.ImmutableMap) KubernetesServerResolverFactory(org.eclipse.che.workspace.infrastructure.kubernetes.server.resolver.KubernetesServerResolverFactory) Collection(java.util.Collection) Set(java.util.Set) KubernetesMachineCache(org.eclipse.che.workspace.infrastructure.kubernetes.cache.KubernetesMachineCache) SidecarToolingProvisioner(org.eclipse.che.workspace.infrastructure.kubernetes.wsplugins.SidecarToolingProvisioner) NoOpURLRewriter(org.eclipse.che.api.workspace.server.URLRewriter.NoOpURLRewriter) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) RuntimeStartInterruptedException(org.eclipse.che.api.workspace.server.spi.RuntimeStartInterruptedException) KubernetesObjectUtil.shouldCreateInCheNamespace(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.shouldCreateInCheNamespace) Objects(java.util.Objects) InternalMachineConfig(org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig) KubernetesSharedPool(org.eclipse.che.workspace.infrastructure.kubernetes.util.KubernetesSharedPool) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) List(java.util.List) WorkspaceVolumesStrategy(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.pvc.WorkspaceVolumesStrategy) CompletionStage(java.util.concurrent.CompletionStage) Ingress(io.fabric8.kubernetes.api.model.networking.v1.Ingress) TracingTags(org.eclipse.che.commons.tracing.TracingTags) UnrecoverablePodEventListenerFactory(org.eclipse.che.workspace.infrastructure.kubernetes.util.UnrecoverablePodEventListenerFactory) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) Entry(java.util.Map.Entry) Secret(io.fabric8.kubernetes.api.model.Secret) Span(io.opentracing.Span) Optional(java.util.Optional) Scope(io.opentracing.Scope) WorkspaceProbesFactory(org.eclipse.che.api.workspace.server.hc.probe.WorkspaceProbesFactory) LogWatcher(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.log.LogWatcher) WAIT_RUNNING_ASYNC(org.eclipse.che.workspace.infrastructure.kubernetes.util.TracingSpanConstants.WAIT_RUNNING_ASYNC) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MachineStatus(org.eclipse.che.api.core.model.workspace.runtime.MachineStatus) InternalEnvironmentProvisioner(org.eclipse.che.api.workspace.server.spi.provision.InternalEnvironmentProvisioner) Container(io.fabric8.kubernetes.api.model.Container) CompletableFuture(java.util.concurrent.CompletableFuture) ServerResolver(org.eclipse.che.workspace.infrastructure.kubernetes.server.resolver.ServerResolver) KubernetesMachineImpl(org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesMachineImpl) Function(java.util.function.Function) Traced(org.eclipse.che.commons.annotation.Traced) ValidationException(org.eclipse.che.api.core.ValidationException) ArrayList(java.util.ArrayList) CheNamespace(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.CheNamespace) LinkedHashMap(java.util.LinkedHashMap) Inject(javax.inject.Inject) ServersChecker(org.eclipse.che.api.workspace.server.hc.ServersChecker) KubernetesNamespace(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespace) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) BiConsumer(java.util.function.BiConsumer) WAIT_MACHINES_START(org.eclipse.che.workspace.infrastructure.kubernetes.util.TracingSpanConstants.WAIT_MACHINES_START) Service(io.fabric8.kubernetes.api.model.Service) Named(javax.inject.Named) RuntimeEventsPublisher(org.eclipse.che.workspace.infrastructure.kubernetes.util.RuntimeEventsPublisher) PodLogToEventPublisher(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.log.PodLogToEventPublisher) ProbeScheduler(org.eclipse.che.api.workspace.server.hc.probe.ProbeScheduler) InternalRuntime(org.eclipse.che.api.workspace.server.spi.InternalRuntime) LogWatchTimeouts(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.log.LogWatchTimeouts) Collections.emptyMap(java.util.Collections.emptyMap) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) Executor(java.util.concurrent.Executor) WorkspaceStatus(org.eclipse.che.api.core.model.workspace.WorkspaceStatus) Pod(io.fabric8.kubernetes.api.model.Pod) SecretAsContainerResourceProvisioner(org.eclipse.che.workspace.infrastructure.kubernetes.provision.secret.SecretAsContainerResourceProvisioner) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) KubernetesRuntimeStateCache(org.eclipse.che.workspace.infrastructure.kubernetes.cache.KubernetesRuntimeStateCache) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) PodEvent(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.event.PodEvent) PreviewUrlCommandProvisioner(org.eclipse.che.workspace.infrastructure.kubernetes.provision.PreviewUrlCommandProvisioner) ProbeStatus(org.eclipse.che.api.workspace.server.hc.probe.ProbeResult.ProbeStatus) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) ServersCheckerFactory(org.eclipse.che.api.workspace.server.hc.ServersCheckerFactory) RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) CompletableFuture(java.util.concurrent.CompletableFuture) ServersChecker(org.eclipse.che.api.workspace.server.hc.ServersChecker) Span(io.opentracing.Span) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) BiConsumer(java.util.function.BiConsumer)

Example 8 with KubernetesMachineImpl

use of org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesMachineImpl in project che-server by eclipse-che.

the class KubernetesInternalRuntime method storeStartingMachine.

/**
 * Puts createdPod in the {@code machines} map and sends the starting event for this machine
 */
private void storeStartingMachine(Pod createdPod, ObjectMeta toCreateMeta, Map<String, InternalMachineConfig> machineConfigs, ServerResolver serverResolver) throws InfrastructureException {
    final String workspaceId = getContext().getIdentity().getWorkspaceId();
    for (Container container : createdPod.getSpec().getContainers()) {
        String machineName = Names.machineName(toCreateMeta, container);
        LOG.debug("Creating machine '{}' in workspace '{}'", machineName, workspaceId);
        // Sometimes we facing NPE trying to retrieve machine config. Possible names mismatch. Need to
        // get more info on that cases.
        InternalMachineConfig machineConfig = Optional.ofNullable(machineConfigs.get(machineName)).orElseThrow(() -> {
            LOG.error("Workspace '{}' start failed. Machine with name '{}' requested but not found in configs map. Present machines are: {}.", workspaceId, machineName, String.join(",", machineConfigs.keySet()));
            return new InfrastructureException(format("Unable to start the workspace '%s' due to an internal inconsistency while composing the workspace runtime." + "Please report a bug. If possible, include the details from Che devfile and server log in bug report (your admin can help with that)", workspaceId));
        });
        machines.put(getContext().getIdentity(), new KubernetesMachineImpl(workspaceId, machineName, createdPod.getMetadata().getName(), container.getName(), MachineStatus.STARTING, machineConfig.getAttributes(), serverResolver.resolve(machineName)));
        eventPublisher.sendStartingEvent(machineName, getContext().getIdentity());
    }
}
Also used : InternalMachineConfig(org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig) Container(io.fabric8.kubernetes.api.model.Container) KubernetesMachineImpl(org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesMachineImpl) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException)

Example 9 with KubernetesMachineImpl

use of org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesMachineImpl in project che-server by eclipse-che.

the class JpaKubernetesMachineCache method doUpdateMachineStatus.

@Transactional
protected void doUpdateMachineStatus(String workspaceId, String machineName, MachineStatus status) throws InfrastructureException {
    EntityManager entityManager = managerProvider.get();
    KubernetesMachineImpl machine = entityManager.find(KubernetesMachineImpl.class, new MachineId(workspaceId, machineName));
    if (machine == null) {
        throw new InfrastructureException(format("Machine '%s:%s' was not found", workspaceId, machineName));
    }
    machine.setStatus(status);
    entityManager.flush();
}
Also used : MachineId(org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesMachineImpl.MachineId) EntityManager(javax.persistence.EntityManager) KubernetesMachineImpl(org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesMachineImpl) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) Transactional(com.google.inject.persist.Transactional)

Example 10 with KubernetesMachineImpl

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

the class KubernetesInternalRuntime method internalStart.

@Override
protected void internalStart(Map<String, String> startOptions) throws InfrastructureException {
    KubernetesRuntimeContext<E> context = getContext();
    String workspaceId = context.getIdentity().getWorkspaceId();
    try {
        startSynchronizer.setStartThread();
        startSynchronizer.start();
        runtimeCleaner.cleanUp(namespace, workspaceId);
        provisionWorkspace(startOptions, context, workspaceId);
        volumesStrategy.prepare(context.getEnvironment(), context.getIdentity(), startSynchronizer.getStartTimeoutMillis(), startOptions);
        startSynchronizer.checkFailure();
        startMachines();
        watchLogsIfDebugEnabled(startOptions);
        previewUrlCommandProvisioner.provision(context.getEnvironment(), namespace);
        runtimeStates.updateCommands(context.getIdentity(), context.getEnvironment().getCommands());
        startSynchronizer.checkFailure();
        final Map<String, CompletableFuture<Void>> machinesFutures = new LinkedHashMap<>();
        // futures that must be cancelled explicitly
        final List<CompletableFuture<?>> toCancelFutures = new CopyOnWriteArrayList<>();
        final EnvironmentContext currentContext = EnvironmentContext.getCurrent();
        CompletableFuture<Void> startFailure = startSynchronizer.getStartFailure();
        Span waitRunningAsyncSpan = tracer.buildSpan(WAIT_MACHINES_START).start();
        try (Scope waitRunningAsyncScope = tracer.scopeManager().activate(waitRunningAsyncSpan)) {
            TracingTags.WORKSPACE_ID.set(waitRunningAsyncSpan, workspaceId);
            for (KubernetesMachineImpl machine : machines.getMachines(context.getIdentity()).values()) {
                String machineName = machine.getName();
                final CompletableFuture<Void> machineBootChain = waitRunningAsync(toCancelFutures, machine).thenComposeAsync(checkFailure(startFailure), executor).thenRun(publishRunningStatus(machineName)).thenCompose(checkFailure(startFailure)).thenCompose(setContext(currentContext, checkServers(toCancelFutures, machine))).exceptionally(publishFailedStatus(startFailure, machineName));
                machinesFutures.put(machineName, machineBootChain);
            }
            waitMachines(machinesFutures, toCancelFutures, startFailure);
        } finally {
            waitRunningAsyncSpan.finish();
        }
        startSynchronizer.complete();
    } catch (InfrastructureException | RuntimeException e) {
        Exception startFailureCause = startSynchronizer.getStartFailureNow();
        if (startFailureCause == null) {
            startFailureCause = e;
        }
        startSynchronizer.completeExceptionally(startFailureCause);
        LOG.warn("Failed to start Kubernetes runtime of workspace {}.", workspaceId, startFailureCause);
        boolean interrupted = Thread.interrupted() || startFailureCause instanceof RuntimeStartInterruptedException;
        // Cancels workspace servers probes if any
        probeScheduler.cancel(workspaceId);
        // stop watching before namespace cleaning up
        namespace.deployments().stopWatch(true);
        try {
            runtimeCleaner.cleanUp(namespace, workspaceId);
        } catch (InfrastructureException cleanUppingEx) {
            LOG.warn("Failed to clean up namespace after workspace '{}' start failing.", context.getIdentity().getWorkspaceId(), cleanUppingEx);
        }
        if (interrupted) {
            throw new RuntimeStartInterruptedException(getContext().getIdentity());
        }
        wrapAndRethrow(startFailureCause);
    } finally {
        namespace.deployments().stopWatch();
    }
}
Also used : Span(io.opentracing.Span) TimeoutException(java.util.concurrent.TimeoutException) StateException(org.eclipse.che.api.workspace.server.spi.StateException) RuntimeStartInterruptedException(org.eclipse.che.api.workspace.server.spi.RuntimeStartInterruptedException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) ValidationException(org.eclipse.che.api.core.ValidationException) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) ExecutionException(java.util.concurrent.ExecutionException) LinkedHashMap(java.util.LinkedHashMap) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) CompletableFuture(java.util.concurrent.CompletableFuture) Scope(io.opentracing.Scope) KubernetesMachineImpl(org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesMachineImpl) RuntimeStartInterruptedException(org.eclipse.che.api.workspace.server.spi.RuntimeStartInterruptedException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

KubernetesMachineImpl (org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesMachineImpl)38 Test (org.testng.annotations.Test)20 RuntimeIdentity (org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity)12 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)8 KubernetesMachineCache (org.eclipse.che.workspace.infrastructure.kubernetes.cache.KubernetesMachineCache)8 KubernetesRuntimeStateCache (org.eclipse.che.workspace.infrastructure.kubernetes.cache.KubernetesRuntimeStateCache)8 KubernetesRuntimeState (org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesRuntimeState)8 KubernetesServerImpl (org.eclipse.che.workspace.infrastructure.kubernetes.model.KubernetesServerImpl)8 TypeLiteral (com.google.inject.TypeLiteral)6 AccountImpl (org.eclipse.che.account.spi.AccountImpl)6 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)6 InternalInfrastructureException (org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException)6 PersistTestModuleBuilder (org.eclipse.che.commons.test.db.PersistTestModuleBuilder)6 TckResourcesCleaner (org.eclipse.che.commons.test.tck.TckResourcesCleaner)6 DBInitializer (org.eclipse.che.core.db.DBInitializer)6 SchemaInitializer (org.eclipse.che.core.db.schema.SchemaInitializer)6 FlywaySchemaInitializer (org.eclipse.che.core.db.schema.impl.flyway.FlywaySchemaInitializer)6 AccountDao (org.eclipse.che.account.spi.AccountDao)3 JpaAccountDao (org.eclipse.che.account.spi.jpa.JpaAccountDao)3 JpaUserDevfileDao (org.eclipse.che.api.devfile.server.jpa.JpaUserDevfileDao)3