diff --git a/CHANGELOG.md b/CHANGELOG.md index 356b8cf447b..e407c63d0bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ #### New Features * Fix #2287: Add support for V1 and V1Beta1 CustomResourceDefinition +* Fix #2319: Create Config without using auto-configure functionality or setting env variables ### 4.10.3 (2020-07-14) #### Bugs diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/Config.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/Config.java index a58d78f2fa3..e4cfbc87ec8 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/Config.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/Config.java @@ -211,16 +211,45 @@ public class Config { */ private Map customHeaders = null; + private Boolean autoConfigure = Boolean.FALSE; + /** * @deprecated use {@link #autoConfigure(String)} or {@link ConfigBuilder} instead */ @Deprecated public Config() { if (!Utils.getSystemPropertyOrEnvVar(KUBERNETES_DISABLE_AUTO_CONFIG_SYSTEM_PROPERTY, false)) { + this.autoConfigure = Boolean.TRUE; + autoConfigure(this, null); + } + } + + private Config(Boolean autoConfigure) { + if (Boolean.TRUE.equals(autoConfigure)) { + this.autoConfigure = Boolean.TRUE; autoConfigure(this, null); } } + /** + * Create an empty {@link Config} class without any automatic configuration + * (i.e. reading system properties/environment variables to load defaults.) + * You can also reuse this object to build your own {@link Config} object + * without any auto configuration like this: + * + *
{
+   * @code
+   * Config configFromBuilder = new ConfigBuilder(Config.empty())
+   *                                // ...
+   *                               .build();
+   * }
+ * + * @return a Config object without any automatic configuration + */ + public static Config empty() { + return new Config(false); + } + /** * Does auto detection with some opinionated defaults. * @@ -1171,6 +1200,10 @@ public void setCustomHeaders(Map customHeaders) { this.customHeaders = customHeaders; } + public Boolean getAutoConfigure() { + return autoConfigure; + } + /** * Returns all the {@link NamedContext}s that exist in the kube config * diff --git a/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/ConfigTest.java b/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/ConfigTest.java index 234cb148c1e..47429b4d240 100644 --- a/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/ConfigTest.java +++ b/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/ConfigTest.java @@ -22,6 +22,7 @@ import okhttp3.TlsVersion; import org.apache.commons.lang.SystemUtils; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -44,6 +45,7 @@ import static org.junit.Assert.assertNull; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -321,7 +323,7 @@ public void testWithNonExistingNamespacePath() { Config config = new Config(); assertNotNull(config); assertEquals("http://somehost:80/", config.getMasterUrl()); - assertEquals(null, config.getNamespace()); + Assertions.assertNull(config.getNamespace()); } @Test @@ -365,7 +367,7 @@ public void testWithNamespacePathAndSytemPropertiesAndBuilder() { @Test public void testWithCustomHeader() { - Map customHeaders = new HashMap(); + Map customHeaders = new HashMap<>(); customHeaders.put("user-id","test-user"); customHeaders.put("cluster-id","test-cluster"); Config config = new ConfigBuilder() @@ -393,12 +395,12 @@ public void shouldSetImpersonateUsernameAndGroupFromSystemProperty() { assertEquals("a", config.getImpersonateUsername()); assertArrayEquals(new String[]{"group"}, config.getImpersonateGroups()); - assertEquals(Arrays.asList("d"), config.getImpersonateExtras().get("c")); + assertEquals(Collections.singletonList("d"), config.getImpersonateExtras().get("c")); } @Test - public void shouldInstantiateClientUsingYaml() throws MalformedURLException { + public void shouldInstantiateClientUsingYaml() { File configYml = new File(TEST_CONFIG_YML_FILE); try (InputStream is = new FileInputStream(configYml)){ KubernetesClient client = DefaultKubernetesClient.fromConfig(is); @@ -409,7 +411,7 @@ public void shouldInstantiateClientUsingYaml() throws MalformedURLException { } @Test - public void shouldInstantiateClientUsingSerializeDeserialize() throws MalformedURLException { + public void shouldInstantiateClientUsingSerializeDeserialize() { DefaultKubernetesClient original = new DefaultKubernetesClient(); String json = Serialization.asJson(original.getConfiguration()); DefaultKubernetesClient copy = DefaultKubernetesClient.fromConfig(json); @@ -464,7 +466,7 @@ public void shouldPropagateImpersonateSettings() { assertEquals("a", currentConfig.getImpersonateUsername()); assertArrayEquals(new String[]{"b"}, currentConfig.getImpersonateGroups()); - assertEquals(Arrays.asList("d"), currentConfig.getImpersonateExtras().get("c")); + assertEquals(Collections.singletonList("d"), currentConfig.getImpersonateExtras().get("c")); } @Test @@ -482,7 +484,7 @@ public void honorClientAuthenticatorCommands() throws Exception { } @Test - public void shouldBeUsedTokenSuppliedByProvider() throws Exception { + public void shouldBeUsedTokenSuppliedByProvider() { Config config = new ConfigBuilder().withOauthToken("oauthToken") .withOauthTokenProvider(() -> "PROVIDER_TOKEN") @@ -508,6 +510,41 @@ public void testKubeConfigWithAuthConfigProvider() throws URISyntaxException { config.getOauthToken()); } + @Test + void testEmptyConfig() { + // Given + Config emptyConfig = null; + + // When + emptyConfig = Config.empty(); + + // Then + assertNotNull(emptyConfig); + assertEquals("https://kubernetes.default.svc", emptyConfig.getMasterUrl()); + assertTrue(emptyConfig.getContexts().isEmpty()); + assertNull(emptyConfig.getCurrentContext()); + assertEquals(64, emptyConfig.getMaxConcurrentRequests()); + assertEquals(5, emptyConfig.getMaxConcurrentRequestsPerHost()); + assertFalse(emptyConfig.isTrustCerts()); + assertFalse(emptyConfig.isDisableHostnameVerification()); + assertEquals("RSA", emptyConfig.getClientKeyAlgo()); + assertEquals("changeit", emptyConfig.getClientKeyPassphrase()); + assertEquals(1000, emptyConfig.getWatchReconnectInterval()); + assertEquals(-1, emptyConfig.getWatchReconnectLimit()); + assertEquals(10000, emptyConfig.getConnectionTimeout()); + assertEquals(10000, emptyConfig.getRequestTimeout()); + assertEquals(900000, emptyConfig.getRollingTimeout()); + assertEquals(600000, emptyConfig.getScaleTimeout()); + assertEquals(20000, emptyConfig.getLoggingInterval()); + assertEquals(5000, emptyConfig.getWebsocketTimeout()); + assertEquals(30000, emptyConfig.getWebsocketPingInterval()); + assertTrue(emptyConfig.getImpersonateExtras().isEmpty()); + assertEquals(0, emptyConfig.getImpersonateGroups().length); + assertFalse(emptyConfig.isHttp2Disable()); + assertEquals(1, emptyConfig.getTlsVersions().length); + assertTrue(emptyConfig.getErrorMessages().isEmpty()); + } + private void assertConfig(Config config) { assertNotNull(config); assertTrue(config.isTrustCerts());