Skip to content

Commit

Permalink
Fix fabric8io#2319: Create Config without using auto-configure functi…
Browse files Browse the repository at this point in the history
…onality or setting env variables
  • Loading branch information
rohanKanojia committed Jul 30, 2020
1 parent 25c09cd commit 8ce81b6
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
Expand Up @@ -211,16 +211,41 @@ public class Config {
*/
private Map<String,String> 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:
*
* <pre>{@code
* Config configFromBuilder = new ConfigBuilder(Config.empty())
* // ...
* .build();
* }</pre>
*
* @return a Config object without any automatic configuration
*/
public static Config empty() {
return new Config(false);
}

/**
* Does auto detection with some opinionated defaults.
*
Expand Down Expand Up @@ -1171,6 +1196,10 @@ public void setCustomHeaders(Map<String, String> customHeaders) {
this.customHeaders = customHeaders;
}

public Boolean getAutoConfigure() {
return autoConfigure;
}

/**
* Returns all the {@link NamedContext}s that exist in the kube config
*
Expand Down
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -141,7 +143,7 @@ public void testWithSystemProperties() {
}

@Test
public void testWithBuilder() {
void testWithBuilder() {
Config config = new ConfigBuilder()
.withMasterUrl("http://somehost:80")
.withApiVersion("v1")
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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");
Expand All @@ -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");

Expand All @@ -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");

Expand All @@ -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");
Expand All @@ -313,19 +315,19 @@ 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");

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");
Expand All @@ -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);
Expand All @@ -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");
Expand All @@ -364,8 +366,8 @@ public void testWithNamespacePathAndSytemPropertiesAndBuilder() {
}

@Test
public void testWithCustomHeader() {
Map<String,String> customHeaders = new HashMap();
void testWithCustomHeader() {
Map<String,String> customHeaders = new HashMap<>();
customHeaders.put("user-id","test-user");
customHeaders.put("cluster-id","test-cluster");
Config config = new ConfigBuilder()
Expand All @@ -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");
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -422,7 +424,7 @@ public void shouldInstantiateClientUsingSerializeDeserialize() throws MalformedU
}

@Test
public void shouldRespectMaxRequests() {
void shouldRespectMaxRequests() {
Config config = new ConfigBuilder()
.withMaxConcurrentRequests(120)
.build();
Expand All @@ -435,7 +437,7 @@ public void shouldRespectMaxRequests() {
}

@Test
public void shouldRespectMaxRequestsPerHost() {
void shouldRespectMaxRequestsPerHost() {
Config config = new ConfigBuilder()
.withMaxConcurrentRequestsPerHost(20)
.build();
Expand All @@ -448,7 +450,7 @@ public void shouldRespectMaxRequestsPerHost() {
}

@Test
public void shouldPropagateImpersonateSettings() {
void shouldPropagateImpersonateSettings() {

final Map<String, List<String>> extras = new HashMap<>();
extras.put("c", Collections.singletonList("d"));
Expand All @@ -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 {
Expand All @@ -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")
Expand All @@ -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");

Expand All @@ -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());
Expand Down

0 comments on commit 8ce81b6

Please sign in to comment.