Skip to content

Commit 49ae642

Browse files
committed
review nits
1 parent d8ebc45 commit 49ae642

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

providers/file/provider.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,18 @@ type Provider struct {
136136

137137
log logr.Logger
138138

139-
clustersLock sync.RWMutex
139+
lock sync.RWMutex
140140
aware multicluster.Aware
141141
clusters map[string]cluster.Cluster
142142
clusterCancel map[string]func()
143143
}
144144

145145
// Start starts the provider and updates the clusters and is blocking.
146146
func (p *Provider) Start(ctx context.Context, aware multicluster.Aware) error {
147+
p.lock.Lock()
148+
defer p.lock.Unlock()
147149
p.aware = aware
150+
148151
if err := p.run(ctx); err != nil {
149152
return fmt.Errorf("initial update failed: %w", err)
150153
}
@@ -202,10 +205,10 @@ func (p *Provider) RunOnce(ctx context.Context) error {
202205
func (p *Provider) addCluster(ctx context.Context, name string, cl cluster.Cluster) {
203206
ctx, cancel := context.WithCancel(ctx)
204207

205-
p.clustersLock.Lock()
208+
p.lock.Lock()
206209
p.clusters[name] = cl
207210
p.clusterCancel[name] = cancel
208-
p.clustersLock.Unlock()
211+
p.lock.Unlock()
209212

210213
go func() {
211214
if err := cl.Start(ctx); err != nil {
@@ -223,8 +226,8 @@ func (p *Provider) addCluster(ctx context.Context, name string, cl cluster.Clust
223226
}
224227

225228
func (p *Provider) removeCluster(name string) {
226-
p.clustersLock.Lock()
227-
defer p.clustersLock.Unlock()
229+
p.lock.Lock()
230+
defer p.lock.Unlock()
228231

229232
if cancel, ok := p.clusterCancel[name]; ok {
230233
cancel()
@@ -276,8 +279,8 @@ func (p *Provider) run(ctx context.Context) error {
276279
// If the cluster name is empty (""), it returns the first cluster
277280
// found.
278281
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()
281284

282285
if clusterName == "" {
283286
for _, cl := range p.clusters {
@@ -294,8 +297,8 @@ func (p *Provider) Get(_ context.Context, clusterName string) (cluster.Cluster,
294297

295298
// IndexField indexes a field on all clusters.
296299
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()
299302

300303
for name, cl := range p.clusters {
301304
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
307310

308311
// ClusterNames returns the names of all clusters known to the provider.
309312
func (p *Provider) ClusterNames() []string {
310-
p.clustersLock.RLock()
311-
defer p.clustersLock.RUnlock()
313+
p.lock.RLock()
314+
defer p.lock.RUnlock()
312315
return slices.Sorted(maps.Keys(p.clusters))
313316
}

providers/namespace/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ func New(cl cluster.Cluster) *Provider {
6262

6363
// Start starts the provider and blocks.
6464
func (p *Provider) Start(ctx context.Context, aware multicluster.Aware) error {
65+
p.lock.Lock()
6566
p.aware = aware
67+
p.lock.Unlock()
6668

6769
nsInf, err := p.cluster.GetCache().GetInformer(ctx, &corev1.Namespace{})
6870
if err != nil {

providers/single/provider.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ var _ multicluster.Provider = &Provider{}
3030

3131
// Provider is a provider that engages the passed cluster.
3232
type Provider struct {
33-
name string
34-
cl cluster.Cluster
35-
aware multicluster.Aware
33+
name string
34+
cl cluster.Cluster
3635
}
3736

3837
// New returns a provider engaging the passed cluster under the given name.
@@ -49,9 +48,8 @@ func (p *Provider) Start(ctx context.Context, aware multicluster.Aware) error {
4948
if aware == nil {
5049
return fmt.Errorf("manager is not set")
5150
}
52-
p.aware = aware
5351

54-
if err := p.aware.Engage(ctx, p.name, p.cl); err != nil {
52+
if err := aware.Engage(ctx, p.name, p.cl); err != nil {
5553
return err
5654
}
5755
<-ctx.Done()

0 commit comments

Comments
 (0)