From 0bda546ae62a4d99c383fe769692a6786cd8c6c0 Mon Sep 17 00:00:00 2001 From: Kenneth Rogers Date: Thu, 10 Dec 2020 17:02:11 -0500 Subject: [PATCH 1/3] [CPLT2-6334] Clean up the snippet generation for a container template. --- .../kubernetes/ContainerLivenessProbe.java | 82 +++++++++++++++++-- .../plugins/kubernetes/ContainerTemplate.java | 32 +++++--- .../ContainerLivenessProbe/config.jelly | 14 ++-- 3 files changed, 102 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerLivenessProbe.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerLivenessProbe.java index 172126d7f6..152259c83a 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerLivenessProbe.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerLivenessProbe.java @@ -1,10 +1,14 @@ package org.csanchez.jenkins.plugins.kubernetes; import hudson.Extension; +import hudson.Util; import hudson.model.AbstractDescribableImpl; import hudson.model.Descriptor; +import hudson.util.FormValidation; +import org.apache.commons.lang.StringUtils; import org.jenkinsci.Symbol; import org.kohsuke.stapler.DataBoundConstructor; +import org.kohsuke.stapler.QueryParameter; import java.io.Serializable; @@ -12,12 +16,12 @@ * Created by fabricio.leotti on 26/04/17. */ public class ContainerLivenessProbe extends AbstractDescribableImpl implements Serializable { - private String execArgs; - private int timeoutSeconds; - private int initialDelaySeconds; - private int failureThreshold; - private int periodSeconds; - private int successThreshold; + private String execArgs = DescriptorImpl.DEFAULT_EXEC_ARGS; + private int timeoutSeconds = DescriptorImpl.DEFAULT_TIMEOUT_SECONDS; + private int initialDelaySeconds = DescriptorImpl.DEFAULT_INITIAL_DELAY_SECONDS; + private int failureThreshold = DescriptorImpl.DEFAULT_FAILURE_THRESHOLD; + private int periodSeconds = DescriptorImpl.DEFAULT_PERIOD_SECONDS; + private int successThreshold = DescriptorImpl.DEFAULT_SUCCESS_THRESHOLD; @DataBoundConstructor public ContainerLivenessProbe(String execArgs, int timeoutSeconds, int initialDelaySeconds, int failureThreshold, int periodSeconds, int successThreshold) { @@ -34,7 +38,7 @@ public String getExecArgs() { } public void setExecArgs(String execArgs) { - this.execArgs = execArgs; + this.execArgs = Util.fixEmpty(execArgs); } public int getTimeoutSeconds() { @@ -96,5 +100,69 @@ public static class DescriptorImpl extends Descriptor { public String getDisplayName() { return "Container Liveness Probe"; } + + public static final String DEFAULT_EXEC_ARGS = "uptime"; + public static final int DEFAULT_TIMEOUT_SECONDS = 1; + public static final int DEFAULT_INITIAL_DELAY_SECONDS = 0; + public static final int DEFAULT_PERIOD_SECONDS = 10; + public static final int DEFAULT_FAILURE_THRESHOLD = 3; + public static final int DEFAULT_SUCCESS_THRESHOLD = 1; + + public FormValidation doCheckExecArgs(@QueryParameter String value) { + if (StringUtils.isEmpty(value)) { + return FormValidation.error("Liveness probe command is mandatory"); + } else { + return FormValidation.ok(); + } + } + + public FormValidation doCheckTimeoutSeconds(@QueryParameter int value) { + if (value < 1) { + return FormValidation.error("Minimum Timeout Seconds value is 1"); + } else { + return FormValidation.ok(); + } + } + + public FormValidation doCheckInitialDelaySeconds(@QueryParameter int value) { + if (value < 0) { + return FormValidation.error("Minimum Initial Delay Seconds value is 0"); + } else { + return FormValidation.ok(); + } + } + + public FormValidation doCheckPeriodSeconds(@QueryParameter int value) { + if (value < 1) { + return FormValidation.error("Minimum Period Seconds value is 1"); + } else { + return FormValidation.ok(); + } + } + + public FormValidation doCheckFailureThreshold(@QueryParameter int value) { + if (value < 1) { + return FormValidation.error("Minimum Failure Threshold value is 1"); + } else { + return FormValidation.ok(); + } + } + + public FormValidation doCheckSuccessThreshold(@QueryParameter int value) { + if (value < 1) { + return FormValidation.error("Minimum Success Threshold value is 1"); + } else { + return FormValidation.ok(); + } + } + } + + public static ContainerLivenessProbe getDefault() { + return new ContainerLivenessProbe(DescriptorImpl.DEFAULT_EXEC_ARGS, + DescriptorImpl.DEFAULT_TIMEOUT_SECONDS, + DescriptorImpl.DEFAULT_INITIAL_DELAY_SECONDS, + DescriptorImpl.DEFAULT_FAILURE_THRESHOLD, + DescriptorImpl.DEFAULT_PERIOD_SECONDS, + DescriptorImpl.DEFAULT_SUCCESS_THRESHOLD); } } diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate.java index 23e43e64a1..1662d741a5 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate.java @@ -130,7 +130,7 @@ public String getImage() { @DataBoundSetter public void setCommand(String command) { - this.command = command; + this.command = Util.fixEmpty(command); } public String getCommand() { @@ -139,7 +139,7 @@ public String getCommand() { @DataBoundSetter public void setArgs(String args) { - this.args = args; + this.args = Util.fixEmpty(args); } public String getArgs() { @@ -222,11 +222,13 @@ public void setEnvVars(List envVars) { } - public ContainerLivenessProbe getLivenessProbe() { return livenessProbe; } + public ContainerLivenessProbe getLivenessProbe() { + return livenessProbe == null ? DescriptorImpl.defaultLivenessProbe() : livenessProbe; + } @DataBoundSetter public void setLivenessProbe(ContainerLivenessProbe livenessProbe) { - this.livenessProbe = livenessProbe; + this.livenessProbe = (livenessProbe == null || livenessProbe.equals(DescriptorImpl.defaultLivenessProbe()) ? null : livenessProbe); } public List getPorts() { @@ -244,7 +246,7 @@ public String getResourceRequestMemory() { @DataBoundSetter public void setResourceRequestMemory(String resourceRequestMemory) { - this.resourceRequestMemory = resourceRequestMemory; + this.resourceRequestMemory = Util.fixEmpty(resourceRequestMemory); } public String getResourceLimitMemory() { @@ -253,7 +255,7 @@ public String getResourceLimitMemory() { @DataBoundSetter public void setResourceLimitMemory(String resourceLimitMemory) { - this.resourceLimitMemory = resourceLimitMemory; + this.resourceLimitMemory = Util.fixEmpty(resourceLimitMemory); } public String getResourceRequestCpu() { @@ -261,8 +263,8 @@ public String getResourceRequestCpu() { } @DataBoundSetter - public void setResourceRequestCpu(String resourceRequestCpu) { - this.resourceRequestCpu = resourceRequestCpu; + public void setResourceRequestCpu(String resourceRequestCpu){ + this.resourceRequestCpu = Util.fixEmpty(resourceRequestCpu); } public String getResourceLimitCpu() { @@ -271,7 +273,7 @@ public String getResourceLimitCpu() { @DataBoundSetter public void setResourceLimitCpu(String resourceLimitCpu) { - this.resourceLimitCpu = resourceLimitCpu; + this.resourceLimitCpu = Util.fixEmpty(resourceLimitCpu); } public String getResourceRequestEphemeralStorage() { @@ -280,7 +282,7 @@ public String getResourceRequestEphemeralStorage() { @DataBoundSetter public void setResourceRequestEphemeralStorage(String resourceRequestEphemeralStorage) { - this.resourceRequestEphemeralStorage = resourceRequestEphemeralStorage; + this.resourceRequestEphemeralStorage = Util.fixEmpty(resourceRequestEphemeralStorage); } public String getResourceLimitEphemeralStorage() { @@ -289,7 +291,7 @@ public String getResourceLimitEphemeralStorage() { @DataBoundSetter public void setResourceLimitEphemeralStorage(String resourceLimitEphemeralStorage) { - this.resourceLimitEphemeralStorage = resourceLimitEphemeralStorage; + this.resourceLimitEphemeralStorage = Util.fixEmpty(resourceLimitEphemeralStorage); } @@ -299,7 +301,7 @@ public String getShell() { @DataBoundSetter public void setShell(String shell) { - this.shell = shell; + this.shell = Util.fixEmpty(shell); } public Map getAsArgs() { @@ -330,6 +332,9 @@ public List getEnvVarsDescriptors() { } public FormValidation doCheckName(@QueryParameter String value) { + if (StringUtils.isEmpty(value)) { + return FormValidation.warning("Container name is mandatory."); + } if(!PodTemplateUtils.validateContainerName(value)) { return FormValidation.error(Messages.RFC1123_error(value)); } @@ -350,6 +355,9 @@ public FormValidation doCheckImage(@QueryParameter String value) { public String getWorkingDir() { return DEFAULT_WORKING_DIR; } + public static final ContainerLivenessProbe defaultLivenessProbe() { + return ContainerLivenessProbe.getDefault(); + } } @Override diff --git a/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/ContainerLivenessProbe/config.jelly b/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/ContainerLivenessProbe/config.jelly index 52ce867369..5389e11efe 100644 --- a/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/ContainerLivenessProbe/config.jelly +++ b/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/ContainerLivenessProbe/config.jelly @@ -5,26 +5,26 @@ - + - + - + - + - + - + - \ No newline at end of file + From 1a779244969aee7316a6212a37d52a000b38c8b4 Mon Sep 17 00:00:00 2001 From: Kenneth Rogers Date: Mon, 14 Dec 2020 12:17:13 -0500 Subject: [PATCH 2/3] [CPLT2-6334] Make liveness probe optional. --- .../jenkins/plugins/kubernetes/ContainerLivenessProbe.java | 2 +- .../jenkins/plugins/kubernetes/ContainerTemplate.java | 4 ++-- .../jenkins/plugins/kubernetes/ContainerTemplate/config.jelly | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerLivenessProbe.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerLivenessProbe.java index 152259c83a..da76a9fa7e 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerLivenessProbe.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerLivenessProbe.java @@ -101,7 +101,7 @@ public String getDisplayName() { return "Container Liveness Probe"; } - public static final String DEFAULT_EXEC_ARGS = "uptime"; + public static final String DEFAULT_EXEC_ARGS = null; public static final int DEFAULT_TIMEOUT_SECONDS = 1; public static final int DEFAULT_INITIAL_DELAY_SECONDS = 0; public static final int DEFAULT_PERIOD_SECONDS = 10; diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate.java index 1662d741a5..3c9c7f93ab 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate.java @@ -223,12 +223,12 @@ public void setEnvVars(List envVars) { public ContainerLivenessProbe getLivenessProbe() { - return livenessProbe == null ? DescriptorImpl.defaultLivenessProbe() : livenessProbe; + return livenessProbe; } @DataBoundSetter public void setLivenessProbe(ContainerLivenessProbe livenessProbe) { - this.livenessProbe = (livenessProbe == null || livenessProbe.equals(DescriptorImpl.defaultLivenessProbe()) ? null : livenessProbe); + this.livenessProbe = livenessProbe; } public List getPorts() { diff --git a/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate/config.jelly b/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate/config.jelly index 4ac6cc6590..1ccd9b6b9e 100644 --- a/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate/config.jelly +++ b/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate/config.jelly @@ -76,13 +76,13 @@ - + - + From 07cb7a8605ea0c692c09524e44794f738486bc49 Mon Sep 17 00:00:00 2001 From: Kenneth Rogers Date: Mon, 4 Jan 2021 16:07:57 -0500 Subject: [PATCH 3/3] [CPLT2-6334] Removed unused function. Removed unnecessary jelly block following optionalProperty. --- .../jenkins/plugins/kubernetes/ContainerTemplate.java | 3 --- .../plugins/kubernetes/ContainerTemplate/config.jelly | 8 +------- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate.java index 3c9c7f93ab..7c83643736 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate.java @@ -355,9 +355,6 @@ public FormValidation doCheckImage(@QueryParameter String value) { public String getWorkingDir() { return DEFAULT_WORKING_DIR; } - public static final ContainerLivenessProbe defaultLivenessProbe() { - return ContainerLivenessProbe.getDefault(); - } } @Override diff --git a/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate/config.jelly b/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate/config.jelly index 1ccd9b6b9e..7bddff43a6 100644 --- a/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate/config.jelly +++ b/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplate/config.jelly @@ -76,13 +76,7 @@ - - - - - - - +