Skip to content

Commit d1b6e05

Browse files
authored
Merge pull request #4087 from c4lm/fix-4057-KubectlTop-NPE-on-error-pod
Fix NPE in KubectlTop - skip pods which are down
2 parents cda0948 + d6c6306 commit d1b6e05

File tree

1 file changed

+8
-1
lines changed
  • extended/src/main/java/io/kubernetes/client/extended/kubectl

1 file changed

+8
-1
lines changed

extended/src/main/java/io/kubernetes/client/extended/kubectl/KubectlTop.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ private static PodMetrics findPodMetric(V1Pod pod, PodMetricsList list) {
138138

139139
public static double podMetricSum(PodMetrics podMetrics, String metricName) {
140140
double sum = 0;
141+
if (podMetrics == null) {
142+
return 0;
143+
}
141144
for (ContainerMetrics containerMetrics : podMetrics.getContainers()) {
142145
Quantity value = containerMetrics.getUsage().get(metricName);
143146
if (value != null) {
@@ -170,7 +173,11 @@ public int compare(V1Pod arg0, V1Pod arg1) {
170173

171174
List<Pair<ApiType, MetricsType>> result = new ArrayList<>();
172175
for (V1Pod pod : items) {
173-
result.add(new ImmutablePair<>((ApiType) pod, (MetricsType) findPodMetric(pod, metrics)));
176+
PodMetrics podMetrics = findPodMetric(pod, metrics);
177+
if (podMetrics == null) {
178+
continue;
179+
}
180+
result.add(new ImmutablePair<>((ApiType) pod, (MetricsType) podMetrics));
174181
}
175182
return result;
176183
}

0 commit comments

Comments
 (0)