Skip to content

NullPointerException when calling Kubectl.top on pods when one of the pods is down #4057

Description

@c4lm

Describe the bug
NullPointerException when calling Kubectl.top if one pod is in erroneous state or down. Stacktrace:

java.lang.NullPointerException: Cannot invoke "io.kubernetes.client.custom.PodMetrics.getContainers()" because "podMetrics" is null
	at io.kubernetes.client.extended.kubectl.KubectlTop.podMetricSum(KubectlTop.java:141)
	at io.kubernetes.client.extended.kubectl.KubectlTop$2.compare(KubectlTop.java:166)
	at io.kubernetes.client.extended.kubectl.KubectlTop$2.compare(KubectlTop.java:160)
	at java.base/java.util.TimSort.countRunAndMakeAscending(TimSort.java:355)
	at java.base/java.util.TimSort.sort(TimSort.java:220)
	at java.base/java.util.Arrays.sort(Arrays.java:1307)
	at java.base/java.util.ArrayList.sort(ArrayList.java:1805)
	at java.base/java.util.Collections.sort(Collections.java:178)
	at io.kubernetes.client.extended.kubectl.KubectlTop.topPods(KubectlTop.java:158)
	at io.kubernetes.client.extended.kubectl.KubectlTop.execute(KubectlTop.java:68)

Client Version
io.kubernetes:client-java:23.0.0
io.kubernetes:client-java-extended:23.0.0

Kubernetes Version
1.30

Server Version: version.Info{Major:"1", Minor:"30+", GitVersion:"v1.30.11-eks-bcf3d70", GitCommit:"2fa190a24584e3b061e4c94d3b4d1608e34e5e3b", GitTreeState:"clean", BuildDate:"2025-03-24T21:51:28Z", GoVersion:"go1.23.6", Compiler:"gc", Platform:"linux/amd64"}

Java Version
Java 23

To Reproduce
Steps to reproduce the behavior:

  1. Have a pod that is down, completed, or in Error state, e.g. deploy a pod with an image that doesn't exist.
  2. Call Kubectl.top on pods:
public List<Pair<V1Pod, PodMetrics>> getPodUsage(String namespace) throws KubectlException {
    return Kubectl.top(V1Pod.class, PodMetrics.class).namespace(namespace).execute();
  }

Expected behavior
Just like in the CLI, I expect to get pod metrics for pods which are up.

Server (please complete the following information):

  • OS: amazonlinux:2023
  • Environment: container in k8s
  • Cloud: AWS

Additional context
Same happens on library version 18.0.1.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueDenotes an issue ready for a new contributor, according to the "help wanted" guidelines.kind/bugCategorizes issue or PR as related to a bug.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions