Skip to content

Commit

Permalink
fix fabric8io#3865: using just a single builder
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Mar 4, 2022
1 parent a476c10 commit fece460
Show file tree
Hide file tree
Showing 8 changed files with 403 additions and 327 deletions.
11 changes: 10 additions & 1 deletion doc/MIGRATION-v6.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,16 @@ This release introduces kubernetes-client-api and openshift-client-api modules.

If you are directly relying on classes in the -client jars other than DefaultKubernetesClient and DefaultOpenShiftClient, please let us know your usage scenario. Moving forward we'd like consider all classes in the -client jars internal.

When you rely solely on a compile dependency to the respective -api dependencies you will not be able to use DefaultKubernetesClient nor DefaultOpenShiftClient directly to create your client instances. You should instead - TBD
When you rely solely on a compile dependency to the respective -api dependencies you will not be able to use DefaultKubernetesClient nor DefaultOpenShiftClient directly to create your client instances. You should instead use KubernetesClientBuilder. Passing a configuration of HttpClient is instead done through builder methods - withConfig and withHttpClientFactory. For example:

```java
KubernetesClient client = new KubernetesClientBuilder().build();
...
```
```java
OpenShiftClient openShiftClient = new new KubernetesClientBuilder().withConfig(new OpenShiftConfigBuilder()...build()).build().adapt(OpenShiftClient.class);
...
```

### OkHttp HttpClient

Expand Down

This file was deleted.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,67 @@

package io.fabric8.kubernetes.client;

public class KubernetesClientBuilder extends BaseKubernetesClientBuilder<KubernetesClientBuilder> {
import io.fabric8.kubernetes.client.http.HttpClient;
import io.fabric8.kubernetes.client.utils.Serialization;

import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;

public class KubernetesClientBuilder {

private Config config;
private HttpClient.Factory factory;
private Class<KubernetesClient> clazz;

public KubernetesClientBuilder() {
super("io.fabric8.kubernetes.client.DefaultKubernetesClient");
// basically the same logic as in KubernetesResourceUtil for finding list types
// we're not guarding against a null context class loader
String className = "io.fabric8.kubernetes.client.DefaultKubernetesClient";
try {
clazz = (Class<KubernetesClient>) Thread.currentThread().getContextClassLoader().loadClass(className);
} catch (ClassNotFoundException | ClassCastException e) {
try {
clazz = (Class<KubernetesClient>) KubernetesClient.class.getClassLoader().loadClass(className);
} catch (Exception ex) {
throw KubernetesClientException.launderThrowable(ex);
}
}
}

public KubernetesClient build() {
if (config == null) {
config = new ConfigBuilder().build();
}
try {
if (factory == null) {
return clazz.getConstructor(Config.class).newInstance(config);
}
HttpClient client = factory.createHttpClient(config);
return clazz.getConstructor(HttpClient.class, Config.class).newInstance(client, config);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException e) {
throw KubernetesClientException.launderThrowable(e);
}
}

public KubernetesClientBuilder withConfig(Config config) {
this.config = config;
return this;
}

public KubernetesClientBuilder withConfig(String config) {
this.config = Serialization.unmarshal(config);
return this;
}

public KubernetesClientBuilder withConfig(InputStream config) {
this.config = Serialization.unmarshal(config);
return this;
}

public KubernetesClientBuilder withHttpClientFactory(HttpClient.Factory factory) {
this.factory = factory;
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -121,26 +121,32 @@
/**
* Class for Default Kubernetes Client implementing KubernetesClient interface.
* It is thread safe.
*
* @deprecated direct usage should no longer be needed. Please use the {@link KubernetesClientBuilder} instead.
*/
@Deprecated
public class DefaultKubernetesClient extends BaseClient implements NamespacedKubernetesClient {

public static final String KUBERNETES_VERSION_ENDPOINT = "version";

static {
Handlers.register(Pod.class, PodOperationsImpl::new);
Handlers.register(Job.class, JobOperationsImpl::new);
Handlers.register(Service.class, ServiceOperationsImpl::new);
Handlers.register(Deployment.class, DeploymentOperationsImpl::new);
Handlers.register(io.fabric8.kubernetes.api.model.extensions.Deployment.class, io.fabric8.kubernetes.client.dsl.internal.extensions.v1beta1.DeploymentOperationsImpl::new);
Handlers.register(io.fabric8.kubernetes.api.model.extensions.Deployment.class,
io.fabric8.kubernetes.client.dsl.internal.extensions.v1beta1.DeploymentOperationsImpl::new);
Handlers.register(ReplicaSet.class, ReplicaSetOperationsImpl::new);
Handlers.register(io.fabric8.kubernetes.api.model.extensions.ReplicaSet.class, io.fabric8.kubernetes.client.dsl.internal.extensions.v1beta1.ReplicaSetOperationsImpl::new);
Handlers.register(io.fabric8.kubernetes.api.model.extensions.ReplicaSet.class,
io.fabric8.kubernetes.client.dsl.internal.extensions.v1beta1.ReplicaSetOperationsImpl::new);
Handlers.register(ReplicationController.class, ReplicationControllerOperationsImpl::new);
Handlers.register(StatefulSet.class, StatefulSetOperationsImpl::new);
Handlers.register(io.fabric8.kubernetes.api.model.certificates.v1.CertificateSigningRequest.class, CertificateSigningRequestOperationsImpl::new);
Handlers.register(io.fabric8.kubernetes.api.model.certificates.v1.CertificateSigningRequest.class,
CertificateSigningRequestOperationsImpl::new);
// trigger a load of the other client handlers and make KubernetesClient and DefaultKubernetesClient
Adapters.initializeHandlers(ResourcedHasMetadataOperation::register);
}

public DefaultKubernetesClient() {
super();
}
Expand All @@ -152,11 +158,11 @@ public DefaultKubernetesClient(String masterUrl) {
public DefaultKubernetesClient(Config config) {
super(config);
}

public DefaultKubernetesClient(HttpClient httpClient, Config config) {
super(httpClient, config);
}

public DefaultKubernetesClient(ClientContext clientContext) {
super(clientContext);
}
Expand All @@ -183,7 +189,7 @@ protected ClientContext createInNamespaceContext(String name, boolean any) {
copy.setDefaultNamespace(false);
return newState(copy);
}

protected Config configCopy() {
return new ConfigBuilder(getConfiguration()).build();
}
Expand All @@ -197,7 +203,7 @@ public LeaderElectorBuilder<? extends NamespacedKubernetesClient> leaderElector(
public FunctionCallable<? extends NamespacedKubernetesClient> withRequestConfig(RequestConfig requestConfig) {
return new WithRequestCallable<>(this, requestConfig);
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -238,7 +244,8 @@ public NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata>
* {@inheritDoc}
*/
@Override
public NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata> resourceList(Collection<? extends HasMetadata> items) {
public NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata> resourceList(
Collection<? extends HasMetadata> items) {
return resourceList(new KubernetesListBuilder().withItems(new ArrayList<>(items)).build());
}

Expand All @@ -249,7 +256,7 @@ public NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata>
public ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata> resourceList(String s) {
return resourceListFor(s);
}

@Override
public <T extends HasMetadata> NamespaceableResource<T> resource(T item) {
// lookup the operation given the item
Expand All @@ -263,15 +270,17 @@ public <T extends HasMetadata> NamespaceableResource<T> resource(T item) {
*/
@Override
public NamespaceableResource<HasMetadata> resource(String s) {
return resource((HasMetadata)Serialization.unmarshal(s));
return resource((HasMetadata) Serialization.unmarshal(s));
}

/**
* {@inheritDoc}
*/
@Override
public MixedOperation<Binding, KubernetesResourceList<Binding>, Resource<Binding>> bindings() {
return resources(Binding.class, (Class<KubernetesResourceList<Binding>>) TypeFactory.rawClass(new TypeReference<KubernetesResourceList<Binding>>(){}.getType()));
return resources(Binding.class,
(Class<KubernetesResourceList<Binding>>) TypeFactory.rawClass(new TypeReference<KubernetesResourceList<Binding>>() {
}.getType()));
}

/**
Expand Down Expand Up @@ -372,7 +381,7 @@ public MixedOperation<ServiceAccount, ServiceAccountList, Resource<ServiceAccoun
*/
@Override
public NonNamespaceOperation<APIService, APIServiceList, Resource<APIService>> apiServices() {
return Handlers.getOperation(APIService.class, APIServiceList.class, this);
return Handlers.getOperation(APIService.class, APIServiceList.class, this);
}

/**
Expand Down Expand Up @@ -432,37 +441,40 @@ public InOutCreateable<TokenReview, TokenReview> tokenReviews() {
* {@inheritDoc}
*/
@Override
public <T extends CustomResource> MixedOperation<T, KubernetesResourceList<T>, Resource<T>> customResources(Class<T> resourceType) {
public <T extends CustomResource> MixedOperation<T, KubernetesResourceList<T>, Resource<T>> customResources(
Class<T> resourceType) {
return customResources(resourceType, null);
}


/**
* {@inheritDoc}
*/
@Override
public <T extends CustomResource, L extends KubernetesResourceList<T>> MixedOperation<T, L, Resource<T>> customResources(Class<T> resourceType, Class<L> listClass) {
public <T extends CustomResource, L extends KubernetesResourceList<T>> MixedOperation<T, L, Resource<T>> customResources(
Class<T> resourceType, Class<L> listClass) {
return customResources(CustomResourceDefinitionContext.fromCustomResourceType(resourceType), resourceType, listClass);
}

@Override
public MixedOperation<GenericKubernetesResource, GenericKubernetesResourceList, Resource<GenericKubernetesResource>> genericKubernetesResources(
String apiVersion, String kind) {
ResourceDefinitionContext context = Handlers.getResourceDefinitionContext(apiVersion, kind, this);
if (context == null) {
throw new KubernetesClientException("Could not find the metadata for the given apiVersion and kind, please pass a ResourceDefinitionContext instead");
throw new KubernetesClientException(
"Could not find the metadata for the given apiVersion and kind, please pass a ResourceDefinitionContext instead");
}
return genericKubernetesResources(context);
}

/**
* {@inheritDoc}
*/
@Override
public <T extends HasMetadata, L extends KubernetesResourceList<T>> HasMetadataOperationsImpl<T, L> customResources(ResourceDefinitionContext rdContext, Class<T> resourceType, Class<L> listClass) {
public <T extends HasMetadata, L extends KubernetesResourceList<T>> HasMetadataOperationsImpl<T, L> customResources(
ResourceDefinitionContext rdContext, Class<T> resourceType, Class<L> listClass) {
return newHasMetadataOperation(rdContext, resourceType, listClass);
}

@Override
public DiscoveryAPIGroupDSL discovery() {
return adapt(DiscoveryAPIGroupClient.class);
Expand Down Expand Up @@ -498,7 +510,7 @@ public FlowControlAPIGroupDSL flowControl() {
public VersionInfo getVersion() {
return getVersionInfo(KUBERNETES_VERSION_ENDPOINT);
}

@Override
public VersionInfo getKubernetesVersion() {
return getVersionInfo(KUBERNETES_VERSION_ENDPOINT);
Expand Down Expand Up @@ -540,37 +552,49 @@ public AutoscalingAPIGroupDSL autoscaling() {
* {@inheritDoc}
*/
@Override
public NetworkAPIGroupDSL network() { return adapt(NetworkAPIGroupClient.class); }
public NetworkAPIGroupDSL network() {
return adapt(NetworkAPIGroupClient.class);
}

/**
* {@inheritDoc}
*/
@Override
public StorageAPIGroupDSL storage() { return adapt(StorageAPIGroupClient.class); }
public StorageAPIGroupDSL storage() {
return adapt(StorageAPIGroupClient.class);
}

/**
* {@inheritDoc}
*/
@Override
public BatchAPIGroupDSL batch() { return adapt(BatchAPIGroupClient.class); }
public BatchAPIGroupDSL batch() {
return adapt(BatchAPIGroupClient.class);
}

/**
* {@inheritDoc}
*/
@Override
public MetricAPIGroupDSL top() { return adapt(MetricAPIGroupClient.class); }
public MetricAPIGroupDSL top() {
return adapt(MetricAPIGroupClient.class);
}

/**
* {@inheritDoc}
*/
@Override
public PolicyAPIGroupDSL policy() { return adapt(PolicyAPIGroupClient.class); }
public PolicyAPIGroupDSL policy() {
return adapt(PolicyAPIGroupClient.class);
}

/**
* {@inheritDoc}
*/
@Override
public RbacAPIGroupDSL rbac() { return adapt(RbacAPIGroupClient.class); }
public RbacAPIGroupDSL rbac() {
return adapt(RbacAPIGroupClient.class);
}

/**
* {@inheritDoc}
Expand Down
Loading

0 comments on commit fece460

Please sign in to comment.