@@ -32,6 +32,9 @@ type MultiClusterController interface {
32
32
Controller
33
33
}
34
34
35
+ // MultiClusterOption is a functional option for MultiClusterController.
36
+ type MultiClusterOption func (* multiClusterController )
37
+
35
38
// ClusterWatcher starts watches for a given Cluster. The ctx should be
36
39
// used to cancel the watch when the Cluster is disengaged.
37
40
type ClusterWatcher interface {
@@ -40,12 +43,25 @@ type ClusterWatcher interface {
40
43
41
44
// NewMultiClusterController creates a new MultiClusterController for the given
42
45
// controller with the given ClusterWatcher.
43
- func NewMultiClusterController (c Controller , watcher ClusterWatcher ) MultiClusterController {
44
- return & multiClusterController {
46
+ func NewMultiClusterController (c Controller , watcher ClusterWatcher , opts ... MultiClusterOption ) MultiClusterController {
47
+ mcc := & multiClusterController {
45
48
Controller : c ,
46
49
watcher : watcher ,
47
50
clusters : map [string ]struct {}{},
48
51
}
52
+ for _ , opt := range opts {
53
+ opt (mcc )
54
+ }
55
+
56
+ return mcc
57
+ }
58
+
59
+ // WithClusterAware adds the given cluster.Aware instances to the MultiClusterController,
60
+ // being engaged and disengaged when the clusters are added or removed.
61
+ func WithClusterAware (awares ... cluster.Aware ) MultiClusterOption {
62
+ return func (c * multiClusterController ) {
63
+ c .awares = append (c .awares , awares ... )
64
+ }
49
65
}
50
66
51
67
type multiClusterController struct {
@@ -54,6 +70,7 @@ type multiClusterController struct {
54
70
55
71
lock sync.Mutex
56
72
clusters map [string ]struct {}
73
+ awares []cluster.Aware
57
74
}
58
75
59
76
// Engage gets called when the runnable should start operations for the given Cluster.
@@ -84,6 +101,17 @@ func (c *multiClusterController) Engage(clusterCtx context.Context, cl cluster.C
84
101
engaged = append (engaged , ctrl )
85
102
}
86
103
104
+ // engage cluster aware instances
105
+ for _ , aware := range c .awares {
106
+ if err := aware .Engage (clusterCtx , cl ); err != nil {
107
+ if err := disengage (); err != nil {
108
+ return err
109
+ }
110
+ return err
111
+ }
112
+ engaged = append (engaged , aware )
113
+ }
114
+
87
115
// start watches on the cluster
88
116
if err := c .watcher .Watch (clusterCtx , cl ); err != nil {
89
117
if err := disengage (); err != nil {
0 commit comments