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 376d0043b8..0f217c7dfa 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java @@ -26,6 +26,7 @@ import hudson.model.ItemGroup; import hudson.util.XStream2; import jenkins.metrics.api.Metrics; +import net.sf.json.JSONObject; import org.apache.commons.lang.StringUtils; import org.csanchez.jenkins.plugins.kubernetes.pipeline.PodTemplateMap; import org.csanchez.jenkins.plugins.kubernetes.pod.retention.Default; @@ -147,6 +148,7 @@ public class KubernetesCloud extends Cloud implements PodTemplateGroup { public KubernetesCloud(String name) { super(name); setMaxRequestsPerHost(DEFAULT_MAX_REQUESTS_PER_HOST); + setPodLabels(null); } /** @@ -215,7 +217,7 @@ public String getDefaultsProviderTemplate() { @DataBoundSetter public void setDefaultsProviderTemplate(String defaultsProviderTemplate) { - this.defaultsProviderTemplate = defaultsProviderTemplate; + this.defaultsProviderTemplate = Util.fixEmpty(defaultsProviderTemplate); } @NonNull @@ -243,7 +245,7 @@ public String getServerUrl() { @DataBoundSetter public void setServerUrl(@NonNull String serverUrl) { - this.serverUrl = serverUrl; + this.serverUrl = Util.fixEmpty(serverUrl); } public String getServerCertificate() { @@ -455,7 +457,7 @@ public void setLabels(Map labels) { */ @NonNull public List getPodLabels() { - return podLabels == null || podLabels.isEmpty() ? PodLabel.fromMap(DEFAULT_POD_LABELS) : podLabels; + return podLabels == null ? List.of() : podLabels; } /** @@ -464,9 +466,7 @@ public List getPodLabels() { @DataBoundSetter public void setPodLabels(@CheckForNull List labels) { this.podLabels = new ArrayList<>(); - if (labels != null) { - this.podLabels.addAll(labels); - } + this.podLabels.addAll(labels == null || labels.isEmpty() ? PodLabel.fromMap(DEFAULT_POD_LABELS) : labels); } /** @@ -997,8 +997,8 @@ private Object readResolve() { if (waitForPodSec == null) { waitForPodSec = DEFAULT_WAIT_FOR_POD_SEC; } - if (podLabels == null && labels != null) { - setPodLabels(PodLabel.fromMap(labels)); + if (podLabels == null) { + setPodLabels(labels == null ? null : PodLabel.fromMap(labels)); } if (containerCap != null && containerCap == 0) { containerCap = null; @@ -1006,6 +1006,14 @@ private Object readResolve() { return this; } + @Override + public Cloud reconfigure(@NonNull StaplerRequest req, JSONObject form) throws Descriptor.FormException { + // cloud configuration doesn't contain templates anymore, so just keep existing ones. + var newInstance = (KubernetesCloud) super.reconfigure(req, form); + newInstance.setTemplates(this.templates); + return newInstance; + } + @Extension public static class PodTemplateSourceImpl extends PodTemplateSource { @NonNull diff --git a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudTest.java b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudTest.java index f840033990..f765b801f7 100644 --- a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudTest.java +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudTest.java @@ -5,8 +5,6 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import hudson.util.VersionNumber; -import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -15,18 +13,8 @@ import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; - import jenkins.model.Jenkins; -import org.htmlunit.ElementNotFoundException; -import org.htmlunit.html.DomElement; -import org.htmlunit.html.DomNodeList; -import org.htmlunit.html.HtmlAnchor; -import org.htmlunit.html.HtmlButton; -import org.htmlunit.html.HtmlElement; -import org.htmlunit.html.HtmlForm; -import org.htmlunit.html.HtmlFormUtil; -import org.htmlunit.html.HtmlInput; -import org.htmlunit.html.HtmlPage; +import jenkins.model.JenkinsLocationConfiguration; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomUtils; @@ -36,6 +24,12 @@ import org.csanchez.jenkins.plugins.kubernetes.volumes.EmptyDirVolume; import org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume; import org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.WorkspaceVolume; +import org.htmlunit.html.DomElement; +import org.htmlunit.html.DomNodeList; +import org.htmlunit.html.HtmlElement; +import org.htmlunit.html.HtmlForm; +import org.htmlunit.html.HtmlInput; +import org.htmlunit.html.HtmlPage; import org.junit.After; import org.junit.Rule; import org.junit.Test; @@ -43,9 +37,6 @@ import org.jvnet.hudson.test.LoggerRule; import org.jvnet.hudson.test.recipes.LocalData; -import jenkins.model.JenkinsLocationConfiguration; -import org.xml.sax.SAXException; - public class KubernetesCloudTest { @Rule @@ -62,18 +53,16 @@ public void tearDown() { @Test public void configRoundTrip() throws Exception { - KubernetesCloud cloud = new KubernetesCloud("kubernetes"); - PodTemplate podTemplate = new PodTemplate(); + var cloud = new KubernetesCloud("kubernetes"); + var podTemplate = new PodTemplate(); podTemplate.setName("test-template"); podTemplate.setLabel("test"); cloud.addTemplate(podTemplate); - j.jenkins.clouds.add(cloud); - j.jenkins.save(); - JenkinsRule.WebClient wc = j.createWebClient(); - HtmlPage p = wc.goTo("cloud/kubernetes/configure"); - HtmlForm f = p.getFormByName("config"); - j.submit(f); - assertEquals("PodTemplate{id='"+podTemplate.getId()+"', name='test-template', label='test'}", podTemplate.toString()); + var jenkins = j.jenkins; + jenkins.clouds.add(cloud); + jenkins.save(); + j.submit(j.createWebClient().goTo("cloud/kubernetes/configure").getFormByName("config")); + assertEquals(cloud, jenkins.clouds.get(KubernetesCloud.class)); } @Test