-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1489 from idlefella/feature/add-generic-ephemeral…
…-storage-support
- Loading branch information
Showing
23 changed files
with
452 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/org/csanchez/jenkins/plugins/kubernetes/volumes/EphemeralVolume.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.csanchez.jenkins.plugins.kubernetes.volumes; | ||
|
||
import io.fabric8.kubernetes.api.model.Volume; | ||
import io.fabric8.kubernetes.api.model.VolumeBuilder; | ||
|
||
/** | ||
* Interface containing common code between {@link GenericEphemeralVolume} and {@link org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.GenericEphemeralWorkspaceVolume}. | ||
*/ | ||
public interface EphemeralVolume extends ProvisionedVolume { | ||
default Volume buildEphemeralVolume(String volumeName) { | ||
return new VolumeBuilder() | ||
.withName(volumeName) | ||
.withNewEphemeral() | ||
.withNewVolumeClaimTemplate() | ||
.withNewSpec() | ||
.withAccessModes(getAccessModesOrDefault()) | ||
.withStorageClassName(getStorageClassNameOrDefault()) | ||
.withNewResources() | ||
.withRequests(getResourceMap()) | ||
.endResources() | ||
.endSpec() | ||
.endVolumeClaimTemplate() | ||
.endEphemeral() | ||
.build(); | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
src/main/java/org/csanchez/jenkins/plugins/kubernetes/volumes/GenericEphemeralVolume.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package org.csanchez.jenkins.plugins.kubernetes.volumes; | ||
|
||
import edu.umd.cs.findbugs.annotations.CheckForNull; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import hudson.Extension; | ||
import hudson.Util; | ||
import hudson.model.Descriptor; | ||
import hudson.util.ListBoxModel; | ||
import io.fabric8.kubernetes.api.model.Volume; | ||
import java.util.Objects; | ||
import org.jenkinsci.Symbol; | ||
import org.kohsuke.accmod.Restricted; | ||
import org.kohsuke.accmod.restrictions.DoNotUse; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
import org.kohsuke.stapler.DataBoundSetter; | ||
import org.kohsuke.stapler.interceptor.RequirePOST; | ||
|
||
/** | ||
* Uses a generic ephemeral volume, that is created before the agent pod is created, and terminated afterwards. | ||
* See <a href="https://kubernetes.io/docs/concepts/storage/ephemeral-volumes/#generic-ephemeral-volumes">Kubernetes documentation</a> | ||
*/ | ||
@SuppressFBWarnings( | ||
value = "SE_NO_SERIALVERSIONID", | ||
justification = "Serialization happens exclusively through XStream and not Java Serialization.") | ||
public class GenericEphemeralVolume extends PodVolume implements EphemeralVolume { | ||
private String storageClassName; | ||
private String requestsSize; | ||
private String accessModes; | ||
private String mountPath; | ||
|
||
@DataBoundConstructor | ||
public GenericEphemeralVolume() {} | ||
|
||
@CheckForNull | ||
public String getAccessModes() { | ||
return accessModes; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setAccessModes(@CheckForNull String accessModes) { | ||
this.accessModes = accessModes; | ||
} | ||
|
||
@CheckForNull | ||
public String getRequestsSize() { | ||
return requestsSize; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setRequestsSize(@CheckForNull String requestsSize) { | ||
this.requestsSize = Util.fixEmptyAndTrim(requestsSize); | ||
} | ||
|
||
@CheckForNull | ||
public String getStorageClassName() { | ||
return storageClassName; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setStorageClassName(@CheckForNull String storageClassName) { | ||
this.storageClassName = Util.fixEmptyAndTrim(storageClassName); | ||
} | ||
|
||
@Override | ||
public String getMountPath() { | ||
return mountPath; | ||
} | ||
|
||
@Override | ||
public Volume buildVolume(String volumeName, String podName) { | ||
return buildEphemeralVolume(volumeName); | ||
} | ||
|
||
@DataBoundSetter | ||
public void setMountPath(String mountPath) { | ||
this.mountPath = mountPath; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
GenericEphemeralVolume that = (GenericEphemeralVolume) o; | ||
return Objects.equals(storageClassName, that.storageClassName) | ||
&& Objects.equals(requestsSize, that.requestsSize) | ||
&& Objects.equals(accessModes, that.accessModes) | ||
&& Objects.equals(mountPath, that.mountPath); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(storageClassName, requestsSize, accessModes, mountPath); | ||
} | ||
|
||
@Extension | ||
@Symbol("genericEphemeralVolume") | ||
public static class DescriptorImpl extends Descriptor<PodVolume> { | ||
@Override | ||
@NonNull | ||
public String getDisplayName() { | ||
return "Generic ephemeral volume"; | ||
} | ||
|
||
@SuppressWarnings("unused") // by stapler | ||
@RequirePOST | ||
@Restricted(DoNotUse.class) // stapler only | ||
public ListBoxModel doFillAccessModesItems() { | ||
return PVCVolumeUtils.ACCESS_MODES_BOX; | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/org/csanchez/jenkins/plugins/kubernetes/volumes/ProvisionedVolume.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.csanchez.jenkins.plugins.kubernetes.volumes; | ||
|
||
import io.fabric8.kubernetes.api.model.Quantity; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import org.apache.commons.lang.StringUtils; | ||
|
||
public interface ProvisionedVolume { | ||
default String getStorageClassNameOrDefault() { | ||
return getStorageClassName(); | ||
} | ||
|
||
String getStorageClassName(); | ||
|
||
default Map<String, Quantity> getResourceMap() { | ||
return Collections.singletonMap("storage", new Quantity(getRequestsSizeOrDefault())); | ||
} | ||
|
||
default String getRequestsSizeOrDefault() { | ||
return StringUtils.defaultString(getRequestsSize(), "10Gi"); | ||
} | ||
|
||
String getRequestsSize(); | ||
|
||
default String getAccessModesOrDefault() { | ||
return StringUtils.defaultString(getAccessModes(), "ReadWriteOnce"); | ||
} | ||
|
||
String getAccessModes(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...sanchez/jenkins/plugins/kubernetes/volumes/workspace/GenericEphemeralWorkspaceVolume.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package org.csanchez.jenkins.plugins.kubernetes.volumes.workspace; | ||
|
||
import edu.umd.cs.findbugs.annotations.CheckForNull; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import hudson.Extension; | ||
import hudson.Util; | ||
import hudson.model.Descriptor; | ||
import hudson.util.ListBoxModel; | ||
import io.fabric8.kubernetes.api.model.Volume; | ||
import org.csanchez.jenkins.plugins.kubernetes.volumes.EphemeralVolume; | ||
import org.csanchez.jenkins.plugins.kubernetes.volumes.PVCVolumeUtils; | ||
import org.jenkinsci.Symbol; | ||
import org.kohsuke.accmod.Restricted; | ||
import org.kohsuke.accmod.restrictions.DoNotUse; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
import org.kohsuke.stapler.DataBoundSetter; | ||
import org.kohsuke.stapler.interceptor.RequirePOST; | ||
|
||
/** | ||
* Uses a generic ephemeral volume, that is created before the agent pod is created, and terminated afterwards. | ||
*/ | ||
@SuppressFBWarnings( | ||
value = "SE_NO_SERIALVERSIONID", | ||
justification = "Serialization happens exclusively through XStream and not Java Serialization.") | ||
public class GenericEphemeralWorkspaceVolume extends WorkspaceVolume implements EphemeralVolume { | ||
|
||
private String storageClassName; | ||
private String requestsSize; | ||
private String accessModes; | ||
|
||
@DataBoundConstructor | ||
public GenericEphemeralWorkspaceVolume() {} | ||
|
||
@Override | ||
public String getStorageClassName() { | ||
return storageClassName; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setStorageClassName(String storageClassName) { | ||
this.storageClassName = Util.fixEmptyAndTrim(storageClassName); | ||
} | ||
|
||
@Override | ||
public String getRequestsSize() { | ||
return requestsSize; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setRequestsSize(@CheckForNull String requestsSize) { | ||
this.requestsSize = Util.fixEmptyAndTrim(requestsSize); | ||
} | ||
|
||
@Override | ||
public String getAccessModes() { | ||
return accessModes; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setAccessModes(String accessModes) { | ||
this.accessModes = accessModes; | ||
} | ||
|
||
@Override | ||
public Volume buildVolume(String volumeName, String podName) { | ||
return buildEphemeralVolume(volumeName); | ||
} | ||
|
||
@Extension | ||
@Symbol("genericEphemeralVolume") | ||
public static class DescriptorImpl extends Descriptor<WorkspaceVolume> { | ||
@Override | ||
@NonNull | ||
public String getDisplayName() { | ||
return "Generic Ephemeral Volume"; | ||
} | ||
|
||
@SuppressWarnings("unused") // by stapler | ||
@RequirePOST | ||
@Restricted(DoNotUse.class) // stapler only | ||
public ListBoxModel doFillAccessModesItems() { | ||
return PVCVolumeUtils.ACCESS_MODES_BOX; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.