forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix fabric8io#2066: Separated Kubernetes and OpenShift Handler interf…
…aces to avoid collission in uberjar
- Loading branch information
1 parent
9069bb5
commit a2eda82
Showing
12 changed files
with
237 additions
and
19 deletions.
There are no files selected for viewing
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
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
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
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
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
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
94 changes: 94 additions & 0 deletions
94
openshift-client/src/main/java/io/fabric8/openshift/client/OpenShiftHandlers.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,94 @@ | ||
package io.fabric8.openshift.client; | ||
|
||
import io.fabric8.kubernetes.api.builder.VisitableBuilder; | ||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.fabric8.kubernetes.client.Handlers; | ||
import io.fabric8.kubernetes.client.ResourceHandler; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.ServiceLoader; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
|
||
public final class OpenShiftHandlers { | ||
|
||
private static final Set<ClassLoader> CLASS_LOADERS = new HashSet<>(); | ||
private static final Map<ResourceHandler.Key, ResourceHandler> RESOURCE_HANDLER_MAP = new HashMap<>(); | ||
static { | ||
// Register Kubernetes Handlers | ||
discoverHandlers(ResourceHandler.class.getClassLoader()); | ||
|
||
// Register OpenShift Handlers | ||
discoverHandlers(OpenShiftResourceHandler.class.getClassLoader()); | ||
} | ||
|
||
public static <T extends HasMetadata, V extends VisitableBuilder<T, V>> void register(ResourceHandler<T,V> handler) { | ||
RESOURCE_HANDLER_MAP.put(new ResourceHandler.Key(handler.getKind().toLowerCase(Locale.ROOT), handler.getApiVersion()), handler); | ||
} | ||
|
||
public static <T extends HasMetadata, V extends VisitableBuilder<T, V>> void unregister(ResourceHandler<T,V> handler) { | ||
RESOURCE_HANDLER_MAP.remove(new ResourceHandler.Key(handler.getKind().toLowerCase(Locale.ROOT), handler.getApiVersion())); | ||
} | ||
|
||
public static <T extends HasMetadata, V extends VisitableBuilder<T, V>> ResourceHandler<T, V> get(String kind, String apiVersion) { | ||
return get(kind, apiVersion, Thread.currentThread().getContextClassLoader()); | ||
} | ||
|
||
public static <T extends HasMetadata, V extends VisitableBuilder<T, V>> ResourceHandler<T, V> get(String kind, String apiVersion, ClassLoader classLoader) { | ||
return get(new ResourceHandler.Key(kind.toLowerCase(Locale.ROOT), apiVersion), classLoader); | ||
} | ||
|
||
public static <T extends HasMetadata, V extends VisitableBuilder<T, V>> ResourceHandler<T, V> get(ResourceHandler.Key key) { | ||
return get(key, Thread.currentThread().getContextClassLoader()); | ||
} | ||
|
||
public static <T extends HasMetadata, V extends VisitableBuilder<T, V>> ResourceHandler<T, V> get(ResourceHandler.Key key, ClassLoader classLoader) { | ||
if (RESOURCE_HANDLER_MAP.containsKey(key)) { | ||
return RESOURCE_HANDLER_MAP.get(key); | ||
} else { | ||
// Check for Kubernetes | ||
ResourceHandler resourceHandler = getResourceHandlerForClass(key, ResourceHandler.class, classLoader); | ||
if (resourceHandler != null) { | ||
return resourceHandler; | ||
} | ||
|
||
// Check for OpenShift | ||
resourceHandler = getResourceHandlerForClass(key, OpenShiftResourceHandler.class, classLoader); | ||
return resourceHandler; | ||
} | ||
} | ||
|
||
private static <T extends HasMetadata, V extends VisitableBuilder<T, V>> ResourceHandler<T, V> fetchResourceHandlerFromServiceLoader(Class<ResourceHandler> handlerClass, ClassLoader classLoader, Predicate<ResourceHandler<T, V>> condition) { | ||
for (ResourceHandler handler : ServiceLoader.load(handlerClass, classLoader)) { | ||
if (condition.test(handler)) { | ||
return handler; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private static <T extends HasMetadata, V extends VisitableBuilder<T, V>> ResourceHandler<T, V> getResourceHandlerForClass(ResourceHandler.Key key, Class className, ClassLoader classLoader) { | ||
//1st pass: match kind and apiVersion | ||
ResourceHandler handler = fetchResourceHandlerFromServiceLoader(className, classLoader, h -> h.getKind().toLowerCase(Locale.ROOT).equals(key.getKind()) && h.getApiVersion().equals(key.getApiVersion())); | ||
if (handler != null) { | ||
return handler; | ||
} | ||
|
||
|
||
//2nd pass: match kind. | ||
handler = fetchResourceHandlerFromServiceLoader(className, classLoader, h -> h.getKind().toLowerCase(Locale.ROOT).equals(key.getKind())); | ||
return handler; | ||
} | ||
|
||
protected static void discoverHandlers(ClassLoader classLoader) { | ||
if (classLoader != null && CLASS_LOADERS.add(classLoader)) { | ||
for (ResourceHandler handler : ServiceLoader.load(ResourceHandler.class, classLoader)) { | ||
register(handler); | ||
} | ||
} | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
openshift-client/src/main/java/io/fabric8/openshift/client/OpenShiftResourceHandler.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,7 @@ | ||
package io.fabric8.openshift.client; | ||
|
||
import io.fabric8.kubernetes.api.builder.VisitableBuilder; | ||
import io.fabric8.kubernetes.client.ResourceHandler; | ||
|
||
public interface OpenShiftResourceHandler<T, V extends VisitableBuilder<T, V>> extends ResourceHandler<T, V> { | ||
} |
34 changes: 34 additions & 0 deletions
34
...l/internal/OpenShiftNamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl.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,34 @@ | ||
package io.fabric8.openshift.client.dsl.internal; | ||
|
||
import io.fabric8.kubernetes.api.builder.Visitor; | ||
import io.fabric8.kubernetes.api.model.DeletionPropagation; | ||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.fabric8.kubernetes.api.model.KubernetesList; | ||
import io.fabric8.kubernetes.client.Config; | ||
import io.fabric8.kubernetes.client.HasMetadataVisitiableBuilder; | ||
import io.fabric8.kubernetes.client.ResourceHandler; | ||
import io.fabric8.kubernetes.client.dsl.internal.NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl; | ||
import io.fabric8.kubernetes.client.handlers.KubernetesListHandler; | ||
import io.fabric8.openshift.client.OpenShiftHandlers; | ||
import okhttp3.OkHttpClient; | ||
|
||
import java.util.List; | ||
|
||
public class OpenShiftNamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl extends NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl { | ||
|
||
public OpenShiftNamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableImpl(OkHttpClient client, Config config, String namespace, String explicitNamespace, Boolean fromServer, Boolean deletingExisting, List<Visitor> visitors, Object item, long gracePeriodSeconds, DeletionPropagation propagationPolicy, Boolean cascading) { | ||
super(client, config, namespace, explicitNamespace, fromServer, deletingExisting, visitors, item, gracePeriodSeconds, propagationPolicy, cascading); | ||
} | ||
|
||
@Override | ||
public <T> ResourceHandler handlerOf(T item) { | ||
if (item instanceof HasMetadata) { | ||
return OpenShiftHandlers.<HasMetadata, HasMetadataVisitiableBuilder>get(((HasMetadata) item).getKind(), ((HasMetadata) item).getApiVersion()); | ||
} else if (item instanceof KubernetesList) { | ||
return new KubernetesListHandler(); | ||
} else { | ||
throw new IllegalArgumentException("Could not find a registered handler for item: [" + item + "]."); | ||
} | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...ternal/OpenShiftNamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl.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,38 @@ | ||
package io.fabric8.openshift.client.dsl.internal; | ||
|
||
import io.fabric8.kubernetes.api.builder.Visitor; | ||
import io.fabric8.kubernetes.api.model.DeletionPropagation; | ||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.fabric8.kubernetes.api.model.KubernetesList; | ||
import io.fabric8.kubernetes.client.Config; | ||
import io.fabric8.kubernetes.client.HasMetadataVisitiableBuilder; | ||
import io.fabric8.kubernetes.client.ResourceHandler; | ||
import io.fabric8.kubernetes.client.dsl.internal.NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl; | ||
import io.fabric8.kubernetes.client.handlers.KubernetesListHandler; | ||
import io.fabric8.openshift.client.OpenShiftHandlers; | ||
import okhttp3.OkHttpClient; | ||
|
||
import java.io.InputStream; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class OpenShiftNamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl extends NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl { | ||
public OpenShiftNamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl(OkHttpClient client, Config config, String namespace, String explicitNamespace, Boolean fromServer, Boolean deletingExisting, List<Visitor> visitors, InputStream is, Map<String, String> parameters, Boolean cascading, DeletionPropagation propagationPolicy) { | ||
super(client, config, namespace, explicitNamespace, fromServer, deletingExisting, visitors, is, parameters, cascading, propagationPolicy); | ||
} | ||
|
||
public OpenShiftNamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl(OkHttpClient client, Config config, String namespace, String explicitNamespace, Boolean fromServer, Boolean deletingExisting, List<Visitor> visitors, Object item, InputStream inputStream, Map<String, String> parameters, long gracePeriodSeconds, DeletionPropagation propagationPolicy, Boolean cascading) { | ||
super(client, config, namespace, explicitNamespace, fromServer, deletingExisting, visitors, item, inputStream, parameters, gracePeriodSeconds, propagationPolicy, cascading); | ||
} | ||
|
||
@Override | ||
public <T> ResourceHandler handlerOf(T item) { | ||
if (item instanceof HasMetadata) { | ||
return OpenShiftHandlers.<HasMetadata, HasMetadataVisitiableBuilder>get(((HasMetadata) item).getKind(), ((HasMetadata) item).getApiVersion()); | ||
} else if (item instanceof KubernetesList) { | ||
return new KubernetesListHandler(); | ||
} else { | ||
throw new IllegalArgumentException("Could not find a registered handler for item: [" + item + "]."); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.