-
Notifications
You must be signed in to change notification settings - Fork 2k
Easy in-memory instantiation of config #168
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
Comments
This was referenced Jan 17, 2018
Will the below ConfigBuilder fit the purpose 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 = "http://localhost";
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 ConfigBuilder() {
}
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() {
return basePath;
}
public ConfigBuilder setbasePath(String basePath) {
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 {
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() {
ApiClient client = new ApiClient();
if( kubeConfig !=null) {
client = Config.fromConfig(kubeConfig);
return client;
}
if(defaultKubeConfigMode == true) {
try {
client = Config.fromConfig(KubeConfig.loadDefaultKubeConfig());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return client;
}
if(clusterMode == true) {
try {
client = Config.fromCluster();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return client;
}
if (basePath != null ) {
client.setBasePath(this.basePath).setVerifyingSsl(verifyingSsl);
}
else {
try {
throw new Exception("set kubernetes URL");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(this.keyMgrs != null) {
client.setKeyManagers(getKeyMgrs() );
}
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) {
client.setAccessToken(accessToken);
}
if(getApiKeyPrefix() !=null) {
client.setApiKeyPrefix(getApiKeyPrefix());
}
if(apiKey !=null) {
if (getApiKeyPrefix() ==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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return client;
}
} and its usage String basePath = "http://localhost";
String apiKey = "XXXXXXX";
String userName = "username";
String password = "password";
String apiKeyPrefix = "Bearer";
String certificateAuthorityData = null;
String certificateAuthorityFile = "~/.minikube/ca.crt";
String clientCertData = null;
String clientCertFile = "~/.minikube/apiserver.crt";
String clientKeyData = null;
String clientKeyFile = "~/.minikube/apiserver.crt";
String algo = "RSA";
String passphrase = null;
String keyStoreFile = null ;
String keyStorePassphrase = null;
KeyManager[] keyMgrs =null;
ApiClient defaultClusterClient = (new ConfigBuilder())
.setClusterMode(true)
.build();
ApiClient defaulClient = (new ConfigBuilder())
.setDefaultClientMode(true)
.build();
ApiClient defaultKubeConfigClient = (new ConfigBuilder())
.setDefaultKubeConfigMode(true)
.build();
ApiClient client1 = (new ConfigBuilder())
.setbasePath(basePath)
.setApiKeyPrefix(apiKeyPrefix)
.setApiKey(apiKey)
.build();
ApiClient client2 = (new ConfigBuilder())
.setbasePath(basePath)
.setUserName(userName)
.setPassword(password)
.build();
try{
keyMgrs = SSLUtils.keyManagers(clientCertData, clientCertFile, clientKeyData, clientKeyFile, algo, passphrase, keyStoreFile, keyStorePassphrase);
}
catch(Exception e){
}
//by default verify ssl is false
ApiClient client3 = (new ConfigBuilder())
.setbasePath(basePath)
.setVerifyingSsl(true)
.setKeyMgrs(keyMgrs)
.setCertificateAuthorityData(certificateAuthorityData)
.setCertificateAuthorityFile(certificateAuthorityFile)
.build(); |
@kondapally1989 that looks great! Want to send a PR? Thanks |
@brendandburns this can be closed out, I think. |
Closing. thanks. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Referenced in #119 and #167
Quote from #119:
Something that would allow me to do something like this is what I'm after:
https://github.com/spotify/styx/blob/c0654ac66a74692ffbc6974b824f3d763a6e6bc4/styx-scheduler-service/src/main/java/com/spotify/styx/StyxScheduler.java#L693-L699
The text was updated successfully, but these errors were encountered: