Search in sources :

Example 41 with Traced

use of org.eclipse.che.commons.annotation.Traced in project che-server by eclipse-che.

the class UniqueNamesProvisioner method provision.

@Override
@Traced
public void provision(T k8sEnv, RuntimeIdentity identity) throws InfrastructureException {
    final String workspaceId = identity.getWorkspaceId();
    TracingTags.WORKSPACE_ID.set(workspaceId);
    final Map<String, ConfigMap> configMaps = k8sEnv.getConfigMaps();
    Map<String, String> configMapNameTranslation = new HashMap<>();
    for (ConfigMap configMap : configMaps.values()) {
        final String originalName = configMap.getMetadata().getName();
        putLabel(configMap.getMetadata(), Constants.CHE_ORIGINAL_NAME_LABEL, originalName);
        final String uniqueName = Names.uniqueResourceName(originalName, workspaceId);
        configMap.getMetadata().setName(uniqueName);
        configMapNameTranslation.put(originalName, uniqueName);
    }
    final Collection<PodData> podData = k8sEnv.getPodsData().values();
    for (PodData pod : podData) {
        final ObjectMeta podMeta = pod.getMetadata();
        putLabel(podMeta, Constants.CHE_ORIGINAL_NAME_LABEL, podMeta.getName());
        final String podName = Names.uniqueResourceName(podMeta.getName(), workspaceId);
        podMeta.setName(podName);
        if (configMapNameTranslation.size() > 0) {
            rewriteConfigMapNames(pod, configMapNameTranslation);
        }
    }
    // We explicitly need to modify the deployments in the environment to provision unique names
    // for them.
    final Collection<Deployment> deployments = k8sEnv.getDeploymentsCopy().values();
    for (Deployment deployment : deployments) {
        final ObjectMeta deploymentMeta = deployment.getMetadata();
        final String originalName = deploymentMeta.getName();
        putLabel(deploymentMeta, Constants.CHE_ORIGINAL_NAME_LABEL, originalName);
        final String deploymentName = Names.uniqueResourceName(originalName, workspaceId);
        deploymentMeta.setName(deploymentName);
    }
    final Set<Ingress> ingresses = new HashSet<>(k8sEnv.getIngresses().values());
    k8sEnv.getIngresses().clear();
    for (Ingress ingress : ingresses) {
        final ObjectMeta ingressMeta = ingress.getMetadata();
        putLabel(ingress, Constants.CHE_ORIGINAL_NAME_LABEL, ingressMeta.getName());
        final String ingressName = Names.generateName("ingress");
        ingressMeta.setName(ingressName);
        k8sEnv.getIngresses().put(ingressName, ingress);
    }
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) HashMap(java.util.HashMap) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) Ingress(io.fabric8.kubernetes.api.model.networking.v1.Ingress) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) HashSet(java.util.HashSet) Traced(org.eclipse.che.commons.annotation.Traced)

Example 42 with Traced

use of org.eclipse.che.commons.annotation.Traced 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 43 with Traced

use of org.eclipse.che.commons.annotation.Traced in project che-server by eclipse-che.

the class EnvVarsConverter method provision.

@Override
@Traced
public void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity) throws InfrastructureException {
    TracingTags.WORKSPACE_ID.set(identity::getWorkspaceId);
    for (PodData pod : k8sEnv.getPodsData().values()) {
        for (Container container : pod.getSpec().getContainers()) {
            String machineName = Names.machineName(pod, container);
            InternalMachineConfig machineConf = k8sEnv.getMachines().get(machineName);
            // we need to combine the env vars from the machine config with the variables already
            // present in the container. Let's key the variables by name and use the map for merging
            Map<String, EnvVar> envVars = machineConf.getEnv().entrySet().stream().map(e -> new EnvVar(e.getKey(), e.getValue(), null)).collect(toMap(EnvVar::getName, identity()));
            // the env vars defined in our machine config take precedence over the ones already defined
            // in the container, if any
            container.getEnv().forEach(v -> envVars.putIfAbsent(v.getName(), v));
            // The environment variable expansion only works if a variable that is referenced
            // is already defined earlier in the list of environment variables.
            // We need to produce a list where variables that reference others always appear later
            // in the list.
            List<EnvVar> sorted = topoSort.sort(envVars.values());
            container.setEnv(sorted);
        }
    }
}
Also used : EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Container(io.fabric8.kubernetes.api.model.Container) MachineConfig(org.eclipse.che.api.core.model.workspace.config.MachineConfig) ConfigurationProvisioner(org.eclipse.che.workspace.infrastructure.kubernetes.provision.ConfigurationProvisioner) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) EnvVars(org.eclipse.che.workspace.infrastructure.kubernetes.util.EnvVars) Singleton(javax.inject.Singleton) Traced(org.eclipse.che.commons.annotation.Traced) InternalMachineConfig(org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig) TopologicalSort(org.eclipse.che.commons.lang.TopologicalSort) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) List(java.util.List) Names(org.eclipse.che.workspace.infrastructure.kubernetes.Names) Collectors.toMap(java.util.stream.Collectors.toMap) TracingTags(org.eclipse.che.commons.tracing.TracingTags) Map(java.util.Map) RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) Function.identity(java.util.function.Function.identity) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) InternalMachineConfig(org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig) Container(io.fabric8.kubernetes.api.model.Container) Function.identity(java.util.function.Function.identity) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Traced(org.eclipse.che.commons.annotation.Traced)

Example 44 with Traced

use of org.eclipse.che.commons.annotation.Traced in project che-server by eclipse-che.

the class ContainerResourceProvisioner method provision.

@Override
@Traced
public void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity) throws InfrastructureException {
    TracingTags.WORKSPACE_ID.set(identity::getWorkspaceId);
    final Map<String, InternalMachineConfig> machines = k8sEnv.getMachines();
    for (PodData pod : k8sEnv.getPodsData().values()) {
        for (Container container : pod.getSpec().getContainers()) {
            // make sure that machine configs have settings for RAM limit and request
            InternalMachineConfig machineConfig = machines.get(machineName(pod, container));
            ResourceLimitAttributesProvisioner.provisionMemory(machineConfig, Containers.getRamLimit(container), Containers.getRamRequest(container), defaultMachineMaxMemorySizeAttribute, defaultMachineRequestMemorySizeAttribute);
            // make sure that machine configs have settings for CPU limit and request
            ResourceLimitAttributesProvisioner.provisionCPU(machineConfig, Containers.getCpuLimit(container), Containers.getCpuRequest(container), defaultMachineCpuLimitAttribute, defaultMachineCpuRequestAttribute);
            // reapply memory and CPU settings to k8s container to make sure that provisioned
            // values above are set. Non-positive value means that limit is disabled, so just
            // ignoring them.
            final Map<String, String> attributes = machineConfig.getAttributes();
            long memLimit = Long.parseLong(attributes.get(MEMORY_LIMIT_ATTRIBUTE));
            if (memLimit > 0) {
                Containers.addRamLimit(container, memLimit);
            }
            long memRequest = Long.parseLong(attributes.get(MEMORY_REQUEST_ATTRIBUTE));
            if (memRequest > 0) {
                Containers.addRamRequest(container, memRequest);
            }
            float cpuLimit = Float.parseFloat(attributes.get(CPU_LIMIT_ATTRIBUTE));
            if (cpuLimit > 0) {
                Containers.addCpuLimit(container, cpuLimit);
            }
            float cpuRequest = Float.parseFloat(attributes.get(CPU_REQUEST_ATTRIBUTE));
            if (cpuRequest > 0)
                Containers.addCpuRequest(container, cpuRequest);
        }
    }
}
Also used : InternalMachineConfig(org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Container(io.fabric8.kubernetes.api.model.Container) Traced(org.eclipse.che.commons.annotation.Traced)

Example 45 with Traced

use of org.eclipse.che.commons.annotation.Traced in project devspaces-images by redhat-developer.

the class SidecarToolingProvisioner method provision.

@Traced
@Beta
public void provision(RuntimeIdentity identity, StartSynchronizer startSynchronizer, E environment, Map<String, String> startOptions) throws InfrastructureException {
    Collection<PluginFQN> pluginFQNs = pluginFQNParser.parsePlugins(environment.getAttributes());
    if (pluginFQNs.isEmpty()) {
        return;
    }
    LOG.debug("Started sidecar tooling provisioning workspace '{}'", identity.getWorkspaceId());
    String recipeType = environment.getType();
    ChePluginsApplier pluginsApplier = workspaceNextAppliers.get(recipeType);
    if (pluginsApplier == null) {
        throw new InfrastructureException("Sidecar tooling configuration is not supported with environment type " + recipeType);
    }
    boolean isEphemeral = EphemeralWorkspaceUtility.isEphemeral(environment.getAttributes());
    boolean mergePlugins = shouldMergePlugins(environment.getAttributes());
    List<ChePlugin> chePlugins = pluginBrokerManager.getTooling(identity, startSynchronizer, pluginFQNs, isEphemeral, mergePlugins, startOptions);
    pluginsApplier.apply(identity, environment, chePlugins);
    artifactsBrokerApplier.apply(environment, identity, pluginFQNs, mergePlugins);
    LOG.debug("Finished sidecar tooling provisioning workspace '{}'", identity.getWorkspaceId());
}
Also used : ChePlugin(org.eclipse.che.api.workspace.server.wsplugins.model.ChePlugin) PluginFQN(org.eclipse.che.api.workspace.server.wsplugins.model.PluginFQN) ChePluginsApplier(org.eclipse.che.api.workspace.server.wsplugins.ChePluginsApplier) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) Traced(org.eclipse.che.commons.annotation.Traced) Beta(com.google.common.annotations.Beta)

Aggregations

Traced (org.eclipse.che.commons.annotation.Traced)54 PodData (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData)14 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)12 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)10 ArrayList (java.util.ArrayList)10 Service (io.fabric8.kubernetes.api.model.Service)8 Map (java.util.Map)8 RuntimeIdentity (org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity)8 InternalMachineConfig (org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig)8 KubernetesEnvironment (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment)8 Beta (com.google.common.annotations.Beta)6 Container (io.fabric8.kubernetes.api.model.Container)6 Route (io.fabric8.openshift.api.model.Route)6 ValidationException (org.eclipse.che.api.core.ValidationException)6 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)6 InternalInfrastructureException (org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException)5 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)4 PersistentVolumeClaim (io.fabric8.kubernetes.api.model.PersistentVolumeClaim)4 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)4 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)4