Skip to content

Commit

Permalink
Merge pull request #1436 from iocanel/resourcehandler-version
Browse files Browse the repository at this point in the history
fix: ResourceHandlers now take into consideration apiVersion.
  • Loading branch information
iocanel authored Mar 16, 2019
2 parents f3252c4 + 90e73ba commit e1f0627
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public final class Handlers {

private static final Set<ClassLoader> CLASS_LOADERS = new HashSet<>();
private static final Map<String, ResourceHandler> RESOURCE_HANDLER_MAP = new HashMap<>();
private static final Map<ResourceHandler.Key, ResourceHandler> RESOURCE_HANDLER_MAP = new HashMap<>();

private Handlers() {
//Utility
Expand All @@ -40,19 +40,30 @@ private Handlers() {
}

public static <T extends HasMetadata, V extends VisitableBuilder<T, V>> void register(ResourceHandler<T,V> handler) {
RESOURCE_HANDLER_MAP.put(handler.getKind().toLowerCase(), handler);
RESOURCE_HANDLER_MAP.put(new ResourceHandler.Key(handler.getKind().toLowerCase(), handler.getApiVersion()), handler);
}

public static <T extends HasMetadata, V extends VisitableBuilder<T, V>> void unregister(ResourceHandler<T,V> handler) {
RESOURCE_HANDLER_MAP.remove(handler.getKind().toLowerCase());
}

public static <T extends HasMetadata, V extends VisitableBuilder<T, V>> ResourceHandler<T, V> get(String kind) {
if (RESOURCE_HANDLER_MAP.containsKey(kind.toLowerCase())) {
return RESOURCE_HANDLER_MAP.get(kind.toLowerCase());
public static <T extends HasMetadata, V extends VisitableBuilder<T, V>> ResourceHandler<T, V> get(String kind, String apiVersion) {
return get(new ResourceHandler.Key(kind.toLowerCase(), apiVersion));
}

public static <T extends HasMetadata, V extends VisitableBuilder<T, V>> ResourceHandler<T, V> get(ResourceHandler.Key key) {
if (RESOURCE_HANDLER_MAP.containsKey(key)) {
return RESOURCE_HANDLER_MAP.get(key);
} else {
//1st pass: match kind and apiVersion
for (ResourceHandler handler : ServiceLoader.load(ResourceHandler.class, Thread.currentThread().getContextClassLoader())) {
if (handler.getKind().toLowerCase().equals(key.getKind()) && handler.getApiVersion().equals(key.getApiVersion())) {
return handler;
}
}
//2nd pass: match kind.
for (ResourceHandler handler : ServiceLoader.load(ResourceHandler.class, Thread.currentThread().getContextClassLoader())) {
if (handler.getKind().equals(kind)) {
if (handler.getKind().toLowerCase().equals(key.getKind())) {
return handler;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,28 @@

public interface ResourceHandler<T, V extends VisitableBuilder<T, V>> {

class Key {
private final String kind;
private final String apiVersion;

public Key(String kind, String apiVersion) {
this.kind = kind;
this.apiVersion = apiVersion;
}

public String getKind() {
return kind;
}

public String getApiVersion() {
return apiVersion;
}
}

String getKind();

String getApiVersion();

/**
* Create the specified resource.
* @param client An instance of the http client.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public KubernetesList get() {
}

private <T extends HasMetadata, V extends VisitableBuilder<T, V>> T create(T resource) {
ResourceHandler<T, V> handler = Handlers.get(resource.getKind());
ResourceHandler<T, V> handler = Handlers.get(resource.getKind(), resource.getApiVersion());
if (handler != null) {
return handler.create(client, config, namespace, resource);
}
Expand All @@ -151,7 +151,7 @@ public Boolean delete(KubernetesList... lists) {
public Boolean delete(List<KubernetesList> lists) {
for (KubernetesList list : lists) {
for (HasMetadata item : list.getItems()) {
ResourceHandler<HasMetadata, HasMetadataVisitiableBuilder> handler = Handlers.get(item.getKind());
ResourceHandler<HasMetadata, HasMetadataVisitiableBuilder> handler = Handlers.get(item.getKind(), item.getApiVersion());
if (!handler.delete(client, config, namespace, item)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ private static <T> HasMetadata asHasMetadata(T item) {

private static <T> ResourceHandler handlerOf(T item) {
if (item instanceof HasMetadata) {
return Handlers.<HasMetadata, HasMetadataVisitiableBuilder>get(((HasMetadata) item).getKind());
return Handlers.<HasMetadata, HasMetadataVisitiableBuilder>get(((HasMetadata) item).getKind(), ((HasMetadata) item).getApiVersion());
} else if (item instanceof KubernetesList) {
return new KubernetesListHandler();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ private static <T> List<HasMetadata> asHasMetadata(T item, Boolean enableProcces

private static <T> ResourceHandler handlerOf(T item) {
if (item instanceof HasMetadata) {
return Handlers.<HasMetadata, HasMetadataVisitiableBuilder>get(((HasMetadata) item).getKind());
return Handlers.<HasMetadata, HasMetadataVisitiableBuilder>get(((HasMetadata) item).getKind(), ((HasMetadata) item).getApiVersion());
} else if (item instanceof KubernetesList) {
return new KubernetesListHandler();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public String getKind() {
return Service.class.getSimpleName();
}

@Override
public String getApiVersion() {
return "v1";
}

@Override
public KubernetesList create(OkHttpClient client, Config config, String namespace, KubernetesList item) {
return new KubernetesListOperationsImpl(client, config, namespace, null, true, false, false, item, null).create();
Expand All @@ -57,7 +62,7 @@ public KubernetesList replace(OkHttpClient client, Config config, String namespa
List<HasMetadata> replacedItems = new ArrayList<>();

for (HasMetadata metadata : item.getItems()) {
ResourceHandler<HasMetadata, HasMetadataVisitiableBuilder> handler = Handlers.get(item.getKind());
ResourceHandler<HasMetadata, HasMetadataVisitiableBuilder> handler = Handlers.get(item.getKind(), item.getApiVersion());
if (handler == null) {
LOGGER.warn("No handler found for:" + item.getKind() + ". Ignoring");
} else {
Expand All @@ -72,7 +77,7 @@ public KubernetesList reload(OkHttpClient client, Config config, String namespac
List<HasMetadata> replacedItems = new ArrayList<>();

for (HasMetadata metadata : item.getItems()) {
ResourceHandler<HasMetadata, HasMetadataVisitiableBuilder> handler = Handlers.get(item.getKind());
ResourceHandler<HasMetadata, HasMetadataVisitiableBuilder> handler = Handlers.get(item.getKind(), item.getApiVersion());
if (handler == null) {
LOGGER.warn("No handler found for:" + item.getKind() + ". Ignoring");
} else {
Expand Down
10 changes: 10 additions & 0 deletions kubernetes-client/src/main/resources/resource-handler.vm
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,21 @@ import java.util.TreeMap;
import java.util.concurrent.TimeUnit;

public class ${model.name}Handler implements ResourceHandler<${model.name}, ${model.name}Builder> {

@Override
public String getKind() {
return ${model.name}.class.getSimpleName();
}

@Override
public String getApiVersion() {
#if (${apiGroup} != "")
return "${apiGroup}/${apiVersion}";
#else
return "${apiVersion}";
#end
}

@Override
public ${model.name} create(OkHttpClient client, Config config, String namespace, ${model.name} item) {
return new ${model.name}OperationsImpl(client, config).withItem(item).inNamespace(namespace).create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.fabric8.openshift.client.DefaultOpenShiftClient;
import io.fabric8.openshift.client.OpenShiftClient;
import io.fabric8.openshift.client.OpenShiftConfig;
import io.fabric8.openshift.client.OpenShiftConfigBuilder;

import java.net.MalformedURLException;
import java.net.URL;
Expand All @@ -46,10 +47,10 @@ public static OperationContext wrap(OperationContext context) {
if (Utils.isNotNullOrEmpty(context.getApiGroupName()) && config.isOpenShiftAPIGroups(oc)) {
String apiGroupUrl = URLUtils.join(config.getMasterUrl(), "apis", context.getApiGroupName(), oapiVersion);
String apiGroupVersion = URLUtils.join(context.getApiGroupName(), oapiVersion);
return context.withConfig(new OpenShiftConfig(config, apiGroupUrl)).withApiGroupName(context.getApiGroupName()).withApiGroupVersion(apiGroupVersion);
return context.withConfig(new OpenShiftConfigBuilder(config).withOpenShiftUrl(apiGroupUrl).build()).withApiGroupName(context.getApiGroupName()).withApiGroupVersion(apiGroupVersion);
} else {
String apiGroupUrl = URLUtils.join(config.getMasterUrl(), "oapi", oapiVersion);
return context.withConfig(new OpenShiftConfig(config, apiGroupUrl)).withApiGroupName(context.getApiGroupName()).withApiGroupVersion(oapiVersion);
return context.withConfig(new OpenShiftConfigBuilder(config).withOpenShiftUrl(apiGroupUrl).build()).withApiGroupName(context.getApiGroupName()).withApiGroupVersion(oapiVersion);
}
}

Expand Down
9 changes: 9 additions & 0 deletions openshift-client/src/main/resources/resource-handler.vm
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ public class ${model.name}Handler implements ResourceHandler<${model.name}, ${mo
return ${model.name}.class.getSimpleName();
}

@Override
public String getApiVersion() {
#if (${apiGroup} != "")
return "${apiGroup}/${apiVersion}";
#else
return "${apiVersion}";
#end
}

@Override
public ${model.name} create(OkHttpClient client, Config config, String namespace, ${model.name} item) {
return new ${model.name}OperationsImpl(client, OpenShiftConfig.wrap(config)).withItem(item).inNamespace(namespace).create();
Expand Down

0 comments on commit e1f0627

Please sign in to comment.