Skip to content

Commit 7e6c171

Browse files
committed
fix: unify how resource information is added, prevent NPEs
Fixes #3183 Some (all?) MDC implementations prevent adding null values so default should be provided or the key omitted. Signed-off-by: Chris Laprun <metacosm@gmail.com>
1 parent fc23fef commit 7e6c171

1 file changed

Lines changed: 33 additions & 31 deletions

File tree

  • operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/MDCUtils.java

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.javaoperatorsdk.operator.processing.event.ResourceID;
2323
import io.javaoperatorsdk.operator.processing.event.source.ResourceAction;
2424

25+
@SuppressWarnings("unused")
2526
public class MDCUtils {
2627

2728
private static final String NAME = "resource.name";
@@ -35,34 +36,23 @@ public class MDCUtils {
3536
private static final boolean enabled =
3637
Utils.getBooleanFromSystemPropsOrDefault(Utils.USE_MDC_ENV_KEY, true);
3738

38-
private static final String EVENT_RESOURCE_NAME = "eventsource.event.resource.name";
39-
private static final String EVENT_RESOURCE_UID = "eventsource.event.resource.uid";
40-
private static final String EVENT_RESOURCE_NAMESPACE = "eventsource.event.resource.namespace";
41-
private static final String EVENT_RESOURCE_KIND = "eventsource.event.resource.kind";
42-
private static final String EVENT_RESOURCE_VERSION = "eventsource.event.resource.resourceVersion";
43-
private static final String EVENT_ACTION = "eventsource.event.action";
39+
private static final String EVENT_SOURCE_PREFIX = "eventSource.event.";
40+
private static final String EVENT_ACTION = EVENT_SOURCE_PREFIX + "action";
4441
private static final String EVENT_SOURCE_NAME = "eventsource.name";
42+
private static final String UNKNOWN_ACTION = "unknown action";
4543

4644
public static void addInformerEventInfo(
4745
HasMetadata resource, ResourceAction action, String eventSourceName) {
4846
if (enabled) {
49-
MDC.put(EVENT_RESOURCE_NAME, resource.getMetadata().getName());
50-
MDC.put(EVENT_RESOURCE_NAMESPACE, resource.getMetadata().getNamespace());
51-
MDC.put(EVENT_RESOURCE_KIND, HasMetadata.getKind(resource.getClass()));
52-
MDC.put(EVENT_RESOURCE_VERSION, resource.getMetadata().getResourceVersion());
53-
MDC.put(EVENT_RESOURCE_UID, resource.getMetadata().getUid());
54-
MDC.put(EVENT_ACTION, action == null ? null : action.name());
47+
addResourceInfo(resource, true);
48+
MDC.put(EVENT_ACTION, action == null ? UNKNOWN_ACTION : action.name());
5549
MDC.put(EVENT_SOURCE_NAME, eventSourceName);
5650
}
5751
}
5852

5953
public static void removeInformerEventInfo() {
6054
if (enabled) {
61-
MDC.remove(EVENT_RESOURCE_NAME);
62-
MDC.remove(EVENT_RESOURCE_NAMESPACE);
63-
MDC.remove(EVENT_RESOURCE_KIND);
64-
MDC.remove(EVENT_RESOURCE_VERSION);
65-
MDC.remove(EVENT_RESOURCE_UID);
55+
removeResourceInfo(true);
6656
MDC.remove(EVENT_ACTION);
6757
MDC.remove(EVENT_SOURCE_NAME);
6858
}
@@ -116,33 +106,45 @@ public static void removeResourceIDInfo() {
116106
}
117107

118108
public static void addResourceInfo(HasMetadata resource) {
109+
addResourceInfo(resource, false);
110+
}
111+
112+
public static void addResourceInfo(HasMetadata resource, boolean forEventSource) {
119113
if (enabled) {
120-
MDC.put(API_VERSION, resource.getApiVersion());
121-
MDC.put(KIND, resource.getKind());
114+
MDC.put(key(API_VERSION, forEventSource), resource.getApiVersion());
115+
MDC.put(key(KIND, forEventSource), resource.getKind());
122116
final var metadata = resource.getMetadata();
123117
if (metadata != null) {
124-
MDC.put(NAME, metadata.getName());
118+
MDC.put(key(NAME, forEventSource), metadata.getName());
125119
if (metadata.getNamespace() != null) {
126-
MDC.put(NAMESPACE, metadata.getNamespace());
120+
MDC.put(key(NAMESPACE, forEventSource), metadata.getNamespace());
127121
}
128-
MDC.put(RESOURCE_VERSION, metadata.getResourceVersion());
122+
MDC.put(key(RESOURCE_VERSION, forEventSource), metadata.getResourceVersion());
129123
if (metadata.getGeneration() != null) {
130-
MDC.put(GENERATION, metadata.getGeneration().toString());
124+
MDC.put(key(GENERATION, forEventSource), metadata.getGeneration().toString());
131125
}
132-
MDC.put(UID, metadata.getUid());
126+
MDC.put(key(UID, forEventSource), metadata.getUid());
133127
}
134128
}
135129
}
136130

131+
private static String key(String key, boolean forEventSource) {
132+
return forEventSource ? EVENT_SOURCE_PREFIX + key : key;
133+
}
134+
137135
public static void removeResourceInfo() {
136+
removeResourceInfo(false);
137+
}
138+
139+
public static void removeResourceInfo(boolean forEventSource) {
138140
if (enabled) {
139-
MDC.remove(API_VERSION);
140-
MDC.remove(KIND);
141-
MDC.remove(NAME);
142-
MDC.remove(NAMESPACE);
143-
MDC.remove(RESOURCE_VERSION);
144-
MDC.remove(GENERATION);
145-
MDC.remove(UID);
141+
MDC.remove(key(API_VERSION, forEventSource));
142+
MDC.remove(key(KIND, forEventSource));
143+
MDC.remove(key(NAME, forEventSource));
144+
MDC.remove(key(NAMESPACE, forEventSource));
145+
MDC.remove(key(RESOURCE_VERSION, forEventSource));
146+
MDC.remove(key(GENERATION, forEventSource));
147+
MDC.remove(key(UID, forEventSource));
146148
}
147149
}
148150
}

0 commit comments

Comments
 (0)