Skip to content

Commit 15b968c

Browse files
committed
providers: unify constructors and runner
Signed-off-by: Dr. Stefan Schimanski <[email protected]>
1 parent e9dcf61 commit 15b968c

File tree

4 files changed

+122
-8
lines changed

4 files changed

+122
-8
lines changed

providers/kind/provider.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"time"
2525

2626
"github.com/go-logr/logr"
27+
2728
mcmanager "github.com/multicluster-runtime/multicluster-runtime/pkg/manager"
2829
"github.com/multicluster-runtime/multicluster-runtime/pkg/multicluster"
2930
"k8s.io/apimachinery/pkg/util/sets"
@@ -34,6 +35,8 @@ import (
3435
kind "sigs.k8s.io/kind/pkg/cluster"
3536
)
3637

38+
var _ multicluster.Provider = &Provider{}
39+
3740
// New creates a new kind cluster Provider.
3841
func New() *Provider {
3942
return &Provider{
@@ -52,8 +55,7 @@ type Provider struct {
5255
cancelFns map[string]context.CancelFunc
5356
}
5457

55-
var _ multicluster.Provider = &Provider{}
56-
58+
// Get returns the cluster with the given name, if it is known.
5759
func (k *Provider) Get(ctx context.Context, clusterName string) (cluster.Cluster, error) {
5860
k.lock.RLock()
5961
defer k.lock.RUnlock()
@@ -64,6 +66,7 @@ func (k *Provider) Get(ctx context.Context, clusterName string) (cluster.Cluster
6466
return nil, fmt.Errorf("cluster %s not found", clusterName)
6567
}
6668

69+
// Run starts the provider and blocks.
6770
func (k *Provider) Run(ctx context.Context, mgr mcmanager.Manager) error {
6871
k.log.Info("Starting kind cluster Provider")
6972

providers/namespace/provider.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"sync"
2323

2424
"github.com/go-logr/logr"
25+
"github.com/multicluster-runtime/multicluster-runtime/pkg/multicluster"
26+
2527
mcmanager "github.com/multicluster-runtime/multicluster-runtime/pkg/manager"
2628
corev1 "k8s.io/api/core/v1"
2729
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
@@ -31,11 +33,13 @@ import (
3133
"sigs.k8s.io/controller-runtime/pkg/manager"
3234
)
3335

34-
// NamespacedClusterProvider is a cluster provider that represents each namespace
36+
var _ multicluster.Provider = &Provider{}
37+
38+
// Provider is a cluster provider that represents each namespace
3539
// as a dedicated cluster with only a "default" namespace. It maps each namespace
3640
// to "default" and vice versa, simulating a multi-cluster setup. It uses one
3741
// informer to watch objects for all namespaces.
38-
type NamespacedClusterProvider struct {
42+
type Provider struct {
3943
cluster cluster.Cluster
4044

4145
mgr manager.Manager
@@ -46,16 +50,17 @@ type NamespacedClusterProvider struct {
4650
cancelFns map[string]context.CancelFunc
4751
}
4852

49-
func NewNamespacedClusterProvider(cl cluster.Cluster) *NamespacedClusterProvider {
50-
return &NamespacedClusterProvider{
53+
func New(cl cluster.Cluster) *Provider {
54+
return &Provider{
5155
cluster: cl,
5256
log: log.Log.WithName("namespaced-cluster-provider"),
5357
clusters: map[string]cluster.Cluster{},
5458
cancelFns: map[string]context.CancelFunc{},
5559
}
5660
}
5761

58-
func (p *NamespacedClusterProvider) Start(ctx context.Context, mgr mcmanager.Manager) error {
62+
// Run starts the provider and blocks.
63+
func (p *Provider) Run(ctx context.Context, mgr mcmanager.Manager) error {
5964
nsInf, err := p.cluster.GetCache().GetInformer(ctx, &corev1.Namespace{})
6065
if err != nil {
6166
return err
@@ -116,10 +121,12 @@ func (p *NamespacedClusterProvider) Start(ctx context.Context, mgr mcmanager.Man
116121
return err
117122
}
118123

124+
<-ctx.Done()
125+
119126
return nil
120127
}
121128

122-
func (p *NamespacedClusterProvider) Get(ctx context.Context, clusterName string) (cluster.Cluster, error) {
129+
func (p *Provider) Get(ctx context.Context, clusterName string) (cluster.Cluster, error) {
123130
p.lock.RLock()
124131
defer p.lock.RUnlock()
125132
if cl, ok := p.clusters[clusterName]; ok {

providers/nop/provider.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package namespace
18+
19+
import (
20+
"context"
21+
"fmt"
22+
23+
mcmanager "github.com/multicluster-runtime/multicluster-runtime/pkg/manager"
24+
"github.com/multicluster-runtime/multicluster-runtime/pkg/multicluster"
25+
"sigs.k8s.io/controller-runtime/pkg/cluster"
26+
)
27+
28+
var _ multicluster.Provider = &Provider{}
29+
30+
// Provider is a provider that does nothing.
31+
type Provider struct{}
32+
33+
// New creates a new provider that does nothing.
34+
func New() *Provider {
35+
return &Provider{}
36+
}
37+
38+
// Run starts the provider and blocks.
39+
func (p *Provider) Run(ctx context.Context, _ mcmanager.Manager) error {
40+
<-ctx.Done()
41+
return nil
42+
}
43+
44+
// Get returns an error for any cluster name.
45+
func (p *Provider) Get(_ context.Context, clusterName string) (cluster.Cluster, error) {
46+
return nil, fmt.Errorf("cluster %s not found", clusterName)
47+
}

providers/single/provider.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package namespace
18+
19+
import (
20+
"context"
21+
"fmt"
22+
23+
mcmanager "github.com/multicluster-runtime/multicluster-runtime/pkg/manager"
24+
"github.com/multicluster-runtime/multicluster-runtime/pkg/multicluster"
25+
"sigs.k8s.io/controller-runtime/pkg/cluster"
26+
)
27+
28+
var _ multicluster.Provider = &Provider{}
29+
30+
// Provider is a provider that engages the passed cluster.
31+
type Provider struct {
32+
name string
33+
cl cluster.Cluster
34+
}
35+
36+
// New returns a provider engaging the passed cluster under the given name.
37+
// It does not start the passed cluster.
38+
func New(name string, cl cluster.Cluster) *Provider {
39+
return &Provider{
40+
name: name,
41+
cl: cl,
42+
}
43+
}
44+
45+
// Run starts the provider and blocks.
46+
func (p *Provider) Run(ctx context.Context, _ mcmanager.Manager) error {
47+
<-ctx.Done()
48+
return nil
49+
}
50+
51+
// Get returns the cluster with the given name.
52+
func (p *Provider) Get(_ context.Context, clusterName string) (cluster.Cluster, error) {
53+
if clusterName == p.name {
54+
return p.cl, nil
55+
}
56+
return nil, fmt.Errorf("cluster %s not found", clusterName)
57+
}

0 commit comments

Comments
 (0)