|
16 | 16 | import java.util.List;
|
17 | 17 | import java.util.Map;
|
18 | 18 | import java.util.Set;
|
| 19 | +import java.util.concurrent.TimeUnit; |
19 | 20 | import java.util.logging.Level;
|
20 | 21 | import java.util.logging.Logger;
|
21 | 22 |
|
22 | 23 | import javax.annotation.CheckForNull;
|
23 | 24 | import javax.annotation.Nonnull;
|
24 | 25 | import javax.servlet.ServletException;
|
25 | 26 |
|
26 |
| -import hudson.model.Environment; |
27 |
| -import jenkins.model.JenkinsLocationConfiguration; |
28 | 27 | import org.apache.commons.codec.binary.Base64;
|
29 | 28 | import org.apache.commons.lang.StringUtils;
|
30 | 29 | import org.kohsuke.stapler.DataBoundConstructor;
|
|
56 | 55 | import hudson.slaves.NodeProvisioner;
|
57 | 56 | import hudson.util.FormValidation;
|
58 | 57 | import hudson.util.ListBoxModel;
|
| 58 | +import io.fabric8.kubernetes.api.model.ContainerBuilder; |
59 | 59 | import io.fabric8.kubernetes.api.model.Pod;
|
| 60 | +import io.fabric8.kubernetes.api.model.PodBuilder; |
60 | 61 | import io.fabric8.kubernetes.api.model.PodList;
|
61 | 62 | import io.fabric8.kubernetes.client.KubernetesClient;
|
62 | 63 | import io.fabric8.kubernetes.client.KubernetesClientException;
|
63 | 64 | import jenkins.model.Jenkins;
|
| 65 | +import jenkins.model.JenkinsLocationConfiguration; |
64 | 66 |
|
65 | 67 | /**
|
66 | 68 | * Kubernetes cloud provider.
|
@@ -184,7 +186,6 @@ public String getServerUrl() {
|
184 | 186 |
|
185 | 187 | @DataBoundSetter
|
186 | 188 | public void setServerUrl(@Nonnull String serverUrl) {
|
187 |
| - Preconditions.checkArgument(!StringUtils.isBlank(serverUrl)); |
188 | 189 | this.serverUrl = serverUrl;
|
189 | 190 | }
|
190 | 191 |
|
@@ -512,16 +513,32 @@ public FormValidation doTestConnection(@QueryParameter String name, @QueryParame
|
512 | 513 | Util.fixEmpty(serverCertificate), Util.fixEmpty(credentialsId), skipTlsVerify,
|
513 | 514 | connectionTimeout, readTimeout).createClient();
|
514 | 515 |
|
| 516 | + // test listing pods |
515 | 517 | 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"); |
517 | 534 | } 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 |
520 | 537 | ? e.getMessage()
|
521 | 538 | : String.format("%s: %s", e.getCause().getClass().getName(), e.getCause().getMessage()));
|
522 | 539 | } 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()); |
525 | 542 | }
|
526 | 543 | }
|
527 | 544 |
|
|
0 commit comments