Skip to content

Commit

Permalink
fix (#2827) Use crd classloader and fallback to tccl when initalizing
Browse files Browse the repository at this point in the history
spec/ status
  • Loading branch information
iocanel authored and manusa committed Feb 23, 2021
1 parent 767ffe0 commit 9d865b4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#### Bugs
* Apiextensions DSL should use NonNamespaceOperation for CustomResourceDefinitions
* CNFE when initialzing CustomResource instances

#### Improvements

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Stream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -329,7 +334,8 @@ private Instantiator getInstantiator(int genericTypeIndex) throws Exception {
// get the associated class from the type name, if not Void
String className = types[genericTypeIndex].getTypeName();
if (!VOID_TYPE_NAME.equals(className)) {
final Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
final Class<?> clazz = loadClass(className, getClass().getClassLoader())
.orElseThrow(() -> new KubernetesClientException("Failed to load class:" + className + ", using neihter " + getClass().getName() + " ClassLoader, nor Thread context classLoader."));
if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) {
throw new IllegalArgumentException(
"Cannot instantiate interface/abstract type " + className);
Expand Down Expand Up @@ -369,4 +375,19 @@ private Object genericInit(int genericTypeIndex) {
private final static String getKey(Class<? extends CustomResource> clazz, int genericTypeIndex) {
return clazz.getCanonicalName() + "_" + genericTypeIndex;
}

private static final Optional<Class<?>> loadClass(String fqcn, ClassLoader ... classLoaders) {
Function<ClassLoader, Class<?>> mapper = cl -> {
try {
return cl.loadClass(fqcn);
} catch (ClassNotFoundException | NoClassDefFoundError e) {
return null;
}
};

return Stream.concat(Stream.of(classLoaders), Stream.of(Thread.currentThread().getContextClassLoader()))
.map(mapper)
.filter(c -> c != null)
.findFirst();
}
}

0 comments on commit 9d865b4

Please sign in to comment.