diff --git a/src/main/java/org/jenkinsci/plugins/kubernetes/auth/impl/AbstractKubernetesAuth.java b/src/main/java/org/jenkinsci/plugins/kubernetes/auth/impl/AbstractKubernetesAuth.java index 821131d..38e2dfc 100644 --- a/src/main/java/org/jenkinsci/plugins/kubernetes/auth/impl/AbstractKubernetesAuth.java +++ b/src/main/java/org/jenkinsci/plugins/kubernetes/auth/impl/AbstractKubernetesAuth.java @@ -26,7 +26,9 @@ public io.fabric8.kubernetes.api.model.ConfigBuilder buildConfigBuilder(Kubernet if (caCertificate != null && !caCertificate.isEmpty()) { cluster.setCertificateAuthorityData(Utils.encodeBase64(Utils.wrapCertificate(caCertificate))); } - cluster.setInsecureSkipTlsVerify(config.isSkipTlsVerify()); + if (config.isSkipTlsVerify()) { + cluster.setInsecureSkipTlsVerify(config.isSkipTlsVerify()); + } configBuilder .addNewCluster() .withName(clusterName) @@ -48,6 +50,7 @@ public io.fabric8.kubernetes.api.model.ConfigBuilder buildConfigBuilder(Kubernet .withUser(username) .endContext() .endContext(); + configBuilder.withCurrentContext(context); return configBuilder; } diff --git a/src/test/java/org/jenkinsci/plugins/kubernetes/auth/impl/AbstractKubernetesAuthTest.java b/src/test/java/org/jenkinsci/plugins/kubernetes/auth/impl/AbstractKubernetesAuthTest.java index d53b6aa..b331117 100644 --- a/src/test/java/org/jenkinsci/plugins/kubernetes/auth/impl/AbstractKubernetesAuthTest.java +++ b/src/test/java/org/jenkinsci/plugins/kubernetes/auth/impl/AbstractKubernetesAuthTest.java @@ -1,6 +1,7 @@ package org.jenkinsci.plugins.kubernetes.auth.impl; import io.fabric8.kubernetes.api.model.AuthInfoBuilder; +import io.fabric8.kubernetes.api.model.Cluster; import io.fabric8.kubernetes.client.ConfigBuilder; import io.fabric8.kubernetes.client.utils.Serialization; import org.jenkinsci.plugins.kubernetes.auth.KubernetesAuthConfig; @@ -9,25 +10,47 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; public class AbstractKubernetesAuthTest { @Test public void createConfig() throws Exception { - AbstractKubernetesAuth b = new AbstractKubernetesAuth() { - @Override - public ConfigBuilder decorate(ConfigBuilder builder, KubernetesAuthConfig config) throws KubernetesAuthException { - return builder; - } - - @Override - AuthInfoBuilder decorate(AuthInfoBuilder builder, KubernetesAuthConfig config) throws KubernetesAuthException { - return builder; - } - }; + AbstractKubernetesAuth b = new NoopKubernetesAuth(); io.fabric8.kubernetes.api.model.Config c = Serialization.yamlMapper().readValue( b.buildKubeConfig(new KubernetesAuthConfig("serverUrl", "caCertificate", false)), io.fabric8.kubernetes.api.model.Config.class ); - assertEquals("serverUrl", c.getClusters().get(0).getCluster().getServer()); - assertEquals(Utils.encodeBase64(Utils.wrapCertificate("caCertificate")), c.getClusters().get(0).getCluster().getCertificateAuthorityData()); + assertEquals(1, c.getClusters().size()); + Cluster cluster = c.getClusters().get(0).getCluster(); + assertEquals("serverUrl", cluster.getServer()); + assertNull(cluster.getInsecureSkipTlsVerify()); + assertEquals("k8s",c.getCurrentContext()); + assertEquals(Utils.encodeBase64(Utils.wrapCertificate("caCertificate")), cluster.getCertificateAuthorityData()); + } + + @Test + public void skipTlsVerify() throws Exception { + AbstractKubernetesAuth b = new NoopKubernetesAuth(); + io.fabric8.kubernetes.api.model.Config c = Serialization.yamlMapper().readValue( + b.buildKubeConfig(new KubernetesAuthConfig("serverUrl", "caCertificate", true)), io.fabric8.kubernetes.api.model.Config.class + ); + assertEquals(1, c.getClusters().size()); + Cluster cluster = c.getClusters().get(0).getCluster(); + assertEquals("serverUrl", cluster.getServer()); + assertTrue(cluster.getInsecureSkipTlsVerify()); + assertEquals("k8s",c.getCurrentContext()); + assertEquals(Utils.encodeBase64(Utils.wrapCertificate("caCertificate")), cluster.getCertificateAuthorityData()); + } + + private static class NoopKubernetesAuth extends AbstractKubernetesAuth { + @Override + public ConfigBuilder decorate(ConfigBuilder builder, KubernetesAuthConfig config) throws KubernetesAuthException { + return builder; + } + + @Override + AuthInfoBuilder decorate(AuthInfoBuilder builder, KubernetesAuthConfig config) throws KubernetesAuthException { + return builder; + } } }