@@ -87,6 +87,12 @@ func New(localMgr manager.Manager, opts Options) (*Provider, error) {
8787 return p , nil
8888}
8989
90+ type index struct {
91+ object client.Object
92+ field string
93+ extractValue client.IndexerFunc
94+ }
95+
9096// Provider is a cluster Provider that works with Datum
9197type Provider struct {
9298 opts Options
@@ -98,6 +104,7 @@ type Provider struct {
98104 mcMgr mcmanager.Manager
99105 projects map [string ]cluster.Cluster
100106 cancelFns map [string ]context.CancelFunc
107+ indexers []index
101108}
102109
103110// Get returns the cluster with the given name, if it is known.
@@ -206,11 +213,11 @@ func (p *Provider) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result
206213 if err != nil {
207214 return ctrl.Result {}, fmt .Errorf ("failed to create cluster: %w" , err )
208215 }
209- // for _, idx := range p.indexers {
210- // if err := cl.GetCache().IndexField(ctx, idx.object, idx.field, idx.extractValue); err != nil {
211- // return ctrl.Result{}, fmt.Errorf("failed to index field %q: %w", idx.field, err)
212- // }
213- // }
216+ for _ , idx := range p .indexers {
217+ if err := cl .GetCache ().IndexField (ctx , idx .object , idx .field , idx .extractValue ); err != nil {
218+ return ctrl.Result {}, fmt .Errorf ("failed to index field %q: %w" , idx .field , err )
219+ }
220+ }
214221
215222 clusterCtx , cancel := context .WithCancel (ctx )
216223 go func () {
@@ -243,7 +250,22 @@ func (p *Provider) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result
243250}
244251
245252func (p * Provider ) IndexField (ctx context.Context , obj client.Object , field string , extractValue client.IndexerFunc ) error {
246- // TODO(jreese)
253+ p .lock .Lock ()
254+ defer p .lock .Unlock ()
255+
256+ // save for future projects.
257+ p .indexers = append (p .indexers , index {
258+ object : obj ,
259+ field : field ,
260+ extractValue : extractValue ,
261+ })
262+
263+ // apply to existing projects.
264+ for name , cl := range p .projects {
265+ if err := cl .GetCache ().IndexField (ctx , obj , field , extractValue ); err != nil {
266+ return fmt .Errorf ("failed to index field %q on project %q: %w" , field , name , err )
267+ }
268+ }
247269 return nil
248270}
249271
0 commit comments