Search in sources :

Example 26 with InternalMachineConfig

use of org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig in project che-server by eclipse-che.

the class JwtProxyProvisionerTest method shouldProvisionJwtProxyRelatedObjectsIntoKubernetesEnvironment.

@Test
public void shouldProvisionJwtProxyRelatedObjectsIntoKubernetesEnvironment() throws Exception {
    // given
    ServerConfigImpl secureServer = new ServerConfigImpl("4401/tcp", "ws", "/", emptyMap());
    ServicePort port = new ServicePort();
    port.setTargetPort(new IntOrString(4401));
    // when
    jwtProxyProvisioner.expose(k8sEnv, podWithName(), "machine", "terminal", port, "TCP", false, ImmutableMap.of("server", secureServer));
    // then
    InternalMachineConfig jwtProxyMachine = k8sEnv.getMachines().get(JwtProxyProvisioner.JWT_PROXY_MACHINE_NAME);
    assertNotNull(jwtProxyMachine);
    ConfigMap configMap = k8sEnv.getConfigMaps().get(jwtProxyProvisioner.getConfigMapName());
    assertNotNull(configMap);
    assertEquals(configMap.getData().get(JWT_PROXY_PUBLIC_KEY_FILE), PUBLIC_KEY_HEADER + Base64.getEncoder().encodeToString("publickey".getBytes()) + PUBLIC_KEY_FOOTER);
    assertNotNull(configMap.getData().get(JWT_PROXY_CONFIG_FILE));
    Pod jwtProxyPod = k8sEnv.getInjectablePodsCopy().getOrDefault("machine", emptyMap()).get("che-jwtproxy");
    assertNotNull(jwtProxyPod);
    assertEquals(1, jwtProxyPod.getSpec().getContainers().size());
    Container jwtProxyContainer = jwtProxyPod.getSpec().getContainers().get(0);
    assertEquals(jwtProxyContainer.getArgs().size(), 2);
    assertEquals(jwtProxyContainer.getArgs().get(0), "-config");
    assertEquals(jwtProxyContainer.getArgs().get(1), JWT_PROXY_CONFIG_FOLDER + "/" + JWT_PROXY_CONFIG_FILE);
    assertEquals(jwtProxyContainer.getEnv().size(), 1);
    EnvVar xdgHome = jwtProxyContainer.getEnv().get(0);
    assertEquals(xdgHome.getName(), "XDG_CONFIG_HOME");
    assertEquals(xdgHome.getValue(), JWT_PROXY_CONFIG_FOLDER);
    Service jwtProxyService = k8sEnv.getServices().get(jwtProxyProvisioner.getServiceName());
    assertNotNull(jwtProxyService);
}
Also used : ServicePort(io.fabric8.kubernetes.api.model.ServicePort) InternalMachineConfig(org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig) Container(io.fabric8.kubernetes.api.model.Container) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Pod(io.fabric8.kubernetes.api.model.Pod) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Service(io.fabric8.kubernetes.api.model.Service) ServerConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl) Test(org.testng.annotations.Test)

Example 27 with InternalMachineConfig

use of org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig in project che-server by eclipse-che.

the class KubernetesInternalRuntime method doStartMachine.

/**
 * Creates Kubernetes pods and resolves servers using the specified serverResolver.
 *
 * @param serverResolver server resolver that provide servers by container
 * @throws InfrastructureException when any error occurs while creating Kubernetes pods
 */
@Traced
protected void doStartMachine(ServerResolver serverResolver) throws InfrastructureException {
    final KubernetesEnvironment environment = getContext().getEnvironment();
    final Map<String, InternalMachineConfig> machineConfigs = environment.getMachines();
    final String workspaceId = getContext().getIdentity().getWorkspaceId();
    LOG.debug("Begin pods creation for workspace '{}'", workspaceId);
    PodMerger podMerger = new PodMerger();
    Map<String, Map<String, Pod>> injectablePods = environment.getInjectablePodsCopy();
    for (Pod toCreate : environment.getPodsCopy().values()) {
        ObjectMeta toCreateMeta = toCreate.getMetadata();
        List<PodData> injectables = getAllInjectablePods(toCreate, injectablePods);
        Pod createdPod;
        if (injectables.isEmpty()) {
            createdPod = namespace.deployments().deploy(toCreate);
        } else {
            try {
                injectables.add(new PodData(toCreate));
                Deployment merged = podMerger.merge(injectables);
                merged.getMetadata().setName(toCreate.getMetadata().getName());
                createdPod = namespace.deployments().deploy(merged);
            } catch (ValidationException e) {
                throw new InfrastructureException(e);
            }
        }
        LOG.debug("Creating pod '{}' in workspace '{}'", toCreateMeta.getName(), workspaceId);
        storeStartingMachine(createdPod, createdPod.getMetadata(), machineConfigs, serverResolver);
    }
    for (Deployment toCreate : environment.getDeploymentsCopy().values()) {
        PodTemplateSpec template = toCreate.getSpec().getTemplate();
        List<PodData> injectables = getAllInjectablePods(template.getMetadata(), template.getSpec().getContainers(), injectablePods);
        Pod createdPod;
        if (injectables.isEmpty()) {
            createdPod = namespace.deployments().deploy(toCreate);
        } else {
            try {
                injectables.add(new PodData(toCreate));
                Deployment deployment = podMerger.merge(injectables);
                deployment.getMetadata().setName(toCreate.getMetadata().getName());
                putAnnotations(deployment.getMetadata(), toCreate.getMetadata().getAnnotations());
                putLabels(deployment.getMetadata(), toCreate.getMetadata().getLabels());
                createdPod = namespace.deployments().deploy(deployment);
            } catch (ValidationException e) {
                throw new InfrastructureException(e);
            }
        }
        LOG.debug("Creating deployment '{}' in workspace '{}'", createdPod.getMetadata().getName(), workspaceId);
        storeStartingMachine(createdPod, createdPod.getMetadata(), machineConfigs, serverResolver);
    }
    LOG.debug("Pods creation finished in workspace '{}'", workspaceId);
}
Also used : PodMerger(org.eclipse.che.workspace.infrastructure.kubernetes.environment.PodMerger) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) ValidationException(org.eclipse.che.api.core.ValidationException) Pod(io.fabric8.kubernetes.api.model.Pod) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) InternalMachineConfig(org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap) Collections.emptyMap(java.util.Collections.emptyMap) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) Traced(org.eclipse.che.commons.annotation.Traced)

Example 28 with InternalMachineConfig

use of org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig 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 29 with InternalMachineConfig

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

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 30 with InternalMachineConfig

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

the class ServersConverter method provision.

@Override
@Traced
public void provision(T k8sEnv, RuntimeIdentity identity) throws InfrastructureException {
    TracingTags.WORKSPACE_ID.set(identity::getWorkspaceId);
    SecureServerExposer<T> secureServerExposer = secureServerExposerFactoryProvider.get(k8sEnv).create(identity);
    for (PodData podConfig : k8sEnv.getPodsData().values()) {
        final PodSpec podSpec = podConfig.getSpec();
        for (Container containerConfig : podSpec.getContainers()) {
            String machineName = Names.machineName(podConfig, containerConfig);
            InternalMachineConfig machineConfig = k8sEnv.getMachines().get(machineName);
            if (!machineConfig.getServers().isEmpty()) {
                KubernetesServerExposer kubernetesServerExposer = new KubernetesServerExposer<>(externalServerExposer, secureServerExposer, machineName, podConfig, containerConfig, k8sEnv);
                kubernetesServerExposer.expose(machineConfig.getServers());
            }
        }
    }
}
Also used : 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) KubernetesServerExposer(org.eclipse.che.workspace.infrastructure.kubernetes.server.KubernetesServerExposer) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) Traced(org.eclipse.che.commons.annotation.Traced)

Aggregations

InternalMachineConfig (org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig)88 Test (org.testng.annotations.Test)64 Container (io.fabric8.kubernetes.api.model.Container)36 ChePlugin (org.eclipse.che.api.workspace.server.wsplugins.model.ChePlugin)18 PodData (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData)16 DevfileImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl)14 EnvVar (io.fabric8.kubernetes.api.model.EnvVar)12 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)12 ComponentImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl)12 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)10 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)10 Pod (io.fabric8.kubernetes.api.model.Pod)10 Secret (io.fabric8.kubernetes.api.model.Secret)10 SecretBuilder (io.fabric8.kubernetes.api.model.SecretBuilder)10 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)10 HashMap (java.util.HashMap)8 CheContainer (org.eclipse.che.api.workspace.server.wsplugins.model.CheContainer)8 Traced (org.eclipse.che.commons.annotation.Traced)8 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)6 PodSpecBuilder (io.fabric8.kubernetes.api.model.PodSpecBuilder)6