@@ -12,6 +12,8 @@ import (
12
12
13
13
"sigs.k8s.io/controller-runtime/pkg/client"
14
14
"sigs.k8s.io/controller-runtime/pkg/cluster"
15
+
16
+ "sigs.k8s.io/multicluster-runtime/pkg/multicluster"
15
17
)
16
18
17
19
// Clusters is a conccurency-safe map of clusters to be used in
@@ -51,9 +53,13 @@ func (c *Clusters) Get(_ context.Context, name string) (cluster.Cluster, error)
51
53
return cl , nil
52
54
}
53
55
56
+ // HandleClusterErrorFunc is called when a cluster encounters an error
57
+ // during its lifecycle.
58
+ type HandleClusterErrorFunc func (string , error )
59
+
54
60
// Add adds a new cluster.
55
61
// If a cluster with the given name already exists, it returns an error.
56
- func (c * Clusters ) Add (ctx context.Context , name string , cl cluster.Cluster , callback func (context. Context , string , cluster. Cluster ) error ) error {
62
+ func (c * Clusters ) Add (ctx context.Context , name string , cl cluster.Cluster , callback multicluster. EngageFunc , handleError HandleClusterErrorFunc ) error {
57
63
c .Lock .Lock ()
58
64
defer c .Lock .Unlock ()
59
65
@@ -72,10 +78,7 @@ func (c *Clusters) Add(ctx context.Context, name string, cl cluster.Cluster, cal
72
78
go func () {
73
79
defer c .Remove (name )
74
80
if err := cl .Start (ctx ); err != nil {
75
- cancel ()
76
- // TODO how to handle errors?
77
- // Another callback?
78
- // pass in a logger on creation?
81
+ handleError (name , err )
79
82
}
80
83
}()
81
84
@@ -98,11 +101,11 @@ func (c *Clusters) Remove(name string) {
98
101
// If a cluster with the name already exists it compares the
99
102
// configuration as returned by cluster.GetConfig() to compare
100
103
// clusters.
101
- func (c * Clusters ) AddOrReplace (ctx context.Context , name string , cl cluster.Cluster , callback func (context. Context , string , cluster. Cluster ) error ) error {
104
+ func (c * Clusters ) AddOrReplace (ctx context.Context , name string , cl cluster.Cluster , callback multicluster. EngageFunc , handleError HandleClusterErrorFunc ) error {
102
105
existing , err := c .Get (ctx , name )
103
106
if err != nil {
104
107
// Cluster does not exist, add it
105
- return c .Add (ctx , name , cl , callback )
108
+ return c .Add (ctx , name , cl , callback , handleError )
106
109
}
107
110
108
111
if cmp .Equal (existing .GetConfig (), cl .GetConfig ()) {
@@ -112,7 +115,7 @@ func (c *Clusters) AddOrReplace(ctx context.Context, name string, cl cluster.Clu
112
115
113
116
// Cluster exists with a different config, replace it
114
117
c .Remove (name )
115
- return c .Add (ctx , name , cl , callback )
118
+ return c .Add (ctx , name , cl , callback , handleError )
116
119
}
117
120
118
121
// IndexField indexes a field on all clusters.
0 commit comments