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 9aad7f1
Show file tree
Hide file tree
Showing 9 changed files with 408 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 @@ -144,6 +144,11 @@ public static String basicCredentials(String username, String password) {

private static final AtomicBoolean WARNED = new AtomicBoolean();

/**
* @deprecated you should not need to call this method directly. Please create your own HttpClient.Factory
* should you need to customize your clients.
*/
@Deprecated
public static HttpClient createHttpClient(Config config) {
ServiceLoader<HttpClient.Factory> loader = ServiceLoader.load(HttpClient.Factory.class);
HttpClient.Factory factory = null;
Expand Down
Loading

0 comments on commit 9aad7f1

Please sign in to comment.