From 36fa649c8da1d0d74db04350b6894abcc4028e73 Mon Sep 17 00:00:00 2001 From: Matt Overstreet Date: Wed, 18 May 2016 09:15:09 -0400 Subject: [PATCH 1/4] Initial support for testing More to come --- pom.xml | 13 ++++ .../kubernetes/KubernetesConfiguration.java | 15 +++- .../grandcentral/kubernetes/PodManager.java | 3 +- .../kubernetes/PodManagerTest.java | 77 +++++++++++++++++++ 4 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 src/test/com/o19s/grandcentral/kubernetes/PodManagerTest.java diff --git a/pom.xml b/pom.xml index dd6a3dc..46e89b5 100644 --- a/pom.xml +++ b/pom.xml @@ -12,6 +12,7 @@ 0.9.1 9.2.13.v20150730 + 4.12 @@ -30,6 +31,18 @@ jetty-proxy ${jetty.version} + + junit + junit + test + ${junit.version} + + + com.github.tomakehurst + wiremock + 1.58 + + diff --git a/src/main/java/com/o19s/grandcentral/kubernetes/KubernetesConfiguration.java b/src/main/java/com/o19s/grandcentral/kubernetes/KubernetesConfiguration.java index c7a2cfb..098adf0 100644 --- a/src/main/java/com/o19s/grandcentral/kubernetes/KubernetesConfiguration.java +++ b/src/main/java/com/o19s/grandcentral/kubernetes/KubernetesConfiguration.java @@ -2,8 +2,6 @@ import com.fasterxml.jackson.annotation.JsonProperty; import io.dropwizard.jackson.JsonSnakeCase; -import io.dropwizard.setup.Environment; -import org.apache.http.client.HttpClient; import org.hibernate.validator.constraints.NotEmpty; import javax.validation.constraints.NotNull; @@ -27,6 +25,9 @@ public class KubernetesConfiguration { @NotEmpty private String namespace; + // this can be null + private String protocol; + @JsonProperty public String getMasterIp() { return masterIp; @@ -42,6 +43,16 @@ public String getUsername() { return username; } + @JsonProperty + public String getProtocol() { + return (protocol == null) ? "https" : protocol; + } + + @JsonProperty + public void setProtocol(String protocol) { + this.protocol = protocol; + } + @JsonProperty public void setUsername(String username) { this.username = username; diff --git a/src/main/java/com/o19s/grandcentral/kubernetes/PodManager.java b/src/main/java/com/o19s/grandcentral/kubernetes/PodManager.java index fa3f567..fbebed2 100644 --- a/src/main/java/com/o19s/grandcentral/kubernetes/PodManager.java +++ b/src/main/java/com/o19s/grandcentral/kubernetes/PodManager.java @@ -75,6 +75,7 @@ public class PodManager { * @param keystorePath Path to the Java Keystore containing trusted certificates * @param maximumPodCount Maximum number of pods to ever have running at once * @param refreshIntervalInMs Interval with which to refresh the pods + * @param podYamlPath the location of the yaml config for the application pod */ public PodManager(KubernetesConfiguration k8sConfiguration, String keystorePath, @@ -343,7 +344,7 @@ else if (left.getLastRequest() < right.getLastRequest()) * @throws IOException */ private void refreshPods() throws IOException { - HttpGet podsGet = new HttpGet("https://" + k8sConfiguration.getMasterIp() + ":443/api/v1/namespaces/" + k8sConfiguration.getNamespace() + "/pods"); + HttpGet podsGet = new HttpGet(k8sConfiguration.getProtocol() + "://" + k8sConfiguration.getMasterIp() + "/api/v1/namespaces/" + k8sConfiguration.getNamespace() + "/pods"); try (CloseableHttpResponse response = httpClient.execute(podsGet, httpContext)) { HttpEntity entity = response.getEntity(); diff --git a/src/test/com/o19s/grandcentral/kubernetes/PodManagerTest.java b/src/test/com/o19s/grandcentral/kubernetes/PodManagerTest.java new file mode 100644 index 0000000..cce2480 --- /dev/null +++ b/src/test/com/o19s/grandcentral/kubernetes/PodManagerTest.java @@ -0,0 +1,77 @@ +package com.o19s.grandcentral.kubernetes; +import com.github.tomakehurst.wiremock.junit.WireMockRule; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import static com.github.tomakehurst.wiremock.client.WireMock.*; + +; +/** + * Created by Omnifroodle on 5/17/16. + */ +public class PodManagerTest { + private KubernetesConfiguration kubecfg; + @Rule + public WireMockRule wireMockRule = new WireMockRule(8888); + + @Before + public void setUp() throws Exception { + this.kubecfg = new KubernetesConfiguration(); + kubecfg.setMasterIp("127.0.0.1:8888"); + kubecfg.setProtocol("http"); + kubecfg.setNamespace("test"); + kubecfg.setUsername("fred"); + kubecfg.setPassword("flintstone"); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGet() throws Exception { + + } + + @Test + public void testContains() throws Exception { + + } + + @Test + public void testAdd() throws Exception { + + } + + @Test + public void testRefreshPods() throws Exception { + stubFor(get(urlEqualTo("/api/v1/namespaces/test/pods")) + .willReturn(aResponse() + .withStatus(200) + .withHeader("Content-Type", "text/json") + .withBody("{\"items\":[" + + " {" + + " \"metadata\":{" + + " \"name\": \"abc\"" + + " }," + + " \"status\": {" + + " \"phase\": \"Running\"," + + " \"podIP\": \"1.1.1.1\"" + + " }" + + " }," + + " {" + + " \"metadata\":{" + + " \"name\": \"abcd\"" + + " }," + + " \"status\": {" + + " \"phase\": \"Running\"," + + " \"podIP\": \"1.1.1.2\"" + + " }" + + " }" + + " ]}"))); + + PodManager manager = new PodManager(this.kubecfg, "config/grandcentral.jks", 100, 1, "./config/configuration.yml"); + } +} \ No newline at end of file From 581ea2d1b7245b4aed146e3fa3a5e77cea418e2b Mon Sep 17 00:00:00 2001 From: Eric Pugh Date: Mon, 23 May 2016 15:07:16 -0400 Subject: [PATCH 2/4] move unit test to the src/test/java so it is properly picked up by Maven IDE generators --- .../com/o19s/grandcentral/kubernetes/PodManagerTest.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/test/{ => java}/com/o19s/grandcentral/kubernetes/PodManagerTest.java (100%) diff --git a/src/test/com/o19s/grandcentral/kubernetes/PodManagerTest.java b/src/test/java/com/o19s/grandcentral/kubernetes/PodManagerTest.java similarity index 100% rename from src/test/com/o19s/grandcentral/kubernetes/PodManagerTest.java rename to src/test/java/com/o19s/grandcentral/kubernetes/PodManagerTest.java From e8e11e32e304a385894b89111a89c129ec8b583f Mon Sep 17 00:00:00 2001 From: Eric Pugh Date: Mon, 23 May 2016 15:07:42 -0400 Subject: [PATCH 3/4] scope normally is the last attribute --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 46e89b5..c27f505 100644 --- a/pom.xml +++ b/pom.xml @@ -34,8 +34,8 @@ junit junit - test ${junit.version} + test com.github.tomakehurst @@ -92,4 +92,4 @@ - \ No newline at end of file + From eee6e6bcf9918d2ba761963ece723761cdffe544 Mon Sep 17 00:00:00 2001 From: Eric Pugh Date: Mon, 23 May 2016 15:54:47 -0400 Subject: [PATCH 4/4] small fixes from running through the process. --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 01da35a..cd3bc16 100644 --- a/README.md +++ b/README.md @@ -64,9 +64,9 @@ The K8S cluster has a self-signed SSL certificate. It must be added to a keystor ``` brew install openssl -echo -n | /usr/local/Cellar/openssl/1.0.2e/bin/openssl s_client -connect :443 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > config/local.pem +echo -n | /usr/local/Cellar/openssl/1.0.2g/bin/openssl s_client -connect :443 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > config/local.pem keytool -importkeystore -srckeystore $JAVA_HOME/jre/lib/security/cacerts -destkeystore config/grandcentral.jks -srcstorepass changeit -deststorepass changeit -ho "yes" | keytool -import -v -trustcacerts -alias local_k8s -file k8s/local.pem -keystore config/grandcentral.jks -keypass changeit -storepass changeit +echo "yes" | keytool -import -v -trustcacerts -alias local_k8s -file config/local.pem -keystore config/grandcentral.jks -keypass changeit -storepass changeit ``` **Linux** @@ -74,7 +74,7 @@ ho "yes" | keytool -import -v -trustcacerts -alias local_k8s -file k8s/local.pem ``` echo -n | openssl s_client -connect 172.17.4.99:443 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > config/local.pem keytool -importkeystore -srckeystore $JAVA_HOME/jre/lib/security/cacerts -destkeystore config/grandcentral.jks -srcstorepass changeit -deststorepass changeit -echo "yes" | keytool -import -v -trustcacerts -alias local_k8s -file k8s/local.pem -keystore config/grandcentral.jks -keypass changeit -storepass changeit +echo "yes" | keytool -import -v -trustcacerts -alias local_k8s -file config/local.pem -keystore config/grandcentral.jks -keypass changeit -storepass changeit ``` ### Logging in to GCR.io