From 10232c51b88e4c1b4fad0cf277c0617bce53852a Mon Sep 17 00:00:00 2001 From: Steve Hawkins Date: Fri, 10 Dec 2021 08:50:59 -0500 Subject: [PATCH] encapsulating additionalConfig on the client factory this makes sure the factory is the primary abstraction for creating clients --- .../client/okhttp/OkHttpClientFactory.java | 32 ++++++++++++------- .../client/utils/HttpClientUtils.java | 12 +++++-- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/okhttp/OkHttpClientFactory.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/okhttp/OkHttpClientFactory.java index 1d0e0b3fa3e..ce23346b745 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/okhttp/OkHttpClientFactory.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/okhttp/OkHttpClientFactory.java @@ -28,18 +28,28 @@ import org.slf4j.LoggerFactory; import java.util.concurrent.TimeUnit; -import java.util.function.Consumer; public class OkHttpClientFactory implements HttpClient.Factory { - @Override - public OkHttpClientImpl createHttpClient(Config config) { - return createHttpClient(config, builder -> {}); + /** + * Subclasses may use this to apply a base configuration to the builder + */ + protected OkHttpClient.Builder newOkHttpClientBuilder() { + return new OkHttpClient.Builder(); + } + + /** + * Subclasses may use this to apply additional configuration after the Config has been applied + * This method is only called for clients constructed using the Config. + * @param builder + */ + protected void additionalConfig(OkHttpClient.Builder builder) { + } @Override public Builder newBuilder() { - return new OkHttpClientBuilderImpl(new OkHttpClient.Builder()); + return new OkHttpClientBuilderImpl(newOkHttpClientBuilder()); } /** @@ -48,10 +58,10 @@ public Builder newBuilder() { * @param additionalConfig a consumer that allows overriding HTTP client properties * @return returns an HTTP client */ - public OkHttpClientImpl createHttpClient(Config config, - final Consumer additionalConfig) { + @Override + public OkHttpClientImpl createHttpClient(Config config) { try { - OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder(); + OkHttpClient.Builder httpClientBuilder = newOkHttpClientBuilder(); if (config.isTrustCerts() || config.isDisableHostnameVerification()) { httpClientBuilder.hostnameVerifier((s, sslSession) -> true); @@ -83,16 +93,14 @@ public OkHttpClientImpl createHttpClient(Config config, builderWrapper.preferHttp11(); } - if (additionalConfig != null) { - additionalConfig.accept(httpClientBuilder); - } + additionalConfig(httpClientBuilder); return builderWrapper.build(); } catch (Exception e) { throw KubernetesClientException.launderThrowable(e); } } - + /** * OkHttp wrongfully detects >JDK8u251 as {@link okhttp3.internal.platform.Jdk9Platform} which enables Http2 * unsupported for JDK8. diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/utils/HttpClientUtils.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/utils/HttpClientUtils.java index cfd45501b7f..3cc0020f57e 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/utils/HttpClientUtils.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/utils/HttpClientUtils.java @@ -25,6 +25,7 @@ import io.fabric8.kubernetes.client.okhttp.OkHttpClientFactory; import io.fabric8.kubernetes.client.okhttp.OkHttpClientImpl; import okhttp3.OkHttpClient; +import okhttp3.OkHttpClient.Builder; import javax.net.ssl.KeyManager; import javax.net.ssl.SSLContext; @@ -67,11 +68,18 @@ private HttpClientUtils() { } * @param config Kubernetes API client config * @param additionalConfig a consumer that allows overriding HTTP client properties * @return returns an HTTP client - * @deprecated use {@link OkHttpClientFactory#createHttpClient(Config, Consumer)} instead + * @deprecated subclass {@link OkHttpClientFactory} and implement the additionalConfig method */ @Deprecated public static OkHttpClientImpl createHttpClient(final Config config, final Consumer additionalConfig) { - return new OkHttpClientFactory().createHttpClient(config, additionalConfig); + return new OkHttpClientFactory() { + @Override + protected void additionalConfig(Builder builder) { + if (additionalConfig != null) { + additionalConfig.accept(builder); + } + } + }.createHttpClient(config); } public static URL getProxyUrl(Config config) throws MalformedURLException {