Repository navigation
Add support for filtering when listing objects in store.Store - #56
Conversation
|
Note Reviews pausedIt 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 Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (6)
🚧 Files skipped from review as they are similar to previous changes (5)
📝 WalkthroughWalkthroughThis 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. ChangesStore Selector Filtering
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
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
storeutils/host/store_test.go (1)
72-104: ⚡ Quick winExact
HaveLenassertions rely on shared store state.
Listwithout a filter usesBeNumerically(">=", 3)to tolerate objects created by other specs, but the filtered assertions (HaveLen(2/1/3)andBeEmpty()) assume no other spec ever creates objects carryingapp/envlabels. This holds today but is fragile under spec randomization or future tests. Consider addingDeferCleanupto 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
📒 Files selected for processing (4)
storeutils/host/store.gostoreutils/host/store_test.gostoreutils/store/listoptions.gostoreutils/store/store.go
There was a problem hiding this comment.
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 winGuard
labels.NewRequirementerrors inHasLabels.ApplyToList
labels.NewRequirement(key, selection.Exists, nil)can returnnil, errorfor malformed keys, and*reqwill 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
📒 Files selected for processing (4)
storeutils/host/host_suite_test.gostoreutils/host/store.gostoreutils/host/store_test.gostoreutils/store/listoptions.go
🚧 Files skipped from review as they are similar to previous changes (1)
- storeutils/host/store.go
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
storeutils/store/store.go (1)
47-56: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winConsider splitting
IndexFieldinto a separate interface.Bundling
IndexFieldintoStore[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 ofclient.Readerandclient.FieldIndexerwould keep the coreStorecontract 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
📒 Files selected for processing (6)
eventutils/event/event.gostoreutils/host/host_suite_test.gostoreutils/host/store.gostoreutils/host/store_test.gostoreutils/store/listoptions.gostoreutils/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
|
@coderabbitai review |
✅ Action performedReview finished.
|
Included additional feature in this PR (MatchingFields filters) that invalidate the former review.
There was a problem hiding this comment.
🧹 Nitpick comments (1)
storeutils/store/store.go (1)
45-59: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueConsider doc comments for new exported API.
IndexerFunc, the updatedStore.Listsignature, andFieldIndexerare all new/changed public API surface in a shared library module, but none carry GoDoc comments explaining semantics (e.g., whatIndexerFuncshould return, thatListwith no options returns everything, or thatIndexFieldmust be called before referencing a field in aFieldSelector). This last point is a hard runtime contract enforced instoreutils/host/store.go(field selector references unindexed field %qerror) 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
📒 Files selected for processing (6)
eventutils/event/event.gostoreutils/host/host_suite_test.gostoreutils/host/store.gostoreutils/host/store_test.gostoreutils/store/listoptions.gostoreutils/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
|
@coderabbitai resume |
✅ Action performedReviews resumed. |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
storeutils/host/store.go (1)
236-249: 🚀 Performance & Scalability | 🔵 Trivial | 💤 Low valueMinor: recomputes all indexers per object regardless of selector fields.
mergedis built by invoking every registeredIndexerFuncper object even when theFieldSelectoronly 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 everyListcall.♻️ 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
📒 Files selected for processing (6)
eventutils/event/event.gostoreutils/host/host_suite_test.gostoreutils/host/store.gostoreutils/host/store_test.gostoreutils/store/listoptions.gostoreutils/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
| List(ctx context.Context) ([]E, error) | ||
| List(ctx context.Context, opts ...ListOption) ([]E, error) | ||
|
|
||
| Watch(ctx context.Context) (Watch[E], error) |
There was a problem hiding this comment.
I'm wondering if we should add the options here as well
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
If we consider to change it, I would prefer to change the interface once but I have no strong opinion on that.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
🧩 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:
- create an object that matches a filtered watch;
- receive
Created; - update it so it no longer matches;
- receive
Deleted; - verify
Getstill 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 aWatchEventTypeCreated. - 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
WatchEventTypeUpdatedafter 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(notDeleted) 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
WatchEventTypeDeletedwhen 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 metadataset. - Watch interaction: enqueues
Deletedonly 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 buildlistOpts, validates field selector, then includes only objects wheres.matchesOptions(object, *listOpts)is true. - Params/Returns:
List(ctx context.Context, opts ...store.ListOption) ([]E, error) - Errors: returns directory read errors,
Geterrors, 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
listOptsfromWatchparameters, validates field selector, then creates/registers awatch[E]containing theoptsand aneventschannel. - Params/Returns:
Watch(_ context.Context, opts ...store.ListOption) (store.Watch[E], error) - Errors: returns errors from field-selector validation.
- Watch interaction: sets
w.optsand initializesw.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>
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (3)
storeutils/host/store_test.go (2)
111-146: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winMissing symmetrical test:
Listwith an unindexed field selector.
ListandWatchboth callvalidateFieldSelectorbefore proceeding (per the upstreamStore[E].List/Store[E].Watchimplementations). This file covers the unindexed-field error case forWatch(Lines 302-306) but not forList. 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 winRepeated Create+DeferCleanup boilerplate across new tests.
Nearly every new
Itblock repeats the sameCreate(...)+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 | 🔵 TrivialFiltered-out objects are silently dropped, not surfaced as removals.
matchesnow suppresses any event whose object failsmatchesOptions. 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
WatchEventTypeDeletedfor 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
📒 Files selected for processing (7)
eventutils/event/event.gostoreutils/host/host_suite_test.gostoreutils/host/store.gostoreutils/host/store_test.gostoreutils/host/watch.gostoreutils/store/listoptions.gostoreutils/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
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>
|
@coderabbitai review |
✅ Action performedReview finished.
|
Summary
Extend the
Listmethod of thestore.Storeinterface to allow filtering ofobjects. 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.Storefor the new interface.Proposed Changes
store.StoreHasLabels,MatchingLabelsandMatchingFieldsListOptionshost.Storeto support ListOptionsFixes #55
Summary by CodeRabbit
Summary