Skip to content

Commit d65bbc7

Browse files
authored
Merge pull request #1471 from Vlatombe/JENKINS-72314
2 parents a7eefda + 859a253 commit d65bbc7

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java

+16-8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import hudson.model.ItemGroup;
2727
import hudson.util.XStream2;
2828
import jenkins.metrics.api.Metrics;
29+
import net.sf.json.JSONObject;
2930
import org.apache.commons.lang.StringUtils;
3031
import org.csanchez.jenkins.plugins.kubernetes.pipeline.PodTemplateMap;
3132
import org.csanchez.jenkins.plugins.kubernetes.pod.retention.Default;
@@ -147,6 +148,7 @@ public class KubernetesCloud extends Cloud implements PodTemplateGroup {
147148
public KubernetesCloud(String name) {
148149
super(name);
149150
setMaxRequestsPerHost(DEFAULT_MAX_REQUESTS_PER_HOST);
151+
setPodLabels(null);
150152
}
151153

152154
/**
@@ -215,7 +217,7 @@ public String getDefaultsProviderTemplate() {
215217

216218
@DataBoundSetter
217219
public void setDefaultsProviderTemplate(String defaultsProviderTemplate) {
218-
this.defaultsProviderTemplate = defaultsProviderTemplate;
220+
this.defaultsProviderTemplate = Util.fixEmpty(defaultsProviderTemplate);
219221
}
220222

221223
@NonNull
@@ -243,7 +245,7 @@ public String getServerUrl() {
243245

244246
@DataBoundSetter
245247
public void setServerUrl(@NonNull String serverUrl) {
246-
this.serverUrl = serverUrl;
248+
this.serverUrl = Util.fixEmpty(serverUrl);
247249
}
248250

249251
public String getServerCertificate() {
@@ -455,7 +457,7 @@ public void setLabels(Map<String, String> labels) {
455457
*/
456458
@NonNull
457459
public List<PodLabel> getPodLabels() {
458-
return podLabels == null || podLabels.isEmpty() ? PodLabel.fromMap(DEFAULT_POD_LABELS) : podLabels;
460+
return podLabels == null ? List.of() : podLabels;
459461
}
460462

461463
/**
@@ -464,9 +466,7 @@ public List<PodLabel> getPodLabels() {
464466
@DataBoundSetter
465467
public void setPodLabels(@CheckForNull List<PodLabel> labels) {
466468
this.podLabels = new ArrayList<>();
467-
if (labels != null) {
468-
this.podLabels.addAll(labels);
469-
}
469+
this.podLabels.addAll(labels == null || labels.isEmpty() ? PodLabel.fromMap(DEFAULT_POD_LABELS) : labels);
470470
}
471471

472472
/**
@@ -997,15 +997,23 @@ private Object readResolve() {
997997
if (waitForPodSec == null) {
998998
waitForPodSec = DEFAULT_WAIT_FOR_POD_SEC;
999999
}
1000-
if (podLabels == null && labels != null) {
1001-
setPodLabels(PodLabel.fromMap(labels));
1000+
if (podLabels == null) {
1001+
setPodLabels(labels == null ? null : PodLabel.fromMap(labels));
10021002
}
10031003
if (containerCap != null && containerCap == 0) {
10041004
containerCap = null;
10051005
}
10061006
return this;
10071007
}
10081008

1009+
@Override
1010+
public Cloud reconfigure(@NonNull StaplerRequest req, JSONObject form) throws Descriptor.FormException {
1011+
// cloud configuration doesn't contain templates anymore, so just keep existing ones.
1012+
var newInstance = (KubernetesCloud) super.reconfigure(req, form);
1013+
newInstance.setTemplates(this.templates);
1014+
return newInstance;
1015+
}
1016+
10091017
@Extension
10101018
public static class PodTemplateSourceImpl extends PodTemplateSource {
10111019
@NonNull

src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudTest.java

+14-25
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import static org.junit.Assert.assertTrue;
66
import static org.junit.Assert.fail;
77

8-
import hudson.util.VersionNumber;
9-
import java.io.IOException;
108
import java.util.ArrayList;
119
import java.util.Arrays;
1210
import java.util.Collections;
@@ -15,18 +13,8 @@
1513
import java.util.Map;
1614
import java.util.logging.Level;
1715
import java.util.logging.Logger;
18-
1916
import jenkins.model.Jenkins;
20-
import org.htmlunit.ElementNotFoundException;
21-
import org.htmlunit.html.DomElement;
22-
import org.htmlunit.html.DomNodeList;
23-
import org.htmlunit.html.HtmlAnchor;
24-
import org.htmlunit.html.HtmlButton;
25-
import org.htmlunit.html.HtmlElement;
26-
import org.htmlunit.html.HtmlForm;
27-
import org.htmlunit.html.HtmlFormUtil;
28-
import org.htmlunit.html.HtmlInput;
29-
import org.htmlunit.html.HtmlPage;
17+
import jenkins.model.JenkinsLocationConfiguration;
3018
import org.apache.commons.beanutils.PropertyUtils;
3119
import org.apache.commons.lang3.RandomStringUtils;
3220
import org.apache.commons.lang3.RandomUtils;
@@ -36,16 +24,19 @@
3624
import org.csanchez.jenkins.plugins.kubernetes.volumes.EmptyDirVolume;
3725
import org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume;
3826
import org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.WorkspaceVolume;
27+
import org.htmlunit.html.DomElement;
28+
import org.htmlunit.html.DomNodeList;
29+
import org.htmlunit.html.HtmlElement;
30+
import org.htmlunit.html.HtmlForm;
31+
import org.htmlunit.html.HtmlInput;
32+
import org.htmlunit.html.HtmlPage;
3933
import org.junit.After;
4034
import org.junit.Rule;
4135
import org.junit.Test;
4236
import org.jvnet.hudson.test.JenkinsRule;
4337
import org.jvnet.hudson.test.LoggerRule;
4438
import org.jvnet.hudson.test.recipes.LocalData;
4539

46-
import jenkins.model.JenkinsLocationConfiguration;
47-
import org.xml.sax.SAXException;
48-
4940
public class KubernetesCloudTest {
5041

5142
@Rule
@@ -62,18 +53,16 @@ public void tearDown() {
6253

6354
@Test
6455
public void configRoundTrip() throws Exception {
65-
KubernetesCloud cloud = new KubernetesCloud("kubernetes");
66-
PodTemplate podTemplate = new PodTemplate();
56+
var cloud = new KubernetesCloud("kubernetes");
57+
var podTemplate = new PodTemplate();
6758
podTemplate.setName("test-template");
6859
podTemplate.setLabel("test");
6960
cloud.addTemplate(podTemplate);
70-
j.jenkins.clouds.add(cloud);
71-
j.jenkins.save();
72-
JenkinsRule.WebClient wc = j.createWebClient();
73-
HtmlPage p = wc.goTo("cloud/kubernetes/configure");
74-
HtmlForm f = p.getFormByName("config");
75-
j.submit(f);
76-
assertEquals("PodTemplate{id='"+podTemplate.getId()+"', name='test-template', label='test'}", podTemplate.toString());
61+
var jenkins = j.jenkins;
62+
jenkins.clouds.add(cloud);
63+
jenkins.save();
64+
j.submit(j.createWebClient().goTo("cloud/kubernetes/configure").getFormByName("config"));
65+
assertEquals(cloud, jenkins.clouds.get(KubernetesCloud.class));
7766
}
7867

7968
@Test

0 commit comments

Comments
 (0)