Skip to content

Commit d8ebc45

Browse files
committed
try 2: runnable provider
1 parent 93f2c78 commit d8ebc45

File tree

17 files changed

+59
-141
lines changed

17 files changed

+59
-141
lines changed

examples/cluster-api/main.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,6 @@ func main() {
9292
os.Exit(1)
9393
}
9494

95-
if err := provider.SetupWithManager(mcMgr); err != nil {
96-
entryLog.Error(err, "unable to set up provider")
97-
os.Exit(1)
98-
}
99-
10095
// Create a configmap controller in the multi-cluster manager.
10196
if err := mcbuilder.ControllerManagedBy(mcMgr).
10297
Named("multicluster-configmaps").
@@ -134,7 +129,7 @@ func main() {
134129
return ignoreCanceled(localMgr.Start(ctx))
135130
})
136131
g.Go(func() error {
137-
return ignoreCanceled(provider.Run(ctx))
132+
return ignoreCanceled(provider.Start(ctx, mcMgr))
138133
})
139134
g.Go(func() error {
140135
return ignoreCanceled(mcMgr.Start(ctx))

examples/file/main.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,6 @@ func main() {
7878
return
7979
}
8080

81-
if err := provider.SetupWithManager(mgr); err != nil {
82-
entryLog.Info("unable to set up provider: %w", err)
83-
return
84-
}
85-
8681
if err := mcbuilder.ControllerManagedBy(mgr).
8782
Named("multicluster-configmaps").
8883
For(&corev1.ConfigMap{}).
@@ -119,7 +114,7 @@ func main() {
119114
return ignoreCanceled(mgr.Start(ctx))
120115
})
121116
g.Go(func() error {
122-
return ignoreCanceled(provider.Run(ctx))
117+
return ignoreCanceled(provider.Start(ctx, mgr))
123118
})
124119
if err := g.Wait(); err != nil {
125120
entryLog.Info("error in errgroup: %w", err)

examples/kind/main.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ func main() {
5151
os.Exit(1)
5252
}
5353

54-
if err := provider.SetupWithManager(mgr); err != nil {
55-
entryLog.Error(err, "unable to set up provider")
56-
os.Exit(1)
57-
}
58-
5954
err = mcbuilder.ControllerManagedBy(mgr).
6055
Named("multicluster-configmaps").
6156
For(&corev1.ConfigMap{}).
@@ -97,7 +92,7 @@ func main() {
9792
// Starting everything.
9893
g, ctx := errgroup.WithContext(ctx)
9994
g.Go(func() error {
100-
return ignoreCanceled(provider.Run(ctx))
95+
return ignoreCanceled(provider.Start(ctx, mgr))
10196
})
10297
g.Go(func() error {
10398
return ignoreCanceled(mgr.Start(ctx))

examples/namespace/main.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,6 @@ func run(ctx context.Context, log logr.Logger, kubeconfig string) error {
122122
return fmt.Errorf("unable to set up overall controller manager: %w", err)
123123
}
124124

125-
if err := provider.SetupWithManager(mgr); err != nil {
126-
return fmt.Errorf("unable to set up provider: %w", err)
127-
}
128-
129125
if err := mcbuilder.ControllerManagedBy(mgr).
130126
Named("multicluster-configmaps").
131127
For(&corev1.ConfigMap{}).
@@ -158,7 +154,7 @@ func run(ctx context.Context, log logr.Logger, kubeconfig string) error {
158154
// Starting everything.
159155
g, ctx := errgroup.WithContext(ctx)
160156
g.Go(func() error {
161-
return ignoreCanceled(provider.Run(ctx))
157+
return ignoreCanceled(provider.Start(ctx, mgr))
162158
})
163159
g.Go(func() error {
164160
return ignoreCanceled(cl.Start(ctx))

pkg/multicluster/multicluster.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ type Provider interface {
6161
// clusters, current and future.
6262
IndexField(ctx context.Context, obj client.Object, field string, extractValue client.IndexerFunc) error
6363

64-
// Run runs the provider. Implenetation of this method should block.
64+
// Start runs the provider. Implementation of this method should block.
6565
// If you need to pass in manager, it is recommended to implement SetupWithManager(mgr mcmanager.Manager) error method on individual providers.
6666
// It is not part of the provider interface because it is not required for all providers.
67-
Run(ctx context.Context) error
67+
Start(context.Context, Aware) error
6868
}

providers/cluster-api/provider.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import (
3939
"sigs.k8s.io/controller-runtime/pkg/manager"
4040
"sigs.k8s.io/controller-runtime/pkg/reconcile"
4141

42-
mcmanager "sigs.k8s.io/multicluster-runtime/pkg/manager"
4342
"sigs.k8s.io/multicluster-runtime/pkg/multicluster"
4443
)
4544

@@ -109,7 +108,7 @@ type Provider struct {
109108
client client.Client
110109

111110
lock sync.Mutex
112-
mcMgr mcmanager.Manager
111+
aware multicluster.Aware
113112
clusters map[string]cluster.Cluster
114113
cancelFns map[string]context.CancelFunc
115114
indexers []index
@@ -126,19 +125,12 @@ func (p *Provider) Get(_ context.Context, clusterName string) (cluster.Cluster,
126125
return nil, multicluster.ErrClusterNotFound
127126
}
128127

129-
func (p *Provider) SetupWithManager(mgr mcmanager.Manager) error {
128+
// Start starts the provider and blocks.
129+
func (p *Provider) Start(ctx context.Context, aware multicluster.Aware) error {
130130
p.lock.Lock()
131131
defer p.lock.Unlock()
132132

133-
p.mcMgr = mgr
134-
return nil
135-
}
136-
137-
// Run starts the provider and blocks.
138-
func (p *Provider) Run(ctx context.Context) error {
139-
if p.mcMgr == nil {
140-
return fmt.Errorf("manager is not set")
141-
}
133+
p.aware = aware
142134

143135
p.log.Info("Starting Cluster-API cluster provider")
144136

@@ -179,7 +171,7 @@ func (p *Provider) Reconcile(ctx context.Context, req reconcile.Request) (reconc
179171
// TODO(sttts): do tighter logging.
180172

181173
// provider already started?
182-
if p.mcMgr == nil {
174+
if p.aware == nil {
183175
return reconcile.Result{RequeueAfter: time.Second * 2}, nil
184176
}
185177

@@ -230,7 +222,7 @@ func (p *Provider) Reconcile(ctx context.Context, req reconcile.Request) (reconc
230222
p.log.Info("Added new cluster")
231223

232224
// engage manager.
233-
if err := p.mcMgr.Engage(clusterCtx, key, cl); err != nil {
225+
if err := p.aware.Engage(clusterCtx, key, cl); err != nil {
234226
log.Error(err, "failed to engage manager")
235227
delete(p.clusters, key)
236228
delete(p.cancelFns, key)

providers/cluster-inventory-api/provider.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ type Provider struct {
7373
strategy kubeconfigstrategy.Interface
7474

7575
lock sync.RWMutex
76-
mcMgr mcmanager.Manager
76+
manager mcmanager.Manager
7777
clusters map[string]cluster.Cluster
7878
cancelFns map[string]context.CancelFunc
7979
kubeconfig map[string]*rest.Config
@@ -114,7 +114,7 @@ func (p *Provider) SetupWithManager(mgr mcmanager.Manager) error {
114114
if mgr == nil {
115115
return fmt.Errorf("manager is nil")
116116
}
117-
p.mcMgr = mgr
117+
p.manager = mgr
118118

119119
// Get the local manager from the multi-cluster manager.
120120
localMgr := mgr.GetLocalManager()
@@ -188,7 +188,7 @@ func (p *Provider) Reconcile(ctx context.Context, req reconcile.Request) (reconc
188188
defer p.lock.Unlock()
189189

190190
// provider already started?
191-
if p.mcMgr == nil {
191+
if p.manager == nil {
192192
log.V(3).Info("Provider not started yet, requeuing")
193193
return reconcile.Result{RequeueAfter: time.Second * 2}, nil
194194
}
@@ -257,7 +257,7 @@ func (p *Provider) Reconcile(ctx context.Context, req reconcile.Request) (reconc
257257
log.Info("Added new cluster for ClusterProfile")
258258

259259
// engage manager.
260-
if err := p.mcMgr.Engage(clusterCtx, key, cl); err != nil {
260+
if err := p.manager.Engage(clusterCtx, key, cl); err != nil {
261261
log.Error(err, "failed to engage manager for ClusterProfile")
262262
delete(p.clusters, key)
263263
delete(p.cancelFns, key)
@@ -292,13 +292,8 @@ func (p *Provider) IndexField(ctx context.Context, obj client.Object, field stri
292292
return nil
293293
}
294294

295-
// Run runs the provider and blocks.
296-
func (p *Provider) Run(ctx context.Context) error {
297-
if p.mcMgr == nil {
298-
return fmt.Errorf("manager is not set")
299-
}
300-
295+
// Start runs the provider and blocks.
296+
func (p *Provider) Start(ctx context.Context, _ multicluster.Aware) error {
301297
<-ctx.Done()
302-
303298
return ctx.Err()
304299
}

providers/file/provider.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"sigs.k8s.io/controller-runtime/pkg/cluster"
3434
"sigs.k8s.io/controller-runtime/pkg/log"
3535

36-
mcmanager "sigs.k8s.io/multicluster-runtime/pkg/manager"
3736
"sigs.k8s.io/multicluster-runtime/pkg/multicluster"
3837
)
3938

@@ -138,21 +137,14 @@ type Provider struct {
138137
log logr.Logger
139138

140139
clustersLock sync.RWMutex
141-
mgr mcmanager.Manager
140+
aware multicluster.Aware
142141
clusters map[string]cluster.Cluster
143142
clusterCancel map[string]func()
144143
}
145144

146-
func (p *Provider) SetupWithManager(mgr mcmanager.Manager) error {
147-
p.clustersLock.Lock()
148-
defer p.clustersLock.Unlock()
149-
150-
p.mgr = mgr
151-
return nil
152-
}
153-
154-
// Run starts the provider and updates the clusters and is blocking.
155-
func (p *Provider) Run(ctx context.Context) error {
145+
// Start starts the provider and updates the clusters and is blocking.
146+
func (p *Provider) Start(ctx context.Context, aware multicluster.Aware) error {
147+
p.aware = aware
156148
if err := p.run(ctx); err != nil {
157149
return fmt.Errorf("initial update failed: %w", err)
158150
}
@@ -222,8 +214,8 @@ func (p *Provider) addCluster(ctx context.Context, name string, cl cluster.Clust
222214
p.removeCluster(name)
223215
}()
224216

225-
if p.mgr != nil {
226-
if err := p.mgr.Engage(ctx, name, cl); err != nil {
217+
if p.aware != nil {
218+
if err := p.aware.Engage(ctx, name, cl); err != nil {
227219
cancel()
228220
p.log.Error(err, "failed to engage cluster", "name", name)
229221
}
@@ -242,8 +234,8 @@ func (p *Provider) removeCluster(name string) {
242234
}
243235

244236
func (p *Provider) run(ctx context.Context) error {
245-
if p.mgr == nil {
246-
return fmt.Errorf("manager is not set")
237+
if p.aware == nil {
238+
return fmt.Errorf("aware is not set")
247239
}
248240

249241
loadedClusters, err := p.loadClusters()

providers/file/provider_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,12 @@ var _ = Describe("Provider File", Ordered, func() {
5454
KubeconfigDirs: []string{discoverDir},
5555
})
5656
Expect(err).NotTo(HaveOccurred())
57-
err = provider.SetupWithManager(nil)
5857
Expect(err).NotTo(HaveOccurred())
5958
})
6059

6160
By("Starting the provider", func() {
6261
g.Go(func() error {
63-
return ignoreCanceled(provider.Run(ctx))
62+
return ignoreCanceled(provider.Start(ctx, nil)) // Pass nil for aware since we are not using a manager in this test
6463
})
6564
})
6665

providers/kind/go.mod

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@ require (
1616
require (
1717
github.com/BurntSushi/toml v1.4.0 // indirect
1818
github.com/alessio/shellescape v1.4.2 // indirect
19-
github.com/beorn7/perks v1.0.1 // indirect
20-
github.com/cespare/xxhash/v2 v2.3.0 // indirect
2119
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
2220
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
2321
github.com/evanphx/json-patch/v5 v5.9.11 // indirect
24-
github.com/fsnotify/fsnotify v1.7.0 // indirect
2522
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
2623
github.com/go-openapi/jsonpointer v0.21.0 // indirect
2724
github.com/go-openapi/jsonreference v0.20.2 // indirect
@@ -41,10 +38,6 @@ require (
4138
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
4239
github.com/pelletier/go-toml v1.9.5 // indirect
4340
github.com/pkg/errors v0.9.1 // indirect
44-
github.com/prometheus/client_golang v1.22.0 // indirect
45-
github.com/prometheus/client_model v0.6.1 // indirect
46-
github.com/prometheus/common v0.62.0 // indirect
47-
github.com/prometheus/procfs v0.15.1 // indirect
4841
github.com/spf13/cobra v1.8.1 // indirect
4942
github.com/spf13/pflag v1.0.5 // indirect
5043
github.com/x448/float16 v0.8.4 // indirect
@@ -54,7 +47,6 @@ require (
5447
golang.org/x/term v0.30.0 // indirect
5548
golang.org/x/text v0.23.0 // indirect
5649
golang.org/x/time v0.9.0 // indirect
57-
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
5850
google.golang.org/protobuf v1.36.5 // indirect
5951
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
6052
gopkg.in/inf.v0 v0.9.1 // indirect

0 commit comments

Comments
 (0)