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