Skip to content

fix: use the null-guarded metrics field in the Controller constructor - #3531

Draft
csviri wants to merge 1 commit into
operator-framework:mainfrom
csviri:fix/controller-null-metrics-guard
Draft

fix: use the null-guarded metrics field in the Controller constructor#3531
csviri wants to merge 1 commit into
operator-framework:mainfrom
csviri:fix/controller-null-metrics-guard

Conversation

@csviri

@csviri csviri commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

The constructor deliberately guards against a null Metrics:

this.metrics = Optional.ofNullable(configurationService.getMetrics()).orElse(Metrics.NOOP);

but its last statement bypasses that guard and calls the getter again:

configurationService.getMetrics().controllerRegistered(this);

so a ConfigurationService whose getMetrics() returns null fails with a
NullPointerException during controller construction, which is exactly what
the guard three lines up is meant to prevent. EventProcessor applies the
same != null ? metrics : Metrics.NOOP guard, so the codebase does treat
a null Metrics as reachable even though the interface default returns
Metrics.NOOP.

Uses the already-resolved metrics field instead.

Part of #3517

The constructor deliberately guards against a null `Metrics`:

    this.metrics = Optional.ofNullable(configurationService.getMetrics()).orElse(Metrics.NOOP);

but its last statement bypasses that guard and calls the getter again:

    configurationService.getMetrics().controllerRegistered(this);

so a `ConfigurationService` whose `getMetrics()` returns null fails with a
NullPointerException during controller construction, which is exactly what
the guard three lines up is meant to prevent. `EventProcessor` applies the
same `!= null ? metrics : Metrics.NOOP` guard, so the codebase does treat
a null `Metrics` as reachable even though the interface default returns
`Metrics.NOOP`.

Uses the already-resolved `metrics` field instead.
Copilot AI review requested due to automatic review settings July 30, 2026 09:05
@openshift-ci openshift-ci Bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jul 30, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes a controller-construction correctness bug in operator-framework-core where the constructor would bypass the null-guarded metrics field and re-fetch Metrics from ConfigurationService, causing a potential NullPointerException when getMetrics() returns null.

Changes:

  • Use the already null-guarded metrics field to call controllerRegistered(this) instead of calling configurationService.getMetrics() again.

configuration.getResourceClass());
initAndRegisterEventSources(eventSourceContext);
configurationService.getMetrics().controllerRegistered(this);
metrics.controllerRegistered(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants