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 36fbf78286..f32388d81b 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateBuilder.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateBuilder.java @@ -53,6 +53,7 @@ import org.csanchez.jenkins.plugins.kubernetes.pod.decorator.PodDecorator; import org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume; import org.csanchez.jenkins.plugins.kubernetes.volumes.ConfigMapVolume; +import org.csanchez.jenkins.plugins.kubernetes.volumes.PersistentVolumeClaim; import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.NoExternalUse; @@ -189,7 +190,11 @@ public Pod build() { String podName = agent.getPodName(); int i = 0; for (final PodVolume volume : template.getVolumes()) { - final String volumeName = "volume-" + i; + String volumeNameTmp = "volume-" + i; + if (volume instanceof PersistentVolumeClaim) { + volumeNameTmp = "volume-" + ((PersistentVolumeClaim)volume).getClaimName(); + } + final String volumeName = volumeNameTmp; final String mountPath = normalizePath(volume.getMountPath()); if (!volumeMounts.containsKey(mountPath)) { VolumeMountBuilder volumeMountBuilder = new VolumeMountBuilder() // @@ -202,8 +207,17 @@ public Pod build() { volumeMountBuilder = volumeMountBuilder.withSubPath(normalizePath(subPath)); } } + if (volume instanceof PersistentVolumeClaim) { + final PersistentVolumeClaim pvcVolume = (PersistentVolumeClaim) volume; + String subPath = pvcVolume.getSubPath(); + if (subPath != null && subPath.length()>0) { + volumeMountBuilder = volumeMountBuilder.withSubPath(normalizePath(subPath)); + } + } volumeMounts.put(mountPath, volumeMountBuilder.build()); - volumes.put(volumeName, volume.buildVolume(volumeName, podName)); + if (!volumes.containsKey(volumeName)) { + volumes.put(volumeName, volume.buildVolume(volumeName, podName)); + } i++; } } diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/volumes/PersistentVolumeClaim.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/volumes/PersistentVolumeClaim.java index 7c8378e3b2..cd0b638893 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/volumes/PersistentVolumeClaim.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/volumes/PersistentVolumeClaim.java @@ -28,6 +28,7 @@ import edu.umd.cs.findbugs.annotations.NonNull; import org.jenkinsci.Symbol; import org.kohsuke.stapler.DataBoundConstructor; +import org.kohsuke.stapler.DataBoundSetter; import hudson.Extension; import hudson.model.Descriptor; @@ -36,6 +37,7 @@ public class PersistentVolumeClaim extends PodVolume { private String mountPath; + private String subPath; private String claimName; @CheckForNull private Boolean readOnly; @@ -61,6 +63,14 @@ public Boolean getReadOnly() { return readOnly != null && readOnly; } + public String getSubPath() { + return subPath; + } + + @DataBoundSetter + public void setSubPath(String subPath) { + this.subPath = subPath; + } @Override public Volume buildVolume(String volumeName) { return new VolumeBuilder() diff --git a/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/volumes/PersistentVolumeClaim/config.jelly b/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/volumes/PersistentVolumeClaim/config.jelly index 104fe0c8bb..b4ee868551 100644 --- a/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/volumes/PersistentVolumeClaim/config.jelly +++ b/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/volumes/PersistentVolumeClaim/config.jelly @@ -13,5 +13,9 @@ + + + + \ No newline at end of file diff --git a/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/volumes/PersistentVolumeClaim/config_zh_CN.properties b/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/volumes/PersistentVolumeClaim/config_zh_CN.properties index 43067846a5..35e1072d35 100644 --- a/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/volumes/PersistentVolumeClaim/config_zh_CN.properties +++ b/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/volumes/PersistentVolumeClaim/config_zh_CN.properties @@ -23,3 +23,4 @@ Claim\ Name=\u7533\u660E\u503C Read\ Only=\u53EA\u8BFB Mount\ path=\u6302\u8F7D\u8DEF\u5F84 +Mount\ subPath=\u5b50\u8def\u5f84 diff --git a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/PersistentVolumeClaimTest.java b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/PersistentVolumeClaimTest.java new file mode 100644 index 0000000000..abacc0415e --- /dev/null +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/PersistentVolumeClaimTest.java @@ -0,0 +1,23 @@ +package org.csanchez.jenkins.plugins.kubernetes; + +import org.csanchez.jenkins.plugins.kubernetes.volumes.PersistentVolumeClaim; + +import static org.junit.Assert.*; +import org.junit.Test; + +public class PersistentVolumeClaimTest { + + @Test + public void testNullSubPathValue() { + PersistentVolumeClaim persistentVolumeClaim= new PersistentVolumeClaim("oneMountPath", "Myvolume",false); + assertNull(persistentVolumeClaim.getSubPath()); + } + + @Test + public void testValidSubPathValue() { + PersistentVolumeClaim persistentVolumeClaim= new PersistentVolumeClaim("oneMountPath", "Myvolume",false); + persistentVolumeClaim.setSubPath("miSubpath"); + assertEquals(persistentVolumeClaim.getSubPath(),"miSubpath"); + } + +}