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));
}
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));
}
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()));
}
}
}
}
}
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));
}
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));
}
Aggregations