Skip to content

Commit

Permalink
Avoid NPE on informers (#1630)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
amuniz authored Dec 19, 2024
1 parent 9879072 commit c91e951
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public class KubernetesCloud extends Cloud implements PodTemplateGroup {
* namespace -> informer
* Use to watch pod events per namespace.
*/
private transient Map<String, SharedIndexInformer<Pod>> informers = new ConcurrentHashMap<>();
private transient volatile Map<String, SharedIndexInformer<Pod>> informers = new ConcurrentHashMap<>();

@DataBoundConstructor
public KubernetesCloud(String name) {
Expand Down Expand Up @@ -1306,9 +1306,6 @@ private Object readResolve() {
if (containerCap != null && containerCap == 0) {
containerCap = null;
}
if (informers == null) {
informers = new ConcurrentHashMap<>();
}
return this;
}

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

public void registerPodInformer(KubernetesSlave node) {
// even having readResolve initializing informers is not enough, there are some special cases where XStream will
// not call it, so let us make sure it is initialized before using
if (informers == null) {
synchronized (this) {
if (informers == null) {
informers = new ConcurrentHashMap<>();
}
}
}
informers.computeIfAbsent(node.getNamespace(), (n) -> {
KubernetesClient client;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ private String formatPodStatus(PodCondition c, String phase, StringBuilder sb) {
// not interesting
return "";
}
String formatted = String.format("%n\tPod [%s][%s] %s", phase, c.getReason(), c.getMessage());
String message = c.getMessage();
String formatted =
String.format("%n\tPod [%s][%s] %s", phase, c.getReason(), message != null ? message : "No message");
return sb.indexOf(formatted) == -1 ? formatted : "";
}

Expand Down

0 comments on commit c91e951

Please sign in to comment.