@@ -136,15 +136,18 @@ type Provider struct {
136
136
137
137
log logr.Logger
138
138
139
- clustersLock sync.RWMutex
139
+ lock sync.RWMutex
140
140
aware multicluster.Aware
141
141
clusters map [string ]cluster.Cluster
142
142
clusterCancel map [string ]func ()
143
143
}
144
144
145
145
// Start starts the provider and updates the clusters and is blocking.
146
146
func (p * Provider ) Start (ctx context.Context , aware multicluster.Aware ) error {
147
+ p .lock .Lock ()
148
+ defer p .lock .Unlock ()
147
149
p .aware = aware
150
+
148
151
if err := p .run (ctx ); err != nil {
149
152
return fmt .Errorf ("initial update failed: %w" , err )
150
153
}
@@ -202,10 +205,10 @@ func (p *Provider) RunOnce(ctx context.Context) error {
202
205
func (p * Provider ) addCluster (ctx context.Context , name string , cl cluster.Cluster ) {
203
206
ctx , cancel := context .WithCancel (ctx )
204
207
205
- p .clustersLock .Lock ()
208
+ p .lock .Lock ()
206
209
p .clusters [name ] = cl
207
210
p .clusterCancel [name ] = cancel
208
- p .clustersLock .Unlock ()
211
+ p .lock .Unlock ()
209
212
210
213
go func () {
211
214
if err := cl .Start (ctx ); err != nil {
@@ -223,8 +226,8 @@ func (p *Provider) addCluster(ctx context.Context, name string, cl cluster.Clust
223
226
}
224
227
225
228
func (p * Provider ) removeCluster (name string ) {
226
- p .clustersLock .Lock ()
227
- defer p .clustersLock .Unlock ()
229
+ p .lock .Lock ()
230
+ defer p .lock .Unlock ()
228
231
229
232
if cancel , ok := p .clusterCancel [name ]; ok {
230
233
cancel ()
@@ -276,8 +279,8 @@ func (p *Provider) run(ctx context.Context) error {
276
279
// If the cluster name is empty (""), it returns the first cluster
277
280
// found.
278
281
func (p * Provider ) Get (_ context.Context , clusterName string ) (cluster.Cluster , error ) {
279
- p .clustersLock .RLock ()
280
- defer p .clustersLock .RUnlock ()
282
+ p .lock .RLock ()
283
+ defer p .lock .RUnlock ()
281
284
282
285
if clusterName == "" {
283
286
for _ , cl := range p .clusters {
@@ -294,8 +297,8 @@ func (p *Provider) Get(_ context.Context, clusterName string) (cluster.Cluster,
294
297
295
298
// IndexField indexes a field on all clusters.
296
299
func (p * Provider ) IndexField (ctx context.Context , obj client.Object , field string , extractValue client.IndexerFunc ) error {
297
- p .clustersLock .RLock ()
298
- defer p .clustersLock .RUnlock ()
300
+ p .lock .RLock ()
301
+ defer p .lock .RUnlock ()
299
302
300
303
for name , cl := range p .clusters {
301
304
if err := cl .GetCache ().IndexField (ctx , obj , field , extractValue ); err != nil {
@@ -307,7 +310,7 @@ func (p *Provider) IndexField(ctx context.Context, obj client.Object, field stri
307
310
308
311
// ClusterNames returns the names of all clusters known to the provider.
309
312
func (p * Provider ) ClusterNames () []string {
310
- p .clustersLock .RLock ()
311
- defer p .clustersLock .RUnlock ()
313
+ p .lock .RLock ()
314
+ defer p .lock .RUnlock ()
312
315
return slices .Sorted (maps .Keys (p .clusters ))
313
316
}
0 commit comments