Skip to content

Commit

Permalink
adding a possible reflection based set of client builders
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Jan 9, 2022
1 parent afa7485 commit 2d22886
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.fabric8.kubernetes.client;

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 BaseKubernetesClientBuilder<T extends BaseKubernetesClientBuilder<T>> {

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

protected BaseKubernetesClientBuilder(String className) {
try {
this.clazz = (Class<KubernetesClient>) Class.forName(className);
} catch (ClassNotFoundException e) {
throw KubernetesClientException.launderThrowable(e);
}
}

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 BaseKubernetesClientBuilder<T> withConfig(Config config) {
this.config = config;
return this;
}

public BaseKubernetesClientBuilder<T> withConfig(String config) {
this.config = Serialization.unmarshal(config);
return this;
}

public BaseKubernetesClientBuilder<T> withConfig(InputStream config) {
this.config = Serialization.unmarshal(config);
return this;
}

public BaseKubernetesClientBuilder<T> withHttpClientFactory(HttpClient.Factory factory) {
this.factory = factory;
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.fabric8.kubernetes.client;

public class KubernetesClientBuilder extends BaseKubernetesClientBuilder<KubernetesClientBuilder> {

public KubernetesClientBuilder() {
super("io.fabric8.kubernetes.client.DefaultKubernetesClient");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.fabric8.openshift.client;

import io.fabric8.kubernetes.client.BaseKubernetesClientBuilder;

public class OpenShiftClientBuilder extends BaseKubernetesClientBuilder<OpenShiftClientBuilder> {

public OpenShiftClientBuilder() {
super("io.fabric8.openshift.client.DefaultOpenShiftClient");
}

public OpenShiftClientBuilder withOpenShiftConfig(OpenShiftConfig config) {
super.withConfig(config);
return this;
}

@Override
public OpenShiftClient build() {
return (OpenShiftClient)super.build();
}

}

0 comments on commit 2d22886

Please sign in to comment.