Skip to content

Commit d3bf08b

Browse files
authored
Merge pull request #1365 from LeoQuote/readonly-hostpath
2 parents eebef04 + a170ed4 commit d3bf08b

File tree

7 files changed

+31
-9
lines changed

7 files changed

+31
-9
lines changed

src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateBuilder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.csanchez.jenkins.plugins.kubernetes.model.TemplateEnvVar;
5252
import org.csanchez.jenkins.plugins.kubernetes.pipeline.PodTemplateStepExecution;
5353
import org.csanchez.jenkins.plugins.kubernetes.pod.decorator.PodDecorator;
54+
import org.csanchez.jenkins.plugins.kubernetes.volumes.HostPathVolume;
5455
import org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume;
5556
import org.csanchez.jenkins.plugins.kubernetes.volumes.ConfigMapVolume;
5657
import org.kohsuke.accmod.Restricted;
@@ -196,6 +197,11 @@ public Pod build() {
196197
volumeMountBuilder = volumeMountBuilder.withSubPath(normalizePath(subPath));
197198
}
198199
}
200+
if (volume instanceof HostPathVolume) {
201+
final HostPathVolume hostPathVolume = (HostPathVolume) volume;
202+
Boolean readOnly = hostPathVolume.getReadOnly();
203+
volumeMountBuilder = volumeMountBuilder.withReadOnly(readOnly);
204+
}
199205
volumeMounts.put(mountPath, volumeMountBuilder.build());
200206
volumes.put(volumeName, volume.buildVolume(volumeName, podName));
201207
i++;

src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodVolumes.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ protected Object readResolve() {
4040
@Deprecated
4141
public static class HostPathVolume extends org.csanchez.jenkins.plugins.kubernetes.volumes.HostPathVolume {
4242

43-
public HostPathVolume(String hostPath, String mountPath) {
44-
super(hostPath, mountPath);
43+
public HostPathVolume(String hostPath, String mountPath, Boolean readOnly) {
44+
super(hostPath, mountPath, readOnly);
4545
}
4646

4747
protected Object readResolve() {
4848
return new org.csanchez.jenkins.plugins.kubernetes.volumes.HostPathVolume(this.getHostPath(),
49-
this.getMountPath());
49+
this.getMountPath(), this.getReadOnly());
5050
}
5151
}
5252

src/main/java/org/csanchez/jenkins/plugins/kubernetes/volumes/HostPathVolume.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
package org.csanchez.jenkins.plugins.kubernetes.volumes;
2626

27+
import edu.umd.cs.findbugs.annotations.CheckForNull;
28+
import edu.umd.cs.findbugs.annotations.NonNull;
2729
import org.jenkinsci.Symbol;
2830
import org.kohsuke.stapler.DataBoundConstructor;
2931

@@ -36,10 +38,14 @@ public class HostPathVolume extends PodVolume {
3638
private String mountPath;
3739
private String hostPath;
3840

41+
@CheckForNull
42+
private Boolean readOnly;
43+
3944
@DataBoundConstructor
40-
public HostPathVolume(String hostPath, String mountPath) {
45+
public HostPathVolume(String hostPath, String mountPath, Boolean readOnly) {
4146
this.hostPath = hostPath;
4247
this.mountPath = mountPath;
48+
this.readOnly = readOnly;
4349
}
4450

4551
public Volume buildVolume(String volumeName) {
@@ -57,6 +63,11 @@ public String getHostPath() {
5763
return hostPath;
5864
}
5965

66+
@NonNull
67+
public Boolean getReadOnly() {
68+
return readOnly != null && readOnly;
69+
}
70+
6071
@Extension
6172
@Symbol("hostPathVolume")
6273
public static class DescriptorImpl extends Descriptor<PodVolume> {

src/main/resources/org/csanchez/jenkins/plugins/kubernetes/volumes/HostPathVolume/config.jelly

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@
1010
<f:textbox />
1111
</f:entry>
1212

13+
<f:entry title="${%Read Only}" field="readOnly">
14+
<f:checkbox />
15+
</f:entry>
16+
1317
</j:jelly>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Flag for read-only mount, set hostPath mount to readOnly is considered best-practice.

src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateBuilderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public void testBuildFromTemplate(boolean directConnection) throws Exception {
289289
template.setHostNetwork(false);
290290

291291
List<PodVolume> volumes = new ArrayList<PodVolume>();
292-
volumes.add(new HostPathVolume("/host/data", "/container/data"));
292+
volumes.add(new HostPathVolume("/host/data", "/container/data", false));
293293
volumes.add(new EmptyDirVolume("/empty/dir", false));
294294
template.setVolumes(volumes);
295295

src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateUtilsTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -527,13 +527,13 @@ public void shouldTreatNullEnvFromSouresAsEmpty(boolean parentEnvNull, boolean t
527527
@Test
528528
public void shouldCombineAllMounts() {
529529
PodTemplate template1 = new PodTemplate();
530-
HostPathVolume hostPathVolume1 = new HostPathVolume("/host/mnt1", "/container/mnt1");
531-
HostPathVolume hostPathVolume2 = new HostPathVolume("/host/mnt2", "/container/mnt2");
530+
HostPathVolume hostPathVolume1 = new HostPathVolume("/host/mnt1", "/container/mnt1", false);
531+
HostPathVolume hostPathVolume2 = new HostPathVolume("/host/mnt2", "/container/mnt2", false);
532532
template1.setVolumes(asList(hostPathVolume1, hostPathVolume2));
533533

534534
PodTemplate template2 = new PodTemplate();
535-
HostPathVolume hostPathVolume3 = new HostPathVolume("/host/mnt3", "/container/mnt3");
536-
HostPathVolume hostPathVolume4 = new HostPathVolume("/host/mnt1", "/container/mnt4");
535+
HostPathVolume hostPathVolume3 = new HostPathVolume("/host/mnt3", "/container/mnt3", false);
536+
HostPathVolume hostPathVolume4 = new HostPathVolume("/host/mnt1", "/container/mnt4", false);
537537
template2.setVolumes(asList(hostPathVolume3, hostPathVolume4));
538538

539539
PodTemplate result = combine(template1, template2);

0 commit comments

Comments
 (0)