From 24280332e4d7b4ae28a6d1284af637139f0173c7 Mon Sep 17 00:00:00 2001 From: kondapally karthik Date: Fri, 2 Feb 2018 23:41:13 +0530 Subject: [PATCH] review changes and ConfigBuilder to ClientBuilder name change --- ...{ConfigBuilder.java => ClientBuilder.java} | 98 +++++++------------ ...uilderTest.java => ClientBuilderTest.java} | 57 ++++++----- 2 files changed, 66 insertions(+), 89 deletions(-) rename util/src/main/java/io/kubernetes/client/util/{ConfigBuilder.java => ClientBuilder.java} (64%) rename util/src/test/java/io/kubernetes/client/util/{ConfigBuilderTest.java => ClientBuilderTest.java} (88%) diff --git a/util/src/main/java/io/kubernetes/client/util/ConfigBuilder.java b/util/src/main/java/io/kubernetes/client/util/ClientBuilder.java similarity index 64% rename from util/src/main/java/io/kubernetes/client/util/ConfigBuilder.java rename to util/src/main/java/io/kubernetes/client/util/ClientBuilder.java index f7b91855f6..6d6063e019 100644 --- a/util/src/main/java/io/kubernetes/client/util/ConfigBuilder.java +++ b/util/src/main/java/io/kubernetes/client/util/ClientBuilder.java @@ -33,11 +33,8 @@ import io.kubernetes.client.util.Config; import io.kubernetes.client.util.KubeConfig; -public class ConfigBuilder { +public class ClientBuilder { - private boolean clusterMode = false; - private boolean defaultKubeConfigMode = false; - private boolean defaultClientMode = false; private boolean verifyingSsl = false; private String basePath = null; private File certificateAuthorityFile = null; @@ -49,6 +46,7 @@ public class ConfigBuilder { private String accessToken = null; private String apiKeyPrefix = null; private KubeConfig kubeConfig = null; + private ApiClient client = null; private static final Logger log = Logger.getLogger(Config.class); @@ -56,7 +54,7 @@ public String getUserName() { return userName; } - public ConfigBuilder setUserName(String userName) { + public ClientBuilder setUserName(String userName) { this.userName = userName; return this; } @@ -65,7 +63,7 @@ public String getPassword() { return password; } - public ConfigBuilder setPassword(String password) { + public ClientBuilder setPassword(String password) { this.password = password; return this; } @@ -74,7 +72,7 @@ public String getApiKey() { return apiKey; } - public ConfigBuilder setApiKey(String apiKey) { + public ClientBuilder setApiKey(String apiKey) { this.apiKey = apiKey; return this; } @@ -83,7 +81,7 @@ public String getBasePath() { return basePath; } - public ConfigBuilder setBasePath(String basePath) { + public ClientBuilder setBasePath(String basePath) { this.basePath = basePath; return this; } @@ -92,7 +90,7 @@ public File getCertificateAuthorityFile() { return certificateAuthorityFile; } - public ConfigBuilder setCertificateAuthority(File certificateAuthorityFile) { + public ClientBuilder setCertificateAuthority(File certificateAuthorityFile) { this.certificateAuthorityFile = certificateAuthorityFile; this.verifyingSsl = true; return this; @@ -102,38 +100,41 @@ public String getCertificateAuthorityData() { return certificateAuthorityData; } - public ConfigBuilder setCertificateAuthority(String certificateAuthorityData) { + public ClientBuilder setCertificateAuthority(String certificateAuthorityData) { this.certificateAuthorityData = certificateAuthorityData; this.verifyingSsl = true; return this; } - public ConfigBuilder setClusterMode() { - this.clusterMode = true; + public ClientBuilder setClusterMode() throws IOException { + this.client = Config.fromCluster(); return this; } - public ConfigBuilder setKubeConfig(KubeConfig config) { + public ClientBuilder setKubeConfig(KubeConfig config) { this.kubeConfig = config; + if( this.kubeConfig !=null) { + this.client = Config.fromConfig(this.kubeConfig); + } return this; } - public ConfigBuilder setDefaultKubeConfigMode() { - this.defaultKubeConfigMode = true; + public ClientBuilder setDefaultKubeConfigMode() throws FileNotFoundException { + this.client = Config.fromConfig(KubeConfig.loadDefaultKubeConfig()); return this; } - public ConfigBuilder setKubeConfig(String fileName) throws FileNotFoundException { - this.kubeConfig = KubeConfig.loadKubeConfig(new FileReader(fileName)); + public ClientBuilder setKubeConfig(File kubeFile) throws FileNotFoundException { + this.kubeConfig = KubeConfig.loadKubeConfig(new FileReader(kubeFile)); return this; } - public ConfigBuilder setKubeConfig(Reader input) { + public ClientBuilder setKubeConfig(Reader input) { this.kubeConfig = KubeConfig.loadKubeConfig(input); return this; } - public ConfigBuilder setKubeConfig(InputStream stream) { + public ClientBuilder setKubeConfig(InputStream stream) { this.kubeConfig = KubeConfig.loadKubeConfig(new InputStreamReader(stream)); return this; } @@ -142,7 +143,7 @@ public KeyManager[] getKeyMgrs() { return keyMgrs; } - public ConfigBuilder setKeyMgrs(KeyManager[] keyMgrs) { + public ClientBuilder setKeyMgrs(KeyManager[] keyMgrs) { this.keyMgrs = keyMgrs; return this; } @@ -151,17 +152,14 @@ public boolean isVerifyingSsl() { return verifyingSsl; } - public ConfigBuilder setVerifyingSsl(boolean verifyingSsl) { + public ClientBuilder setVerifyingSsl(boolean verifyingSsl) { this.verifyingSsl = verifyingSsl; return this; } - public boolean isDefaultClientMode() { - return defaultClientMode; - } - public ConfigBuilder setDefaultClientMode() { - this.defaultClientMode = true; + public ClientBuilder setDefaultClientMode() throws IOException { + client = Config.defaultClient(); return this; } @@ -169,50 +167,26 @@ public String getApiKeyPrefix() { return apiKeyPrefix; } - public ConfigBuilder setApiKeyPrefix(String apiKeyPrefix) { + public ClientBuilder setApiKeyPrefix(String apiKeyPrefix) { this.apiKeyPrefix = apiKeyPrefix; return this; } - public ApiClient build() { - ApiClient client = new ApiClient(); - - if( kubeConfig !=null) { - client = Config.fromConfig(kubeConfig); - } - - if(defaultKubeConfigMode == true) { - try { - client = Config.fromConfig(KubeConfig.loadDefaultKubeConfig()); - } catch (FileNotFoundException e) { - log.error("Unable to find the file", e); - } - } - - if(clusterMode == true) { - try { - client = Config.fromCluster(); - } catch (IOException e) { - log.error("Exception ->", e); - } + public ApiClient build() throws FileNotFoundException { + if(client == null) { + client = new ApiClient(); } - if(defaultClientMode ==true ) { - try { - client = Config.defaultClient(); - } catch (IOException e) { - log.error("Exception -> ", e); - } - } + String localBasePath = client.getBasePath(); - if (basePath != null ) { + if (basePath != null) { if(basePath.endsWith("/")) { basePath = basePath.substring(0, basePath.length() - 1); } client.setBasePath(basePath); - } else { - if((clusterMode == false) && (defaultClientMode == false) && (defaultKubeConfigMode == false)) { - throw new IllegalArgumentException("please set kubernetes URL ex:http://localhost"); + }else { + if (localBasePath.length() == 0) { + client.setBasePath("http://localhost:8080"); } } @@ -255,11 +229,7 @@ public ApiClient build() { client.setVerifyingSsl(verifyingSsl); if(certificateAuthorityFile != null) { - try { - client.setSslCaCert(new FileInputStream(certificateAuthorityFile)); - } catch (FileNotFoundException e) { - log.error("Unable to find the file", e); - } + client.setSslCaCert(new FileInputStream(certificateAuthorityFile)); } if(certificateAuthorityData != null) { diff --git a/util/src/test/java/io/kubernetes/client/util/ConfigBuilderTest.java b/util/src/test/java/io/kubernetes/client/util/ClientBuilderTest.java similarity index 88% rename from util/src/test/java/io/kubernetes/client/util/ConfigBuilderTest.java rename to util/src/test/java/io/kubernetes/client/util/ClientBuilderTest.java index 5295abf80c..0027cbc65f 100644 --- a/util/src/test/java/io/kubernetes/client/util/ConfigBuilderTest.java +++ b/util/src/test/java/io/kubernetes/client/util/ClientBuilderTest.java @@ -35,7 +35,7 @@ /** * Tests for the ConfigBuilder helper class */ -public class ConfigBuilderTest { +public class ClientBuilderTest { String basePath = "http://localhost"; String apiKey = "ABCD"; String userName = "userName"; @@ -64,10 +64,16 @@ public class ConfigBuilderTest { @Test public void testDefaultClientNothingPresent() { environmentVariables.set("HOME", "/non-existent"); - ApiClient client = (new ConfigBuilder()) - .setDefaultClientMode() - .build(); - assertEquals("http://localhost:8080", client.getBasePath()); + ApiClient client; + try { + client = (new ClientBuilder()) + .setDefaultClientMode() + .build(); + assertEquals("http://localhost:8080", client.getBasePath()); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } public static String HOME_CONFIG = @@ -121,7 +127,7 @@ public void setUp() throws IOException { public void testDefaultClientHomeDir() { try { environmentVariables.set("HOME", dir.getCanonicalPath()); - ApiClient client = new ConfigBuilder() + ApiClient client = new ClientBuilder() .setDefaultClientMode() .build(); assertEquals("http://home.dir.com", client.getBasePath()); @@ -135,7 +141,7 @@ public void testDefaultClientHomeDir() { public void testDefaultClientKubeConfig() { try { environmentVariables.set("KUBECONFIG", configFile.getCanonicalPath()); - ApiClient client = new ConfigBuilder() + ApiClient client = new ClientBuilder() .setDefaultClientMode() .build(); assertEquals("http://kubeconfig.dir.com", client.getBasePath()); @@ -150,7 +156,7 @@ public void testDefaultClientPrecedence() { try { environmentVariables.set("HOME", dir.getCanonicalPath()); environmentVariables.set("KUBECONFIG", configFile.getCanonicalPath()); - ApiClient client = new ConfigBuilder() + ApiClient client = new ClientBuilder() .setDefaultClientMode() .build(); // $KUBECONFIG should take precedence over $HOME/.kube/config @@ -162,9 +168,9 @@ public void testDefaultClientPrecedence() { } @Test - public void testUserNamePasswordConfigBuilder() { + public void testUserNamePasswordClientBuilder() { try { - ApiClient client = (new ConfigBuilder()) + ApiClient client = (new ClientBuilder()) .setBasePath(basePath) .setUserName(userName) .setPassword(password) @@ -185,11 +191,16 @@ public void testUserNamePasswordConfigBuilder() { @Test public void testApiKeyConfigbuilder() { ApiClient client = null; - client = (new ConfigBuilder()) - .setBasePath(basePath) - .setApiKeyPrefix(apiKeyPrefix) - .setApiKey(apiKey) - .build(); + try { + client = (new ClientBuilder()) + .setBasePath(basePath) + .setApiKeyPrefix(apiKeyPrefix) + .setApiKey(apiKey) + .build(); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } assertEquals(basePath, client.getBasePath()); assertEquals(false, client.isVerifyingSsl()); assertEquals(apiKeyPrefix, ((io.kubernetes.client.auth.ApiKeyAuth)client.getAuthentications().get("BearerToken")).getApiKeyPrefix()); @@ -203,7 +214,7 @@ public void testKeyMgrANDCertConfigBUilder() { try{ keyMgrs = SSLUtils.keyManagers(clientCertData, clientCertFile, clientKeyData, clientKeyFile, algo, passphrase, keyStoreFile, keyStorePassphrase); //by default verify ssl is false - ApiClient client = (new ConfigBuilder()) + ApiClient client = (new ClientBuilder()) .setBasePath(basePath) .setKeyMgrs(keyMgrs) .setCertificateAuthority(certificateAuthorityData) @@ -220,24 +231,20 @@ public void testKeyMgrANDCertConfigBUilder() { } @Test - public void testBasePathIllegalArgumentException() throws IOException { + public void testBasePath() throws IOException { ApiClient client = null ; - try { - client = (new ConfigBuilder()) + client = (new ClientBuilder()) .setUserName("user") .build(); - } - catch(IllegalArgumentException ie) { - assertEquals(IllegalArgumentException.class, ie.getClass()); - } + environmentVariables.set("HOME", "/non-existent"); - client = (new ConfigBuilder()) + client = (new ClientBuilder()) .setDefaultClientMode() .setUserName("user") .build(); assertEquals("http://localhost:8080", client.getBasePath()); environmentVariables.set("KUBECONFIG", configFile.getCanonicalPath()); - client = new ConfigBuilder() + client = new ClientBuilder() .setDefaultClientMode() .setBasePath("http://testkubeconfig.dir.com") .build();