Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JENKINS-68757 - adding subPath support for PersistentVolumeClaim volumes #1195

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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() //
Expand All @@ -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++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,6 +37,7 @@

public class PersistentVolumeClaim extends PodVolume {
private String mountPath;
private String subPath;
private String claimName;
@CheckForNull
private Boolean readOnly;
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
<f:entry title="${%Mount path}" field="mountPath">
<f:textbox />
</f:entry>

<f:entry title="${%Mount subPath}" field="subPath">
<f:textbox />
</f:entry>

</j:jelly>
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
Claim\ Name=\u7533\u660E\u503C
Read\ Only=\u53EA\u8BFB
Mount\ path=\u6302\u8F7D\u8DEF\u5F84
Mount\ subPath=\u5b50\u8def\u5f84
Original file line number Diff line number Diff line change
@@ -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");
}

}