From 753e51d5c703d03463bb80fdc7f7653ab0356926 Mon Sep 17 00:00:00 2001 From: Vincent Latombe Date: Wed, 9 Aug 2017 15:17:23 +0200 Subject: [PATCH] Allow to create non-configurable instances programmatically. --- .../plugins/kubernetes/KubernetesCloud.java | 23 +++++++++ .../NonConfigurableKubernetesCloud.java | 48 +++++++++++++++++++ .../plugins/kubernetes/Messages.properties | 1 + .../config.jelly | 6 +++ 4 files changed, 78 insertions(+) create mode 100644 src/main/java/org/csanchez/jenkins/plugins/kubernetes/NonConfigurableKubernetesCloud.java create mode 100644 src/main/resources/org/csanchez/jenkins/plugins/kubernetes/NonConfigurableKubernetesCloud/config.jelly diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java index 0d2580c43a..28fd43ed2f 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java @@ -30,6 +30,7 @@ import javax.annotation.Nonnull; import javax.servlet.ServletException; +import edu.umd.cs.findbugs.annotations.NonNull; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang.StringUtils; import org.csanchez.jenkins.plugins.kubernetes.pipeline.PodTemplateStepExecution; @@ -149,6 +150,28 @@ public KubernetesCloud(String name) { super(name); } + /** + * Copy constructor. + * Allows to create copies of the original kubernetes cloud. Since it's a singleton + * by design, this method also allows specifying a new name. + * @param name Name of the cloud to be created + * @param source Source Kubernetes cloud implementation + * @since 0.13 + */ + public KubernetesCloud(@NonNull String name, @NonNull KubernetesCloud source) { + super(name); + this.defaultsProviderTemplate = source.defaultsProviderTemplate; + this.templates.addAll(source.templates); + this.serverUrl = source.serverUrl; + this.skipTlsVerify = source.skipTlsVerify; + this.namespace = source.namespace; + this.jenkinsUrl = source.jenkinsUrl; + this.credentialsId = source.credentialsId; + this.containerCap = source.containerCap; + this.retentionTimeout = source.retentionTimeout; + this.connectTimeout = source.connectTimeout; + } + @Deprecated public KubernetesCloud(String name, List templates, String serverUrl, String namespace, String jenkinsUrl, String containerCapStr, int connectTimeout, int readTimeout, int retentionTimeout) { diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/NonConfigurableKubernetesCloud.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/NonConfigurableKubernetesCloud.java new file mode 100644 index 0000000000..581272066e --- /dev/null +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/NonConfigurableKubernetesCloud.java @@ -0,0 +1,48 @@ +package org.csanchez.jenkins.plugins.kubernetes; + +import edu.umd.cs.findbugs.annotations.NonNull; +import hudson.Extension; +import hudson.model.Descriptor; +import hudson.model.DescriptorVisibilityFilter; +import hudson.slaves.Cloud; +import jenkins.model.Jenkins; +import net.sf.json.JSONObject; +import org.kohsuke.stapler.StaplerRequest; + +public class NonConfigurableKubernetesCloud extends KubernetesCloud { + public NonConfigurableKubernetesCloud(@NonNull String name, @NonNull KubernetesCloud source) { + super(name, source); + } + + @Extension + public static class FilterImpl extends DescriptorVisibilityFilter { + @Override + public boolean filter(Object context, Descriptor descriptor) { + return !(descriptor instanceof DescriptorImpl); + } + } + + @Extension + public static class DescriptorImpl extends KubernetesCloud.DescriptorImpl { + @Override + public String getDisplayName() { + return Messages.NonConfigurableKubernetesCloud_displayName(); + } + + @Override + public boolean configure(StaplerRequest request, JSONObject object) throws Descriptor.FormException { + return true; + } + + @Override + public Cloud newInstance(StaplerRequest req, JSONObject formData) throws Descriptor.FormException { + if (req != null) { + // We prevent the cloud reconfiguration from the web UI + String cloudName = req.getParameter("cloudName"); + return Jenkins.getInstance().getCloud(cloudName); + } else { + throw new IllegalStateException("Expecting req to be non-null"); + } + } + } +} diff --git a/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/Messages.properties b/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/Messages.properties index 4f79f0770d..cb49c3af30 100644 --- a/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/Messages.properties +++ b/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/Messages.properties @@ -1 +1,2 @@ offline=Kubernetes agent is going offline +NonConfigurableKubernetesCloud.displayName=Kubernetes (predefined settings) diff --git a/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/NonConfigurableKubernetesCloud/config.jelly b/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/NonConfigurableKubernetesCloud/config.jelly new file mode 100644 index 0000000000..1b81f532a6 --- /dev/null +++ b/src/main/resources/org/csanchez/jenkins/plugins/kubernetes/NonConfigurableKubernetesCloud/config.jelly @@ -0,0 +1,6 @@ + + + + + +