Skip to content

Commit c91e951

Browse files
authored
Avoid NPE on informers (#1630)
I found out when working on a CloudBees proprietary feature that `KubernetesCloud#readResolve` is not always called on deserialization. I think this will not happen in OSS, but let's stay on the safe side and initialize the field before using it.
1 parent 9879072 commit c91e951

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

Diff for: src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public class KubernetesCloud extends Cloud implements PodTemplateGroup {
172172
* namespace -> informer
173173
* Use to watch pod events per namespace.
174174
*/
175-
private transient Map<String, SharedIndexInformer<Pod>> informers = new ConcurrentHashMap<>();
175+
private transient volatile Map<String, SharedIndexInformer<Pod>> informers = new ConcurrentHashMap<>();
176176

177177
@DataBoundConstructor
178178
public KubernetesCloud(String name) {
@@ -1306,9 +1306,6 @@ private Object readResolve() {
13061306
if (containerCap != null && containerCap == 0) {
13071307
containerCap = null;
13081308
}
1309-
if (informers == null) {
1310-
informers = new ConcurrentHashMap<>();
1311-
}
13121309
return this;
13131310
}
13141311

@@ -1321,6 +1318,15 @@ public Cloud reconfigure(@NonNull StaplerRequest req, JSONObject form) throws De
13211318
}
13221319

13231320
public void registerPodInformer(KubernetesSlave node) {
1321+
// even having readResolve initializing informers is not enough, there are some special cases where XStream will
1322+
// not call it, so let us make sure it is initialized before using
1323+
if (informers == null) {
1324+
synchronized (this) {
1325+
if (informers == null) {
1326+
informers = new ConcurrentHashMap<>();
1327+
}
1328+
}
1329+
}
13241330
informers.computeIfAbsent(node.getNamespace(), (n) -> {
13251331
KubernetesClient client;
13261332
try {

Diff for: src/main/java/org/csanchez/jenkins/plugins/kubernetes/watch/PodStatusEventHandler.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ private String formatPodStatus(PodCondition c, String phase, StringBuilder sb) {
5050
// not interesting
5151
return "";
5252
}
53-
String formatted = String.format("%n\tPod [%s][%s] %s", phase, c.getReason(), c.getMessage());
53+
String message = c.getMessage();
54+
String formatted =
55+
String.format("%n\tPod [%s][%s] %s", phase, c.getReason(), message != null ? message : "No message");
5456
return sb.indexOf(formatted) == -1 ? formatted : "";
5557
}
5658

0 commit comments

Comments
 (0)