Skip to content

Commit c1a47c8

Browse files
committed
fix (kubernetes-client-api) : Config's autoConfigure should disable auto configuration
`Config`'s autoConfigure field should behave as per expectations. In order to make auto configuration configurable via builder, we need to move it after builder initialization is done. Do not autoConfigure in the default constructor `new Config()`. Auto configuration would be done in the final constructor annotated with the sundrio's `@Buildable` annotation. Then merge user provided fields with autoConfigured values. Signed-off-by: Rohan Kumar <[email protected]>
1 parent ad4fcaf commit c1a47c8

File tree

5 files changed

+520
-94
lines changed

5 files changed

+520
-94
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#### Bugs
66
* Fix #6038: Support for Gradle configuration cache
77
* Fix #6110: VolumeSource (and other file mode fields) in Octal are correctly interpreted
8+
* Fix #6137: `ConfigBuilder.withAutoConfigure` is not working
89

910
#### Improvements
1011
* Fix #6008: removing the optional dependency on bouncy castle

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/Config.java

Lines changed: 197 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class Config {
6969
private static final Logger LOGGER = LoggerFactory.getLogger(Config.class);
7070

7171
/**
72-
* Disables auto-configuration based on opinionated defaults in a {@link Config} object in the default constructor
72+
* Disables auto-configuration based on opinionated defaults in a {@link Config} object in the Builder constructor
7373
*/
7474
public static final String KUBERNETES_DISABLE_AUTO_CONFIG_SYSTEM_PROPERTY = "kubernetes.disable.autoConfig";
7575
public static final String KUBERNETES_MASTER_SYSTEM_PROPERTY = "kubernetes.master";
@@ -151,6 +151,9 @@ public class Config {
151151

152152
private static final String ACCESS_TOKEN = "access-token";
153153
private static final String ID_TOKEN = "id-token";
154+
private static final TlsVersion[] DEFAULT_TLS_VERSIONS_LIST = new TlsVersion[] { TlsVersion.TLS_1_3, TlsVersion.TLS_1_2 };
155+
private static final int DEFAULT_WATCH_RECONNECT_INTERVAL = 1000;
156+
private static final int DEFAULT_CONNECTION_TIMEOUT = 10 * 1000;
154157

155158
private boolean trustCerts;
156159
private boolean disableHostnameVerification;
@@ -178,7 +181,7 @@ public class Config {
178181
private volatile String autoOAuthToken;
179182
private OAuthTokenProvider oauthTokenProvider;
180183
private long websocketPingInterval = DEFAULT_WEBSOCKET_PING_INTERVAL;
181-
private int connectionTimeout = 10 * 1000;
184+
private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
182185
private int maxConcurrentRequests = DEFAULT_MAX_CONCURRENT_REQUESTS;
183186
private int maxConcurrentRequestsPerHost = DEFAULT_MAX_CONCURRENT_REQUESTS_PER_HOST;
184187

@@ -190,7 +193,7 @@ public class Config {
190193
/**
191194
* fields not used but needed for builder generation.
192195
*/
193-
private int watchReconnectInterval = 1000;
196+
private int watchReconnectInterval = DEFAULT_WATCH_RECONNECT_INTERVAL;
194197
private int watchReconnectLimit = -1;
195198
private int uploadRequestTimeout = DEFAULT_UPLOAD_REQUEST_TIMEOUT;
196199
private int requestRetryBackoffLimit;
@@ -218,7 +221,7 @@ public class Config {
218221
private String proxyPassword;
219222
private String[] noProxy;
220223
private String userAgent = "fabric8-kubernetes-client/" + Version.clientVersion();
221-
private TlsVersion[] tlsVersions = new TlsVersion[] { TlsVersion.TLS_1_3, TlsVersion.TLS_1_2 };
224+
private TlsVersion[] tlsVersions = DEFAULT_TLS_VERSIONS_LIST;
222225

223226
private boolean onlyHttpWatches;
224227

@@ -239,7 +242,7 @@ public class Config {
239242
*/
240243
@Deprecated
241244
public Config() {
242-
this(!disableAutoConfig());
245+
this.autoConfigure = true;
243246
}
244247

245248
private static boolean disableAutoConfig() {
@@ -336,7 +339,7 @@ public Config(String masterUrl, String apiVersion, String namespace, boolean tru
336339
DEFAULT_UPLOAD_REQUEST_TIMEOUT, false, null, Collections.emptyList());
337340
}
338341

339-
@Buildable(builderPackage = "io.fabric8.kubernetes.api.builder", editableEnabled = false)
342+
@Deprecated
340343
public Config(String masterUrl, String apiVersion, String namespace, boolean trustCerts, boolean disableHostnameVerification,
341344
String caCertFile, String caCertData, String clientCertFile, String clientCertData, String clientKeyFile,
342345
String clientKeyData, String clientKeyAlgo, String clientKeyPassphrase, String username, String password,
@@ -350,56 +353,198 @@ public Config(String masterUrl, String apiVersion, String namespace, boolean tru
350353
OAuthTokenProvider oauthTokenProvider, Map<String, String> customHeaders, int requestRetryBackoffLimit,
351354
int requestRetryBackoffInterval, int uploadRequestTimeout, boolean onlyHttpWatches, NamedContext currentContext,
352355
List<NamedContext> contexts) {
353-
this.apiVersion = apiVersion;
354-
this.namespace = namespace;
355-
this.trustCerts = trustCerts;
356-
this.disableHostnameVerification = disableHostnameVerification;
357-
this.caCertFile = caCertFile;
358-
this.caCertData = caCertData;
359-
this.clientCertFile = clientCertFile;
360-
this.clientCertData = clientCertData;
361-
this.clientKeyFile = clientKeyFile;
362-
this.clientKeyData = clientKeyData;
363-
this.clientKeyAlgo = clientKeyAlgo;
364-
this.clientKeyPassphrase = clientKeyPassphrase;
365-
this.username = username;
366-
this.password = password;
367-
this.oauthToken = oauthToken;
368-
this.websocketPingInterval = websocketPingInterval;
369-
this.connectionTimeout = connectionTimeout;
370-
371-
this.requestConfig = new RequestConfig(watchReconnectLimit, watchReconnectInterval,
372-
requestTimeout, scaleTimeout, loggingInterval,
373-
requestRetryBackoffLimit, requestRetryBackoffInterval, uploadRequestTimeout);
374-
this.requestConfig.setImpersonateUsername(impersonateUsername);
375-
this.requestConfig.setImpersonateGroups(impersonateGroups);
376-
this.requestConfig.setImpersonateExtras(impersonateExtras);
356+
this(masterUrl, apiVersion, namespace, trustCerts, disableHostnameVerification, caCertFile, caCertData,
357+
clientCertFile, clientCertData, clientKeyFile, clientKeyData, clientKeyAlgo, clientKeyPassphrase, username,
358+
password, oauthToken, autoOAuthToken, watchReconnectInterval, watchReconnectLimit, connectionTimeout, requestTimeout,
359+
scaleTimeout, loggingInterval, maxConcurrentRequests, maxConcurrentRequestsPerHost, http2Disable,
360+
httpProxy, httpsProxy, noProxy, userAgent, tlsVersions, websocketPingInterval, proxyUsername, proxyPassword,
361+
trustStoreFile, trustStorePassphrase, keyStoreFile, keyStorePassphrase, impersonateUsername, impersonateGroups,
362+
impersonateExtras, oauthTokenProvider, customHeaders, requestRetryBackoffLimit, requestRetryBackoffInterval,
363+
uploadRequestTimeout, onlyHttpWatches, currentContext, contexts, false);
364+
}
377365

378-
this.http2Disable = http2Disable;
379-
this.httpProxy = httpProxy;
380-
this.httpsProxy = httpsProxy;
381-
this.noProxy = noProxy;
382-
this.proxyUsername = proxyUsername;
383-
this.proxyPassword = proxyPassword;
384-
this.userAgent = userAgent;
385-
this.tlsVersions = tlsVersions;
386-
this.trustStoreFile = trustStoreFile;
387-
this.trustStorePassphrase = trustStorePassphrase;
388-
this.keyStoreFile = keyStoreFile;
389-
this.keyStorePassphrase = keyStorePassphrase;
390-
this.oauthTokenProvider = oauthTokenProvider;
391-
this.customHeaders = customHeaders;
366+
@Buildable(builderPackage = "io.fabric8.kubernetes.api.builder", editableEnabled = false)
367+
public Config(String masterUrl, String apiVersion, String namespace, boolean trustCerts, boolean disableHostnameVerification,
368+
String caCertFile, String caCertData, String clientCertFile, String clientCertData, String clientKeyFile,
369+
String clientKeyData, String clientKeyAlgo, String clientKeyPassphrase, String username, String password,
370+
String oauthToken, String autoOAuthToken, int watchReconnectInterval, int watchReconnectLimit, int connectionTimeout,
371+
int requestTimeout,
372+
long scaleTimeout, int loggingInterval, int maxConcurrentRequests, int maxConcurrentRequestsPerHost,
373+
boolean http2Disable, String httpProxy, String httpsProxy, String[] noProxy,
374+
String userAgent, TlsVersion[] tlsVersions, long websocketPingInterval, String proxyUsername,
375+
String proxyPassword, String trustStoreFile, String trustStorePassphrase, String keyStoreFile, String keyStorePassphrase,
376+
String impersonateUsername, String[] impersonateGroups, Map<String, List<String>> impersonateExtras,
377+
OAuthTokenProvider oauthTokenProvider, Map<String, String> customHeaders, int requestRetryBackoffLimit,
378+
int requestRetryBackoffInterval, int uploadRequestTimeout, boolean onlyHttpWatches, NamedContext currentContext,
379+
List<NamedContext> contexts, boolean autoConfigure) {
380+
if (autoConfigure && !disableAutoConfig()) {
381+
autoConfigure(this, null);
382+
}
383+
if (Utils.isNotNullOrEmpty(apiVersion)) {
384+
this.apiVersion = apiVersion;
385+
}
386+
if (Utils.isNotNullOrEmpty(namespace)) {
387+
this.namespace = namespace;
388+
}
389+
if (trustCerts) {
390+
this.trustCerts = trustCerts;
391+
}
392+
if (disableHostnameVerification) {
393+
this.disableHostnameVerification = disableHostnameVerification;
394+
}
395+
if (Utils.isNotNullOrEmpty(caCertData)) {
396+
this.caCertData = caCertData;
397+
}
398+
if (Utils.isNotNullOrEmpty(caCertFile)) {
399+
this.caCertFile = caCertFile;
400+
}
401+
if (Utils.isNotNullOrEmpty(clientCertFile)) {
402+
this.clientCertFile = clientCertFile;
403+
}
404+
if (Utils.isNotNullOrEmpty(clientCertData)) {
405+
this.clientCertData = clientCertData;
406+
}
407+
if (Utils.isNotNullOrEmpty(clientKeyFile)) {
408+
this.clientKeyFile = clientKeyFile;
409+
}
410+
if (Utils.isNotNullOrEmpty(clientKeyData)) {
411+
this.clientKeyData = clientKeyData;
412+
}
413+
if (Utils.isNotNullOrEmpty(clientKeyAlgo) && !clientKeyAlgo.equals("RSA")) {
414+
this.clientKeyAlgo = clientKeyAlgo;
415+
}
416+
if (Utils.isNotNullOrEmpty(clientKeyPassphrase) && !clientKeyPassphrase.equals("changeit")) {
417+
this.clientKeyPassphrase = clientKeyPassphrase;
418+
}
419+
if (Utils.isNotNullOrEmpty(username)) {
420+
this.username = username;
421+
}
422+
if (Utils.isNotNullOrEmpty(password)) {
423+
this.password = password;
424+
}
425+
if (Utils.isNotNullOrEmpty(oauthToken)) {
426+
this.oauthToken = oauthToken;
427+
}
428+
if (websocketPingInterval != DEFAULT_WEBSOCKET_PING_INTERVAL) {
429+
this.websocketPingInterval = websocketPingInterval;
430+
}
431+
if (connectionTimeout != DEFAULT_CONNECTION_TIMEOUT) {
432+
this.connectionTimeout = connectionTimeout;
433+
}
434+
if (http2Disable) {
435+
this.http2Disable = http2Disable;
436+
}
437+
if (Utils.isNotNullOrEmpty(httpProxy)) {
438+
this.httpProxy = httpProxy;
439+
}
440+
if (Utils.isNotNullOrEmpty(httpsProxy)) {
441+
this.httpsProxy = httpsProxy;
442+
}
443+
if (Utils.isNotNullOrEmpty(noProxy)) {
444+
this.noProxy = noProxy;
445+
}
446+
if (Utils.isNotNullOrEmpty(proxyUsername)) {
447+
this.proxyUsername = proxyUsername;
448+
}
449+
if (Utils.isNotNullOrEmpty(proxyPassword)) {
450+
this.proxyPassword = proxyPassword;
451+
}
452+
if (Utils.isNotNullOrEmpty(userAgent)) {
453+
this.userAgent = userAgent;
454+
}
455+
if (tlsVersions != null && tlsVersions.length > 0 && !Arrays.equals(tlsVersions, DEFAULT_TLS_VERSIONS_LIST)) {
456+
this.tlsVersions = tlsVersions;
457+
}
458+
if (Utils.isNotNullOrEmpty(trustStoreFile)) {
459+
this.trustStoreFile = trustStoreFile;
460+
}
461+
if (Utils.isNotNullOrEmpty(trustStorePassphrase)) {
462+
this.trustStorePassphrase = trustStorePassphrase;
463+
}
464+
if (Utils.isNotNullOrEmpty(keyStoreFile)) {
465+
this.keyStoreFile = keyStoreFile;
466+
}
467+
if (Utils.isNotNullOrEmpty(keyStorePassphrase)) {
468+
this.keyStorePassphrase = keyStorePassphrase;
469+
}
470+
if (Utils.isNotNull(oauthTokenProvider)) {
471+
this.oauthTokenProvider = oauthTokenProvider;
472+
}
473+
if (Utils.isNotNullOrEmpty(customHeaders)) {
474+
this.customHeaders = customHeaders;
475+
}
392476

393477
//We need to keep this after ssl configuration & masterUrl
394478
//We set the masterUrl because it's needed by ensureHttps
395-
this.masterUrl = masterUrl;
396-
this.masterUrl = ensureEndsWithSlash(ensureHttps(masterUrl, this));
397-
this.maxConcurrentRequests = maxConcurrentRequests;
398-
this.maxConcurrentRequestsPerHost = maxConcurrentRequestsPerHost;
399-
this.autoOAuthToken = autoOAuthToken;
400-
this.onlyHttpWatches = onlyHttpWatches;
401-
this.contexts = contexts;
402-
this.currentContext = currentContext;
479+
if (Utils.isNotNullOrEmpty(masterUrl) && !masterUrl.equals(DEFAULT_MASTER_URL)) {
480+
this.masterUrl = masterUrl;
481+
this.masterUrl = ensureEndsWithSlash(ensureHttps(masterUrl, this));
482+
}
483+
if (maxConcurrentRequests != DEFAULT_MAX_CONCURRENT_REQUESTS) {
484+
this.maxConcurrentRequests = maxConcurrentRequests;
485+
}
486+
if (maxConcurrentRequestsPerHost != DEFAULT_MAX_CONCURRENT_REQUESTS_PER_HOST) {
487+
this.maxConcurrentRequestsPerHost = maxConcurrentRequestsPerHost;
488+
}
489+
if (Utils.isNotNullOrEmpty(autoOAuthToken)) {
490+
this.autoOAuthToken = autoOAuthToken;
491+
}
492+
if (onlyHttpWatches) {
493+
this.onlyHttpWatches = onlyHttpWatches;
494+
}
495+
if (contexts != null && !contexts.isEmpty()) {
496+
this.contexts = contexts;
497+
}
498+
if (currentContext != null) {
499+
this.currentContext = currentContext;
500+
}
501+
502+
RequestConfig autoConfiguredRequestConfig = this.requestConfig;
503+
this.requestConfig = mergeRequestConfig(new RequestConfig(watchReconnectLimit, watchReconnectInterval,
504+
requestTimeout, scaleTimeout, loggingInterval,
505+
requestRetryBackoffLimit, requestRetryBackoffInterval, uploadRequestTimeout), autoConfiguredRequestConfig);
506+
this.requestConfig.setImpersonateUsername(
507+
Optional.ofNullable(impersonateUsername).orElse(autoConfiguredRequestConfig.getImpersonateUsername()));
508+
this.requestConfig.setImpersonateGroups(
509+
Utils.isNotNullOrEmpty(impersonateGroups) ? impersonateGroups : autoConfiguredRequestConfig.getImpersonateGroups());
510+
this.requestConfig.setImpersonateExtras(
511+
Utils.isNotNullOrEmpty(impersonateExtras) ? impersonateExtras : autoConfiguredRequestConfig.getImpersonateExtras());
512+
}
513+
514+
private RequestConfig mergeRequestConfig(RequestConfig userProvidedRequestConfig, RequestConfig autoConfiguredRequestConfig) {
515+
RequestConfigBuilder requestConfigBuilder = new RequestConfigBuilder(userProvidedRequestConfig);
516+
if (userProvidedRequestConfig.getRequestTimeout() == DEFAULT_REQUEST_TIMEOUT
517+
&& autoConfiguredRequestConfig.getRequestTimeout() != DEFAULT_REQUEST_TIMEOUT) {
518+
requestConfigBuilder.withRequestTimeout(autoConfiguredRequestConfig.getRequestTimeout());
519+
}
520+
if (userProvidedRequestConfig.getWatchReconnectLimit() == -1 && autoConfiguredRequestConfig.getWatchReconnectLimit() > 0) {
521+
requestConfigBuilder.withWatchReconnectLimit(autoConfiguredRequestConfig.getWatchReconnectLimit());
522+
}
523+
if (userProvidedRequestConfig.getWatchReconnectInterval() == DEFAULT_WATCH_RECONNECT_INTERVAL
524+
&& autoConfiguredRequestConfig.getWatchReconnectInterval() != DEFAULT_WATCH_RECONNECT_INTERVAL) {
525+
requestConfigBuilder.withWatchReconnectInterval(autoConfiguredRequestConfig.getWatchReconnectInterval());
526+
}
527+
if (userProvidedRequestConfig.getRequestTimeout() == DEFAULT_REQUEST_TIMEOUT
528+
&& autoConfiguredRequestConfig.getRequestTimeout() != DEFAULT_REQUEST_TIMEOUT) {
529+
requestConfigBuilder.withRequestTimeout(autoConfiguredRequestConfig.getRequestTimeout());
530+
}
531+
if (userProvidedRequestConfig.getScaleTimeout() == DEFAULT_SCALE_TIMEOUT
532+
&& autoConfiguredRequestConfig.getScaleTimeout() != DEFAULT_SCALE_TIMEOUT) {
533+
requestConfigBuilder.withScaleTimeout(autoConfiguredRequestConfig.getScaleTimeout());
534+
}
535+
if (userProvidedRequestConfig.getRequestRetryBackoffLimit() == DEFAULT_REQUEST_RETRY_BACKOFFLIMIT
536+
&& autoConfiguredRequestConfig.getRequestRetryBackoffLimit() != DEFAULT_REQUEST_RETRY_BACKOFFLIMIT) {
537+
requestConfigBuilder.withRequestRetryBackoffLimit(autoConfiguredRequestConfig.getRequestRetryBackoffLimit());
538+
}
539+
if (userProvidedRequestConfig.getRequestRetryBackoffInterval() == DEFAULT_REQUEST_RETRY_BACKOFFINTERVAL
540+
&& autoConfiguredRequestConfig.getRequestRetryBackoffInterval() != DEFAULT_REQUEST_RETRY_BACKOFFINTERVAL) {
541+
requestConfigBuilder.withRequestRetryBackoffInterval(autoConfiguredRequestConfig.getRequestRetryBackoffInterval());
542+
}
543+
if (userProvidedRequestConfig.getUploadRequestTimeout() == DEFAULT_UPLOAD_REQUEST_TIMEOUT
544+
&& autoConfiguredRequestConfig.getRequestTimeout() != DEFAULT_UPLOAD_REQUEST_TIMEOUT) {
545+
requestConfigBuilder.withUploadRequestTimeout(autoConfiguredRequestConfig.getUploadRequestTimeout());
546+
}
547+
return requestConfigBuilder.build();
403548
}
404549

405550
public static void configFromSysPropsOrEnvVars(Config config) {

0 commit comments

Comments
 (0)