-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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-46253 Add support for subPath to all volume types #1031
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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.*; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto above. |
||
|
||
/** | ||
* Helper class to build Pods from PodTemplates | ||
|
@@ -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("\\", "/")); | ||
Comment on lines
+157
to
+158
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is that normalization really needed? What happens if the agent is on Windows? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hello Viatombe, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's fine, I didn't see that the same code was applied to mountPath. |
||
if (StringUtils.isNotBlank(subPath)) { | ||
volumeMountBuilder = volumeMountBuilder.withSubPath(subPath); | ||
} | ||
} | ||
volumeMounts.put(mountPath, volumeMountBuilder.build()); | ||
volumes.put(volumeName, volume.buildVolume(volumeName)); | ||
i++; | ||
} | ||
|
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; | ||
|
||
Comment on lines
+5
to
+6
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please revert |
||
@Deprecated | ||
public class PodVolumes { | ||
|
||
|
@@ -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); | ||
} | ||
} | ||
|
||
|
@@ -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); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
Comment on lines
+31
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please avoid this kind of change that is just making reviews and blaming more complex. |
||
|
||
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) { | ||
Comment on lines
+41
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of this, you could add a setter for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hello, viatombe, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can read the explanation here -- Constructor vs. setters. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
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) { | ||
|
@@ -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> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use wildcard imports in production code. Please revert.