Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhandled NullPointerException in loadRelated #4791

Closed
sfc-gh-valiu opened this issue Jan 24, 2023 · 0 comments · Fixed by #4847
Closed

Unhandled NullPointerException in loadRelated #4791

sfc-gh-valiu opened this issue Jan 24, 2023 · 0 comments · Fixed by #4847
Assignees
Milestone

Comments

@sfc-gh-valiu
Copy link

sfc-gh-valiu commented Jan 24, 2023

Describe the bug

Background

Hi, I am working on the Calico GlobalNetworkPolicy CRD with Fabric8 6.0.0. After generating the Java classes from the CRD YAML with the java-generator-maven-plugin and manually fixed the namespaced issue, I always got NullPointerException when I try with

public enum MyKubeTypeHandler {
    // these work
    POD(Pod.class, KubernetesClient::pods),
    DEPLOYMENT(Deployment.class, client -> client.apps().deployments()),
    ......
    // this DOES NOT work due to unhandled `NullPointerException`
    CALICO_GLOBAL_NETWORK_POLICY(GlobalNetworkPolicy.class, client -> client.resources(GlobalNetworkPolicy.class));
    ......
    public NonNamespaceOperation<
              HasMetadata,
              KubernetesResourceList<? extends HasMetadata>,
              ? extends Resource<HasMetadata>>
          withAllNamespaces(KubernetesClient client) {
        // This is where it failed
        return this.ops.apply(apiClient);
    }
}

The stack trace is as follow

java.lang.NullPointerException: null
	at io.fabric8.kubernetes.client.utils.KubernetesResourceUtil.loadRelated(KubernetesResourceUtil.java:415)
	at io.fabric8.kubernetes.client.utils.KubernetesResourceUtil.inferListType(KubernetesResourceUtil.java:406)
	at io.fabric8.kubernetes.client.dsl.internal.HasMetadataOperationsImpl.<init>(HasMetadataOperationsImpl.java:50)
	at io.fabric8.kubernetes.client.dsl.internal.HasMetadataOperationsImpl.<init>(HasMetadataOperationsImpl.java:38)
	at io.fabric8.kubernetes.client.BaseClient.newHasMetadataOperation(BaseClient.java:288)
	at io.fabric8.kubernetes.client.BaseClient.resources(BaseClient.java:279)
	at io.fabric8.kubernetes.client.Client.resources(Client.java:162)
	at io.fabric8.kubernetes.client.KubernetesClient.resources(KubernetesClient.java:140)
	at com.foo.bar.kube.MyKubeTypeHandler.lambda$static$6(MyKubeTypeHandler.java:61)

Java version

$ java -version
Picked up JAVA_TOOL_OPTIONS:  -Djavax.net.ssl.trustStore=/etc/pki/ca-trust/extracted/java/cacerts
openjdk version "11.0.17" 2022-10-18
OpenJDK Runtime Environment Temurin-11.0.17+8 (build 11.0.17+8)
OpenJDK 64-Bit Server VM Temurin-11.0.17+8 (build 11.0.17+8, mixed mode)

Fabric8 source code

When digging into the source code of fabric8 this is where I found that threw the NullPointerExceptions

    public static <T extends HasMetadata> Class<? extends KubernetesResourceList> inferListType(Class<T> type) {
        return loadRelated(type, "List", DefaultKubernetesResourceList.class);
    }
    ......
    private static Class<?> loadRelated(Class<?> type, String suffix, Class<?> defaultClass) {
        try {
            // Here the `getContextClassLoader()` may throw `null`
            return Thread.currentThread().getContextClassLoader().loadClass(type.getName() + suffix);
        } catch (ClassCastException | ClassNotFoundException var6) {
            try {
                return type.getClassLoader().loadClass(type.getName() + suffix);
            } catch (ClassCastException | ClassNotFoundException var5) {
                return defaultClass;
            }
        }
    }

Proposal

After checking the java 11 docs for getContextClassLoader method, it could return null, which makes the subsequent method invocation, loadClass() from a null object.

I checked the latest code (1a3b2b6) that this unhandled NullPointerException issue may still exists.

My suggestion is simply expect and handle the NullPointerException in the catch clause, as in #4792

Fabric8 Kubernetes Client version

6.0.0

Steps to reproduce

  1. Use openjdk 11.0.17
  2. Follow the docs to generate the collection of classes for GlobalNetworkPolicy from the Calico GlobalNetworkPolicy CRD.
  3. Run the code snippet above and check the logs

Expected behavior

The NullPointerException should be expected and handled.

Runtime

Kubernetes (vanilla)

Kubernetes API Server version

1.24

Environment

Linux

Fabric8 Kubernetes Client Logs

No response

Additional context

No response

@sfc-gh-valiu sfc-gh-valiu changed the title Unhandled NullPointerException in when inferList Unhandled NullPointerException in inferList Jan 24, 2023
@sfc-gh-valiu sfc-gh-valiu changed the title Unhandled NullPointerException in inferList Unhandled NullPointerException in loadRelated Jan 24, 2023
@manusa manusa added this to the 6.5.0 milestone Feb 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment