Skip to content

Comments

fix: unify how resource information is added, prevent NPEs#3185

Merged
metacosm merged 4 commits intonextfrom
fix-npe
Feb 25, 2026
Merged

fix: unify how resource information is added, prevent NPEs#3185
metacosm merged 4 commits intonextfrom
fix-npe

Conversation

@metacosm
Copy link
Collaborator

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

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>
Copilot AI review requested due to automatic review settings February 25, 2026 13:36
@metacosm metacosm self-assigned this Feb 25, 2026
@metacosm metacosm requested review from csviri and xstefank February 25, 2026 13:36
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses MDC-related null handling during event/resource logging (Fixes #3183) by centralizing how resource information is written to and removed from MDC, especially for informer event handling where clustered (namespace-less) resources can surface null metadata fields.

Changes:

  • Refactors informer event MDC population to reuse addResourceInfo(...)/removeResourceInfo(...) via a new forEventSource flag.
  • Introduces an event-source key prefixing mechanism and a default "unknown action" value when the action is null.
  • Adjusts removal logic to match the new prefixed key strategy.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 113 to +126
if (enabled) {
MDC.put(API_VERSION, resource.getApiVersion());
MDC.put(KIND, resource.getKind());
MDC.put(key(API_VERSION, forEventSource), resource.getApiVersion());
MDC.put(key(KIND, forEventSource), resource.getKind());
final var metadata = resource.getMetadata();
if (metadata != null) {
MDC.put(NAME, metadata.getName());
MDC.put(key(NAME, forEventSource), metadata.getName());
if (metadata.getNamespace() != null) {
MDC.put(NAMESPACE, metadata.getNamespace());
MDC.put(key(NAMESPACE, forEventSource), metadata.getNamespace());
}
MDC.put(RESOURCE_VERSION, metadata.getResourceVersion());
MDC.put(key(RESOURCE_VERSION, forEventSource), metadata.getResourceVersion());
if (metadata.getGeneration() != null) {
MDC.put(GENERATION, metadata.getGeneration().toString());
MDC.put(key(GENERATION, forEventSource), metadata.getGeneration().toString());
}
MDC.put(UID, metadata.getUid());
MDC.put(key(UID, forEventSource), metadata.getUid());
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change aims to avoid MDC backends that reject nulls, but MDC.put(...) is still called with values that can be null (e.g., resource.getApiVersion(), resource.getKind(), metadata.getName(), metadata.getResourceVersion(), metadata.getUid()). To fully prevent NPEs/IAEs, guard these puts (e.g., only put when non-null, otherwise omit/remove or supply a default).

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that these other values should never be null for "real" resources coming from a live cluster so this should be fine.

Comment on lines 46 to 50
if (enabled) {
MDC.put(EVENT_RESOURCE_NAME, resource.getMetadata().getName());
MDC.put(EVENT_RESOURCE_NAMESPACE, resource.getMetadata().getNamespace());
MDC.put(EVENT_RESOURCE_KIND, HasMetadata.getKind(resource.getClass()));
MDC.put(EVENT_RESOURCE_VERSION, resource.getMetadata().getResourceVersion());
MDC.put(EVENT_RESOURCE_UID, resource.getMetadata().getUid());
MDC.put(EVENT_ACTION, action == null ? null : action.name());
addResourceInfo(resource, true);
MDC.put(EVENT_ACTION, action == null ? UNKNOWN_ACTION : action.name());
MDC.put(EVENT_SOURCE_NAME, eventSourceName);
}
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MDC.put(EVENT_SOURCE_NAME, eventSourceName) will still fail on MDC implementations that don't accept null values if eventSourceName is null. Consider applying the same strategy as for the action (default value) or omitting/removing the key when the name is null.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eventSourceName is never supposed to be null.

import io.javaoperatorsdk.operator.processing.event.ResourceID;
import io.javaoperatorsdk.operator.processing.event.source.ResourceAction;

@SuppressWarnings("unused")
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SuppressWarnings("unused") on the whole utility class is very broad and can hide legitimate unused warnings for newly added members. If this was added to address a specific warning, consider narrowing its scope (or removing it if it's no longer needed).

Suggested change
@SuppressWarnings("unused")

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a utility class that users are expected to use in their own code so having unused methods is fine, imo. And actually, removing such methods would be a breaking API change because, while they might not be used within the context of the project, they might be used in users' projects.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not released we can remove those IMO, not sure were user would use that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You added them so I didn't want to remove them without your agreement since I wasn't sure what their purpose was.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry, my mistake. Feel free to remove it.

metacosm and others added 3 commits February 25, 2026 14:44
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Chris Laprun <metacosm@gmail.com>
Copy link
Collaborator

@csviri csviri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonder if this needs an unit test :)
but LGTM!

thank you!

@metacosm
Copy link
Collaborator Author

metacosm commented Feb 25, 2026

I'm actually not sure how to test that because to be useful, it would need a whole bunch of infrastructure to add an MDC backend and would need to check log messages, which is quite tedious to do, typically and is also dependent on logging backends, so it definitely wouldn't be a unit test, in any case.

@csviri
Copy link
Collaborator

csviri commented Feb 25, 2026

I'm actually not sure how to test that because to be useful, it would need a whole bunch of infrastructure to add an MDC backend and would need to check log messages, which is quite tedious to do, typically and is also dependent on logging backends, so it definitely wouldn't be a unit test, in any case.

There is a get in MDC to return the value, but it is not important IMO, since this is trivial logic mostly

@metacosm
Copy link
Collaborator Author

There is a get in MDC to return the value, but it is not important IMO, since this is trivial logic mostly

Maybe, I'm not familiar with MDC… The issue, though, is that the put is causing the NPE and that seems implementation-dependent so I'm not sure how getting the value would help in this case.

@csviri
Copy link
Collaborator

csviri commented Feb 25, 2026

There is a get in MDC to return the value, but it is not important IMO, since this is trivial logic mostly

Maybe, I'm not familiar with MDC… The issue, though, is that the put is causing the NPE and that seems implementation-dependent so I'm not sure how getting the value would help in this case.

np, I think this is good as it is now.

@metacosm metacosm merged commit 51ee0ff into next Feb 25, 2026
24 of 25 checks passed
@metacosm metacosm deleted the fix-npe branch February 25, 2026 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants