12
12
*/
13
13
package io .kubernetes .client .util ;
14
14
15
- import io .kubernetes .client .util .credentials .AccessTokenCredentialProvider ;
16
- import io .kubernetes .client .util .credentials .CredentialProvider ;
17
- import io .kubernetes .client .util .credentials .KubeconfigCredentialProvider ;
15
+ import io .kubernetes .client .util .credentials .AccessTokenAuthentication ;
16
+ import io .kubernetes .client .util .credentials .Authentication ;
17
+ import io .kubernetes .client .util .credentials .KubeconfigAuthentication ;
18
18
import java .io .ByteArrayInputStream ;
19
19
import java .io .File ;
20
- import java .io .FileInputStream ;
21
20
import java .io .FileNotFoundException ;
22
21
import java .io .FileReader ;
23
22
import java .io .IOException ;
24
23
25
- import java .io .InputStream ;
24
+ import java .io .Reader ;
26
25
import java .nio .charset .Charset ;
27
26
import java .nio .file .Files ;
28
27
import java .nio .file .Paths ;
29
- import org .apache .commons .codec .binary .Base64 ;
30
28
import org .apache .log4j .Logger ;
31
29
32
30
import io .kubernetes .client .ApiClient ;
38
36
import static io .kubernetes .client .util .Config .SERVICEACCOUNT_TOKEN_PATH ;
39
37
import static io .kubernetes .client .util .KubeConfig .*;
40
38
39
+ /**
40
+ * A Builder which allows the construction of {@link ApiClient}s in a fluent fashion.
41
+ */
41
42
public class ClientBuilder {
42
43
43
44
private static final Logger log = Logger .getLogger (ClientBuilder .class );
44
45
45
46
private String basePath = Config .DEFAULT_FALLBACK_HOST ;
46
47
private byte [] caCertBytes = null ;
47
48
private boolean verifyingSsl = true ;
48
- private CredentialProvider credentialProvider ;
49
+ private Authentication authentication ;
50
+
51
+ /**
52
+ * Creates an {@link ApiClient} by calling {@link #standard()} and {@link #build()}.
53
+ *
54
+ * @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.
57
+ */
58
+ public static ApiClient defaultClient () throws IOException {
59
+ return ClientBuilder .standard ().build ();
60
+ }
49
61
50
- public static ClientBuilder defaults () throws IOException {
62
+ /**
63
+ * Creates a builder which is pre-configured in the following way
64
+ *
65
+ * <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>
70
+ * </ul>
71
+ *
72
+ * @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.
75
+ */
76
+ public static ClientBuilder standard () throws IOException {
51
77
final FileReader kubeConfigReader = findConfigFromEnv ();
52
78
if (kubeConfigReader != null ) {
53
- return fromKubeConfig (loadKubeConfig (kubeConfigReader ));
79
+ return kubeconfig (loadKubeConfig (kubeConfigReader ));
54
80
}
55
81
final FileReader configReader = findConfigInHomeDir ();
56
82
if (configReader != null ) {
57
- return fromKubeConfig (loadKubeConfig (configReader ));
83
+ return kubeconfig (loadKubeConfig (configReader ));
58
84
}
59
85
final File clusterCa = new File (SERVICEACCOUNT_CA_PATH );
60
86
if (clusterCa .exists ()) {
61
- return fromCluster ();
87
+ return cluster ();
62
88
}
63
89
return new ClientBuilder ();
64
90
}
65
91
66
- private static FileReader findConfigFromEnv () throws FileNotFoundException {
92
+ private static FileReader findConfigFromEnv () {
67
93
try {
68
94
String kubeConfig = System .getenv (ENV_KUBECONFIG );
69
95
if (kubeConfig == null ) {
70
96
return null ;
71
97
}
72
98
return new FileReader (kubeConfig );
73
99
} catch (FileNotFoundException e ) {
74
- log .info ("Could not find KUBECONFIG in environment " );
100
+ log .info ("Could not find file specified in $KUBECONFIG " );
75
101
return null ;
76
102
}
77
103
}
78
104
79
- private static FileReader findConfigInHomeDir () throws FileNotFoundException {
105
+ private static FileReader findConfigInHomeDir () {
80
106
try {
81
107
File config = new File (new File (System .getenv (ENV_HOME ), KUBEDIR ), KUBECONFIG );
82
108
return new FileReader (config );
@@ -86,7 +112,14 @@ private static FileReader findConfigInHomeDir() throws FileNotFoundException {
86
112
}
87
113
}
88
114
89
- public static ClientBuilder fromCluster () throws IOException {
115
+ /**
116
+ * Creates a builder which is pre-configured from the cluster configuration.
117
+ *
118
+ * @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.
121
+ */
122
+ public static ClientBuilder cluster () throws IOException {
90
123
final ClientBuilder builder = new ClientBuilder ();
91
124
92
125
final String host = System .getenv (ENV_SERVICE_HOST );
@@ -96,12 +129,23 @@ public static ClientBuilder fromCluster() throws IOException {
96
129
final String token = new String (Files .readAllBytes (Paths .get (SERVICEACCOUNT_TOKEN_PATH )),
97
130
Charset .defaultCharset ());
98
131
builder .setCertificateAuthority (Files .readAllBytes (Paths .get (SERVICEACCOUNT_CA_PATH )));
99
- builder .setCredentialProvider (new AccessTokenCredentialProvider (token ));
132
+ builder .setAuthentication (new AccessTokenAuthentication (token ));
100
133
101
134
return builder ;
102
135
}
103
136
104
- public static ClientBuilder fromKubeConfig (KubeConfig config ) throws IOException {
137
+ /**
138
+ * Creates a builder which is pre-configured from a {@link KubeConfig}.
139
+ *
140
+ * To load a <tt>KubeConfig</tt>, see {@link KubeConfig#loadKubeConfig(Reader)}.
141
+ *
142
+ * @param config
143
+ * The {@link KubeConfig} to configure the builder from.
144
+ * @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
147
+ */
148
+ public static ClientBuilder kubeconfig (KubeConfig config ) throws IOException {
105
149
final ClientBuilder builder = new ClientBuilder ();
106
150
107
151
String server = config .getServer ();
@@ -122,7 +166,7 @@ public static ClientBuilder fromKubeConfig(KubeConfig config) throws IOException
122
166
}
123
167
124
168
builder .setBasePath (server );
125
- builder .setCredentialProvider (new KubeconfigCredentialProvider (config ));
169
+ builder .setAuthentication (new KubeconfigAuthentication (config ));
126
170
return builder ;
127
171
}
128
172
@@ -135,12 +179,12 @@ public ClientBuilder setBasePath(String basePath) {
135
179
return this ;
136
180
}
137
181
138
- public CredentialProvider getCredentialProvider () {
139
- return credentialProvider ;
182
+ public Authentication getAuthentication () {
183
+ return authentication ;
140
184
}
141
185
142
- public ClientBuilder setCredentialProvider (final CredentialProvider credentialProvider ) {
143
- this .credentialProvider = credentialProvider ;
186
+ public ClientBuilder setAuthentication (final Authentication authentication ) {
187
+ this .authentication = authentication ;
144
188
return this ;
145
189
}
146
190
@@ -175,8 +219,8 @@ public ApiClient build() {
175
219
client .setSslCaCert (new ByteArrayInputStream (caCertBytes ));
176
220
}
177
221
178
- if (credentialProvider != null ) {
179
- credentialProvider .provide (client );
222
+ if (authentication != null ) {
223
+ authentication .provide (client );
180
224
}
181
225
182
226
return client ;
0 commit comments