diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate.java index 961aaf1a65..5121bc33e0 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate.java @@ -18,6 +18,7 @@ import com.google.common.base.Preconditions; import hudson.Extension; +import hudson.Util; import hudson.model.AbstractDescribableImpl; import hudson.model.Descriptor; import hudson.model.DescriptorVisibilityFilter; @@ -151,7 +152,7 @@ public String getDisplayName() { @DataBoundSetter public void setWorkingDir(String workingDir) { - this.workingDir = workingDir; + this.workingDir = Util.fixEmpty(workingDir); } public String getWorkingDir() { @@ -238,6 +239,15 @@ public void setResourceRequestCpu(String resourceRequestCpu) { this.resourceRequestCpu = resourceRequestCpu; } + public String getShell() { + return shell; + } + + @DataBoundSetter + public void setShell(String shell) { + this.shell = shell; + } + public Map getAsArgs() { Map argMap = new TreeMap<>(); @@ -374,12 +384,8 @@ public int hashCode() { return result; } - public String getShell() { - return shell; - } - - @DataBoundSetter - public void setShell(String shell) { - this.shell = shell; + private Object readResolve() { + this.workingDir = Util.fixEmpty(workingDir); + return this; } } diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateBuilder.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateBuilder.java index 71263d3c7b..f7c6ccd4b7 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateBuilder.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateBuilder.java @@ -133,9 +133,12 @@ public Pod build() { } if (template.getWorkspaceVolume() != null) { + LOGGER.log(Level.FINE, "Adding workspace volume from template: {0}", + template.getWorkspaceVolume().toString()); volumes.put(WORKSPACE_VOLUME_NAME, template.getWorkspaceVolume().buildVolume(WORKSPACE_VOLUME_NAME)); } else { // add an empty volume to share the workspace across the pod + LOGGER.log(Level.FINE, "Adding empty workspace volume"); volumes.put(WORKSPACE_VOLUME_NAME, new VolumeBuilder().withName(WORKSPACE_VOLUME_NAME).withNewEmptyDir().endEmptyDir().build()); } diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/volumes/EmptyDirVolume.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/volumes/EmptyDirVolume.java index ed9f484fe7..4f1297c4a7 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/volumes/EmptyDirVolume.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/volumes/EmptyDirVolume.java @@ -69,6 +69,11 @@ public Volume buildVolume(String volumeName) { return new VolumeBuilder().withName(volumeName).withNewEmptyDir().withMedium(getMedium()).endEmptyDir().build(); } + @Override + public String toString() { + return "EmptyDirVolume [mountPath=" + mountPath + ", memory=" + memory + "]"; + } + @Extension @Symbol("emptyDirVolume") public static class DescriptorImpl extends Descriptor { diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/volumes/SecretVolume.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/volumes/SecretVolume.java index 6a9a9ea227..1e7467f586 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/volumes/SecretVolume.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/volumes/SecretVolume.java @@ -60,6 +60,11 @@ public String getMountPath() { return mountPath; } + @Override + public String toString() { + return "SecretVolume [mountPath=" + mountPath + ", secretName=" + secretName + "]"; + } + @Extension @Symbol("secretVolume") public static class DescriptorImpl extends Descriptor { diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/volumes/workspace/EmptyDirWorkspaceVolume.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/volumes/workspace/EmptyDirWorkspaceVolume.java index c818d00d11..f389f7bba1 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/volumes/workspace/EmptyDirWorkspaceVolume.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/volumes/workspace/EmptyDirWorkspaceVolume.java @@ -62,6 +62,11 @@ public Volume buildVolume(String volumeName) { return new VolumeBuilder().withName(volumeName).withNewEmptyDir().withMedium(getMedium()).endEmptyDir().build(); } + @Override + public String toString() { + return "EmptyDirWorkspaceVolume [memory=" + memory + "]"; + } + @Extension @Symbol("emptyDirWorkspaceVolume") public static class DescriptorImpl extends Descriptor { diff --git a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateBuilderTest.java b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateBuilderTest.java index b51704769e..1dcb3392bc 100644 --- a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateBuilderTest.java +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateBuilderTest.java @@ -13,8 +13,11 @@ import java.util.stream.Collectors; import org.apache.commons.compress.utils.IOUtils; +import org.csanchez.jenkins.plugins.kubernetes.volumes.EmptyDirVolume; +import org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.EmptyDirWorkspaceVolume; import org.junit.Rule; import org.junit.Test; +import org.jvnet.hudson.test.Issue; import org.jvnet.hudson.test.LoggerRule; import org.mockito.Mock; import org.mockito.Spy; @@ -93,6 +96,27 @@ public void testBuildFromYaml() throws Exception { assertThat(pod.getMetadata().getLabels(), hasEntry("jenkins", "slave")); } + @Test + @Issue("JENKINS-50525") + public void testBuildWithCustomWorkspaceVolume() throws Exception { + PodTemplate template = new PodTemplate(); + template.setCustomWorkspaceVolumeEnabled(true); + template.setWorkspaceVolume(new EmptyDirWorkspaceVolume(false)); + ContainerTemplate containerTemplate = new ContainerTemplate("name", "image"); + containerTemplate.setWorkingDir(""); + template.getContainers().add(containerTemplate); + setupStubs(); + Pod pod = new PodTemplateBuilder(template).withSlave(slave).build(); + List containers = pod.getSpec().getContainers(); + assertEquals(2, containers.size()); + Container container0 = containers.get(0); + Container container1 = containers.get(1); + ImmutableList volumeMounts = ImmutableList + .of(new VolumeMount("/home/jenkins", "workspace-volume", false, null)); + assertEquals(volumeMounts, container0.getVolumeMounts()); + assertEquals(volumeMounts, container1.getVolumeMounts()); + } + private void setupStubs() { doReturn(JENKINS_URL).when(cloud).getJenkinsUrlOrDie(); when(computer.getName()).thenReturn(AGENT_NAME);