Search in sources :

Example 91 with InfrastructureException

use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project devspaces-images by redhat-developer.

the class KubernetesNamespace method waitDefaultServiceAccount.

/**
 * Waits few seconds until 'default' service account become available otherwise an infrastructure
 * exception will be thrown.
 */
protected void waitDefaultServiceAccount(String namespaceName, KubernetesClient client) throws InfrastructureException {
    final Predicate<ServiceAccount> predicate = Objects::nonNull;
    final CompletableFuture<ServiceAccount> future = new CompletableFuture<>();
    Watch watch = null;
    try {
        final Resource<ServiceAccount> saResource = client.serviceAccounts().inNamespace(namespaceName).withName(DEFAULT_SERVICE_ACCOUNT_NAME);
        watch = saResource.watch(new Watcher<>() {

            @Override
            public void eventReceived(Action action, ServiceAccount serviceAccount) {
                if (predicate.test(serviceAccount)) {
                    future.complete(serviceAccount);
                }
            }

            @Override
            public void onClose(WatcherException cause) {
                future.completeExceptionally(new InfrastructureException("Waiting for service account '" + DEFAULT_SERVICE_ACCOUNT_NAME + "' was interrupted"));
            }
        });
        if (predicate.test(saResource.get())) {
            return;
        }
        try {
            future.get(SERVICE_ACCOUNT_READINESS_TIMEOUT_SEC, TimeUnit.SECONDS);
        } catch (ExecutionException ex) {
            throw new InfrastructureException(ex.getCause().getMessage(), ex);
        } catch (TimeoutException ex) {
            throw new InfrastructureException("Waiting for service account '" + DEFAULT_SERVICE_ACCOUNT_NAME + "' reached timeout");
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
            throw new InfrastructureException("Waiting for service account '" + DEFAULT_SERVICE_ACCOUNT_NAME + "' was interrupted");
        }
    } catch (KubernetesClientException ex) {
        throw new KubernetesInfrastructureException(ex);
    } finally {
        if (watch != null) {
            watch.close();
        }
    }
}
Also used : ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount) Watcher(io.fabric8.kubernetes.client.Watcher) KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) WatcherException(io.fabric8.kubernetes.client.WatcherException) CompletableFuture(java.util.concurrent.CompletableFuture) Watch(io.fabric8.kubernetes.client.Watch) ExecutionException(java.util.concurrent.ExecutionException) KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) TimeoutException(java.util.concurrent.TimeoutException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 92 with InfrastructureException

use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project devspaces-images by redhat-developer.

the class KubernetesNamespace method label.

/**
 * Applies given `ensureLabels` into given `namespace` and update the `namespace` in the
 * Kubernetes.
 *
 * <p>If we do not have permissions to do so (code=403), this method does not throw any exception.
 *
 * @param namespace namespace to label
 * @param ensureLabels these labels should be applied on given `namespace`
 * @throws InfrastructureException if something goes wrong with update, except lack of permissions
 */
protected void label(Namespace namespace, Map<String, String> ensureLabels) throws InfrastructureException {
    if (ensureLabels.isEmpty()) {
        return;
    }
    Map<String, String> currentLabels = namespace.getMetadata().getLabels();
    Map<String, String> newLabels = currentLabels != null ? new HashMap<>(currentLabels) : new HashMap<>();
    if (newLabels.entrySet().containsAll(ensureLabels.entrySet())) {
        LOG.debug("Nothing to do, namespace [{}] already has all required labels.", namespace.getMetadata().getName());
        return;
    }
    try {
        // update the namespace with new labels
        cheSAClientFactory.create().namespaces().createOrReplace(new NamespaceBuilder(namespace).editMetadata().addToLabels(ensureLabels).endMetadata().build());
    } catch (KubernetesClientException kce) {
        if (kce.getCode() == 403) {
            LOG.warn("Can't label the namespace due to lack of permissions. Grant cluster-wide permissions " + "to `get` and `update` the `namespaces` to the `che` service account " + "(Che operator might have already prepared a cluster role called " + "`che-namespace-editor` for this, depending on its configuration). " + "Alternatively, consider disabling the feature by setting " + "`che.infra.kubernetes.namepsace.label` to `false`.");
            return;
        }
        throw new InfrastructureException(kce);
    }
}
Also used : NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder) KubernetesInfrastructureException(org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 93 with InfrastructureException

use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project devspaces-images by redhat-developer.

the class KubernetesNamespaceFactory method checkIfNamespaceIsAllowed.

/**
 * Checks if the current user is able to use the specified namespace for their new workspaces.
 *
 * @param namespaceName namespace name to check
 * @throws ValidationException if the specified namespace is not permitted for the current user
 */
public void checkIfNamespaceIsAllowed(String namespaceName) throws ValidationException {
    NamespaceResolutionContext context = new NamespaceResolutionContext(EnvironmentContext.getCurrent().getSubject());
    final String defaultNamespace = findStoredNamespace(context).orElse(evalPlaceholders(defaultNamespaceName, context));
    if (!namespaceName.equals(defaultNamespace)) {
        try {
            List<KubernetesNamespaceMeta> labeledNamespaces = findPreparedNamespaces(context);
            if (labeledNamespaces.stream().noneMatch(n -> n.getName().equals(namespaceName))) {
                throw new ValidationException(format("User defined namespaces are not allowed. Only the default namespace '%s' is available.", defaultNamespace));
            }
        } catch (InfrastructureException e) {
            throw new ValidationException("Some infrastructure failure caused failed validation.", e);
        }
    }
}
Also used : NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext) ValidationException(org.eclipse.che.api.core.ValidationException) KubernetesNamespaceMeta(org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Example 94 with InfrastructureException

use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project devspaces-images by redhat-developer.

the class KubernetesNamespaceFactory method canCreateNamespace.

/**
 * Tells the caller whether the namespace that is being prepared for the provided workspace
 * runtime identity can be created or is expected to already be present.
 *
 * <p>Note that this method cannot be reduced to merely checking if user-defined namespaces are
 * allowed or not (and depending on prior validation using the {@link
 * #checkIfNamespaceIsAllowed(String)} method during the workspace creation) because workspace
 * start is a) async from workspace creation and the underlying namespaces might have disappeared
 * and b) can be called during workspace recovery, where we don't even have the current user in
 * the context.
 *
 * @param identity the identity of the workspace runtime
 * @return true if the namespace can be created, false if the namespace is expected to already
 *     exist
 * @throws InfrastructureException on failure
 */
protected boolean canCreateNamespace(RuntimeIdentity identity) throws InfrastructureException {
    if (!namespaceCreationAllowed) {
        return false;
    }
    // we need to make sure that the provided namespace is indeed the one provided by our
    // configuration
    User owner;
    try {
        owner = userManager.getById(identity.getOwnerId());
    } catch (NotFoundException | ServerException e) {
        throw new InfrastructureException("Failed to resolve workspace owner. Cause: " + e.getMessage(), e);
    }
    String requiredNamespace = identity.getInfrastructureNamespace();
    NamespaceResolutionContext resolutionContext = new NamespaceResolutionContext(identity.getWorkspaceId(), identity.getOwnerId(), owner.getName());
    String resolvedDefaultNamespace = evaluateNamespaceName(resolutionContext);
    return resolvedDefaultNamespace.equals(requiredNamespace);
}
Also used : NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext) User(org.eclipse.che.api.core.model.user.User) ServerException(org.eclipse.che.api.core.ServerException) NotFoundException(org.eclipse.che.api.core.NotFoundException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Example 95 with InfrastructureException

use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project devspaces-images by redhat-developer.

the class KubernetesInternalRuntime method markStopping.

@Override
protected void markStopping() throws InfrastructureException {
    RuntimeIdentity runtimeId = getContext().getIdentity();
    // Check if runtime is in STARTING phase to actualize state of startSynchronizer.
    Optional<WorkspaceStatus> statusOpt = runtimeStates.getStatus(runtimeId);
    if (statusOpt.isPresent() && statusOpt.get() == WorkspaceStatus.STARTING) {
        startSynchronizer.start();
    }
    if (!runtimeStates.updateStatus(runtimeId, s -> s == WorkspaceStatus.RUNNING || s == WorkspaceStatus.STARTING, WorkspaceStatus.STOPPING)) {
        throw new StateException("The environment must be running or starting");
    }
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) 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) WorkspaceStatus(org.eclipse.che.api.core.model.workspace.WorkspaceStatus) StateException(org.eclipse.che.api.workspace.server.spi.StateException)

Aggregations

InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)242 InternalInfrastructureException (org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException)64 Test (org.testng.annotations.Test)56 KubernetesInfrastructureException (org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException)44 RuntimeIdentity (org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity)42 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)38 CompletableFuture (java.util.concurrent.CompletableFuture)36 ExecutionException (java.util.concurrent.ExecutionException)36 TimeoutException (java.util.concurrent.TimeoutException)32 ServerException (org.eclipse.che.api.core.ServerException)32 Pod (io.fabric8.kubernetes.api.model.Pod)30 Map (java.util.Map)26 ValidationException (org.eclipse.che.api.core.ValidationException)22 Traced (org.eclipse.che.commons.annotation.Traced)20 Container (io.fabric8.kubernetes.api.model.Container)18 List (java.util.List)18 Set (java.util.Set)18 Inject (javax.inject.Inject)18 RuntimeStartInterruptedException (org.eclipse.che.api.workspace.server.spi.RuntimeStartInterruptedException)18 KubernetesEnvironment (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment)18