Skip to content

Commit 8cae67a

Browse files
committed
If namespace is not provided nor autoconfigured should use default
Test pod creation on global config file
1 parent 132c66c commit 8cae67a

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

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

+25-8
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616
import java.util.List;
1717
import java.util.Map;
1818
import java.util.Set;
19+
import java.util.concurrent.TimeUnit;
1920
import java.util.logging.Level;
2021
import java.util.logging.Logger;
2122

2223
import javax.annotation.CheckForNull;
2324
import javax.annotation.Nonnull;
2425
import javax.servlet.ServletException;
2526

26-
import hudson.model.Environment;
27-
import jenkins.model.JenkinsLocationConfiguration;
2827
import org.apache.commons.codec.binary.Base64;
2928
import org.apache.commons.lang.StringUtils;
3029
import org.kohsuke.stapler.DataBoundConstructor;
@@ -56,11 +55,14 @@
5655
import hudson.slaves.NodeProvisioner;
5756
import hudson.util.FormValidation;
5857
import hudson.util.ListBoxModel;
58+
import io.fabric8.kubernetes.api.model.ContainerBuilder;
5959
import io.fabric8.kubernetes.api.model.Pod;
60+
import io.fabric8.kubernetes.api.model.PodBuilder;
6061
import io.fabric8.kubernetes.api.model.PodList;
6162
import io.fabric8.kubernetes.client.KubernetesClient;
6263
import io.fabric8.kubernetes.client.KubernetesClientException;
6364
import jenkins.model.Jenkins;
65+
import jenkins.model.JenkinsLocationConfiguration;
6466

6567
/**
6668
* Kubernetes cloud provider.
@@ -184,7 +186,6 @@ public String getServerUrl() {
184186

185187
@DataBoundSetter
186188
public void setServerUrl(@Nonnull String serverUrl) {
187-
Preconditions.checkArgument(!StringUtils.isBlank(serverUrl));
188189
this.serverUrl = serverUrl;
189190
}
190191

@@ -512,16 +513,32 @@ public FormValidation doTestConnection(@QueryParameter String name, @QueryParame
512513
Util.fixEmpty(serverCertificate), Util.fixEmpty(credentialsId), skipTlsVerify,
513514
connectionTimeout, readTimeout).createClient();
514515

516+
// test listing pods
515517
client.pods().list();
516-
return FormValidation.ok("Connection successful");
518+
519+
// test creating pods
520+
Pod pod = client.pods().create(new PodBuilder() //
521+
.withNewMetadata().withGenerateName("kubernetes-plugin-").endMetadata() //
522+
.withNewSpec() //
523+
.withContainers( //
524+
new ContainerBuilder().withName("alpine").withImage("alpine").withCommand("cat")
525+
.withTty(true).build()) //
526+
.endSpec().build());
527+
String podName = pod.getMetadata().getName();
528+
LOGGER.log(Level.FINE, "Created test pod: {0}/{1}",
529+
new String[] { pod.getMetadata().getNamespace(), podName });
530+
client.pods().withName(podName).waitUntilReady(30, TimeUnit.SECONDS);
531+
client.pods().withName(podName).delete();
532+
533+
return FormValidation.ok("Connection test successful");
517534
} catch (KubernetesClientException e) {
518-
LOGGER.log(Level.FINE, String.format("Error connecting to %s", serverUrl), e);
519-
return FormValidation.error("Error connecting to %s: %s", serverUrl, e.getCause() == null
535+
LOGGER.log(Level.FINE, String.format("Error testing connection %s", serverUrl), e);
536+
return FormValidation.error("Error testing connection %s: %s", serverUrl, e.getCause() == null
520537
? e.getMessage()
521538
: String.format("%s: %s", e.getCause().getClass().getName(), e.getCause().getMessage()));
522539
} catch (Exception e) {
523-
LOGGER.log(Level.FINE, String.format("Error connecting to %s", serverUrl), e);
524-
return FormValidation.error("Error connecting to %s: %s", serverUrl, e.getMessage());
540+
LOGGER.log(Level.FINE, String.format("Error testing connection %s", serverUrl), e);
541+
return FormValidation.error("Error testing connection %s: %s", serverUrl, e.getMessage());
525542
}
526543
}
527544

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public KubernetesFactoryAdapter(String serviceAddress, String namespace, @CheckF
7575
public KubernetesFactoryAdapter(String serviceAddress, String namespace, @CheckForNull String caCertData,
7676
@CheckForNull String credentials, boolean skipTlsVerify, int connectTimeout, int readTimeout, int maxRequestsPerHost) {
7777
this.serviceAddress = serviceAddress;
78-
this.namespace = namespace;
78+
this.namespace = StringUtils.isBlank(namespace) ? "default" : namespace;
7979
this.caCertData = caCertData;
8080
this.credentials = credentials != null ? getCredentials(credentials) : null;
8181
this.skipTlsVerify = skipTlsVerify;

0 commit comments

Comments
 (0)