-
Notifications
You must be signed in to change notification settings - Fork 2k
adding custom ConfigBuilder #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,285 @@ | ||
/* | ||
Copyright 2017 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package io.kubernetes.client.util; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.Reader; | ||
import java.nio.charset.Charset; | ||
|
||
import javax.net.ssl.KeyManager; | ||
|
||
import okio.ByteString; | ||
import io.kubernetes.client.ApiClient; | ||
import io.kubernetes.client.util.Config; | ||
import io.kubernetes.client.util.KubeConfig; | ||
import io.kubernetes.client.util.SSLUtils; | ||
|
||
public class ConfigBuilder { | ||
|
||
private boolean trustCerts = false; | ||
private boolean clusterMode = false; | ||
private boolean defaultKubeConfigMode = false; | ||
private boolean defaultClientMode = false; | ||
private boolean verifyingSsl = false; | ||
private String basePath = null; | ||
private String certificateAuthorityFile = null; | ||
private String certificateAuthorityData = null; | ||
private String apiKey = null; | ||
private String userName = null; | ||
private String password = null; | ||
private KeyManager[] keyMgrs = null; | ||
private String accessToken = null; | ||
private String apiKeyPrefix = null; | ||
private InputStream sslCaCert = null; | ||
private KubeConfig kubeConfig = null; | ||
|
||
public String getUserName() { | ||
return userName; | ||
} | ||
|
||
public ConfigBuilder setUserName(String userName) { | ||
this.userName = userName; | ||
return this; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public ConfigBuilder setPassword(String password) { | ||
this.password = password; | ||
return this; | ||
} | ||
|
||
public String getApiKey() { | ||
return apiKey; | ||
} | ||
|
||
public ConfigBuilder setApiKey(String apiKey) { | ||
this.apiKey = apiKey; | ||
return this; | ||
} | ||
|
||
public boolean isTrustCerts() { | ||
return trustCerts; | ||
} | ||
|
||
public ConfigBuilder setTrustCerts(boolean trustCerts) { | ||
this.trustCerts = trustCerts; | ||
return this; | ||
} | ||
|
||
public String getbasePath() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return basePath; | ||
} | ||
|
||
public ConfigBuilder setbasePath(String basePath) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
this.basePath = basePath; | ||
return this; | ||
} | ||
|
||
public String getCertificateAuthorityFile() { | ||
return certificateAuthorityFile; | ||
} | ||
|
||
public ConfigBuilder setCertificateAuthorityFile(String certificateAuthorityFile) { | ||
this.certificateAuthorityFile = certificateAuthorityFile; | ||
return this; | ||
|
||
} | ||
|
||
public String getCertificateAuthorityData() { | ||
return certificateAuthorityData; | ||
} | ||
|
||
public ConfigBuilder setCertificateAuthorityData(String certificateAuthorityData) { | ||
this.certificateAuthorityData = certificateAuthorityData; | ||
return this; | ||
} | ||
|
||
public ConfigBuilder setClusterMode(boolean clusterMode) { | ||
this.clusterMode = clusterMode; | ||
return this; | ||
} | ||
|
||
public ConfigBuilder setKubeConfig(KubeConfig config) { | ||
this.kubeConfig = config; | ||
return this; | ||
} | ||
|
||
public ConfigBuilder setDefaultKubeConfigMode(boolean defaultKubeConfigMode) { | ||
this.defaultKubeConfigMode = defaultKubeConfigMode; | ||
return this; | ||
} | ||
|
||
public ConfigBuilder setKubeConfig(String fileName) throws FileNotFoundException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a little weird to use |
||
this.kubeConfig = KubeConfig.loadKubeConfig(new FileReader(fileName)); | ||
return this; | ||
} | ||
|
||
public ConfigBuilder setKubeConfig(Reader input) { | ||
this.kubeConfig = KubeConfig.loadKubeConfig(input); | ||
return this; | ||
} | ||
|
||
public ConfigBuilder setKubeConfig(InputStream stream) { | ||
this.kubeConfig = KubeConfig.loadKubeConfig(new InputStreamReader(stream)); | ||
return this; | ||
} | ||
|
||
public KeyManager[] getKeyMgrs() { | ||
return keyMgrs; | ||
} | ||
|
||
public ConfigBuilder setKeyMgrs(KeyManager[] keyMgrs) { | ||
this.keyMgrs = keyMgrs; | ||
return this; | ||
} | ||
|
||
public boolean isVerifyingSsl() { | ||
return verifyingSsl; | ||
} | ||
|
||
public ConfigBuilder setVerifyingSsl(boolean verifyingSsl) { | ||
this.verifyingSsl = verifyingSsl; | ||
return this; | ||
} | ||
|
||
public boolean isDefaultClientMode() { | ||
return defaultClientMode; | ||
} | ||
|
||
public ConfigBuilder setDefaultClientMode(boolean defaultClientMode) { | ||
this.defaultClientMode = defaultClientMode; | ||
return this; | ||
} | ||
|
||
public String getApiKeyPrefix() { | ||
return apiKeyPrefix; | ||
} | ||
|
||
public ConfigBuilder setApiKeyPrefix(String apiKeyPrefix) { | ||
this.apiKeyPrefix = apiKeyPrefix; | ||
return this; | ||
} | ||
|
||
public ApiClient build() { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delete newline |
||
ApiClient client = new ApiClient(); | ||
|
||
if( kubeConfig !=null) { | ||
client = Config.fromConfig(kubeConfig); | ||
return client; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this logic is a little odd. I would expect that I could do:
And it would mostly load the KubeConfig, but then override the basepath with the custom value I set. Instead, it just returns the existing KubeConfig and ignores any other I think that this is going to confuse people, and doesn't match the desired use case. |
||
} | ||
|
||
if(defaultKubeConfigMode == true) { | ||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better: move There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @brendandburns public ConfigBuilder setDefaultMode() {
this.defaultMode = true;
return this;
}
build() throws Exception{
....
if(defaultMode == true) {
client = Config.fromConfig(KubeConfig.loadDefaultKubeConfig());
}
...
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kondapally1989 no, what I was thinking was this: public ConfigBuilder setDefaultMode() throws IOException {
this.client = config.fromConfig(KubeConfig.loadDefaultKubeConfig());
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @brendandburns and setDefaultClientMode points to Config.defaultClient(); isn't the naming convention confusing. instead of setDefaultMode() cant we have setDefaultKubeConfigMode() |
||
client = Config.fromConfig(KubeConfig.loadDefaultKubeConfig()); | ||
} catch (FileNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
return client; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as above. |
||
} | ||
|
||
if(clusterMode == true) { | ||
try { | ||
client = Config.fromCluster(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return client; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as above. |
||
} | ||
|
||
if(defaultClientMode ==true ) { | ||
try { | ||
client = Config.defaultClient(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here too? |
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return client; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. delete newline (here and everywhere it's extraneous) |
||
} | ||
|
||
if (basePath != null ) { | ||
if(basePath.endsWith("/")) { | ||
basePath = basePath.substring(0, basePath.length() - 1); | ||
} | ||
client.setBasePath(basePath) | ||
.setVerifyingSsl(verifyingSsl); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are you setting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. by default(if user doesn't set anything ), want to set verifyingSsl to false. |
||
} | ||
|
||
else { | ||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is weird. You are |
||
throw new Exception("set kubernetes URL. example: http://localhost"); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
if(keyMgrs != null) { | ||
client.setKeyManagers(keyMgrs); | ||
} | ||
|
||
if(userName != null){ | ||
client.setUsername(userName); | ||
} | ||
|
||
if(password != null){ | ||
client.setPassword(password); | ||
} | ||
|
||
if(( userName != null )&&(password != null)) { | ||
final String usernameAndPassword = userName + ":" + password; | ||
client.setApiKeyPrefix("Basic"); | ||
client.setApiKey(ByteString.of(usernameAndPassword.getBytes(Charset.forName("ISO-8859-1"))).base64()); | ||
} | ||
|
||
if(accessToken != null) { | ||
if (apiKeyPrefix == null){ | ||
client.setApiKeyPrefix("Bearer"); | ||
} | ||
client.setAccessToken(accessToken); | ||
} | ||
|
||
if(apiKeyPrefix != null) { | ||
client.setApiKeyPrefix(apiKeyPrefix); | ||
} | ||
|
||
if(apiKey != null) { | ||
if (apiKeyPrefix == null){ | ||
client.setApiKeyPrefix("Bearer"); | ||
} | ||
client.setApiKey(apiKey); | ||
} | ||
|
||
if(sslCaCert != null) { | ||
client.setSslCaCert(sslCaCert); | ||
} | ||
|
||
if(verifyingSsl){ | ||
if((certificateAuthorityData != null) || (certificateAuthorityFile != null)){ | ||
try { | ||
client.setSslCaCert(SSLUtils.getInputStreamFromDataOrFile(certificateAuthorityData, certificateAuthorityFile)); | ||
} catch (FileNotFoundException e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should load the cert when someone calls And not throw/catch here. |
||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
return client; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem to do anything. Delete?