Skip to content

feat: add a desired state aspect - #3598

Open
xstefank wants to merge 1 commit into
operator-framework:nextfrom
xstefank:aspect-3094-next
Open

feat: add a desired state aspect#3598
xstefank wants to merge 1 commit into
operator-framework:nextfrom
xstefank:aspect-3094-next

Conversation

@xstefank

@xstefank xstefank commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

Fixes #3094

Signed-off-by: xstefank <xstefank122@gmail.com>
Copilot AI lite review requested due to automatic review settings September 4, 2026 12:37
@openshift-ci
openshift-ci Bot requested review from csviri and metacosm September 4, 2026 12:37
@coderabbitai

coderabbitai Bot commented Sep 4, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Team

Run ID: 5b26e75f-a4df-4896-83db-38a770af5825

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

🟡 Changes recommended

The newly added examples/tests assume metadata.labels is always non-null/mutable, which can cause NPEs and should be made null-safe (and documented accordingly).

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Adds a new “desired state aspect” extension point to Java Operator SDK so operators can apply common cross-cutting mutations (e.g., labels/annotations) to all Kubernetes dependent resources’ desired state, and have those mutations participate in matching/update decisions.

Changes:

  • Introduces DesiredStateAspect API and wires it into DefaultContext#getOrComputeDesiredStateFor so aspects run once per dependent per reconciliation (with caching).
  • Extends ConfigurationService / ConfigurationServiceOverrider to configure desired state aspects, and adjusts bulk dependent matching to use the processed desired state.
  • Adds unit + integration tests and documentation describing how to use desired state aspects to apply common metadata.
File summaries
File Description
operator-framework/src/test/java/io/javaoperatorsdk/operator/dependent/desiredstateaspect/DesiredStateAspectReconciler.java Test reconciler wiring a dependent resource for desired-state-aspect integration coverage.
operator-framework/src/test/java/io/javaoperatorsdk/operator/dependent/desiredstateaspect/DesiredStateAspectIT.java Integration test demonstrating global aspects adding labels to all managed resources.
operator-framework/src/test/java/io/javaoperatorsdk/operator/dependent/desiredstateaspect/DesiredStateAspectCustomResource.java Test custom resource used by the desired state aspect sample/IT.
operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/GenericKubernetesResourceMatcherTest.java Updates test context construction to include a mocked configuration service exposing aspects.
operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/dependent/AbstractDependentResourceTest.java Adds coverage ensuring aspects are applied in order and only once due to desired-state caching.
operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/config/ConfigurationServiceOverriderTest.java Adds tests for default/overridden/appended desired state aspects configuration.
operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/BulkDependentResourceReconciler.java Ensures bulk dependent matching uses the same desired-state pipeline (including aspects).
operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/dependent/DesiredStateAspect.java New public functional interface documenting desired state aspects.
operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/DefaultContext.java Applies configured aspects to freshly computed desired state before caching.
operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ConfigurationServiceOverrider.java Adds override/append support for desired state aspects.
operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ConfigurationService.java Adds desiredStateAspects() as a default empty list configuration hook.
docs/content/en/docs/documentation/dependent-resource-and-workflows/dependent-resources.md Documents desired state aspects usage for adding common metadata to managed resources.
Review details

Suppressed comments (1)

operator-framework/src/test/java/io/javaoperatorsdk/operator/dependent/desiredstateaspect/DesiredStateAspectIT.java:65

  • Same issue as above: desired.getMetadata().getLabels() may be null, so this aspect can NPE on resources that don’t already define labels.
                      (desired, dependentResource, context) ->
                          desired
                              .getMetadata()
                              .getLabels()
                              .put(
  • Files reviewed: 12/12 changed files
  • Comments generated: 4
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +132 to +138
(desired, dependentResource, ctx) -> {
assertSame(testDependentResource, dependentResource);
assertSame(primary, ctx.getPrimaryResource());
desired.getMetadata().getLabels().put("aspect", "first");
},
(desired, dependentResource, ctx) ->
desired.getMetadata().getLabels().put("aspect", "second")));
Comment on lines +56 to +60
(desired, dependentResource, context) ->
desired
.getMetadata()
.getLabels()
.put(MANAGED_BY_LABEL_KEY, MANAGED_BY_LABEL_VALUE),
Comment on lines +543 to +546
Operator operator = new Operator(overrider -> overrider
.withDesiredStateAspects(List.of(
(desired, dependentResource, context) -> desired.getMetadata().getLabels()
.put("app.kubernetes.io/managed-by", "my-operator"))));
Comment on lines +38 to +39
* <p>Implementations are expected to mutate the provided desired state in place and must be
* thread-safe as they can be called concurrently for different primary resources.
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