Search in sources :

Example 36 with ValidationException

use of org.eclipse.che.api.core.ValidationException in project devspaces-images by redhat-developer.

the class PodMergerTest method shouldBeAbleToMergeTerminationGracePeriodS.

@Test(dataProvider = "terminationGracePeriodProvider")
public void shouldBeAbleToMergeTerminationGracePeriodS(List<Long> terminationGracePeriods, Long expectedResultLong) throws ValidationException {
    List<PodData> podData = terminationGracePeriods.stream().map(p -> new PodData(new PodSpecBuilder().withTerminationGracePeriodSeconds(p).build(), new ObjectMetaBuilder().build())).collect(Collectors.toList());
    // when
    Deployment merged = podMerger.merge(podData);
    // then
    PodTemplateSpec podTemplate = merged.getSpec().getTemplate();
    assertEquals(podTemplate.getSpec().getTerminationGracePeriodSeconds(), expectedResultLong);
}
Also used : PodSecurityContext(io.fabric8.kubernetes.api.model.PodSecurityContext) Arrays(java.util.Arrays) Container(io.fabric8.kubernetes.api.model.Container) DataProvider(org.testng.annotations.DataProvider) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) LocalObjectReference(io.fabric8.kubernetes.api.model.LocalObjectReference) ValidationException(org.eclipse.che.api.core.ValidationException) HashSet(java.util.HashSet) PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) Map(java.util.Map) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) Assert.assertNotEquals(org.testng.Assert.assertNotEquals) ImmutableMap(com.google.common.collect.ImmutableMap) BeforeMethod(org.testng.annotations.BeforeMethod) Toleration(io.fabric8.kubernetes.api.model.Toleration) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) Collectors(java.util.stream.Collectors) LocalObjectReferenceBuilder(io.fabric8.kubernetes.api.model.LocalObjectReferenceBuilder) List(java.util.List) PodSecurityContextBuilder(io.fabric8.kubernetes.api.model.PodSecurityContextBuilder) PROJECTS_VOLUME_NAME(org.eclipse.che.api.workspace.shared.Constants.PROJECTS_VOLUME_NAME) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Assert.assertTrue(org.testng.Assert.assertTrue) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) Collections(java.util.Collections) PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) Test(org.testng.annotations.Test)

Example 37 with ValidationException

use of org.eclipse.che.api.core.ValidationException in project devspaces-images by redhat-developer.

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 38 with ValidationException

use of org.eclipse.che.api.core.ValidationException in project devspaces-images by redhat-developer.

the class PodMerger method merge.

/**
 * Creates a new deployments that contains all pods from the specified pods data lists.
 *
 * <p>If multiple pods have labels, annotations, additionalProperties with the same key then
 * override will happen and result pod template will have only the last value. You may need to
 * reconfigure objects that rely on these value, like selector field in Service. You can use all
 * of pod template labels for reconfiguring or single {@link #DEFAULT_DEPLOYMENT_NAME} label that
 * contains deployment name;
 *
 * @param podsData the pods data to be merged
 * @return a new Deployment that have pod template that is result of merging the specified lists
 * @throws ValidationException if pods can not be merged because of critical names collisions like
 *     volumes names
 */
public Deployment merge(List<PodData> podsData) throws ValidationException {
    Deployment baseDeployment = createEmptyDeployment(DEFAULT_DEPLOYMENT_NAME);
    PodTemplateSpec basePodTemplate = baseDeployment.getSpec().getTemplate();
    ObjectMeta basePodMeta = basePodTemplate.getMetadata();
    PodSpec baseSpec = basePodTemplate.getSpec();
    Set<String> containerNames = new HashSet<>();
    Set<String> initContainerNames = new HashSet<>();
    Set<String> volumes = new HashSet<>();
    Set<String> pullSecrets = new HashSet<>();
    for (PodData podData : podsData) {
        // if there are entries with such keys then values will be overridden
        ObjectMeta podMeta = podData.getMetadata();
        putLabels(basePodMeta, podMeta.getLabels());
        putAnnotations(basePodMeta, podMeta.getAnnotations());
        basePodMeta.getAdditionalProperties().putAll(podMeta.getAdditionalProperties());
        for (Container container : podData.getSpec().getContainers()) {
            String containerName = container.getName();
            // generate container name to avoid collisions
            while (!containerNames.add(container.getName())) {
                containerName = NameGenerator.generate(container.getName(), 4);
                container.setName(containerName);
            }
            // store original recipe machine name
            Names.putMachineName(basePodMeta, containerName, Names.machineName(podMeta, container));
            baseSpec.getContainers().add(container);
        }
        for (Container initContainer : podData.getSpec().getInitContainers()) {
            // generate container name to avoid collisions
            while (!initContainerNames.add(initContainer.getName())) {
                initContainer.setName(NameGenerator.generate(initContainer.getName(), 4));
            }
            // store original recipe machine name
            Names.putMachineName(basePodMeta, initContainer.getName(), Names.machineName(podMeta, initContainer));
            baseSpec.getInitContainers().add(initContainer);
        }
        for (Volume volume : podData.getSpec().getVolumes()) {
            if (!volumes.add(volume.getName())) {
                if (volume.getName().equals(PROJECTS_VOLUME_NAME)) {
                    // project volume already added, can be skipped
                    continue;
                }
                throw new ValidationException(format("Pods have to have volumes with unique names but there are multiple `%s` volumes", volume.getName()));
            }
            baseSpec.getVolumes().add(volume);
        }
        for (LocalObjectReference pullSecret : podData.getSpec().getImagePullSecrets()) {
            if (pullSecrets.add(pullSecret.getName())) {
                // add pull secret only if it is not present yet
                baseSpec.getImagePullSecrets().add(pullSecret);
            }
        }
        if (podData.getSpec().getTerminationGracePeriodSeconds() != null) {
            if (baseSpec.getTerminationGracePeriodSeconds() != null) {
                baseSpec.setTerminationGracePeriodSeconds(Long.max(baseSpec.getTerminationGracePeriodSeconds(), podData.getSpec().getTerminationGracePeriodSeconds()));
            } else {
                baseSpec.setTerminationGracePeriodSeconds(podData.getSpec().getTerminationGracePeriodSeconds());
            }
        }
        baseSpec.setSecurityContext(mergeSecurityContexts(baseSpec.getSecurityContext(), podData.getSpec().getSecurityContext()));
        baseSpec.setServiceAccount(mergeServiceAccount(baseSpec.getServiceAccount(), podData.getSpec().getServiceAccount()));
        baseSpec.setServiceAccountName(mergeServiceAccountName(baseSpec.getServiceAccountName(), podData.getSpec().getServiceAccountName()));
        baseSpec.setNodeSelector(mergeNodeSelector(baseSpec.getNodeSelector(), podData.getSpec().getNodeSelector()));
        // if there are entries with such keys then values will be overridden
        baseSpec.getAdditionalProperties().putAll(podData.getSpec().getAdditionalProperties());
        // add tolerations to baseSpec if any
        for (Toleration toleration : podData.getSpec().getTolerations()) {
            if (!baseSpec.getTolerations().contains(toleration)) {
                baseSpec.getTolerations().add(toleration);
            }
        }
    }
    Map<String, String> matchLabels = new HashMap<>();
    matchLabels.put(DEPLOYMENT_NAME_LABEL, baseDeployment.getMetadata().getName());
    putLabels(basePodMeta, matchLabels);
    baseDeployment.getSpec().getSelector().setMatchLabels(matchLabels);
    return baseDeployment;
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) ValidationException(org.eclipse.che.api.core.ValidationException) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) HashMap(java.util.HashMap) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) Container(io.fabric8.kubernetes.api.model.Container) Volume(io.fabric8.kubernetes.api.model.Volume) LocalObjectReference(io.fabric8.kubernetes.api.model.LocalObjectReference) Toleration(io.fabric8.kubernetes.api.model.Toleration) HashSet(java.util.HashSet)

Example 39 with ValidationException

use of org.eclipse.che.api.core.ValidationException in project devspaces-images by redhat-developer.

the class BrokerStatusListenerTest method shouldNotCallAddResultWhenValidationFails.

@Test
public void shouldNotCallAddResultWhenValidationFails() throws Exception {
    // given
    doThrow(new ValidationException("test")).when(validator).validatePluginNames(anyList());
    BrokerEvent event = new BrokerEvent().withRuntimeId(new RuntimeIdentityImpl(WORKSPACE_ID, null, null, null)).withStatus(BrokerStatus.DONE).withTooling(emptyList());
    // when
    brokerStatusListener.onEvent(event);
    // then
    verify(brokersResult, never()).setResult(anyList());
}
Also used : ValidationException(org.eclipse.che.api.core.ValidationException) RuntimeIdentityImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl) Test(org.testng.annotations.Test)

Example 40 with ValidationException

use of org.eclipse.che.api.core.ValidationException in project devspaces-images by redhat-developer.

the class BrokerStatusListenerTest method shouldSubmitErrorWhenDoneEventIsReceivedButToolingIsValidationFails.

@Test
public void shouldSubmitErrorWhenDoneEventIsReceivedButToolingIsValidationFails() throws Exception {
    // given
    doThrow(new ValidationException("test")).when(validator).validatePluginNames(anyList());
    BrokerEvent event = new BrokerEvent().withRuntimeId(new RuntimeIdentityImpl(WORKSPACE_ID, null, null, null)).withStatus(BrokerStatus.DONE).withTooling(emptyList());
    // when
    brokerStatusListener.onEvent(event);
    // then
    verify(brokersResult).error(any(ValidationException.class));
}
Also used : ValidationException(org.eclipse.che.api.core.ValidationException) RuntimeIdentityImpl(org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl) Test(org.testng.annotations.Test)

Aggregations

ValidationException (org.eclipse.che.api.core.ValidationException)42 Test (org.testng.annotations.Test)16 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)12 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)10 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)10 PodData (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData)8 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)6 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)6 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)6 Pod (io.fabric8.kubernetes.api.model.Pod)6 PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ConflictException (org.eclipse.che.api.core.ConflictException)6 ServerException (org.eclipse.che.api.core.ServerException)6 WorkspaceStatus (org.eclipse.che.api.core.model.workspace.WorkspaceStatus)6 RuntimeIdentityImpl (org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl)6 WorkspaceConfigImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl)6 InternalInfrastructureException (org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException)6 Traced (org.eclipse.che.commons.annotation.Traced)6