Skip to content
This repository has been archived by the owner on Nov 6, 2018. It is now read-only.

feat: KubernetesDeserializer can now lookup for resource mappings via… #307

Merged
merged 3 commits into from
Jun 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kubernetes-model-annotator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-generator</artifactId>
<version>2.0-SNAPSHOT</version>
<version>2.1-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion kubernetes-model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-generator</artifactId>
<version>2.0-SNAPSHOT</version>
<version>2.1-SNAPSHOT</version>
</parent>

<artifactId>kubernetes-model</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (C) 2011 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.api;

import java.util.Map;

import io.fabric8.kubernetes.api.model.KubernetesResource;

public interface KubernetesResourceMappingProvider {

Map<String, Class<? extends KubernetesResource>> getMappings();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (C) 2011 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.internal;

import io.fabric8.kubernetes.api.KubernetesResourceMappingProvider;
import io.fabric8.kubernetes.api.model.KubernetesList;
import io.fabric8.kubernetes.api.model.KubernetesResource;

import java.util.HashMap;
import java.util.Map;

public class InternalResourceMappingProvider implements KubernetesResourceMappingProvider {

private final Map<String, Class<? extends KubernetesResource>> mappings = new HashMap<>();

public InternalResourceMappingProvider() {
mappings.put("List", KubernetesList.class);
mappings.put("v1#List", KubernetesList.class);
}

@Override
public Map<String, Class<? extends KubernetesResource>> getMappings() {
return mappings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,27 @@
*/
package io.fabric8.kubernetes.internal;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;

import com.fasterxml.jackson.databind.node.ObjectNode;
import io.fabric8.kubernetes.api.model.KubernetesList;
import io.fabric8.kubernetes.api.model.KubernetesResource;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import io.fabric8.kubernetes.api.KubernetesResourceMappingProvider;

public class KubernetesDeserializer extends JsonDeserializer<KubernetesResource> {

private static final String KIND = "kind";
private static final String API_VERSION = "apiVersion";
private static final String KEY_SEPARATOR = "#";

private static final String KUBERNETES_PACKAGE_PREFIX = "io.fabric8.kubernetes.api.model.";
private static final String KUBERNETES_EXTENSIONS_PACKAGE_PREFIX = "io.fabric8.kubernetes.api.model.extensions.";
Expand All @@ -39,39 +44,85 @@ public class KubernetesDeserializer extends JsonDeserializer<KubernetesResource>

private static final Map<String, Class<? extends KubernetesResource>> MAP = new HashMap<>();


static {
// Exceptions (not just package prefix + class name) can be added here.
MAP.put("List", KubernetesList.class);
//Use service loader to load extension types.
for (KubernetesResourceMappingProvider provider : ServiceLoader.load(KubernetesResourceMappingProvider.class)) {
MAP.putAll(provider.getMappings());
}
}

@Override
public KubernetesResource deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectNode node = jp.readValueAsTree();
JsonNode kind = node.get(KIND);
if (kind != null) {
String value = kind.textValue();
Class<? extends KubernetesResource> resourceType = getTypeForName(value);
String key = getKey(node);
if (key != null) {
Class<? extends KubernetesResource> resourceType = getTypeForKey(key);
if (resourceType == null) {
throw ctxt.mappingException("No resource type found for kind:" + value);
throw ctxt.mappingException("No resource type found for:" + key);
} else {
return jp.getCodec().treeToValue(node, resourceType);
}
}
return null;
}

/**
* Return a string representation of the key of the type: <version>#<kind>.
*/
private static final String getKey(ObjectNode node) {
JsonNode apiVersion = node.get(API_VERSION);
JsonNode kind = node.get(KIND);

return getKey(apiVersion != null ? apiVersion.textValue() : null,
kind != null ? kind.textValue() : null);
}

/**
* Returns a composite key for apiVersion and kind.
*/
private static final String getKey(String apiVersion, String kind) {
if (kind == null) {
return null;
} else if (apiVersion == null) {
return kind;
} else {
return String.format("%s#%s", apiVersion, kind);
}
}

/**
* Registers a Custom Resource Definition Kind
*/
public static void registerCustomKind(String kind, Class<? extends KubernetesResource> clazz) {
MAP.put(kind, clazz);
registerCustomKind(null, kind, clazz);
}

private static Class getTypeForName(String name) {
Class result = MAP.get(name);
/**
* Registers a Custom Resource Definition Kind
*/
public static void registerCustomKind(String apiVersion, String kind, Class<? extends KubernetesResource> clazz) {
MAP.put(getKey(apiVersion, kind), clazz);
}

static Class getTypeForKey(String key) {
Class result = MAP.get(key);
if (result == null) {
result = loadClassIfExists(KUBERNETES_PACKAGE_PREFIX + name);
String name = key != null && key.contains(KEY_SEPARATOR) ?
key.substring(key.indexOf(KEY_SEPARATOR) + 1) :
key;

System.out.println("Trying internal type for name:"+name);
result = getInternalTypeForName(name);
}

if (result != null) {
MAP.put(key, result);
}
return result;
}

private static Class getInternalTypeForName(String name) {
Class result = loadClassIfExists(KUBERNETES_PACKAGE_PREFIX + name);
if (result == null) {
result = loadClassIfExists(KUBERNETES_EXTENSIONS_PACKAGE_PREFIX + name);
if (result == null) {
Expand All @@ -81,13 +132,8 @@ private static Class getTypeForName(String name) {
}
}
}
}

if (result != null) {
MAP.put(name, result);
}
return result;
}
return result;
}

private static Class loadClassIfExists(String className) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.fabric8.kubernetes.internal.InternalResourceMappingProvider
2 changes: 1 addition & 1 deletion model-generator-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-generator</artifactId>
<version>2.0-SNAPSHOT</version>
<version>2.1-SNAPSHOT</version>
</parent>

<artifactId>model-generator-app</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</parent>

<groupId>io.fabric8</groupId>
<version>2.0-SNAPSHOT</version>
<version>2.1-SNAPSHOT</version>

<artifactId>kubernetes-model-generator</artifactId>
<packaging>pom</packaging>
Expand Down