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 4b41470
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 7 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,45 @@ 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.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:
*
* <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 +1200,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 @@ -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
Expand Down Expand Up @@ -365,7 +367,7 @@ public void testWithNamespacePathAndSytemPropertiesAndBuilder() {

@Test
public void testWithCustomHeader() {
Map<String,String> customHeaders = new HashMap();
Map<String,String> customHeaders = new HashMap<>();
customHeaders.put("user-id","test-user");
customHeaders.put("cluster-id","test-cluster");
Config config = new ConfigBuilder()
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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")
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 4b41470

Please sign in to comment.