Skip to content

Commit

Permalink
Add support for subPath to all volume types
Browse files Browse the repository at this point in the history
Includes work from Miguel Campos (#1024)

refs JENKINS-46253
  • Loading branch information
mcamposv authored and hameno committed Sep 15, 2021
1 parent f70e44e commit 92fc68c
Show file tree
Hide file tree
Showing 24 changed files with 199 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,16 @@

package org.csanchez.jenkins.plugins.kubernetes;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.TcpSlaveAgentListener;
import hudson.Util;
import io.fabric8.kubernetes.api.model.PodSpecFluent;
import hudson.slaves.SlaveComputer;
import io.fabric8.kubernetes.api.model.*;
import io.fabric8.kubernetes.api.model.PodFluent.MetadataNested;
import io.fabric8.kubernetes.api.model.PodFluent.SpecNested;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.jenkins.lib.versionnumber.JavaSpecificationVersion;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.csanchez.jenkins.plugins.kubernetes.model.TemplateEnvVar;
import org.csanchez.jenkins.plugins.kubernetes.pipeline.PodTemplateStepExecution;
Expand All @@ -50,37 +42,19 @@
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

import hudson.TcpSlaveAgentListener;
import hudson.slaves.SlaveComputer;
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.ContainerBuilder;
import io.fabric8.kubernetes.api.model.ContainerPort;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.ExecAction;
import io.fabric8.kubernetes.api.model.LocalObjectReference;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.api.model.PodFluent.MetadataNested;
import io.fabric8.kubernetes.api.model.PodFluent.SpecNested;
import io.fabric8.kubernetes.api.model.Probe;
import io.fabric8.kubernetes.api.model.ProbeBuilder;
import io.fabric8.kubernetes.api.model.Quantity;
import io.fabric8.kubernetes.api.model.ResourceRequirements;
import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
import io.fabric8.kubernetes.api.model.Volume;
import io.fabric8.kubernetes.api.model.VolumeMount;
import io.fabric8.kubernetes.api.model.VolumeMountBuilder;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.jenkins.lib.versionnumber.JavaSpecificationVersion;
import jenkins.model.Jenkins;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static org.csanchez.jenkins.plugins.kubernetes.KubernetesCloud.JNLP_NAME;
import static org.csanchez.jenkins.plugins.kubernetes.PodTemplateUtils.combine;
import static org.csanchez.jenkins.plugins.kubernetes.PodTemplateUtils.isNullOrEmpty;
import static org.csanchez.jenkins.plugins.kubernetes.PodTemplateUtils.substituteEnv;
import static org.csanchez.jenkins.plugins.kubernetes.PodTemplateUtils.*;

/**
* Helper class to build Pods from PodTemplates
Expand Down Expand Up @@ -173,8 +147,20 @@ public Pod build() {
//We need to normalize the path or we can end up in really hard to debug issues.
final String mountPath = substituteEnv(Paths.get(volume.getMountPath()).normalize().toString().replace("\\", "/"));
if (!volumeMounts.containsKey(mountPath)) {
volumeMounts.put(mountPath, new VolumeMountBuilder() //
.withMountPath(mountPath).withName(volumeName).withReadOnly(false).build());
VolumeMountBuilder volumeMountBuilder = new VolumeMountBuilder() //
.withMountPath(mountPath)
.withName(volumeName)
.withReadOnly(false);

final String volumeSubPath = volume.getSubPath();
if (StringUtils.isNotBlank(volumeSubPath)) {
// We need to normalize the subPath, or we can end up in really hard to debug issues Just in case.
final String subPath = substituteEnv(Paths.get(volumeSubPath).normalize().toString().replace("\\", "/"));
if (StringUtils.isNotBlank(subPath)) {
volumeMountBuilder = volumeMountBuilder.withSubPath(subPath);
}
}
volumeMounts.put(mountPath, volumeMountBuilder.build());
volumes.put(volumeName, volume.buildVolume(volumeName));
i++;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.csanchez.jenkins.plugins.kubernetes;

import java.util.List;

import io.fabric8.kubernetes.api.model.VolumeMount;

import java.util.List;

@Deprecated
public class PodVolumes {

Expand All @@ -15,12 +15,12 @@ public static abstract class PodVolume extends org.csanchez.jenkins.plugins.kube
public static class EmptyDirVolume extends org.csanchez.jenkins.plugins.kubernetes.volumes.EmptyDirVolume {

public EmptyDirVolume(String mountPath, Boolean memory) {
super(mountPath, memory);
super(mountPath, memory, null);
}

protected Object readResolve() {
return new org.csanchez.jenkins.plugins.kubernetes.volumes.EmptyDirVolume(this.getMountPath(),
this.getMemory());
this.getMemory(), null);
}
}

Expand All @@ -41,25 +41,25 @@ protected Object readResolve() {
public static class HostPathVolume extends org.csanchez.jenkins.plugins.kubernetes.volumes.HostPathVolume {

public HostPathVolume(String hostPath, String mountPath) {
super(hostPath, mountPath);
super(hostPath, mountPath, null);
}

protected Object readResolve() {
return new org.csanchez.jenkins.plugins.kubernetes.volumes.HostPathVolume(this.getHostPath(),
this.getMountPath());
this.getMountPath(), null);
}
}

@Deprecated
public static class NfsVolume extends org.csanchez.jenkins.plugins.kubernetes.volumes.NfsVolume {

public NfsVolume(String serverAddress, String serverPath, Boolean readOnly, String mountPath) {
super(serverAddress, serverPath, readOnly, mountPath);
super(serverAddress, serverPath, readOnly, mountPath, null);
}

protected Object readResolve() {
return new org.csanchez.jenkins.plugins.kubernetes.volumes.NfsVolume(this.getServerAddress(),
this.getServerPath(), this.getReadOnly(), this.getMountPath());
this.getServerPath(), this.getReadOnly(), this.getMountPath(), null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,31 @@

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

import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;

import hudson.Extension;
import hudson.model.Descriptor;
import io.fabric8.kubernetes.api.model.Volume;
import io.fabric8.kubernetes.api.model.VolumeBuilder;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;

public class ConfigMapVolume extends PodVolume {

private String mountPath;
private String subPath;
private String configMapName;
private Boolean optional;

@DataBoundConstructor
public ConfigMapVolume(String mountPath, String configMapName, Boolean optional) {
@DataBoundConstructor
public ConfigMapVolume(String mountPath, String configMapName, Boolean optional, String subPath) {
this.mountPath = mountPath;
this.subPath = subPath;
this.configMapName = configMapName;
this.optional = optional;
}

public ConfigMapVolume(String mountPath, String configMapName, Boolean optional) {
this(mountPath, configMapName, optional, (String) null);
}

@Override
public Volume buildVolume(String volumeName) {
Expand All @@ -68,7 +73,11 @@ public String getMountPath() {
public Boolean getOptional() {
return optional;
}


public String getSubPath() {
return subPath;
}

@Extension
@Symbol("configMapVolume")
public static class DescriptorImpl extends Descriptor<PodVolume> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@

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

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;

import hudson.Extension;
import hudson.model.Descriptor;
import io.fabric8.kubernetes.api.model.Volume;
import io.fabric8.kubernetes.api.model.VolumeBuilder;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

public class EmptyDirVolume extends PodVolume {

Expand All @@ -43,18 +42,25 @@ public class EmptyDirVolume extends PodVolume {
private String mountPath;
@CheckForNull
private Boolean memory;
private String subPath;

@DataBoundConstructor
public EmptyDirVolume(String mountPath, Boolean memory) {
public EmptyDirVolume(String mountPath, Boolean memory, String subPath) {
this.mountPath = mountPath;
this.memory = memory;
this.subPath = subPath;
}

@Override
public String getMountPath() {
return mountPath;
}

@Override
public String getSubPath() {
return subPath;
}

public String getMedium() {
return getMemory() ? MEMORY_MEDIUM : DEFAULT_MEDIUM;
}
Expand Down Expand Up @@ -82,4 +88,4 @@ public String getDisplayName() {
return "Empty Dir Volume";
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@

public class HostPathVolume extends PodVolume {
private String mountPath;
private String subPath;
private String hostPath;

@DataBoundConstructor
public HostPathVolume(String hostPath, String mountPath) {
public HostPathVolume(String hostPath, String mountPath, String subPath) {
this.hostPath = hostPath;
this.mountPath = mountPath;
this.subPath = subPath;
}

public Volume buildVolume(String volumeName) {
Expand All @@ -53,6 +55,11 @@ public String getMountPath() {
return mountPath;
}

@Override
public String getSubPath() {
return subPath;
}

public String getHostPath() {
return hostPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,31 @@

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

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;

import hudson.Extension;
import hudson.model.Descriptor;
import io.fabric8.kubernetes.api.model.Volume;
import io.fabric8.kubernetes.api.model.VolumeBuilder;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

public class NfsVolume extends PodVolume {
private String mountPath;
private String subPath;
private String serverAddress;
private String serverPath;
@CheckForNull
private Boolean readOnly;

@DataBoundConstructor
public NfsVolume(String serverAddress, String serverPath, Boolean readOnly, String mountPath) {
public NfsVolume(String serverAddress, String serverPath, Boolean readOnly, String mountPath, String subPath) {
this.serverAddress = serverAddress;
this.serverPath = serverPath;
this.readOnly = readOnly;
this.mountPath = mountPath;
this.subPath = subPath;
}

public Volume buildVolume(String volumeName) {
Expand All @@ -61,6 +62,11 @@ public String getMountPath() {
return mountPath;
}

@Override
public String getSubPath() {
return subPath;
}

public String getServerAddress() {
return serverAddress;
}
Expand All @@ -82,4 +88,4 @@ public String getDisplayName() {
return "NFS Volume";
}
}
}
}

0 comments on commit 92fc68c

Please sign in to comment.