Search in sources :

Example 11 with PodData

use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project che-server by eclipse-che.

the class KubernetesEnvironmentFactory method mergePods.

/**
 * Merges the specified pods and deployments to a single Deployment.
 *
 * <p>Note that method will modify the specified collections and put work result there.
 *
 * @param pods pods to merge
 * @param deployments deployments to merge
 * @param services services to reconfigure to point new deployment
 * @throws ValidationException if the specified lists has pods with conflicting configuration
 */
private void mergePods(Map<String, Pod> pods, Map<String, Deployment> deployments, Map<String, Service> services) throws ValidationException {
    List<PodData> podsData = Stream.concat(pods.values().stream().map(PodData::new), deployments.values().stream().map(PodData::new)).collect(Collectors.toList());
    Deployment deployment = podMerger.merge(podsData);
    String deploymentName = deployment.getMetadata().getName();
    // provision merged deployment instead of recipe pods/deployments
    pods.clear();
    deployments.clear();
    deployments.put(deploymentName, deployment);
    // multiple pods/deployments are merged to one deployment
    // to avoid issues because of overriding labels
    // provision const label and selector to match all services to merged Deployment
    putLabel(deployment.getSpec().getTemplate().getMetadata(), DEPLOYMENT_NAME_LABEL, deploymentName);
    services.values().forEach(s -> setSelector(s, DEPLOYMENT_NAME_LABEL, deploymentName));
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment)

Example 12 with PodData

use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project che-server by eclipse-che.

the class KubernetesEnvironmentPodsValidator method ensureConfiguredMachinesHaveContainers.

private void ensureConfiguredMachinesHaveContainers(KubernetesEnvironment env) throws ValidationException {
    Set<String> missingMachines = new HashSet<>(env.getMachines().keySet());
    for (PodData pod : env.getPodsData().values()) {
        if (pod.getSpec() != null && pod.getSpec().getContainers() != null) {
            for (Container container : pod.getSpec().getContainers()) {
                missingMachines.remove(Names.machineName(pod, container));
            }
            for (Container container : pod.getSpec().getInitContainers()) {
                missingMachines.remove(Names.machineName(pod, container));
            }
        }
    }
    checkArgument(missingMachines.isEmpty(), "Environment contains machines that are missing in recipe: %s", Joiner.on(", ").join(missingMachines));
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Container(io.fabric8.kubernetes.api.model.Container) HashSet(java.util.HashSet)

Example 13 with PodData

use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project che-server by eclipse-che.

the class KubernetesEnvironmentPodsValidator method validatePodVolumes.

private void validatePodVolumes(KubernetesEnvironment env) throws ValidationException {
    Set<String> pvcsNames = env.getPersistentVolumeClaims().keySet();
    for (PodData pod : env.getPodsData().values()) {
        Set<String> volumesNames = new HashSet<>();
        for (Volume volume : pod.getSpec().getVolumes()) {
            volumesNames.add(volume.getName());
            PersistentVolumeClaimVolumeSource pvcSource = volume.getPersistentVolumeClaim();
            if (pvcSource != null && !pvcsNames.contains(pvcSource.getClaimName())) {
                throw new ValidationException(String.format("Pod '%s' contains volume '%s' with PVC sources that references missing PVC '%s'", pod.getMetadata().getName(), volume.getName(), pvcSource.getClaimName()));
            }
        }
        List<Container> containers = new ArrayList<>();
        containers.addAll(pod.getSpec().getContainers());
        containers.addAll(pod.getSpec().getInitContainers());
        for (Container container : containers) {
            for (VolumeMount volumeMount : container.getVolumeMounts()) {
                if (!volumesNames.contains(volumeMount.getName())) {
                    throw new ValidationException(String.format("Container '%s' in pod '%s' contains volume mount that references missing volume '%s'", container.getName(), pod.getMetadata().getName(), volumeMount.getName()));
                }
            }
        }
    }
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Container(io.fabric8.kubernetes.api.model.Container) ValidationException(org.eclipse.che.api.core.ValidationException) Volume(io.fabric8.kubernetes.api.model.Volume) ArrayList(java.util.ArrayList) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) PersistentVolumeClaimVolumeSource(io.fabric8.kubernetes.api.model.PersistentVolumeClaimVolumeSource) HashSet(java.util.HashSet)

Example 14 with PodData

use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project che-server by eclipse-che.

the class OpenShiftEnvironmentFactory method mergePods.

/**
 * Merges the specified pods and deployments to a single Deployment.
 *
 * <p>Note that method will modify the specified collections and put work result there.
 *
 * @param pods pods to merge
 * @param deployments deployments to merge
 * @param services services to reconfigure to point new deployment
 * @throws ValidationException if the specified lists has pods with conflicting configuration
 */
private void mergePods(Map<String, Pod> pods, Map<String, Deployment> deployments, Map<String, Service> services) throws ValidationException {
    List<PodData> podsData = Stream.concat(pods.values().stream().map(PodData::new), deployments.values().stream().map(PodData::new)).collect(Collectors.toList());
    Deployment deployment = podMerger.merge(podsData);
    String deploymentName = deployment.getMetadata().getName();
    // provision merged deployment instead of recipe pods/deployments
    pods.clear();
    deployments.clear();
    deployments.put(deploymentName, deployment);
    // multiple pods/deployments are merged to one deployment
    // to avoid issues because of overriding labels
    // provision const label and selector to match all services to merged Deployment
    putLabel(deployment.getSpec().getTemplate().getMetadata(), DEPLOYMENT_NAME_LABEL, deploymentName);
    services.values().forEach(s -> setSelector(s, DEPLOYMENT_NAME_LABEL, deploymentName));
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment)

Example 15 with PodData

use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project che-server by eclipse-che.

the class EnvVarsTest method shouldProvisionEnvIntoK8SListIfContainerAlreadyHasSomeEnvVars.

@Test
public void shouldProvisionEnvIntoK8SListIfContainerAlreadyHasSomeEnvVars() throws Exception {
    // given
    EnvVar existingInitCEnvVar = new EnvVar("ENV", "value", null);
    EnvVar existingCEnvVar = new EnvVar("ENV", null, new EnvVarSource());
    PodData pod = new PodData(new PodBuilder().withNewMetadata().withName("pod").endMetadata().withNewSpec().withInitContainers(new ContainerBuilder().withName("initContainer").withEnv(copy(existingInitCEnvVar)).build()).withContainers(new ContainerBuilder().withName("container").withEnv(copy(existingCEnvVar)).build()).endSpec().build());
    // when
    envVars.apply(pod, singletonList(new EnvImpl("TEST_ENV", "anyValue")));
    // then
    List<EnvVar> initCEnv = pod.getSpec().getInitContainers().get(0).getEnv();
    assertEquals(initCEnv.size(), 2);
    assertEquals(initCEnv.get(0), existingInitCEnvVar);
    assertEquals(initCEnv.get(1), new EnvVar("TEST_ENV", "anyValue", null));
    List<EnvVar> containerEnv = pod.getSpec().getContainers().get(0).getEnv();
    assertEquals(containerEnv.size(), 2);
    assertEquals(containerEnv.get(0), existingCEnvVar);
    assertEquals(containerEnv.get(1), new EnvVar("TEST_ENV", "anyValue", null));
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) EnvImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EnvImpl) EnvVarSource(io.fabric8.kubernetes.api.model.EnvVarSource) Test(org.testng.annotations.Test)

Aggregations

PodData (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData)156 Test (org.testng.annotations.Test)86 Container (io.fabric8.kubernetes.api.model.Container)62 Pod (io.fabric8.kubernetes.api.model.Pod)56 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)52 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)52 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)40 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)36 PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)34 PodSpecBuilder (io.fabric8.kubernetes.api.model.PodSpecBuilder)30 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)28 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)26 Map (java.util.Map)22 InternalMachineConfig (org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig)22 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)20 VolumeBuilder (io.fabric8.kubernetes.api.model.VolumeBuilder)20 KubernetesEnvironment (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment)20 EnvVar (io.fabric8.kubernetes.api.model.EnvVar)16 Volume (io.fabric8.kubernetes.api.model.Volume)16 HashMap (java.util.HashMap)16