@@ -149,7 +149,7 @@ type ClusterCache interface {
149
149
// During a disconnect existing watches (i.e. informers) are shutdown when stopping the cache.
150
150
// After a re-connect watches will be re-added (assuming the Watch method is called again).
151
151
// If there is no connection to the workload cluster ErrClusterNotConnected will be returned.
152
- Watch (ctx context.Context , cluster client.ObjectKey , input WatchInput ) error
152
+ Watch (ctx context.Context , cluster client.ObjectKey , watcher Watcher ) error
153
153
154
154
// GetLastProbeSuccessTimestamp returns the time when the health probe was successfully executed last.
155
155
GetLastProbeSuccessTimestamp (ctx context.Context , cluster client.ObjectKey ) time.Time
@@ -169,34 +169,74 @@ type ClusterCache interface {
169
169
// because there is no connection to the workload cluster.
170
170
var ErrClusterNotConnected = errors .New ("connection to the workload cluster is down" )
171
171
172
- // Watcher is a scoped-down interface from Controller that only has the Watch func .
172
+ // Watcher is an interface that can start a Watch.
173
173
type Watcher interface {
174
- // Watch watches the provided Source.
175
- Watch (src source.Source ) error
174
+ Name () string
175
+ Object () client.Object
176
+ Watch (cache cache.Cache ) error
176
177
}
177
178
178
- // WatchInput specifies the parameters used to establish a new watch for a workload cluster.
179
- // A source.Kind source (configured with Kind, EventHandler and Predicates) will be added to the Watcher.
180
- // To watch for events, the source.Kind will create an informer on the Cache that we have created and cached
179
+ // SourceWatcher is a scoped-down interface from Controller that only has the Watch func.
180
+ type SourceWatcher [request comparable ] interface {
181
+ Watch (src source.TypedSource [request ]) error
182
+ }
183
+
184
+ // WatcherOptions specifies the parameters used to establish a new watch for a workload cluster.
185
+ // A source.TypedKind source (configured with Kind, TypedEventHandler and Predicates) will be added to the Watcher.
186
+ // To watch for events, the source.TypedKind will create an informer on the Cache that we have created and cached
187
+ // for the given Cluster.
188
+ type WatcherOptions = TypedWatcherOptions [client.Object , ctrl.Request ]
189
+
190
+ // TypedWatcherOptions specifies the parameters used to establish a new watch for a workload cluster.
191
+ // A source.TypedKind source (configured with Kind, TypedEventHandler and Predicates) will be added to the Watcher.
192
+ // To watch for events, the source.TypedKind will create an informer on the Cache that we have created and cached
181
193
// for the given Cluster.
182
- type WatchInput struct {
194
+ type TypedWatcherOptions [ object client. Object , request comparable ] struct {
183
195
// Name represents a unique Watch request for the specified Cluster.
184
196
// The name is used to track that a specific watch is only added once to a cache.
185
197
// After a connection (and thus also the cache) has been re-created, watches have to be added
186
198
// again by calling the Watch method again.
187
199
Name string
188
200
189
201
// Watcher is the watcher (controller) whose Reconcile() function will be called for events.
190
- Watcher Watcher
202
+ Watcher SourceWatcher [ request ]
191
203
192
204
// Kind is the type of resource to watch.
193
- Kind client. Object
205
+ Kind object
194
206
195
207
// EventHandler contains the event handlers to invoke for resource events.
196
- EventHandler handler.EventHandler
208
+ EventHandler handler.TypedEventHandler [ object , request ]
197
209
198
210
// Predicates is used to filter resource events.
199
- Predicates []predicate.Predicate
211
+ Predicates []predicate.TypedPredicate [object ]
212
+ }
213
+
214
+ // NewWatcher creates a Watcher for the workload cluster.
215
+ // A source.TypedKind source (configured with Kind, TypedEventHandler and Predicates) will be added to the SourceWatcher.
216
+ // To watch for events, the source.TypedKind will create an informer on the Cache that we have created and cached
217
+ // for the given Cluster.
218
+ func NewWatcher [object client.Object , request comparable ](options TypedWatcherOptions [object , request ]) Watcher {
219
+ return & watcher [object , request ]{
220
+ name : options .Name ,
221
+ kind : options .Kind ,
222
+ eventHandler : options .EventHandler ,
223
+ predicates : options .Predicates ,
224
+ watcher : options .Watcher ,
225
+ }
226
+ }
227
+
228
+ type watcher [object client.Object , request comparable ] struct {
229
+ name string
230
+ kind object
231
+ eventHandler handler.TypedEventHandler [object , request ]
232
+ predicates []predicate.TypedPredicate [object ]
233
+ watcher SourceWatcher [request ]
234
+ }
235
+
236
+ func (tw * watcher [object , request ]) Name () string { return tw .name }
237
+ func (tw * watcher [object , request ]) Object () client.Object { return tw .kind }
238
+ func (tw * watcher [object , request ]) Watch (cache cache.Cache ) error {
239
+ return tw .watcher .Watch (source .TypedKind [object , request ](cache , tw .kind , tw .eventHandler , tw .predicates ... ))
200
240
}
201
241
202
242
// GetClusterSourceOption is an option that modifies GetClusterSourceOptions for a GetClusterSource call.
@@ -342,12 +382,12 @@ func (cc *clusterCache) GetClientCertificatePrivateKey(ctx context.Context, clus
342
382
return accessor .GetClientCertificatePrivateKey (ctx ), nil
343
383
}
344
384
345
- func (cc * clusterCache ) Watch (ctx context.Context , cluster client.ObjectKey , input WatchInput ) error {
385
+ func (cc * clusterCache ) Watch (ctx context.Context , cluster client.ObjectKey , watcher Watcher ) error {
346
386
accessor := cc .getClusterAccessor (cluster )
347
387
if accessor == nil {
348
- return errors .Wrapf (ErrClusterNotConnected , "error creating watch %s for %T" , input .Name , input . Kind )
388
+ return errors .Wrapf (ErrClusterNotConnected , "error creating watch %s for %T" , watcher .Name (), watcher . Object () )
349
389
}
350
- return accessor .Watch (ctx , input )
390
+ return accessor .Watch (ctx , watcher )
351
391
}
352
392
353
393
func (cc * clusterCache ) GetLastProbeSuccessTimestamp (ctx context.Context , cluster client.ObjectKey ) time.Time {
0 commit comments