Skip to content

Commit 853e799

Browse files
embikvincepristtts
committed
Add cluster.Aware interface to support dynamic cluster engagement
On-behalf-of: SAP [email protected] Co-authored-by: Vince Prignano <[email protected]> Co-authored-by: Dr. Stefan Schimanski <[email protected]> Signed-off-by: Marvin Beckers <[email protected]>
1 parent 1947a94 commit 853e799

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

pkg/cluster/cluster.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ import (
3737

3838
// Cluster provides various methods to interact with a cluster.
3939
type Cluster interface {
40+
// Name returns the name of the cluster. It identifies the cluster in the
41+
// manager if that is attached to a cluster provider. The value is usually
42+
// empty for the default cluster of a manager.
43+
Name() string
44+
4045
// GetHTTPClient returns an HTTP client that can be used to talk to the apiserver
4146
GetHTTPClient() *http.Client
4247

@@ -75,6 +80,11 @@ type Cluster interface {
7580

7681
// Options are the possible options that can be configured for a Cluster.
7782
type Options struct {
83+
// name is the name of the cluster. It identifies the cluster in the manager
84+
// if that is attached to a cluster provider. The value is usually empty for
85+
// the default cluster of a manager.
86+
Name string
87+
7888
// Scheme is the scheme used to resolve runtime.Objects to GroupVersionKinds / Resources
7989
// Defaults to the kubernetes/client-go scheme.Scheme, but it's almost always better
8090
// idea to pass your own scheme in. See the documentation in pkg/scheme for more information.
@@ -234,6 +244,7 @@ func New(config *rest.Config, opts ...Option) (Cluster, error) {
234244
}
235245

236246
return &cluster{
247+
name: options.Name,
237248
config: originalConfig,
238249
httpClient: options.HTTPClient,
239250
scheme: options.Scheme,
@@ -300,3 +311,13 @@ func setOptionsDefaults(options Options, config *rest.Config) (Options, error) {
300311

301312
return options, nil
302313
}
314+
315+
// WithName sets the name of the cluster. The name can only be set once.
316+
func WithName(name string) Option {
317+
return func(o *Options) {
318+
if o.Name != "" {
319+
panic("cluster name cannot be set more than once")
320+
}
321+
o.Name = name
322+
}
323+
}

pkg/cluster/internal.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ import (
3232
)
3333

3434
type cluster struct {
35+
// name is the name of the cluster. It identifies the cluster in the manager
36+
// if that is attached to a cluster provider. The value is usually empty for
37+
// the default cluster of a manager.
38+
name string
39+
3540
// config is the rest.config used to talk to the apiserver. Required.
3641
config *rest.Config
3742

@@ -59,6 +64,10 @@ type cluster struct {
5964
logger logr.Logger
6065
}
6166

67+
func (c *cluster) Name() string {
68+
return c.name
69+
}
70+
6271
func (c *cluster) GetConfig() *rest.Config {
6372
return c.config
6473
}

pkg/cluster/multicluster.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright 2024 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 cluster
18+
19+
import (
20+
"context"
21+
)
22+
23+
// Aware is an interface that can be implemented by components that
24+
// can engage and disengage when clusters are added or removed at runtime.
25+
type Aware interface {
26+
// Engage gets called when the component should start operations for the given Cluster.
27+
// The given context is tied to the Cluster's lifecycle and will be cancelled when the
28+
// Cluster is removed or an error occurs.
29+
//
30+
// Implementers should return an error if they cannot start operations for the given Cluster,
31+
// and should ensure this operation is re-entrant and non-blocking.
32+
//
33+
// \_________________|)____.---'--`---.____
34+
// || \----.________.----/
35+
// || / / `--'
36+
// __||____/ /_
37+
// |___ \
38+
// `--------'
39+
Engage(context.Context, Cluster) error
40+
41+
// Disengage gets called when the component should stop operations for the given Cluster.
42+
Disengage(context.Context, Cluster) error
43+
}
44+
45+
// Provider defines methods to retrieve clusters by name. The provider is
46+
// responsible for discovering and managing the lifecycle of each cluster.
47+
//
48+
// Example: A Cluster API provider would be responsible for discovering and
49+
// managing clusters that are backed by Cluster API resources, which can live
50+
// in multiple namespaces in a single management cluster.
51+
type Provider interface {
52+
// Get returns a cluster for the given identifying cluster name. Get
53+
// returns an existing cluster if it has been created before.
54+
Get(ctx context.Context, clusterName string) (Cluster, error)
55+
}

pkg/source/source.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"k8s.io/client-go/util/workqueue"
2626
"k8s.io/utils/ptr"
2727
"sigs.k8s.io/controller-runtime/pkg/client"
28+
"sigs.k8s.io/controller-runtime/pkg/cluster"
2829
"sigs.k8s.io/controller-runtime/pkg/event"
2930
"sigs.k8s.io/controller-runtime/pkg/handler"
3031
internal "sigs.k8s.io/controller-runtime/pkg/internal/source"
@@ -69,6 +70,13 @@ type TypedSyncingSource[request comparable] interface {
6970
WaitForSync(ctx context.Context) error
7071
}
7172

73+
// TypedClusterAwareSource is a source that can be engaged and disengaged when
74+
// clusters are added or removed from the manager.
75+
type TypedClusterAwareSource[request comparable] interface {
76+
TypedSource[request]
77+
cluster.Aware
78+
}
79+
7280
// Kind creates a KindSource with the given cache provider.
7381
func Kind[object client.Object](
7482
cache cache.Cache,

0 commit comments

Comments
 (0)