From 8ce81b6b17da2e6404a22d5f523211a786200a9b Mon Sep 17 00:00:00 2001 From: Rohan Kumar Date: Wed, 29 Jul 2020 19:13:50 +0530 Subject: [PATCH] Fix #2319: Create Config without using auto-configure functionality or setting env variables --- CHANGELOG.md | 1 + .../io/fabric8/kubernetes/client/Config.java | 31 +++++- .../fabric8/kubernetes/client/ConfigTest.java | 97 +++++++++++++------ 3 files changed, 98 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 356b8cf447..e407c63d0b 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 a58d78f2fa..be92f82c74 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,41 @@ 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(!Utils.getSystemPropertyOrEnvVar(KUBERNETES_DISABLE_AUTO_CONFIG_SYSTEM_PROPERTY, false)); + } + + 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 +1196,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 234cb148c1..9b1943036d 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; @@ -103,7 +105,7 @@ public void tearDown() { } @Test - public void testWithSystemProperties() { + void testWithSystemProperties() { System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, "http://somehost:80"); System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "testns"); @@ -141,7 +143,7 @@ public void testWithSystemProperties() { } @Test - public void testWithBuilder() { + void testWithBuilder() { Config config = new ConfigBuilder() .withMasterUrl("http://somehost:80") .withApiVersion("v1") @@ -175,7 +177,7 @@ public void testWithBuilder() { @Test - public void testWithBuilderAndSystemProperties() { + void testWithBuilderAndSystemProperties() { System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, "http://tobeoverriden:80"); System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "tobeoverriden"); @@ -214,7 +216,7 @@ public void testWithBuilderAndSystemProperties() { } @Test - public void testMasterUrlWithServiceAccount() { + void testMasterUrlWithServiceAccount() { System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, "/dev/null"); System.setProperty(Config.KUBERNETES_SERVICE_HOST_PROPERTY, "10.0.0.1"); System.setProperty(Config.KUBERNETES_SERVICE_PORT_PROPERTY, "443"); @@ -224,7 +226,7 @@ public void testMasterUrlWithServiceAccount() { } @Test - public void testMasterUrlWithServiceAccountIPv6() { + void testMasterUrlWithServiceAccountIPv6() { System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, "/dev/null"); System.setProperty(Config.KUBERNETES_SERVICE_HOST_PROPERTY, "2001:db8:1f70::999:de8:7648:6e8"); System.setProperty(Config.KUBERNETES_SERVICE_PORT_PROPERTY, "443"); @@ -234,7 +236,7 @@ public void testMasterUrlWithServiceAccountIPv6() { } @Test - public void testWithKubeConfig() { + void testWithKubeConfig() { System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, TEST_KUBECONFIG_FILE); Config config = new Config(); assertNotNull(config); @@ -247,7 +249,7 @@ public void testWithKubeConfig() { } @Test - public void testWithKubeConfigAndOverrideContext() { + void testWithKubeConfigAndOverrideContext() { System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, TEST_KUBECONFIG_FILE); Config config = Config.autoConfigure("production/172-28-128-4:8443/root"); assertNotNull(config); @@ -260,7 +262,7 @@ public void testWithKubeConfigAndOverrideContext() { } @Test - public void testWithMultipleKubeConfigAndOverrideContext() { + void testWithMultipleKubeConfigAndOverrideContext() { System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, TEST_KUBECONFIG_FILE + File.pathSeparator + "some-other-file"); Config config = Config.autoConfigure("production/172-28-128-4:8443/root"); @@ -274,7 +276,7 @@ public void testWithMultipleKubeConfigAndOverrideContext() { } @Test - public void testWithKubeConfigAndSystemProperties() { + void testWithKubeConfigAndSystemProperties() { System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, TEST_KUBECONFIG_FILE); System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, "http://somehost:80"); @@ -286,7 +288,7 @@ public void testWithKubeConfigAndSystemProperties() { } @Test - public void testWithKubeConfigAndSytemPropertiesAndBuilder() { + void testWithKubeConfigAndSytemPropertiesAndBuilder() { System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, TEST_KUBECONFIG_FILE); System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, "http://somehost:80"); @@ -301,7 +303,7 @@ public void testWithKubeConfigAndSytemPropertiesAndBuilder() { } @Test - public void testWithNamespacePath() { + void testWithNamespacePath() { System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, "nokubeconfigfile"); System.setProperty(Config.KUBERNETES_NAMESPACE_FILE, TEST_NAMESPACE_FILE); System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, "http://somehost:80"); @@ -313,7 +315,7 @@ public void testWithNamespacePath() { } @Test - public void testWithNonExistingNamespacePath() { + void testWithNonExistingNamespacePath() { System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, "nokubeconfigfile"); System.setProperty(Config.KUBERNETES_NAMESPACE_FILE, "nonamespace"); System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, "http://somehost:80"); @@ -321,11 +323,11 @@ public void testWithNonExistingNamespacePath() { Config config = new Config(); assertNotNull(config); assertEquals("http://somehost:80/", config.getMasterUrl()); - assertEquals(null, config.getNamespace()); + Assertions.assertNull(config.getNamespace()); } @Test - public void testWithNamespacePathAndSystemProperties() { + void testWithNamespacePathAndSystemProperties() { System.setProperty(Config.KUBERNETES_NAMESPACE_FILE, TEST_NAMESPACE_FILE); System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, "http://somehost:80"); System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "testns"); @@ -337,7 +339,7 @@ public void testWithNamespacePathAndSystemProperties() { } @Test - public void testWithKubeConfigAndNoContext() { + void testWithKubeConfigAndNoContext() { System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, TEST_KUBECONFIG_NO_CURRENT_CONTEXT_FILE); Config config = new Config(); assertNotNull(config); @@ -349,7 +351,7 @@ public void testWithKubeConfigAndNoContext() { } @Test - public void testWithNamespacePathAndSytemPropertiesAndBuilder() { + void testWithNamespacePathAndSytemPropertiesAndBuilder() { System.setProperty(Config.KUBERNETES_NAMESPACE_FILE, TEST_NAMESPACE_FILE); System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, "http://somehost:80"); System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "tobeoverriden"); @@ -364,8 +366,8 @@ public void testWithNamespacePathAndSytemPropertiesAndBuilder() { } @Test - public void testWithCustomHeader() { - Map customHeaders = new HashMap(); + void testWithCustomHeader() { + Map customHeaders = new HashMap<>(); customHeaders.put("user-id","test-user"); customHeaders.put("cluster-id","test-cluster"); Config config = new ConfigBuilder() @@ -378,7 +380,7 @@ public void testWithCustomHeader() { } @Test - public void shouldSetImpersonateUsernameAndGroupFromSystemProperty() { + void shouldSetImpersonateUsernameAndGroupFromSystemProperty() { System.setProperty(Config.KUBERNETES_IMPERSONATE_USERNAME, "username"); System.setProperty(Config.KUBERNETES_IMPERSONATE_GROUP, "group"); @@ -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 { + 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 { + void shouldInstantiateClientUsingSerializeDeserialize() { DefaultKubernetesClient original = new DefaultKubernetesClient(); String json = Serialization.asJson(original.getConfiguration()); DefaultKubernetesClient copy = DefaultKubernetesClient.fromConfig(json); @@ -422,7 +424,7 @@ public void shouldInstantiateClientUsingSerializeDeserialize() throws MalformedU } @Test - public void shouldRespectMaxRequests() { + void shouldRespectMaxRequests() { Config config = new ConfigBuilder() .withMaxConcurrentRequests(120) .build(); @@ -435,7 +437,7 @@ public void shouldRespectMaxRequests() { } @Test - public void shouldRespectMaxRequestsPerHost() { + void shouldRespectMaxRequestsPerHost() { Config config = new ConfigBuilder() .withMaxConcurrentRequestsPerHost(20) .build(); @@ -448,7 +450,7 @@ public void shouldRespectMaxRequestsPerHost() { } @Test - public void shouldPropagateImpersonateSettings() { + void shouldPropagateImpersonateSettings() { final Map> extras = new HashMap<>(); extras.put("c", Collections.singletonList("d")); @@ -464,11 +466,11 @@ 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 - public void honorClientAuthenticatorCommands() throws Exception { + void honorClientAuthenticatorCommands() throws Exception { if (SystemUtils.IS_OS_WINDOWS) { System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, TEST_KUBECONFIG_EXEC_WIN_FILE); } else { @@ -482,7 +484,7 @@ public void honorClientAuthenticatorCommands() throws Exception { } @Test - public void shouldBeUsedTokenSuppliedByProvider() throws Exception { + void shouldBeUsedTokenSuppliedByProvider() { Config config = new ConfigBuilder().withOauthToken("oauthToken") .withOauthTokenProvider(() -> "PROVIDER_TOKEN") @@ -492,14 +494,14 @@ public void shouldBeUsedTokenSuppliedByProvider() throws Exception { } @Test - public void shouldHonorDefaultWebsocketPingInterval() { + void shouldHonorDefaultWebsocketPingInterval() { Config config = new ConfigBuilder().build(); assertEquals(30000L, config.getWebsocketPingInterval()); } @Test - public void testKubeConfigWithAuthConfigProvider() throws URISyntaxException { + void testKubeConfigWithAuthConfigProvider() throws URISyntaxException { System.setProperty("kubeconfig", new File(getClass().getResource("/test-kubeconfig").toURI()).getAbsolutePath()); Config config = Config.autoConfigure("production/172-28-128-4:8443/mmosley"); @@ -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());