Skip to content

Commit

Permalink
Merge pull request #1471 from Vlatombe/JENKINS-72314
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlatombe authored Nov 16, 2023
2 parents a7eefda + 859a253 commit d65bbc7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -215,7 +217,7 @@ public String getDefaultsProviderTemplate() {

@DataBoundSetter
public void setDefaultsProviderTemplate(String defaultsProviderTemplate) {
this.defaultsProviderTemplate = defaultsProviderTemplate;
this.defaultsProviderTemplate = Util.fixEmpty(defaultsProviderTemplate);
}

@NonNull
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -455,7 +457,7 @@ public void setLabels(Map<String, String> labels) {
*/
@NonNull
public List<PodLabel> getPodLabels() {
return podLabels == null || podLabels.isEmpty() ? PodLabel.fromMap(DEFAULT_POD_LABELS) : podLabels;
return podLabels == null ? List.of() : podLabels;
}

/**
Expand All @@ -464,9 +466,7 @@ public List<PodLabel> getPodLabels() {
@DataBoundSetter
public void setPodLabels(@CheckForNull List<PodLabel> 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);
}

/**
Expand Down Expand Up @@ -997,15 +997,23 @@ 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;
}
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -36,16 +24,19 @@
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;
import org.jvnet.hudson.test.JenkinsRule;
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
Expand All @@ -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
Expand Down

0 comments on commit d65bbc7

Please sign in to comment.