Skip to content

Commit 41e3fbc

Browse files
committed
Make Config use ClientBuilder
1 parent fea316a commit 41e3fbc

File tree

1 file changed

+20
-104
lines changed
  • util/src/main/java/io/kubernetes/client/util

1 file changed

+20
-104
lines changed

util/src/main/java/io/kubernetes/client/util/Config.java

Lines changed: 20 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
package io.kubernetes.client.util;
1414

1515
import io.kubernetes.client.ApiClient;
16+
import io.kubernetes.client.util.credentials.AccessTokenCredentialProvider;
17+
import io.kubernetes.client.util.credentials.UsernamePasswordCredentialProvider;
1618
import java.io.ByteArrayInputStream;
1719
import okio.ByteString;
1820
import org.apache.log4j.Logger;
@@ -41,26 +43,11 @@ public class Config {
4143
public static final String ENV_SERVICE_PORT = "KUBERNETES_SERVICE_PORT";
4244
// The last resort host to try
4345
public static final String DEFAULT_FALLBACK_HOST = "http://localhost:8080";
44-
public static final Charset BASIC_AUTH_CHARSET = Charset.forName("ISO-8859-1");
4546

4647
private static final Logger log = Logger.getLogger(Config.class);
4748

4849
public static ApiClient fromCluster() throws IOException {
49-
String host = System.getenv(ENV_SERVICE_HOST);
50-
String port = System.getenv(ENV_SERVICE_PORT);
51-
52-
FileInputStream caCert = new FileInputStream(SERVICEACCOUNT_CA_PATH);
53-
BufferedReader tokenReader = new BufferedReader(new FileReader(SERVICEACCOUNT_TOKEN_PATH));
54-
StringBuilder builder = new StringBuilder();
55-
for (String line = tokenReader.readLine(); line != null; line = tokenReader.readLine()) {
56-
builder.append(line);
57-
}
58-
ApiClient result = new ApiClient();
59-
result.setBasePath("https://" + host + ":" + port);
60-
result.setSslCaCert(caCert);
61-
result.setApiKey("Bearer " + builder.toString());
62-
63-
return result;
50+
return ClientBuilder.fromCluster().build();
6451
}
6552

6653
public static ApiClient fromUrl(String url) {
@@ -78,95 +65,41 @@ public static ApiClient fromUserPassword(String url, String user, String passwor
7865
}
7966

8067
public static ApiClient fromUserPassword(String url, String user, String password, boolean validateSSL) {
81-
ApiClient client = fromUrl(url, validateSSL);
82-
final String usernameAndPassword = user + ":" + password;
83-
client.setApiKeyPrefix("Basic");
84-
client.setApiKey(ByteString.of(usernameAndPassword.getBytes(BASIC_AUTH_CHARSET)).base64());
85-
return client;
68+
return new ClientBuilder()
69+
.setBasePath(url)
70+
.setCredentialProvider(new UsernamePasswordCredentialProvider(user, password))
71+
.setVerifyingSsl(validateSSL)
72+
.build();
8673
}
8774

8875
public static ApiClient fromToken(String url, String token) {
8976
return fromToken(url, token, true);
9077
}
9178

9279
public static ApiClient fromToken(String url, String token, boolean validateSSL) {
93-
ApiClient client = fromUrl(url, validateSSL);
94-
client.setApiKeyPrefix("Bearer");
95-
client.setApiKey(token);
96-
return client;
80+
return new ClientBuilder()
81+
.setBasePath(url)
82+
.setCredentialProvider(new AccessTokenCredentialProvider(token))
83+
.setVerifyingSsl(validateSSL)
84+
.build();
9785
}
9886

9987
public static ApiClient fromConfig(String fileName) throws IOException {
10088
return fromConfig(new FileReader(fileName));
10189
}
10290

103-
public static ApiClient fromConfig(InputStream stream) {
91+
public static ApiClient fromConfig(InputStream stream) throws IOException {
10492
return fromConfig(new InputStreamReader(stream));
10593
}
10694

107-
public static ApiClient fromConfig(Reader input) {
95+
public static ApiClient fromConfig(Reader input) throws IOException {
10896
return fromConfig(KubeConfig.loadKubeConfig(input));
10997
}
11098

111-
public static ApiClient fromConfig(KubeConfig config) {
112-
ApiClient client = new ApiClient();
113-
String server = config.getServer();
114-
if (!server.startsWith("http://") && !server.startsWith("https://")) {
115-
if (server.indexOf(":443") != -1) {
116-
server = "https://" + server;
117-
} else {
118-
server = "http://" + server;
119-
}
120-
}
121-
client.setBasePath(server);
122-
123-
try {
124-
KeyManager[] mgrs = SSLUtils.keyManagers(
125-
KubeConfig.getDataOrFile(config.getClientCertificateData(), config.getClientCertificateFile()),
126-
KubeConfig.getDataOrFile(config.getClientKeyData(), config.getClientKeyFile()),
127-
"RSA", "",
128-
null, null);
129-
client.setKeyManagers(mgrs);
130-
} catch (Exception ex) {
131-
log.error("Failed to invoke build key managers", ex);
132-
}
133-
134-
if (config.verifySSL()) {
135-
// It's silly to have to do it in this order, but each SSL setup
136-
// consumes the CA cert, so if we do this before the client certs
137-
// are injected the cert input stream is exhausted and things get
138-
// grumpy
139-
String caCert = config.getCertificateAuthorityData();
140-
String caCertFile = config.getCertificateAuthorityFile();
141-
if (caCert != null || caCertFile != null) {
142-
try {
143-
client.setSslCaCert(new ByteArrayInputStream(KubeConfig.getDataOrFile(caCert, caCertFile)));
144-
} catch (IOException ex) {
145-
log.error("Failed to read CA Cert file", ex);
146-
}
147-
}
148-
} else {
149-
client.setVerifyingSsl(false);
150-
}
151-
152-
String token = config.getAccessToken();
153-
if (token != null) {
154-
// This is kind of a hack, except not, because I don't think we actually
155-
// want to use oauth here.
156-
client.setApiKey("Bearer " + token);
157-
}
158-
159-
String username = config.getUsername();
160-
if (username != null) {
161-
client.setUsername(username);
162-
}
163-
164-
String password = config.getPassword();
165-
if (password != null) {
166-
client.setPassword(password);
167-
}
168-
169-
return client;
99+
public static ApiClient fromConfig(KubeConfig config) throws IOException {
100+
return ClientBuilder
101+
.fromKubeConfig(config)
102+
.build();
170103
}
171104

172105
/**
@@ -181,23 +114,6 @@ public static ApiClient fromConfig(KubeConfig config) {
181114
* @return The best APIClient given the previously described rules
182115
*/
183116
public static ApiClient defaultClient() throws IOException {
184-
String kubeConfig = System.getenv(ENV_KUBECONFIG);
185-
if (kubeConfig != null) {
186-
return fromConfig(new FileReader(kubeConfig));
187-
}
188-
File config = new File(
189-
new File(System.getenv(KubeConfig.ENV_HOME),
190-
KubeConfig.KUBEDIR),
191-
KubeConfig.KUBECONFIG);
192-
if (config.exists()) {
193-
return fromConfig(new FileReader(config));
194-
}
195-
File clusterCA = new File(SERVICEACCOUNT_CA_PATH);
196-
if (clusterCA.exists()) {
197-
return fromCluster();
198-
}
199-
ApiClient client = new ApiClient();
200-
client.setBasePath(DEFAULT_FALLBACK_HOST);
201-
return client;
117+
return ClientBuilder.defaults().build();
202118
}
203119
}

0 commit comments

Comments
 (0)