use of org.csanchez.jenkins.plugins.kubernetes.volumes.ConfigMapVolume in project kubernetes-plugin by jenkinsci.
the class ConfigMapVolumeTest method testNullSubPathValue.
@Test
public void testNullSubPathValue() {
ConfigMapVolume configMapVolume = new ConfigMapVolume("oneMountPath", "Myvolume", false);
assertNull(configMapVolume.getSubPath());
}
use of org.csanchez.jenkins.plugins.kubernetes.volumes.ConfigMapVolume in project kubernetes-plugin by jenkinsci.
the class ConfigMapVolumeTest method testValidSubPathValue.
@Test
public void testValidSubPathValue() {
ConfigMapVolume configMapVolume = new ConfigMapVolume("oneMountPath", "Myvolume", false);
configMapVolume.setSubPath("miSubpath");
assertEquals(configMapVolume.getSubPath(), "miSubpath");
}
use of org.csanchez.jenkins.plugins.kubernetes.volumes.ConfigMapVolume in project kubernetes-plugin by jenkinsci.
the class PodTemplateBuilder method build.
/**
* Create a Pod object from a PodTemplate
*/
public Pod build() {
// Build volumes and volume mounts.
Map<String, Volume> volumes = new HashMap<>();
Map<String, VolumeMount> volumeMounts = new HashMap<>();
if (agent == null) {
throw new IllegalStateException("No KubernetesSlave is set");
}
String podName = agent.getPodName();
int i = 0;
for (final PodVolume volume : template.getVolumes()) {
final String volumeName = "volume-" + i;
final String mountPath = normalizePath(volume.getMountPath());
if (!volumeMounts.containsKey(mountPath)) {
VolumeMountBuilder volumeMountBuilder = //
new VolumeMountBuilder().withMountPath(mountPath).withName(volumeName).withReadOnly(false);
if (volume instanceof ConfigMapVolume) {
final ConfigMapVolume configmapVolume = (ConfigMapVolume) volume;
String subPath = configmapVolume.getSubPath();
if (subPath != null) {
volumeMountBuilder = volumeMountBuilder.withSubPath(normalizePath(subPath));
}
}
volumeMounts.put(mountPath, volumeMountBuilder.build());
volumes.put(volumeName, volume.buildVolume(volumeName, podName));
i++;
}
}
volumes.put(WORKSPACE_VOLUME_NAME, template.getWorkspaceVolume().buildVolume(WORKSPACE_VOLUME_NAME, podName));
Map<String, Container> containers = new HashMap<>();
// containers from pod template
for (ContainerTemplate containerTemplate : template.getContainers()) {
containers.put(containerTemplate.getName(), createContainer(containerTemplate, template.getEnvVars(), volumeMounts.values()));
}
MetadataNested<PodBuilder> metadataBuilder = new PodBuilder().withNewMetadata();
if (agent != null) {
metadataBuilder.withName(agent.getPodName());
}
Map<String, String> labels = new HashMap<>();
if (agent != null) {
labels.putAll(agent.getKubernetesCloud().getPodLabelsMap());
}
labels.putAll(template.getLabelsMap());
if (!labels.isEmpty()) {
metadataBuilder.withLabels(labels);
}
Map<String, String> annotations = getAnnotationsMap(template.getAnnotations());
if (!annotations.isEmpty()) {
metadataBuilder.withAnnotations(annotations);
}
SpecNested<PodBuilder> builder = metadataBuilder.endMetadata().withNewSpec();
if (template.getActiveDeadlineSeconds() > 0) {
builder = builder.withActiveDeadlineSeconds(Long.valueOf(template.getActiveDeadlineSeconds()));
}
if (!volumes.isEmpty()) {
builder.withVolumes(volumes.values().toArray(new Volume[volumes.size()]));
}
if (template.getServiceAccount() != null) {
builder.withServiceAccountName(substituteEnv(template.getServiceAccount()));
}
if (template.getSchedulerName() != null) {
builder.withSchedulerName(substituteEnv(template.getSchedulerName()));
}
List<LocalObjectReference> imagePullSecrets = template.getImagePullSecrets().stream().map((x) -> x.toLocalObjectReference()).collect(Collectors.toList());
if (!imagePullSecrets.isEmpty()) {
builder.withImagePullSecrets(imagePullSecrets);
}
Map<String, String> nodeSelector = getNodeSelectorMap(template.getNodeSelector());
if (!nodeSelector.isEmpty()) {
builder.withNodeSelector(nodeSelector);
}
if (template.getTerminationGracePeriodSeconds() != null) {
builder.withTerminationGracePeriodSeconds(template.getTerminationGracePeriodSeconds());
}
builder.withContainers(containers.values().toArray(new Container[containers.size()]));
Long runAsUser = template.getRunAsUserAsLong();
Long runAsGroup = template.getRunAsGroupAsLong();
String supplementalGroups = template.getSupplementalGroups();
if (runAsUser != null || runAsGroup != null || supplementalGroups != null) {
PodSpecFluent.SecurityContextNested<SpecNested<PodBuilder>> securityContext = builder.editOrNewSecurityContext();
if (runAsUser != null) {
securityContext.withRunAsUser(runAsUser);
}
if (runAsGroup != null) {
securityContext.withRunAsGroup(runAsGroup);
}
if (supplementalGroups != null) {
securityContext.withSupplementalGroups(parseSupplementalGroupList(supplementalGroups));
}
securityContext.endSecurityContext();
}
if (template.isHostNetworkSet()) {
builder.withHostNetwork(template.isHostNetwork());
}
// merge with the yaml fragments
Pod pod = combine(template.getYamlsPod(), builder.endSpec().build());
// Apply defaults
// default jnlp container
Optional<Container> jnlpOpt = pod.getSpec().getContainers().stream().filter(c -> JNLP_NAME.equals(c.getName())).findFirst();
Container jnlp = jnlpOpt.orElse(new ContainerBuilder().withName(JNLP_NAME).withVolumeMounts(volumeMounts.values().toArray(new VolumeMount[volumeMounts.values().size()])).build());
if (!jnlpOpt.isPresent()) {
pod.getSpec().getContainers().add(jnlp);
}
pod.getSpec().getContainers().stream().filter(c -> c.getWorkingDir() == null).forEach(c -> c.setWorkingDir(jnlp.getWorkingDir()));
if (StringUtils.isBlank(jnlp.getImage())) {
String jnlpImage = DEFAULT_JNLP_IMAGE;
if (StringUtils.isNotEmpty(DEFAULT_JNLP_DOCKER_REGISTRY_PREFIX)) {
jnlpImage = Util.ensureEndsWith(DEFAULT_JNLP_DOCKER_REGISTRY_PREFIX, "/") + jnlpImage;
}
jnlp.setImage(jnlpImage);
}
Map<String, EnvVar> envVars = new HashMap<>();
envVars.putAll(jnlpEnvVars(jnlp.getWorkingDir()));
envVars.putAll(defaultEnvVars(template.getEnvVars()));
Optional.ofNullable(jnlp.getEnv()).ifPresent(jnlpEnv -> {
jnlpEnv.forEach(var -> envVars.put(var.getName(), var));
});
jnlp.setEnv(new ArrayList<>(envVars.values()));
if (jnlp.getResources() == null) {
Map<String, Quantity> reqMap = new HashMap<>();
Map<String, Quantity> limMap = new HashMap<>();
reqMap.put("cpu", new Quantity(DEFAULT_JNLP_CONTAINER_CPU_REQUEST));
reqMap.put("memory", new Quantity(DEFAULT_JNLP_CONTAINER_MEMORY_REQUEST));
if (DEFAULT_JNLP_CONTAINER_CPU_LIMIT != null) {
limMap.put("cpu", new Quantity(DEFAULT_JNLP_CONTAINER_CPU_LIMIT));
}
if (DEFAULT_JNLP_CONTAINER_MEMORY_LIMIT != null) {
limMap.put("memory", new Quantity(DEFAULT_JNLP_CONTAINER_MEMORY_LIMIT));
}
ResourceRequirements reqs = new ResourceRequirementsBuilder().withRequests(reqMap).withLimits(limMap).build();
jnlp.setResources(reqs);
}
if (cloud != null) {
pod = PodDecorator.decorateAll(cloud, pod);
}
Pod finalPod = pod;
LOGGER.finest(() -> "Pod built: " + Serialization.asYaml(finalPod));
return pod;
}
use of org.csanchez.jenkins.plugins.kubernetes.volumes.ConfigMapVolume in project kubernetes-plugin by jenkinsci.
the class PodTemplateBuilderTest method yamlOverrideVolume.
@Test
public void yamlOverrideVolume() throws Exception {
PodTemplate parent = new PodTemplate();
parent.setYaml("apiVersion: v1\n" + "kind: Pod\n" + "metadata:\n" + " labels:\n" + " some-label: some-label-value\n" + "spec:\n" + " containers:\n" + " - name: jnlp\n" + " volumeMounts:\n" + " - name: host-volume\n" + " mountPath: /etc/config\n" + " subPath: mypath\n" + " volumes:\n" + " - name: host-volume\n" + " hostPath:\n" + " path: /host/data\n");
PodTemplate child = new PodTemplate();
child.setYaml("spec:\n" + " volumes:\n" + " - name: host-volume\n" + " hostPath:\n" + " path: /host/data2\n");
child.setContainers(Collections.singletonList(new ContainerTemplate("jnlp", "image")));
ConfigMapVolume cmVolume = new ConfigMapVolume("/etc/configmap", "my-configmap", false);
cmVolume.setSubPath("subpath");
child.setVolumes(Collections.singletonList(cmVolume));
child.setInheritFrom("parent");
child.setYamlMergeStrategy(merge());
setupStubs();
PodTemplate result = combine(parent, child);
Pod pod = new PodTemplateBuilder(result, slave).build();
// inherit from parent
assertEquals("some-label-value", pod.getMetadata().getLabels().get("some-label"));
Optional<Volume> maybeVolume = pod.getSpec().getVolumes().stream().filter(v -> "host-volume".equals(v.getName())).findFirst();
assertTrue(maybeVolume.isPresent());
// child value overrides parent value
assertThat(maybeVolume.get().getHostPath().getPath(), equalTo("/host/data2"));
assertThat(pod.getSpec().getContainers(), hasSize(1));
Container container = pod.getSpec().getContainers().get(0);
Optional<VolumeMount> maybeVolumeMount = container.getVolumeMounts().stream().filter(vm -> "host-volume".equals(vm.getName())).findFirst();
assertTrue(maybeVolumeMount.isPresent());
VolumeMount volumeMount = maybeVolumeMount.get();
assertEquals("/etc/config", volumeMount.getMountPath());
assertEquals("mypath", volumeMount.getSubPath());
Optional<VolumeMount> maybeVolumeMountCm = container.getVolumeMounts().stream().filter(vm -> "/etc/configmap".equals(vm.getMountPath())).findFirst();
assertTrue(maybeVolumeMountCm.isPresent());
VolumeMount cmVolumeMount = maybeVolumeMountCm.get();
assertEquals("subpath", cmVolumeMount.getSubPath());
}
Aggregations