Skip to content

Commit

Permalink
Adding an api layer to isolate Okhttp (#3549)
Browse files Browse the repository at this point in the history
* introducing a jdk like set of interfaces to encapsulate okhttp

* further refining httpclientutils and simplifying mock client creation

* adding default client construction

this allows moving openid logic away from okhttp

* moving as much as possible to common configuration

* javadoc corrections

* canceling only if the future is canceled

* addressing code smells and re-enabling the token test

* creating a utility class to move more test logic off okhttp

* migrating the rest of the tests off of okhttp

* checking for empty status

* isolating how much the httpclient is passed around

* ensuring websocket closure

* adding a changelog and ensuring consistency with the prior header calls

* various updates to allow the jdk client to work

* adding back deprecated constructors and httpclientutils methods

also passing the factory for use by the token interceptor

* addressing code smells

* moving okhttp to a non-internal package, and refining the ExecListener

* making interceptor name constants, removing todo, splitting lines

* encapsulating additionalConfig on the client factory

this makes sure the factory is the primary abstraction for creating
clients

* correcting javadoc

* one more change to mention

* removing zero width character

* converting the istio client to the new api

* renaming clientstate to clientcontext
  • Loading branch information
shawkins authored Dec 16, 2021
1 parent 6480899 commit 7ef49cd
Show file tree
Hide file tree
Showing 325 changed files with 4,710 additions and 3,522 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
* Fix #3593: Add support for Istio extension

#### _**Note**_: Breaking changes in the API
* Refactoring #3547: due to an abstraction layer added over okHttp, the following api changes were made:
* OperationContext withOkHttpClient was removed, it should be needed to be directly called
* PatchType.getMediaType was replaced with PatchType.getContentType
* ExecListener no longer passes the okhttp3.Response to onOpen. onFailure will pass a simplified ExecListener.Response when possible.
* okhttp3.TlsVersions has been replaced by io.fabric8.kubernetes.client.http.TlsVersion
* HttpClientUtils.createHttpClient(config, additionalConfig) has been deprecated and now returns an OkHttpClientImpl
* The client is no longer adaptable to an OkHttpClient, use Client.getHttpClient instead

### 5.10.1 (2021-11-12)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@
*/
package io.fabric8.camelk.client;

import io.fabric8.kubernetes.client.ExtensionAdapterSupport;
import io.fabric8.kubernetes.client.Client;
import io.fabric8.kubernetes.client.ExtensionAdapter;
import okhttp3.OkHttpClient;
import io.fabric8.kubernetes.client.ExtensionAdapterSupport;

import java.net.URL;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class CamelKExtensionAdapter extends ExtensionAdapterSupport implements ExtensionAdapter<CamelKClient> {

static final ConcurrentMap<URL, Boolean> IS_CAMELK = new ConcurrentHashMap<>();
static final ConcurrentMap<URL, Boolean> USES_CAMELK_APIGROUPS = new ConcurrentHashMap<>();

@Override
public Class<CamelKClient> getExtensionType() {
return CamelKClient.class;
Expand All @@ -41,7 +40,7 @@ public Boolean isAdaptable(Client client) {

@Override
public CamelKClient adapt(Client client) {
return new DefaultCamelKClient(client.adapt(OkHttpClient.class), client.getConfiguration());
return new DefaultCamelKClient(client);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
*/
package io.fabric8.camelk.client;

import io.fabric8.camelk.client.dsl.V1alpha1APIGroupDSL;
import io.fabric8.camelk.client.dsl.V1APIGroupDSL;
import okhttp3.OkHttpClient;
import io.fabric8.camelk.client.dsl.V1alpha1APIGroupDSL;
import io.fabric8.kubernetes.client.BaseClient;
import io.fabric8.kubernetes.client.ClientContext;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.RequestConfig;
import io.fabric8.kubernetes.client.WithRequestCallable;
import io.fabric8.kubernetes.client.dsl.FunctionCallable;
import io.fabric8.kubernetes.client.BaseClient;
import io.fabric8.kubernetes.client.Config;

public class DefaultCamelKClient extends BaseClient implements NamespacedCamelKClient {

Expand All @@ -35,8 +35,8 @@ public DefaultCamelKClient(Config configuration) {
super(configuration);
}

public DefaultCamelKClient(OkHttpClient httpClient, Config configuration) {
super(httpClient, configuration);
public DefaultCamelKClient(ClientContext clientContext) {
super(clientContext);
}

@Override
Expand All @@ -50,10 +50,10 @@ public NamespacedCamelKClient inNamespace(String namespace) {
.withNamespace(namespace)
.build();

return new DefaultCamelKClient(getHttpClient(), updated);
return new DefaultCamelKClient(newState(updated));
}

@Override
@Override
public FunctionCallable<NamespacedCamelKClient> withRequestConfig(RequestConfig requestConfig) {
return new WithRequestCallable<>(this, requestConfig);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
*/
package io.fabric8.camelk.client;

import io.fabric8.kubernetes.client.BaseClient;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.Handlers;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.camelk.client.dsl.V1APIGroupDSL;
import io.fabric8.camelk.v1.Build;
import io.fabric8.camelk.v1.BuildList;
Expand All @@ -32,40 +26,44 @@
import io.fabric8.camelk.v1.IntegrationList;
import io.fabric8.camelk.v1.IntegrationPlatform;
import io.fabric8.camelk.v1.IntegrationPlatformList;
import okhttp3.OkHttpClient;
import io.fabric8.kubernetes.client.BaseClient;
import io.fabric8.kubernetes.client.ClientContext;
import io.fabric8.kubernetes.client.Handlers;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.Resource;

public class V1APIGroupClient extends BaseClient implements V1APIGroupDSL {
public V1APIGroupClient() {
super();
}

public V1APIGroupClient(OkHttpClient httpClient, final Config config) {
super(httpClient, config);
public V1APIGroupClient(ClientContext clientContext) {
super(clientContext);
}

@Override
public MixedOperation<Build, BuildList, Resource<Build>> builds() {
return Handlers.getOperation(Build.class, BuildList.class, httpClient, getConfiguration());
return Handlers.getOperation(Build.class, BuildList.class, this);
}

@Override
public MixedOperation<CamelCatalog, CamelCatalogList, Resource<CamelCatalog>> camelCatalogs() {
return Handlers.getOperation(CamelCatalog.class, CamelCatalogList.class, httpClient, getConfiguration());
return Handlers.getOperation(CamelCatalog.class, CamelCatalogList.class, this);
}

@Override
public MixedOperation<Integration, IntegrationList, Resource<Integration>> integrations() {
return Handlers.getOperation(Integration.class, IntegrationList.class, httpClient, getConfiguration());
return Handlers.getOperation(Integration.class, IntegrationList.class, this);
}

@Override
public MixedOperation<IntegrationKit, IntegrationKitList, Resource<IntegrationKit>> integrationKits() {
return Handlers.getOperation(IntegrationKit.class, IntegrationKitList.class, httpClient, getConfiguration());
return Handlers.getOperation(IntegrationKit.class, IntegrationKitList.class, this);
}

@Override
public MixedOperation<IntegrationPlatform, IntegrationPlatformList, Resource<IntegrationPlatform>> integrationPlatforms() {
return Handlers.getOperation(IntegrationPlatform.class, IntegrationPlatformList.class, httpClient, getConfiguration());
return Handlers.getOperation(IntegrationPlatform.class, IntegrationPlatformList.class, this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import io.fabric8.kubernetes.client.APIGroupExtensionAdapter;
import io.fabric8.kubernetes.client.Client;
import okhttp3.OkHttpClient;

public class V1APIGroupExtensionAdapter extends APIGroupExtensionAdapter<V1APIGroupClient> {
@Override
Expand All @@ -32,6 +31,6 @@ public Class<V1APIGroupClient> getExtensionType() {

@Override
protected V1APIGroupClient newInstance(Client client) {
return new V1APIGroupClient(client.adapt(OkHttpClient.class), client.getConfiguration());
return new V1APIGroupClient(client);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,27 @@
*/
package io.fabric8.camelk.client;

import io.fabric8.camelk.client.dsl.V1alpha1APIGroupDSL;
import io.fabric8.camelk.v1alpha1.Kamelet;
import io.fabric8.camelk.v1alpha1.KameletList;
import io.fabric8.kubernetes.client.BaseClient;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ClientContext;
import io.fabric8.kubernetes.client.Handlers;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.camelk.client.dsl.V1alpha1APIGroupDSL;
import io.fabric8.camelk.v1alpha1.Kamelet;
import io.fabric8.camelk.v1alpha1.KameletList;
import okhttp3.OkHttpClient;

public class V1alpha1APIGroupClient extends BaseClient implements V1alpha1APIGroupDSL {

public V1alpha1APIGroupClient() {
super();
}

public V1alpha1APIGroupClient(OkHttpClient httpClient, final Config config) {
super(httpClient, config);
public V1alpha1APIGroupClient(ClientContext clientContext) {
super(clientContext);
}

@Override
public MixedOperation<Kamelet, KameletList, Resource<Kamelet>> kamelets() {
return Handlers.getOperation(Kamelet.class, KameletList.class, httpClient, getConfiguration());
return Handlers.getOperation(Kamelet.class, KameletList.class, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import io.fabric8.kubernetes.client.APIGroupExtensionAdapter;
import io.fabric8.kubernetes.client.Client;
import okhttp3.OkHttpClient;

public class V1alpha1APIGroupExtensionAdapter extends APIGroupExtensionAdapter<V1alpha1APIGroupClient> {
@Override
Expand All @@ -32,6 +31,6 @@ public Class<V1alpha1APIGroupClient> getExtensionType() {

@Override
protected V1alpha1APIGroupClient newInstance(Client client) {
return new V1alpha1APIGroupClient(client.adapt(OkHttpClient.class), client.getConfiguration());
return new V1alpha1APIGroupClient(client);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,19 @@
*/
package io.fabric8.camelk.mock;

import io.fabric8.camelk.client.DefaultCamelKClient;
import io.fabric8.camelk.client.NamespacedCamelKClient;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
import io.fabric8.mockwebserver.Context;
import io.fabric8.mockwebserver.ServerRequest;
import io.fabric8.mockwebserver.ServerResponse;
import io.fabric8.camelk.client.DefaultCamelKClient;
import io.fabric8.camelk.client.CamelKClient;
import okhttp3.mockwebserver.Dispatcher;
import okhttp3.mockwebserver.MockWebServer;

import java.util.Map;
import java.util.Queue;

import static io.fabric8.kubernetes.client.utils.HttpClientUtils.createHttpClientForMockServer;
import static okhttp3.TlsVersion.TLS_1_2;

public class CamelKMockServer extends KubernetesMockServer {
private boolean disableApiGroupCheck = true;

public CamelKMockServer() {
super();
Expand All @@ -54,12 +47,6 @@ public String[] getRootPaths() {
}

public NamespacedCamelKClient createCamelKClient() {
Config config = new ConfigBuilder()
.withMasterUrl(url("/"))
.withNamespace("test")
.withTrustCerts(true)
.withTlsVersions(TLS_1_2)
.build();
return new DefaultCamelKClient(createHttpClientForMockServer(config), config);
return new DefaultCamelKClient(getMockConfiguration());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
*/
package io.fabric8.certmanager.client;

import io.fabric8.kubernetes.client.ExtensionAdapterSupport;
import io.fabric8.kubernetes.client.Client;
import io.fabric8.kubernetes.client.ExtensionAdapter;
import okhttp3.OkHttpClient;
import io.fabric8.kubernetes.client.ExtensionAdapterSupport;

import java.net.URL;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -42,6 +41,6 @@ public Boolean isAdaptable(Client client) {

@Override
public CertManagerClient adapt(Client client) {
return new DefaultCertManagerClient(client.adapt(OkHttpClient.class), client.getConfiguration());
return new DefaultCertManagerClient(client);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
import io.fabric8.certmanager.client.dsl.V1alpha2APIGroupDSL;
import io.fabric8.certmanager.client.dsl.V1alpha3APIGroupDSL;
import io.fabric8.certmanager.client.dsl.V1beta1APIGroupDSL;
import io.fabric8.kubernetes.client.*;
import io.fabric8.kubernetes.client.BaseClient;
import io.fabric8.kubernetes.client.ClientContext;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.RequestConfig;
import io.fabric8.kubernetes.client.WithRequestCallable;
import io.fabric8.kubernetes.client.dsl.FunctionCallable;
import okhttp3.OkHttpClient;

public class DefaultCertManagerClient extends BaseClient implements NamespacedCertManagerClient {

Expand All @@ -33,8 +37,8 @@ public DefaultCertManagerClient(Config configuration) {
super(configuration);
}

public DefaultCertManagerClient(OkHttpClient httpClient, Config configuration) {
super(httpClient, configuration);
public DefaultCertManagerClient(ClientContext clientContext) {
super(clientContext);
}

@Override
Expand All @@ -48,7 +52,7 @@ public NamespacedCertManagerClient inNamespace(String namespace) {
.withNamespace(namespace)
.build();

return new DefaultCertManagerClient(getHttpClient(), updated);
return new DefaultCertManagerClient(newState(updated));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,56 @@
import io.fabric8.certmanager.api.model.acme.v1.ChallengeList;
import io.fabric8.certmanager.api.model.acme.v1.Order;
import io.fabric8.certmanager.api.model.acme.v1.OrderList;
import io.fabric8.certmanager.api.model.v1.*;
import io.fabric8.certmanager.api.model.v1.Certificate;
import io.fabric8.certmanager.api.model.v1.CertificateList;
import io.fabric8.certmanager.api.model.v1.CertificateRequest;
import io.fabric8.certmanager.api.model.v1.CertificateRequestList;
import io.fabric8.certmanager.api.model.v1.ClusterIssuer;
import io.fabric8.certmanager.api.model.v1.ClusterIssuerList;
import io.fabric8.certmanager.api.model.v1.Issuer;
import io.fabric8.certmanager.api.model.v1.IssuerList;
import io.fabric8.certmanager.client.dsl.V1APIGroupDSL;
import io.fabric8.kubernetes.client.BaseClient;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ClientContext;
import io.fabric8.kubernetes.client.Handlers;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import okhttp3.OkHttpClient;

public class V1APIGroupClient extends BaseClient implements V1APIGroupDSL {
public V1APIGroupClient() {super();}

public V1APIGroupClient(OkHttpClient httpClient, final Config config) {
super(httpClient, config);
public V1APIGroupClient(ClientContext clientContext) {
super(clientContext);
}

@Override
public MixedOperation<Certificate, CertificateList, Resource<Certificate>> certificates() {
return Handlers.getOperation(Certificate.class, CertificateList.class, this.getHttpClient(), this.getConfiguration());
return Handlers.getOperation(Certificate.class, CertificateList.class, this);
}

@Override
public MixedOperation<CertificateRequest, CertificateRequestList, Resource<CertificateRequest>> certificateRequests() {
return Handlers.getOperation(CertificateRequest.class, CertificateRequestList.class, this.getHttpClient(), this.getConfiguration());
return Handlers.getOperation(CertificateRequest.class, CertificateRequestList.class, this);
}

@Override
public MixedOperation<Issuer, IssuerList, Resource<Issuer>> issuers() {
return Handlers.getOperation(Issuer.class, IssuerList.class, this.getHttpClient(), this.getConfiguration());
return Handlers.getOperation(Issuer.class, IssuerList.class, this);
}

@Override
public NonNamespaceOperation<ClusterIssuer, ClusterIssuerList, Resource<ClusterIssuer>> clusterIssuers() {
return Handlers.getOperation(ClusterIssuer.class, ClusterIssuerList.class, this.getHttpClient(), this.getConfiguration());
return Handlers.getOperation(ClusterIssuer.class, ClusterIssuerList.class, this);
}

@Override
public MixedOperation<Challenge, ChallengeList, Resource<Challenge>> challenges() {
return Handlers.getOperation(Challenge.class, ChallengeList.class, this.getHttpClient(), this.getConfiguration());
return Handlers.getOperation(Challenge.class, ChallengeList.class, this);
}

@Override
public MixedOperation<Order, OrderList, Resource<Order>> orders() {
return Handlers.getOperation(Order.class, OrderList.class, this.getHttpClient(), this.getConfiguration());
return Handlers.getOperation(Order.class, OrderList.class, this);
}
}
Loading

0 comments on commit 7ef49cd

Please sign in to comment.