Skip to content

Commit 228c6d1

Browse files
committed
Add an optional ForOptions field to the ResourceReconciler
1 parent 601a369 commit 228c6d1

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

reconcilers/resource.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ type ResourceReconciler[Type client.Object] struct {
126126
// +optional
127127
SkipResource func(ctx context.Context, resource Type) bool
128128

129+
// ForOptions is called to build predicates/options passed to the builder's For method.
130+
//
131+
// +optional
132+
ForOptions func() ([]builder.ForOption, error)
133+
129134
Config Config
130135

131136
lazyInit sync.Once
@@ -137,6 +142,11 @@ func (r *ResourceReconciler[T]) init() {
137142
var nilT T
138143
r.Type = newEmpty(nilT).(T)
139144
}
145+
if r.ForOptions == nil {
146+
r.ForOptions = func() ([]builder.ForOption, error) {
147+
return nil, nil
148+
}
149+
}
140150
if r.Name == "" {
141151
r.Name = fmt.Sprintf("%sResourceReconciler", typeName(r.Type))
142152
}
@@ -189,9 +199,14 @@ func (r *ResourceReconciler[T]) SetupWithManagerYieldingController(ctx context.C
189199
return nil, err
190200
}
191201

202+
forOptions, err := r.ForOptions()
203+
if err != nil {
204+
return nil, err
205+
}
206+
192207
bldr := ctrl.NewControllerManagedBy(mgr)
193208
if !duck.IsDuck(r.Type, r.Config.Scheme()) {
194-
bldr.For(r.Type)
209+
bldr.For(r.Type, forOptions...)
195210
} else {
196211
gvk, err := r.Config.GroupVersionKindFor(r.Type)
197212
if err != nil {
@@ -201,7 +216,7 @@ func (r *ResourceReconciler[T]) SetupWithManagerYieldingController(ctx context.C
201216
u := &unstructured.Unstructured{}
202217
u.SetAPIVersion(apiVersion)
203218
u.SetKind(kind)
204-
bldr.For(u)
219+
bldr.For(u, forOptions...)
205220
}
206221

207222
ctx = r.withContext(ctx)

0 commit comments

Comments
 (0)