use of org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.WorkspaceVolume in project kubernetes-plugin by jenkinsci.
the class CasCTest method assertConfiguredAsExpected.
@Override
protected void assertConfiguredAsExpected(RestartableJenkinsRule r, String configContent) {
List<KubernetesCloud> all = r.j.jenkins.clouds.getAll(KubernetesCloud.class);
assertThat(all, hasSize(1));
KubernetesCloud cloud = all.get(0);
assertNotNull(cloud);
assertEquals(10, cloud.getContainerCap());
assertEquals("http://jenkinshost:8080/jenkins/", cloud.getJenkinsUrl());
assertEquals(32, cloud.getMaxRequestsPerHost());
assertEquals("kubernetes", cloud.name);
List<PodTemplate> templates = cloud.getTemplates();
assertNotNull(templates);
assertEquals(3, templates.size());
PodTemplate podTemplate = templates.get(0);
assertFalse(podTemplate.isHostNetwork());
assertEquals("java", podTemplate.getLabel());
assertEquals("default-java", podTemplate.getName());
assertEquals(10, podTemplate.getInstanceCap());
assertEquals(123, podTemplate.getSlaveConnectTimeout());
assertEquals(5, podTemplate.getIdleMinutes());
assertEquals(66, podTemplate.getActiveDeadlineSeconds());
assertThat(podTemplate.getYamlMergeStrategy(), isA(Overrides.class));
podTemplate = templates.get(1);
assertFalse(podTemplate.isHostNetwork());
assertEquals("dynamic-pvc", podTemplate.getLabel());
assertEquals("dynamic-pvc", podTemplate.getName());
assertThat(podTemplate.getYamlMergeStrategy(), isA(Overrides.class));
WorkspaceVolume workspaceVolume = podTemplate.getWorkspaceVolume();
assertNotNull(workspaceVolume);
assertThat(workspaceVolume, isA(DynamicPVCWorkspaceVolume.class));
DynamicPVCWorkspaceVolume dynamicPVCVolume = (DynamicPVCWorkspaceVolume) workspaceVolume;
assertEquals("ReadWriteOnce", dynamicPVCVolume.getAccessModes());
assertEquals("1", dynamicPVCVolume.getRequestsSize());
assertEquals("hostpath", dynamicPVCVolume.getStorageClassName());
podTemplate = templates.get(2);
assertFalse(podTemplate.isHostNetwork());
assertEquals("test", podTemplate.getLabel());
assertEquals("test", podTemplate.getName());
assertThat(podTemplate.getYamlMergeStrategy(), isA(Merge.class));
List<ContainerTemplate> containers = podTemplate.getContainers();
assertNotNull(containers);
assertEquals(1, containers.size());
ContainerTemplate container = containers.get(0);
assertEquals("cat", container.getArgs());
assertEquals("/bin/sh -c", container.getCommand());
assertEquals("maven:3.6.3-jdk-8", container.getImage());
ContainerLivenessProbe livenessProbe = container.getLivenessProbe();
assertEquals(1, livenessProbe.getFailureThreshold());
assertEquals(2, livenessProbe.getInitialDelaySeconds());
assertEquals(3, livenessProbe.getPeriodSeconds());
assertEquals(4, livenessProbe.getSuccessThreshold());
assertEquals(5, livenessProbe.getTimeoutSeconds());
assertEquals("maven", container.getName());
assertTrue(container.isTtyEnabled());
assertEquals("/src", container.getWorkingDir());
}
use of org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.WorkspaceVolume in project kubernetes-plugin by jenkinsci.
the class PodTemplateUtils method combine.
/**
* Combines a {@link PodTemplate} with its parent.
* @param parent The parent container template (nullable).
* @param template The actual container template
* @return The combined container template.
*/
public static PodTemplate combine(PodTemplate parent, PodTemplate template) {
if (template == null) {
throw new IllegalArgumentException("Pod template should not be null");
}
if (parent == null) {
return template;
}
LOGGER.log(Level.FINEST, "Combining pod templates, parent: {0}", parent);
LOGGER.log(Level.FINEST, "Combining pod templates, template: {0}", template);
String name = template.getName();
String label = template.getLabel();
String nodeSelector = isNullOrEmpty(template.getNodeSelector()) ? parent.getNodeSelector() : template.getNodeSelector();
String serviceAccount = isNullOrEmpty(template.getServiceAccount()) ? parent.getServiceAccount() : template.getServiceAccount();
String schedulerName = isNullOrEmpty(template.getSchedulerName()) ? parent.getSchedulerName() : template.getSchedulerName();
Node.Mode nodeUsageMode = template.getNodeUsageMode() == null ? parent.getNodeUsageMode() : template.getNodeUsageMode();
Set<PodAnnotation> podAnnotations = new LinkedHashSet<>();
podAnnotations.addAll(template.getAnnotations());
podAnnotations.addAll(parent.getAnnotations());
Set<PodImagePullSecret> imagePullSecrets = new LinkedHashSet<>();
imagePullSecrets.addAll(parent.getImagePullSecrets());
imagePullSecrets.addAll(template.getImagePullSecrets());
Map<String, ContainerTemplate> combinedContainers = new HashMap<>();
Map<String, PodVolume> combinedVolumes = new HashMap<>();
// Containers
Map<String, ContainerTemplate> parentContainers = parent.getContainers().stream().collect(toMap(c -> c.getName(), c -> c));
combinedContainers.putAll(parentContainers);
combinedContainers.putAll(template.getContainers().stream().collect(toMap(c -> c.getName(), c -> combine(parentContainers.get(c.getName()), c))));
// Volumes
Map<String, PodVolume> parentVolumes = parent.getVolumes().stream().collect(toMap(v -> v.getMountPath(), v -> v));
combinedVolumes.putAll(parentVolumes);
combinedVolumes.putAll(template.getVolumes().stream().collect(toMap(v -> v.getMountPath(), v -> v)));
WorkspaceVolume workspaceVolume = WorkspaceVolume.merge(parent.getWorkspaceVolume(), template.getWorkspaceVolume());
// Tool location node properties
List<NodeProperty<?>> nodeProperties = new ArrayList<>(parent.getNodeProperties());
nodeProperties.addAll(template.getNodeProperties());
PodTemplate podTemplate = new PodTemplate(template.getId());
podTemplate.setName(name);
podTemplate.setNamespace(!isNullOrEmpty(template.getNamespace()) ? template.getNamespace() : parent.getNamespace());
podTemplate.setLabel(label);
podTemplate.setNodeSelector(nodeSelector);
podTemplate.setServiceAccount(serviceAccount);
podTemplate.setSchedulerName(schedulerName);
podTemplate.setEnvVars(combineEnvVars(parent, template));
podTemplate.setContainers(new ArrayList<>(combinedContainers.values()));
podTemplate.setWorkspaceVolume(workspaceVolume);
podTemplate.setVolumes(new ArrayList<>(combinedVolumes.values()));
podTemplate.setImagePullSecrets(new ArrayList<>(imagePullSecrets));
podTemplate.setAnnotations(new ArrayList<>(podAnnotations));
podTemplate.setNodeProperties(nodeProperties);
podTemplate.setNodeUsageMode(nodeUsageMode);
podTemplate.setYamlMergeStrategy(template.getYamlMergeStrategy());
podTemplate.setInheritFrom(!isNullOrEmpty(template.getInheritFrom()) ? template.getInheritFrom() : parent.getInheritFrom());
podTemplate.setInstanceCap(template.getInstanceCap() != Integer.MAX_VALUE ? template.getInstanceCap() : parent.getInstanceCap());
podTemplate.setSlaveConnectTimeout(template.getSlaveConnectTimeout() != PodTemplate.DEFAULT_SLAVE_JENKINS_CONNECTION_TIMEOUT ? template.getSlaveConnectTimeout() : parent.getSlaveConnectTimeout());
podTemplate.setIdleMinutes(template.getIdleMinutes() != 0 ? template.getIdleMinutes() : parent.getIdleMinutes());
podTemplate.setActiveDeadlineSeconds(template.getActiveDeadlineSeconds() != 0 ? template.getActiveDeadlineSeconds() : parent.getActiveDeadlineSeconds());
podTemplate.setServiceAccount(!isNullOrEmpty(template.getServiceAccount()) ? template.getServiceAccount() : parent.getServiceAccount());
podTemplate.setSchedulerName(!isNullOrEmpty(template.getSchedulerName()) ? template.getSchedulerName() : parent.getSchedulerName());
podTemplate.setPodRetention(template.getPodRetention());
podTemplate.setShowRawYaml(template.isShowRawYamlSet() ? template.isShowRawYaml() : parent.isShowRawYaml());
podTemplate.setRunAsUser(template.getRunAsUser() != null ? template.getRunAsUser() : parent.getRunAsUser());
podTemplate.setRunAsGroup(template.getRunAsGroup() != null ? template.getRunAsGroup() : parent.getRunAsGroup());
podTemplate.setSupplementalGroups(template.getSupplementalGroups() != null ? template.getSupplementalGroups() : parent.getSupplementalGroups());
if (template.isHostNetworkSet()) {
podTemplate.setHostNetwork(template.isHostNetwork());
} else if (parent.isHostNetworkSet()) {
podTemplate.setHostNetwork(parent.isHostNetwork());
}
List<String> yamls = new ArrayList<>(parent.getYamls());
yamls.addAll(template.getYamls());
podTemplate.setYamls(yamls);
podTemplate.setListener(template.getListener());
LOGGER.log(Level.FINEST, "Pod templates combined: {0}", podTemplate);
return podTemplate;
}
use of org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.WorkspaceVolume in project kubernetes-plugin by jenkinsci.
the class PodTemplateUtils method combine.
/**
* Combines a Pod with its parent.
* @param parent The parent Pod (nullable).
* @param template The child Pod
*/
public static Pod combine(Pod parent, Pod template) {
if (template == null) {
throw new IllegalArgumentException("Pod template should not be null");
}
if (parent == null) {
return template;
}
LOGGER.finest(() -> "Combining pods, parent: " + Serialization.asYaml(parent) + " template: " + Serialization.asYaml(template));
Map<String, String> nodeSelector = mergeMaps(parent.getSpec().getNodeSelector(), template.getSpec().getNodeSelector());
String serviceAccount = isNullOrEmpty(template.getSpec().getServiceAccount()) ? parent.getSpec().getServiceAccount() : template.getSpec().getServiceAccount();
String serviceAccountName = isNullOrEmpty(template.getSpec().getServiceAccountName()) ? parent.getSpec().getServiceAccountName() : template.getSpec().getServiceAccountName();
String schedulerName = isNullOrEmpty(template.getSpec().getSchedulerName()) ? parent.getSpec().getSchedulerName() : template.getSpec().getSchedulerName();
Boolean hostNetwork = template.getSpec().getHostNetwork() != null ? template.getSpec().getHostNetwork() : parent.getSpec().getHostNetwork();
Map<String, String> podAnnotations = mergeMaps(parent.getMetadata().getAnnotations(), template.getMetadata().getAnnotations());
Map<String, String> podLabels = mergeMaps(parent.getMetadata().getLabels(), template.getMetadata().getLabels());
Set<LocalObjectReference> imagePullSecrets = new LinkedHashSet<>();
imagePullSecrets.addAll(parent.getSpec().getImagePullSecrets());
imagePullSecrets.addAll(template.getSpec().getImagePullSecrets());
// Containers
List<Container> combinedContainers = combineContainers(parent.getSpec().getContainers(), template.getSpec().getContainers());
// Init containers
List<Container> combinedInitContainers = combineContainers(parent.getSpec().getInitContainers(), template.getSpec().getInitContainers());
// Volumes
List<Volume> combinedVolumes = combineVolumes(parent.getSpec().getVolumes(), template.getSpec().getVolumes());
// Tolerations
List<Toleration> combinedTolerations = new LinkedList<>();
Optional.ofNullable(parent.getSpec().getTolerations()).ifPresent(combinedTolerations::addAll);
Optional.ofNullable(template.getSpec().getTolerations()).ifPresent(combinedTolerations::addAll);
// WorkspaceVolume workspaceVolume = template.isCustomWorkspaceVolumeEnabled() && template.getWorkspaceVolume() != null ? template.getWorkspaceVolume() : parent.getWorkspaceVolume();
// Tool location node properties
// List<ToolLocationNodeProperty> toolLocationNodeProperties = new ArrayList<>();
// toolLocationNodeProperties.addAll(parent.getNodeProperties());
// toolLocationNodeProperties.addAll(template.getNodeProperties());
MetadataNested<PodBuilder> metadataBuilder = //
new PodBuilder(parent).withNewMetadataLike(parent.getMetadata()).withAnnotations(podAnnotations).withLabels(podLabels);
if (!isNullOrEmpty(template.getMetadata().getName())) {
metadataBuilder.withName(template.getMetadata().getName());
}
if (!isNullOrEmpty(template.getMetadata().getNamespace())) {
metadataBuilder.withNamespace(template.getMetadata().getNamespace());
}
SpecNested<PodBuilder> specBuilder = //
metadataBuilder.endMetadata().withNewSpecLike(//
parent.getSpec()).withNodeSelector(//
nodeSelector).withServiceAccount(//
serviceAccount).withServiceAccountName(//
serviceAccountName).withSchedulerName(schedulerName).withHostNetwork(//
hostNetwork).withContainers(//
combinedContainers).withInitContainers(//
combinedInitContainers).withVolumes(//
combinedVolumes).withTolerations(//
combinedTolerations).withImagePullSecrets(new ArrayList<>(imagePullSecrets));
// Security context
if (template.getSpec().getSecurityContext() != null || parent.getSpec().getSecurityContext() != null) {
specBuilder.editOrNewSecurityContext().withRunAsUser(template.getSpec().getSecurityContext() != null && template.getSpec().getSecurityContext().getRunAsUser() != null ? template.getSpec().getSecurityContext().getRunAsUser() : (parent.getSpec().getSecurityContext() != null && parent.getSpec().getSecurityContext().getRunAsUser() != null ? parent.getSpec().getSecurityContext().getRunAsUser() : null)).withRunAsGroup(template.getSpec().getSecurityContext() != null && template.getSpec().getSecurityContext().getRunAsGroup() != null ? template.getSpec().getSecurityContext().getRunAsGroup() : (parent.getSpec().getSecurityContext() != null && parent.getSpec().getSecurityContext().getRunAsGroup() != null ? parent.getSpec().getSecurityContext().getRunAsGroup() : null)).endSecurityContext();
}
// podTemplate.setLabel(label);
// podTemplate.setEnvVars(combineEnvVars(parent, template));
// podTemplate.setWorkspaceVolume(workspaceVolume);
// podTemplate.setNodeProperties(toolLocationNodeProperties);
// podTemplate.setNodeUsageMode(nodeUsageMode);
// podTemplate.setYaml(template.getYaml() == null ? parent.getYaml() : template.getYaml());
Pod pod = specBuilder.endSpec().build();
LOGGER.finest(() -> "Pods combined: " + Serialization.asYaml(pod));
return pod;
}
Aggregations