-
Notifications
You must be signed in to change notification settings - Fork 13
/
multicluster_reconciler.go
62 lines (52 loc) · 1.79 KB
/
multicluster_reconciler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package reconcile
import (
"sync"
"github.com/solo-io/go-utils/errors"
"github.com/solo-io/solo-kit/pkg/api/v1/clients"
"github.com/solo-io/solo-kit/pkg/api/v1/reconcile"
"github.com/solo-io/solo-kit/pkg/api/v1/resources"
"go.uber.org/multierr"
)
type MultiClusterReconciler interface {
reconcile.Reconciler
AddClusterClient(cluster string, client clients.ResourceClient)
RemoveClusterClient(cluster string)
}
/*
A MultiClusterReconciler takes a map of cluster names to resource clients
It can then perform reconciles against lists of desiredResources with resources
desired across multiple clusters
*/
type multiClusterReconciler struct {
rcs map[string]clients.ResourceClient
access sync.RWMutex
}
func (r *multiClusterReconciler) AddClusterClient(cluster string, client clients.ResourceClient) {
r.access.Lock()
r.rcs[cluster] = client
r.access.Unlock()
}
func (r *multiClusterReconciler) RemoveClusterClient(cluster string) {
r.access.Lock()
delete(r.rcs, cluster)
r.access.Unlock()
}
func NewMultiClusterReconciler(rcs map[string]clients.ResourceClient) MultiClusterReconciler {
return &multiClusterReconciler{rcs: rcs}
}
func (r *multiClusterReconciler) Reconcile(namespace string, desiredResources resources.ResourceList, transitionFunc reconcile.TransitionResourcesFunc, opts clients.ListOpts) error {
byCluster := desiredResources.ByCluster()
r.access.RLock()
defer r.access.RUnlock()
var errs error
for cluster, desiredForCluster := range byCluster {
rc, ok := r.rcs[cluster]
if !ok {
return errors.Errorf("no client found for cluster %v", cluster)
}
if err := reconcile.NewReconciler(rc).Reconcile(namespace, desiredForCluster, transitionFunc, opts); err != nil {
errs = multierr.Append(errs, errors.Wrapf(err, "reconciling cluster %v", cluster))
}
}
return errs
}