Skip to content

Commit 6f9433a

Browse files
committed
Use File#exists and fix VerifyingSsl
1 parent 89b677b commit 6f9433a

File tree

1 file changed

+31
-39
lines changed

1 file changed

+31
-39
lines changed

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

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ public class ClientBuilder {
5252
* Creates an {@link ApiClient} by calling {@link #standard()} and {@link #build()}.
5353
*
5454
* @return An <tt>ApiClient</tt> configured using the precedence specified for {@link #standard()}.
55-
* @throws IOException
56-
* if the configuration file or a file specified in a configuration file cannot be read.
55+
* @throws IOException if the configuration file or a file specified in a configuration file cannot be read.
5756
*/
5857
public static ApiClient defaultClient() throws IOException {
5958
return ClientBuilder.standard().build();
@@ -63,23 +62,22 @@ public static ApiClient defaultClient() throws IOException {
6362
* Creates a builder which is pre-configured in the following way
6463
*
6564
* <ul>
66-
* <li>If $KUBECONFIG is defined, use that config file.</li>
67-
* <li>If $HOME/.kube/config can be found, use that.</li>
68-
* <li>If the in-cluster service account can be found, assume in cluster config.</li>
69-
* <li>Default to localhost:8080 as a last resort.</li>
65+
* <li>If $KUBECONFIG is defined, use that config file.</li>
66+
* <li>If $HOME/.kube/config can be found, use that.</li>
67+
* <li>If the in-cluster service account can be found, assume in cluster config.</li>
68+
* <li>Default to localhost:8080 as a last resort.</li>
7069
* </ul>
7170
*
7271
* @return <tt>ClientBuilder</tt> pre-configured using the above precedence
73-
* @throws IOException
74-
* if the configuration file or a file specified in a configuration file cannot be read.
72+
* @throws IOException if the configuration file or a file specified in a configuration file cannot be read.
7573
*/
7674
public static ClientBuilder standard() throws IOException {
7775
final FileReader kubeConfigReader = findConfigFromEnv();
78-
if(kubeConfigReader != null) {
76+
if (kubeConfigReader != null) {
7977
return kubeconfig(loadKubeConfig(kubeConfigReader));
8078
}
8179
final FileReader configReader = findConfigInHomeDir();
82-
if(configReader != null) {
80+
if (configReader != null) {
8381
return kubeconfig(loadKubeConfig(configReader));
8482
}
8583
final File clusterCa = new File(SERVICEACCOUNT_CA_PATH);
@@ -89,25 +87,26 @@ public static ClientBuilder standard() throws IOException {
8987
return new ClientBuilder();
9088
}
9189

92-
private static FileReader findConfigFromEnv() {
93-
try {
94-
String kubeConfig = System.getenv(ENV_KUBECONFIG);
95-
if(kubeConfig == null) {
96-
return null;
97-
}
90+
private static FileReader findConfigFromEnv() throws FileNotFoundException {
91+
String kubeConfigPath = System.getenv(ENV_KUBECONFIG);
92+
if (kubeConfigPath == null) {
93+
return null;
94+
}
95+
final File kubeConfig = new File(kubeConfigPath);
96+
if (kubeConfig.exists()) {
9897
return new FileReader(kubeConfig);
99-
} catch (FileNotFoundException e) {
100-
log.info("Could not find file specified in $KUBECONFIG");
98+
} else {
99+
log.debug("Could not find file specified in $KUBECONFIG");
101100
return null;
102101
}
103102
}
104103

105-
private static FileReader findConfigInHomeDir() {
106-
try {
107-
File config = new File(new File(System.getenv(ENV_HOME), KUBEDIR), KUBECONFIG);
104+
private static FileReader findConfigInHomeDir() throws FileNotFoundException {
105+
final File config = new File(new File(System.getenv(ENV_HOME), KUBEDIR), KUBECONFIG);
106+
if(config.exists()) {
108107
return new FileReader(config);
109-
} catch (FileNotFoundException e) {
110-
log.info("Could not find ~/.kube/config");
108+
} else {
109+
log.debug("Could not find ~/.kube/config");
111110
return null;
112111
}
113112
}
@@ -116,8 +115,7 @@ private static FileReader findConfigInHomeDir() {
116115
* Creates a builder which is pre-configured from the cluster configuration.
117116
*
118117
* @return <tt>ClientBuilder</tt> configured from the cluster configuration.
119-
* @throws IOException
120-
* if the Service Account Token Path or CA Path is not readable.
118+
* @throws IOException if the Service Account Token Path or CA Path is not readable.
121119
*/
122120
public static ClientBuilder cluster() throws IOException {
123121
final ClientBuilder builder = new ClientBuilder();
@@ -139,11 +137,9 @@ public static ClientBuilder cluster() throws IOException {
139137
*
140138
* To load a <tt>KubeConfig</tt>, see {@link KubeConfig#loadKubeConfig(Reader)}.
141139
*
142-
* @param config
143-
* The {@link KubeConfig} to configure the builder from.
140+
* @param config The {@link KubeConfig} to configure the builder from.
144141
* @return <tt>ClientBuilder</tt> configured from the provided <tt>KubeConfig</tt>
145-
* @throws IOException
146-
* if the files specified in the provided <tt>KubeConfig</tt> are not readable
142+
* @throws IOException if the files specified in the provided <tt>KubeConfig</tt> are not readable
147143
*/
148144
public static ClientBuilder kubeconfig(KubeConfig config) throws IOException {
149145
final ClientBuilder builder = new ClientBuilder();
@@ -157,17 +153,14 @@ public static ClientBuilder kubeconfig(KubeConfig config) throws IOException {
157153
}
158154
}
159155

160-
if(config.verifySSL()) {
161-
final byte[] caBytes = KubeConfig.getDataOrFile(config.getCertificateAuthorityData(),
162-
config.getCertificateAuthorityFile());
163-
if(caBytes != null) {
164-
builder.setCertificateAuthority(caBytes);
165-
}
166-
builder.setVerifyingSsl(true);
167-
} else {
168-
builder.setVerifyingSsl(false);
156+
final byte[] caBytes = KubeConfig.getDataOrFile(config.getCertificateAuthorityData(),
157+
config.getCertificateAuthorityFile());
158+
if (caBytes != null) {
159+
builder.setCertificateAuthority(caBytes);
169160
}
170161

162+
builder.setVerifyingSsl(config.verifySSL());
163+
171164
builder.setBasePath(server);
172165
builder.setAuthentication(new KubeconfigAuthentication(config));
173166
return builder;
@@ -193,7 +186,6 @@ public ClientBuilder setAuthentication(final Authentication authentication) {
193186

194187
public ClientBuilder setCertificateAuthority(final byte[] caCertBytes) {
195188
this.caCertBytes = caCertBytes;
196-
this.verifyingSsl = true;
197189
return this;
198190
}
199191

0 commit comments

Comments
 (0)