-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding a possible reflection based set of client builders
- Loading branch information
Showing
3 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
...es-client-api/src/main/java/io/fabric8/kubernetes/client/BaseKubernetesClientBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
...rnetes-client-api/src/main/java/io/fabric8/kubernetes/client/KubernetesClientBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
openshift-client-api/src/main/java/io/fabric8/openshift/client/OpenShiftClientBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |