Skip to content

Add support for filtering when listing objects in store.Store - #56

Merged
gonzolino merged 7 commits into
ironcore-dev:mainfrom
gonzolino:enh/extend-store
Aug 3, 2026
Merged

gonzolino merged 7 commits into
ironcore-dev:mainfrom
gonzolino:enh/extend-store

Conversation

@gonzolino

@gonzolino gonzolino commented Jun 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Extend the List method of the store.Store interface to allow filtering of
objects. This allows for native filtering, instead of requiring clients to list
all objects in the store and then filter on the cleint side.

Also extend host.Store for the new interface.

Proposed Changes

  • Add ListOptions to store.Store
  • Implement HasLabels, MatchingLabels and MatchingFields ListOptions
  • Extend host.Store to support ListOptions

Fixes #55

Summary by CodeRabbit

Summary

  • New Features
    • Listing and watching now support variadic selector filtering via list options (label selectors and field selectors).
    • Added helper list options for exact label matches, “has label key” filtering, and field value matching (enabled when the store is configured for field indexing).
  • Bug Fixes
    • Ensures label + field constraints are applied together (AND semantics), including correct watch event scoping and transitions out of scope.
  • Tests
    • Expanded list/watch coverage, including empty-result cases and clear errors for unindexed field filters.
  • Documentation
    • Updated list/watch related signatures to accept the new list options.

@gonzolino
gonzolino requested a review from a team June 2, 2026 11:02
@github-actions github-actions Bot added size/L enhancement New feature or request labels Jun 2, 2026
@coderabbitai

coderabbitai Bot commented Jun 2, 2026 •

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • ✅ Review completed - (🔄 Check again to review again)

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 89cb61f5-da23-4858-9d78-68df3da4ae13

📥 Commits

Reviewing files that changed from the base of the PR and between 6fb95de and 0d334da.

📒 Files selected for processing (6)
  • eventutils/event/event.go
  • storeutils/host/host_suite_test.go
  • storeutils/host/store.go
  • storeutils/host/store_test.go
  • storeutils/host/watch.go
  • storeutils/store/store.go
🚧 Files skipped from review as they are similar to previous changes (5)
  • storeutils/host/store_test.go
  • storeutils/host/watch.go
  • storeutils/store/store.go
  • storeutils/host/host_suite_test.go
  • storeutils/host/store.go

📝 Walkthrough

Walkthrough

This PR adds variadic list and watch options, composable label and field selectors, host-store field indexing, filtered watch events, list-watch option propagation, and tests for selector combinations and invalid fields.

Changes

Store Selector Filtering

Layer / File(s) Summary
List filtering contract and selectors
storeutils/store/store.go, storeutils/store/listoptions.go, eventutils/event/event.go
Store.List and Store.Watch accept variadic options. Label and field selectors compose, and list-watch operations forward configured options.
Host store selector evaluation
storeutils/host/store.go
The host store registers field indexers, validates selectors, and filters list results by label and field matches.
Filtered watch event delivery
storeutils/host/watch.go, storeutils/host/store.go
Watch handlers retain selectors, track matching objects, and emit filtered events including scope-exit deletions.
Selector filtering validation
storeutils/host/host_suite_test.go, storeutils/host/store_test.go
Tests add a field-indexed dummy store and cover label, field, combined, watch, invalid-field, and cleanup behavior.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant HostStore
  participant FieldIndexer
  participant WatchHandler
  Caller->>HostStore: Watch(ctx, MatchingLabels, MatchingFields)
  HostStore->>HostStore: apply and validate selectors
  HostStore->>FieldIndexer: derive indexed field values
  FieldIndexer-->>HostStore: candidate fields
  HostStore->>WatchHandler: store resolved options
  HostStore->>WatchHandler: enqueue matching event
  WatchHandler-->>Caller: filtered watch event
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately summarizes the main change: adding filtering to store.Store listings.
Description check ✅ Passed The description matches the template with Proposed Changes bullets and a Fixes reference, and covers the core change.
Linked Issues check ✅ Passed The PR implements #55 by adding List(ctx, opts ...ListOption) with filtering support and label-based selectors.
Out of Scope Changes check ✅ Passed The additional watch and indexing changes still support the list-filtering feature and do not appear unrelated.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
storeutils/host/store_test.go (1)

72-104: ⚡ Quick win

Exact HaveLen assertions rely on shared store state.

List without a filter uses BeNumerically(">=", 3) to tolerate objects created by other specs, but the filtered assertions (HaveLen(2/1/3) and BeEmpty()) assume no other spec ever creates objects carrying app/env labels. This holds today but is fragile under spec randomization or future tests. Consider adding DeferCleanup to delete the created objects, or scope this test to a dedicated store instance to keep counts deterministic.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@storeutils/host/store_test.go` around lines 72 - 104, The test relies on
exact counts against shared dummyStore state (calls to dummyStore.List with
store.MatchingLabels and store.HasLabels), which makes HaveLen assertions
fragile; modify the test to either create and use a dedicated store instance for
this spec or ensure created objects are removed after the test using
t.DeferCleanup (or Ginkgo’s DeferCleanup) to delete the specific objects you
inserted, so that filtered assertions (Expect(filtered).To(HaveLen(...)) and
Expect(filtered).To(BeEmpty())) are deterministic; update the setup/teardown
around dummyStore, the object creation helpers, and the List calls accordingly
to scope state cleanup.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@storeutils/host/store_test.go`:
- Around line 72-104: The test relies on exact counts against shared dummyStore
state (calls to dummyStore.List with store.MatchingLabels and store.HasLabels),
which makes HaveLen assertions fragile; modify the test to either create and use
a dedicated store instance for this spec or ensure created objects are removed
after the test using t.DeferCleanup (or Ginkgo’s DeferCleanup) to delete the
specific objects you inserted, so that filtered assertions
(Expect(filtered).To(HaveLen(...)) and Expect(filtered).To(BeEmpty())) are
deterministic; update the setup/teardown around dummyStore, the object creation
helpers, and the List calls accordingly to scope state cleanup.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 296e2d3d-9e56-444b-ac8d-061bf204e07e

📥 Commits

Reviewing files that changed from the base of the PR and between fb34fe8 and a085a13.

📒 Files selected for processing (4)
  • storeutils/host/store.go
  • storeutils/host/store_test.go
  • storeutils/store/listoptions.go
  • storeutils/store/store.go

adracus
adracus previously approved these changes Jun 3, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
storeutils/store/listoptions.go (1)

30-37: 🩺 Stability & Availability | 🔴 Critical | ⚡ Quick win

Guard labels.NewRequirement errors in HasLabels.ApplyToList
labels.NewRequirement(key, selection.Exists, nil) can return nil, error for malformed keys, and *req will panic when that happens. Handle the error instead of dereferencing unconditionally.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@storeutils/store/listoptions.go` around lines 30 - 37,
`HasLabels.ApplyToList` currently ignores the error from `labels.NewRequirement`
and blindly dereferences the returned requirement, which can panic on malformed
keys. Update the loop in `HasLabels.ApplyToList` to handle the
`labels.NewRequirement` error explicitly before calling `sel.Add`, and only
append valid requirements to the selector; keep `andSelectors` and
`ListOptions.LabelSelector` behavior unchanged for valid labels.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@storeutils/store/listoptions.go`:
- Around line 47-51: The MatchingFields.ApplyToList method currently overwrites
any existing field selector on ListOptions, which discards earlier filters
applied by other options. Update ApplyToList to merge the new selector with
o.FieldSelector using fields.AndSelectors, matching the composition behavior
used by MatchingLabels and HasLabels, so existing selectors are preserved
instead of replaced.

---

Outside diff comments:
In `@storeutils/store/listoptions.go`:
- Around line 30-37: `HasLabels.ApplyToList` currently ignores the error from
`labels.NewRequirement` and blindly dereferences the returned requirement, which
can panic on malformed keys. Update the loop in `HasLabels.ApplyToList` to
handle the `labels.NewRequirement` error explicitly before calling `sel.Add`,
and only append valid requirements to the selector; keep `andSelectors` and
`ListOptions.LabelSelector` behavior unchanged for valid labels.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: b23350ed-c705-47ac-97ed-5b7159b564ff

📥 Commits

Reviewing files that changed from the base of the PR and between 72c44ab and 088269a.

📒 Files selected for processing (4)
  • storeutils/host/host_suite_test.go
  • storeutils/host/store.go
  • storeutils/host/store_test.go
  • storeutils/store/listoptions.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • storeutils/host/store.go

Comment thread storeutils/store/listoptions.go Resolved

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
storeutils/store/store.go (1)

47-56: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Consider splitting IndexField into a separate interface.

Bundling IndexField into Store[E] forces every implementation (including future ones like the ceph-provider store mentioned in the linked issue) to support field indexing even if they only need label-based filtering. Mirroring controller-runtime's separation of client.Reader and client.FieldIndexer would keep the core Store contract minimal and let implementations opt into field indexing.

♻️ Possible interface split
 type Store[E api.Object] interface {
 	Create(ctx context.Context, obj E) (E, error)
 	Get(ctx context.Context, id string) (E, error)
 	Update(ctx context.Context, obj E) (E, error)
 	Delete(ctx context.Context, id string) error
 	List(ctx context.Context, opts ...ListOption) ([]E, error)

 	Watch(ctx context.Context) (Watch[E], error)
+}
 
-	IndexField(field string, fn FieldIndexerFunc[E])
+type FieldIndexer[E api.Object] interface {
+	IndexField(field string, fn FieldIndexerFunc[E])
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@storeutils/store/store.go` around lines 47 - 56, The Store[E] contract
currently includes IndexField, which forces every implementation to support
field indexing even when it is unnecessary. Split this out of Store[E] in
storeutils/store/store.go by moving IndexField(field string, fn
FieldIndexerFunc[E]) into a separate opt-in interface, and update the affected
Store-related types/usages to depend only on the minimal Store methods unless
field indexing is explicitly required. Use the Store[E] interface and IndexField
method as the key symbols when making the change.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@storeutils/host/store.go`:
- Line 70: The Store indexers map is accessed concurrently without
synchronization, since IndexField mutates indexers while List iterates over it.
Add the same mutex protection used for watches around all indexers reads and
writes in the Store methods, and update IndexField and List to lock/unlock
consistently when touching indexers.
- Around line 205-214: The Store.List field-selector check only rejects
selectors when no indexers exist, so invalid selectors on partially indexed
stores still fall through and behave like no-op filters. Update Store.List to
validate the requested FieldSelector against the registered indexers before
calling Matches(merged), and return a clear error when the selector references
any unindexed field; use the existing List and FieldSelector handling in
Store[E] to keep the validation alongside the current list option merging.

---

Nitpick comments:
In `@storeutils/store/store.go`:
- Around line 47-56: The Store[E] contract currently includes IndexField, which
forces every implementation to support field indexing even when it is
unnecessary. Split this out of Store[E] in storeutils/store/store.go by moving
IndexField(field string, fn FieldIndexerFunc[E]) into a separate opt-in
interface, and update the affected Store-related types/usages to depend only on
the minimal Store methods unless field indexing is explicitly required. Use the
Store[E] interface and IndexField method as the key symbols when making the
change.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f95517af-8cd5-4096-b7ae-ed15a72555ec

📥 Commits

Reviewing files that changed from the base of the PR and between 9059c5b and d9bacb1.

📒 Files selected for processing (6)
  • eventutils/event/event.go
  • storeutils/host/host_suite_test.go
  • storeutils/host/store.go
  • storeutils/host/store_test.go
  • storeutils/store/listoptions.go
  • storeutils/store/store.go
🚧 Files skipped from review as they are similar to previous changes (4)
  • eventutils/event/event.go
  • storeutils/host/host_suite_test.go
  • storeutils/host/store_test.go
  • storeutils/store/listoptions.go

Comment thread storeutils/host/store.go Resolved
Comment thread storeutils/host/store.go Resolved
@gonzolino

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 3, 2026 •

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@gonzolino
gonzolino dismissed adracus’s stale review July 3, 2026 12:43

Included additional feature in this PR (MatchingFields filters) that invalidate the former review.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
storeutils/store/store.go (1)

45-59: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider doc comments for new exported API.

IndexerFunc, the updated Store.List signature, and FieldIndexer are all new/changed public API surface in a shared library module, but none carry GoDoc comments explaining semantics (e.g., what IndexerFunc should return, that List with no options returns everything, or that IndexField must be called before referencing a field in a FieldSelector). This last point is a hard runtime contract enforced in storeutils/host/store.go (field selector references unindexed field %q error) that isn't otherwise documented at the interface level.

📝 Suggested doc comments
+// IndexerFunc extracts an indexable value from an object for a given field.
 type IndexerFunc[E api.Object] func(E) string

 type Store[E api.Object] interface {
 	Create(ctx context.Context, obj E) (E, error)
 	Get(ctx context.Context, id string) (E, error)
 	Update(ctx context.Context, obj E) (E, error)
 	Delete(ctx context.Context, id string) error
+	// List returns objects matching all provided ListOptions. Implementations
+	// may return an error if a FieldSelector references a field that has not
+	// been registered via FieldIndexer.IndexField.
 	List(ctx context.Context, opts ...ListOption) ([]E, error)

 	Watch(ctx context.Context) (Watch[E], error)
 }

+// FieldIndexer allows registering additional indexed fields that can later
+// be used in ListOptions.FieldSelector.
 type FieldIndexer[E api.Object] interface {
 	IndexField(field string, fn IndexerFunc[E])
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@storeutils/store/store.go` around lines 45 - 59, Add GoDoc comments for the
exported API in Store and related types: document IndexerFunc, Store.List, and
FieldIndexer so callers understand their behavior and contracts. In particular,
explain what IndexerFunc should return for a field value, that List with no
options returns all objects, and that IndexField must be registered before any
FieldSelector can reference that field. Use the symbols IndexerFunc, Store.List,
and FieldIndexer/IndexField to place the comments near the interface
definitions.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@storeutils/store/store.go`:
- Around line 45-59: Add GoDoc comments for the exported API in Store and
related types: document IndexerFunc, Store.List, and FieldIndexer so callers
understand their behavior and contracts. In particular, explain what IndexerFunc
should return for a field value, that List with no options returns all objects,
and that IndexField must be registered before any FieldSelector can reference
that field. Use the symbols IndexerFunc, Store.List, and FieldIndexer/IndexField
to place the comments near the interface definitions.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 349d2c8e-c597-44c4-9106-75f2268d3128

📥 Commits

Reviewing files that changed from the base of the PR and between 9059c5b and 3062587.

📒 Files selected for processing (6)
  • eventutils/event/event.go
  • storeutils/host/host_suite_test.go
  • storeutils/host/store.go
  • storeutils/host/store_test.go
  • storeutils/store/listoptions.go
  • storeutils/store/store.go
🚧 Files skipped from review as they are similar to previous changes (5)
  • storeutils/host/store_test.go
  • eventutils/event/event.go
  • storeutils/store/listoptions.go
  • storeutils/host/host_suite_test.go
  • storeutils/host/store.go

@gonzolino

Copy link
Copy Markdown
Contributor Author

@coderabbitai resume

@coderabbitai

coderabbitai Bot commented Jul 5, 2026

Copy link
Copy Markdown
✅ Action performed

Reviews resumed.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
storeutils/host/store.go (1)

236-249: 🚀 Performance & Scalability | 🔵 Trivial | 💤 Low value

Minor: recomputes all indexers per object regardless of selector fields.

merged is built by invoking every registered IndexerFunc per object even when the FieldSelector only references a subset of fields. With few indexers this is negligible, but for stores with many field indexers this adds unnecessary per-object work on every List call.

♻️ Optional: only compute fields referenced by the selector
 		if listOpts.FieldSelector != nil {
 			merged := fields.Set{}
-			for field, fn := range s.indexers {
-				merged[field] = fn(object)
+			for _, req := range listOpts.FieldSelector.Requirements() {
+				if fn, ok := s.indexers[req.Field]; ok {
+					merged[req.Field] = fn(object)
+				}
 			}
 			if !listOpts.FieldSelector.Matches(merged) {
 				continue
 			}
 		}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@storeutils/host/store.go` around lines 236 - 249, In
storeutils/host/store.go, the List path in the field-selector check builds
merged by calling every indexer for every object even when only a few fields are
actually queried. Update the List logic around the FieldSelector handling to
compute only the fields referenced by listOpts.FieldSelector instead of
iterating all entries in s.indexers, while preserving the existing matches
behavior for object.GetLabels() and field matching.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@storeutils/host/store.go`:
- Around line 236-249: In storeutils/host/store.go, the List path in the
field-selector check builds merged by calling every indexer for every object
even when only a few fields are actually queried. Update the List logic around
the FieldSelector handling to compute only the fields referenced by
listOpts.FieldSelector instead of iterating all entries in s.indexers, while
preserving the existing matches behavior for object.GetLabels() and field
matching.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 549995bc-00f2-47db-b70b-b9d0d11fd513

📥 Commits

Reviewing files that changed from the base of the PR and between 3062587 and 0ab4eff.

📒 Files selected for processing (6)
  • eventutils/event/event.go
  • storeutils/host/host_suite_test.go
  • storeutils/host/store.go
  • storeutils/host/store_test.go
  • storeutils/store/listoptions.go
  • storeutils/store/store.go
🚧 Files skipped from review as they are similar to previous changes (4)
  • storeutils/host/store_test.go
  • eventutils/event/event.go
  • storeutils/store/listoptions.go
  • storeutils/host/host_suite_test.go

@maxmoehl maxmoehl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just some nits.

Comment thread eventutils/event/event.go Resolved
Comment thread storeutils/host/store.go Resolved
Comment thread eventutils/event/event.go Resolved
Comment thread storeutils/store/listoptions.go Resolved
Comment thread storeutils/store/store.go Outdated
List(ctx context.Context) ([]E, error)
List(ctx context.Context, opts ...ListOption) ([]E, error)

Watch(ctx context.Context) (Watch[E], error)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm wondering if we should add the options here as well

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That might be a good idea. But I wonder if should do this in a separate PR/feature?

We could also evaluate if we want this for other functions (e.g. in upstream there are also DeleteOptions that are very similar to the ListOptions).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we consider to change it, I would prefer to change the interface once but I have no strong opinion on that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added options to the Watch method. One thing I am unsure about is this finding flagged by @coderabbitai.

I applied the suggested fix and now enqueue will emit a Deleted event if an object no longer matches the filter of the Watch (see here). But I am unsure if this is the right behavior, as the object was deleted from the watch, but the object might still exist in store.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

store_file="$(fd -a '^store\.go$' storeutils/host | head -n1)"
watch_file="$(fd -a '^watch\.go$' storeutils/host | head -n1)"
test_file="$(fd -a '^store_test\.go$' storeutils/host | head -n1)"

printf '%s\n' '--- store structure ---'
ast-grep outline "$store_file" --items all --type function,method --match 'enqueue|matchesOptions|Watch'

printf '%s\n' '--- enqueue and selector logic ---'
rg -n -A80 -B15 'func \(s \*Store\[E\]\) enqueue|func \(s \*Store\[E\]\) matchesOptions|func \(s \*Store\[E\]\) Watch' "$store_file"

printf '%s\n' '--- watch membership logic ---'
cat -n "$watch_file"

printf '%s\n' '--- filtered-watch tests ---'
rg -n -A45 -B10 '\bWatch\s*\(|WatchEventTypeDeleted|MatchingLabels|MatchingFields' "$test_file"

Length of output: 22282


@gonzolino, having verified the implementation: I’d keep the Deleted event for this case.

Store.Watch with selectors should describe membership in the filtered result set, not merely physical object existence. In storeutils/host/store.go, Lines 356-366 already track whether an object was previously delivered to each watch. When an update makes a previously matching object fail the selector, removing it from members and sending WatchEventTypeDeleted correctly tells the consumer to remove that object from its local selected view.

So Deleted means:

  • the object was physically deleted from the store, or
  • the object was removed from this watch’s scope because it no longer matches.

The latter is necessary; otherwise consumers retaining state from watch events would never learn that the object should disappear from their filtered view.

I recommend documenting that WatchEventTypeDeleted has this selector-relative meaning, and adding a regression test for:

  1. create an object that matches a filtered watch;
  2. receive Created;
  3. update it so it no longer matches;
  4. receive Deleted;
  5. verify Get still succeeds, demonstrating that this is a watch-scope removal rather than physical deletion.

A complementary test for an object transitioning from non-matching to matching would also be useful. The current implementation forwards the original Updated event for that transition; deciding whether that should remain Updated or be normalized to Created would make the filtered-watch contract fully explicit.


🧬 Code Graph Analysis Results

Relevant snippets

storeutils/host/store.go (lines 89-120)

func (s *Store[E]) Create(_ context.Context, obj E) (E, error) {
	s.idMu.Lock(obj.GetID())
	defer s.idMu.Unlock(obj.GetID())

	_, err := s.get(obj.GetID())
	switch {
	case err == nil:
		return utils.Zero[E](), fmt.Errorf("object with id %q %w", obj.GetID(), store.ErrAlreadyExists)
	case errors.Is(err, store.ErrNotFound):
	default:
		return utils.Zero[E](), fmt.Errorf("failed to get object with id %q %w", obj.GetID(), err)
	}

	if s.createStrategy != nil {
		s.createStrategy.PrepareForCreate(obj)
	}

	obj.SetCreatedAt(time.Now())
	obj.IncrementResourceVersion()

	obj, err = s.set(obj)
	if err != nil {
		return utils.Zero[E](), err
	}

	s.enqueue(store.WatchEvent[E]{
		Type:   store.WatchEventTypeCreated,
		Object: obj,
	})

	return obj, nil
}
  • Purpose (for watch event behavior): creates an object, persists it, then calls s.enqueue(...) with a WatchEventTypeCreated.
  • Params/Returns: (_ context.Context, obj E) (E, error)
  • Errors: returns errors for already-existing ID, get failures, or persistence/set failures.

storeutils/host/store.go (lines 134-171)

func (s *Store[E]) Update(_ context.Context, obj E) (E, error) {
	s.idMu.Lock(obj.GetID())
	defer s.idMu.Unlock(obj.GetID())

	oldObj, err := s.get(obj.GetID())
	if err != nil {
		return utils.Zero[E](), err
	}

	if obj.GetDeletedAt() != nil && len(obj.GetFinalizers()) == 0 {
		if err := s.delete(obj); err != nil {
			return utils.Zero[E](), fmt.Errorf("failed to delete object metadata: %w", err)
		}
		return obj, nil
	}

	if oldObj.GetResourceVersion() != obj.GetResourceVersion() {
		return utils.Zero[E](), fmt.Errorf("failed to update object: %w", store.ErrResourceVersionNotLatest)
	}

	if reflect.DeepEqual(oldObj, obj) {
		return obj, nil
	}

	obj.IncrementResourceVersion()

	obj, err = s.set(obj)
	if err != nil {
		return utils.Zero[E](), err
	}

	s.enqueue(store.WatchEvent[E]{
		Type:   store.WatchEventTypeUpdated,
		Object: obj,
	})

	return obj, nil
}
  • Purpose (for watch event behavior): updates an object (or final-delete case) and enqueues a WatchEventTypeUpdated after persistence.
  • Params/Returns: (_ context.Context, obj E) (E, error)
  • Errors: returns store.ErrResourceVersionNotLatest (wrapped) for RV mismatch, and persistence/get/delete errors.
  • Watch interaction: only enqueues Updated (not Deleted) in the normal update path.

storeutils/host/store.go (lines 173-204)

func (s *Store[E]) Delete(_ context.Context, id string) error {
	s.idMu.Lock(id)
	defer s.idMu.Unlock(id)

	obj, err := s.get(id)
	if err != nil {
		return err
	}

	if len(obj.GetFinalizers()) == 0 {
		return s.delete(obj)
	}

	if obj.GetDeletedAt() != nil {
		return nil
	}

	now := time.Now()
	obj.SetDeletedAt(&now)
	obj.IncrementResourceVersion()

	if _, err := s.set(obj); err != nil {
		return fmt.Errorf("failed to set object metadata: %w", err)
	}

	s.enqueue(store.WatchEvent[E]{
		Type:   store.WatchEventTypeDeleted,
		Object: obj,
	})

	return nil
}
  • Purpose (for watch event behavior): deletion enqueues WatchEventTypeDeleted when the object has finalizers (i.e., deletion timestamp is set rather than immediate removal).
  • Params/Returns: (_ context.Context, id string) error
  • Errors: returns errors from get, delete, or metadata set.
  • Watch interaction: enqueues Deleted only in the “finalizers present, first delete timestamp set” path.

storeutils/host/store.go (lines 236-271)

func (s *Store[E]) List(ctx context.Context, opts ...store.ListOption) ([]E, error) {
	listOpts := &store.ListOptions{}
	for _, opt := range opts {
		opt.ApplyToList(listOpts)
	}

	if err := s.validateFieldSelector(*listOpts); err != nil {
		return nil, err
	}

	entries, err := os.ReadDir(s.dir)
	if err != nil {
		return nil, fmt.Errorf("failed to list objects: %w", err)
	}

	//nolint:prealloc
	var objs []E
	for _, entry := range entries {
		if entry.IsDir() {
			continue
		}

		object, err := s.Get(ctx, entry.Name())
		if err != nil {
			return nil, fmt.Errorf("failed to read object: %w", err)
		}

		if !s.matchesOptions(object, *listOpts) {
			continue
		}

		objs = append(objs, object)
	}

	return objs, nil
}
  • Purpose (for watch filter semantics): applies ListOptions to build listOpts, validates field selector, then includes only objects where s.matchesOptions(object, *listOpts) is true.
  • Params/Returns: List(ctx context.Context, opts ...store.ListOption) ([]E, error)
  • Errors: returns directory read errors, Get errors, or field-selector validation errors.

storeutils/host/store.go (lines 273-296)

func (s *Store[E]) Watch(_ context.Context, opts ...store.ListOption) (store.Watch[E], error) {
	listOpts := &store.ListOptions{}
	for _, opt := range opts {
		opt.ApplyToList(listOpts)
	}

	if err := s.validateFieldSelector(*listOpts); err != nil {
		return nil, err
	}

	s.watchesMu.Lock()
	defer s.watchesMu.Unlock()

	w := &watch[E]{
		store:   s,
		events:  make(chan store.WatchEvent[E], s.watchBufferSize),
		opts:    *listOpts,
		members: sets.New[string](),
	}

	s.watches.Insert(w)

	return w, nil
}
  • Purpose (for watch options): builds listOpts from Watch parameters, validates field selector, then creates/registers a watch[E] containing the opts and an events channel.
  • Params/Returns: Watch(_ context.Context, opts ...store.ListOption) (store.Watch[E], error)
  • Errors: returns errors from field-selector validation.
  • Watch interaction: sets w.opts and initializes w.members (tracking of watched matches).

storeutils/host/watch.go (lines 23-28)

func (w *watch[E]) Stop() {
	w.store.watchesMu.Lock()
	defer w.store.watchesMu.Unlock()

	w.store.watches.Delete(w)
}
  • Purpose: stops the watch by removing it from the store’s watch set.
  • Params/Returns: Stop()
  • Errors: none.

storeutils/host/watch.go (lines 30-32)

func (w *watch[E]) Events() <-chan store.WatchEvent[E] {
	return w.events
}
  • Purpose: returns the read-only channel of watch events.
  • Params/Returns: Events() <-chan store.WatchEvent[E]
  • Errors: none.

apiutils/api/api.go (lines 85-103)

Object interface {
	GetID() string
	GetAnnotations() map[string]string
	GetLabels() map[string]string
	GetCreatedAt() time.Time
	GetDeletedAt() *time.Time
	GetGeneration() int64
	GetFinalizers() []string
	GetResourceVersion() uint64

	SetID(id string)
	SetAnnotations(annotations map[string]string)
	SetLabels(labels map[string]string)
	SetCreatedAt(createdAt time.Time)
	SetDeletedAt(deleted *time.Time)
	SetGeneration(generation int64)
	SetFinalizers(finalizers []string)
	IncrementResourceVersion()
}
  • Purpose (for matching/filtering and deletion semantics): defines the object fields/mutators the store/watch logic may use (ID, deleted timestamp, finalizers, resourceVersion, etc.).
  • Params/Returns/Errors: interface only.

Allow filtering results in the `store.Store` `List(ctx)` method by
adding a new `ListOption` parameter.

Signed-off-by: Daniel Gonzalez Nothnagel <daniel.gonzalez.nothnagel@sap.com>
These ListOptions allow to filter objects based on their labels.

Signed-off-by: Daniel Gonzalez Nothnagel <daniel.gonzalez.nothnagel@sap.com>
This ListOptions allows to filter objects based on their fields.

Signed-off-by: Daniel Gonzalez Nothnagel <daniel.gonzalez.nothnagel@sap.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (3)
storeutils/host/store_test.go (2)

111-146: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Missing symmetrical test: List with an unindexed field selector.

List and Watch both call validateFieldSelector before proceeding (per the upstream Store[E].List/Store[E].Watch implementations). This file covers the unindexed-field error case for Watch (Lines 302-306) but not for List. Consider adding a parallel negative test here for coverage symmetry.

✅ Example test
It("should return an error when List references an unindexed field", func(ctx SpecContext) {
	_, err := dummyStoreField.List(ctx, store.MatchingFields{"nonexistent": "value"})
	Expect(err).To(HaveOccurred())
	Expect(err.Error()).To(ContainSubstring("unindexed field"))
})
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@storeutils/host/store_test.go` around lines 111 - 146, Add a negative test
alongside the existing MatchingFields List test that calls dummyStoreField.List
with an unindexed field such as "nonexistent", then assert an error occurs and
its message contains "unindexed field". Keep the existing indexed-field
filtering assertions unchanged.

46-301: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Repeated Create+DeferCleanup boilerplate across new tests.

Nearly every new It block repeats the same Create(...) + DeferCleanup(store.Delete, ctx, id) pattern for 2-3 objects. A small helper (e.g. createDummy(ctx, store, id, labels, spec) that also registers the cleanup) would reduce repetition across these ~10 call sites and make future test additions easier.

♻️ Example helper
func createDummy(ctx SpecContext, s *host.Store[*Dummy], id string, labels map[string]string, spec string) *Dummy {
	obj, err := s.Create(ctx, &Dummy{
		Metadata: api.Metadata{ID: id, Labels: labels},
		Spec:     spec,
	})
	Expect(err).NotTo(HaveOccurred())
	DeferCleanup(s.Delete, ctx, id)
	return obj
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@storeutils/host/store_test.go` around lines 46 - 301, Introduce a shared
createDummy test helper near the existing test utilities that accepts the
context, store, ID, labels, and spec, creates the Dummy, asserts creation
succeeds, registers DeferCleanup with the store’s Delete method, and returns the
created object. Replace the repeated Create plus DeferCleanup blocks in the new
label, field, combined-selector, and Watch tests with this helper while
preserving each object’s metadata and spec values.
storeutils/host/watch.go (1)

14-18: 🗄️ Data Integrity & Integration | 🔵 Trivial

Filtered-out objects are silently dropped, not surfaced as removals.

matches now suppresses any event whose object fails matchesOptions. If an object initially matches a watch's selector and is later updated so it no longer matches, the consumer never receives a signal that the object left its filtered view — it just stops seeing updates for an ID it may still be caching. For consumers that build an in-memory index/cache from watch events (informer-style), this can leave stale entries behind indefinitely.

Worth confirming this is acceptable for current consumers (e.g., ceph-provider), or whether such transitions should emit a synthetic WatchEventTypeDeleted for watchers whose filter the object no longer satisfies.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@storeutils/host/watch.go` around lines 14 - 18, Update the watch event
filtering around watch.matches so an object transitioning from matching to
non-matching emits a synthetic WatchEventTypeDeleted event. Track or consult the
object’s prior match state to distinguish this transition; continue suppressing
objects that never matched and preserve normal events for objects that still
satisfy store.matchesOptions.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@storeutils/host/store.go`:
- Around line 350-353: Update the watch event filtering around handler.matches
so it tracks each object’s prior membership for every watch handler. When an
update transitions an object from matching to non-matching, emit the appropriate
removal event instead of continuing silently; preserve normal enqueue behavior
for matching objects and avoid removal events when the object was not previously
a member.

---

Nitpick comments:
In `@storeutils/host/store_test.go`:
- Around line 111-146: Add a negative test alongside the existing MatchingFields
List test that calls dummyStoreField.List with an unindexed field such as
"nonexistent", then assert an error occurs and its message contains "unindexed
field". Keep the existing indexed-field filtering assertions unchanged.
- Around line 46-301: Introduce a shared createDummy test helper near the
existing test utilities that accepts the context, store, ID, labels, and spec,
creates the Dummy, asserts creation succeeds, registers DeferCleanup with the
store’s Delete method, and returns the created object. Replace the repeated
Create plus DeferCleanup blocks in the new label, field, combined-selector, and
Watch tests with this helper while preserving each object’s metadata and spec
values.

In `@storeutils/host/watch.go`:
- Around line 14-18: Update the watch event filtering around watch.matches so an
object transitioning from matching to non-matching emits a synthetic
WatchEventTypeDeleted event. Track or consult the object’s prior match state to
distinguish this transition; continue suppressing objects that never matched and
preserve normal events for objects that still satisfy store.matchesOptions.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 60a9a725-f54f-4d2f-9ccd-dff427673e95

📥 Commits

Reviewing files that changed from the base of the PR and between c38e7b8 and 6fb95de.

📒 Files selected for processing (7)
  • eventutils/event/event.go
  • storeutils/host/host_suite_test.go
  • storeutils/host/store.go
  • storeutils/host/store_test.go
  • storeutils/host/watch.go
  • storeutils/store/listoptions.go
  • storeutils/store/store.go
🚧 Files skipped from review as they are similar to previous changes (3)
  • storeutils/store/store.go
  • storeutils/host/host_suite_test.go
  • storeutils/store/listoptions.go

Comment thread storeutils/host/store.go Resolved
Signed-off-by: Daniel Gonzalez Nothnagel <daniel.gonzalez.nothnagel@sap.com>
Signed-off-by: Daniel Gonzalez Nothnagel <daniel.gonzalez.nothnagel@sap.com>
Reuse `ListOptions` introduced to allow native filtering in
`List(ctx, opts)` for `Watch(ctx, opts)` to allow watches to filter
events.

Signed-off-by: Daniel Gonzalez Nothnagel <daniel.gonzalez.nothnagel@sap.com>
Signed-off-by: Daniel Gonzalez Nothnagel <daniel.gonzalez.nothnagel@sap.com>
@gonzolino

gonzolino commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 22, 2026 •

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@gonzolino
gonzolino merged commit aa1a26a into ironcore-dev:main Aug 3, 2026
8 checks passed
@gonzolino
gonzolino deleted the enh/extend-store branch August 4, 2026 08:40
@hardikdr hardikdr added the area/iaas Issues related to IronCore IaaS development. label Aug 21, 2026
@hardikdr hardikdr added this to Roadmap Aug 22, 2026
@github-project-automation github-project-automation Bot moved this to Done in Roadmap Aug 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/iaas Issues related to IronCore IaaS development. enhancement New feature or request size/L

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Support filtering in store.Store.List(ctx)

5 participants