Skip to content

Commit

Permalink
review changes and ConfigBuilder to ClientBuilder name change
Browse files Browse the repository at this point in the history
  • Loading branch information
karthikkondapally committed Feb 2, 2018
1 parent bf764a4 commit 2428033
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -49,14 +46,15 @@ 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);

public String getUserName() {
return userName;
}

public ConfigBuilder setUserName(String userName) {
public ClientBuilder setUserName(String userName) {
this.userName = userName;
return this;
}
Expand All @@ -65,7 +63,7 @@ public String getPassword() {
return password;
}

public ConfigBuilder setPassword(String password) {
public ClientBuilder setPassword(String password) {
this.password = password;
return this;
}
Expand All @@ -74,7 +72,7 @@ public String getApiKey() {
return apiKey;
}

public ConfigBuilder setApiKey(String apiKey) {
public ClientBuilder setApiKey(String apiKey) {
this.apiKey = apiKey;
return this;
}
Expand All @@ -83,7 +81,7 @@ public String getBasePath() {
return basePath;
}

public ConfigBuilder setBasePath(String basePath) {
public ClientBuilder setBasePath(String basePath) {
this.basePath = basePath;
return this;
}
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -142,7 +143,7 @@ public KeyManager[] getKeyMgrs() {
return keyMgrs;
}

public ConfigBuilder setKeyMgrs(KeyManager[] keyMgrs) {
public ClientBuilder setKeyMgrs(KeyManager[] keyMgrs) {
this.keyMgrs = keyMgrs;
return this;
}
Expand All @@ -151,68 +152,41 @@ 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;
}

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");
}
}

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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());
Expand All @@ -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)
Expand All @@ -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();
Expand Down

0 comments on commit 2428033

Please sign in to comment.