-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmulticlient.go
207 lines (176 loc) · 5.21 KB
/
multiclient.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package client
import (
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/symcn/api"
"k8s.io/klog/v2"
)
type BuildClientFunc func(api.ClusterCfgInfo, *Options) (api.MingleClient, error)
type multiClient struct {
*CompletedConfig
MingleClientMap map[string]api.MingleClient
BeforStartHandleList []api.BeforeStartHandle
l sync.Mutex
ctx context.Context
stopCh chan struct{}
started int32
buildClientFunc BuildClientFunc
clusterEventHandlerList []api.ClusterEventHandler
}
func (mc *multiClient) Start(ctx context.Context) error {
if !atomic.CompareAndSwapInt32(&mc.started, 0, 1) {
return errors.New("multiclient can't repeat start")
}
// save ctx, when add new client
mc.ctx = ctx
if err := mc.loopFetchClient(); err != nil {
return err
}
<-ctx.Done()
mc.clean()
return nil
}
func (mc *multiClient) loopFetchClient() error {
err := mc.FetchClientInfoOnce()
if err != nil {
return err
}
if mc.FetchInterval <= 0 {
return nil
}
go func() {
var err error
timer := time.NewTicker(mc.FetchInterval)
for {
select {
case <-timer.C:
err = mc.FetchClientInfoOnce()
if err != nil {
klog.ErrorS(err, "FetchClientInfoOnce failed")
}
case <-mc.stopCh:
}
}
}()
return nil
}
func (mc *multiClient) clean() {
close(mc.stopCh)
}
// AddClusterEventHandler implements api.MultiMingleClient
func (mc *multiClient) AddClusterEventHandler(handler api.ClusterEventHandler) {
mc.l.Lock()
defer mc.l.Unlock()
// ignore multi client start already, and AddClusterEventHandler invoke get empty client.
for _, cli := range mc.MingleClientMap {
handler.OnAdd(mc.ctx, cli)
}
mc.clusterEventHandlerList = append(mc.clusterEventHandlerList, handler)
}
// FetchClientInfoOnce get clusterconfigurationmanager GetAll and rebuild clusterClientMap
func (mc *multiClient) FetchClientInfoOnce() error {
if atomic.LoadInt32(&mc.started) == 0 {
klog.Warningln("MultiClient not started, rebuild failed.")
return nil
}
mc.l.Lock()
defer mc.l.Unlock()
freshList, err := mc.ClusterCfgManager.GetAll()
if err != nil {
return fmt.Errorf("get all cluster info failed %+v", err)
}
freshCliMap := make(map[string]api.MingleClient, len(freshList))
var change int
// add and check new cluster
for _, freshClsInfo := range freshList {
// get old cluster info
currentCli, exist := mc.MingleClientMap[freshClsInfo.GetName()]
if exist &&
currentCli.GetClusterCfgInfo().GetKubeConfigType() == freshClsInfo.GetKubeConfigType() &&
currentCli.GetClusterCfgInfo().GetKubeConfig() == freshClsInfo.GetKubeConfig() &&
currentCli.GetClusterCfgInfo().GetKubeContext() == freshClsInfo.GetKubeContext() {
// kubetype, kubeconfig, kubecontext not modify
freshCliMap[currentCli.GetClusterCfgInfo().GetName()] = currentCli
continue
}
cli, err := mc.buildNewCluster(freshClsInfo, mc.Options)
if err != nil {
// !import ignore err, because one cluster disconnected not affect connected cluster.
klog.ErrorS(err, "buildNewCluster failed (ignore!!!).", "clusterName", freshClsInfo.GetName())
continue
}
if exist {
// kubeconfig modify, should stop old client
klog.InfoS("Configuration modified, stop old mingle client", "clusterName", cli.GetClusterCfgInfo().GetName())
mc.stopCluster(currentCli)
}
freshCliMap[freshClsInfo.GetName()] = cli
klog.InfoS("Auto add mingle client successful!", "clusterName", freshClsInfo.GetName())
change++
}
// remove unexpect cluster
for name, currentCli := range mc.MingleClientMap {
if _, ok := freshCliMap[name]; !ok {
change++
// not exist, should stop
go func(cli api.MingleClient) {
klog.InfoS("Stop mingle client", "clusterName", cli.GetClusterCfgInfo().GetName())
mc.stopCluster(cli)
}(currentCli)
}
}
// client list changed.
if change > 0 {
mc.MingleClientMap = freshCliMap
}
return nil
}
func (mc *multiClient) buildNewCluster(newClsInfo api.ClusterCfgInfo, options *Options) (api.MingleClient, error) {
// build new client
cli, err := mc.buildClientFunc(newClsInfo, options)
if err != nil {
return nil, err
}
// start new client
err = start(mc.ctx, cli, mc.BeforStartHandleList)
if err != nil {
return nil, err
}
if len(mc.clusterEventHandlerList) > 0 {
for _, handler := range mc.clusterEventHandlerList {
handler.OnAdd(mc.ctx, cli)
}
}
return cli, nil
}
func start(ctx context.Context, cli api.MingleClient, beforStartHandleList []api.BeforeStartHandle) error {
var err error
for _, handler := range beforStartHandleList {
err = handler(ctx, cli)
if err != nil {
return fmt.Errorf("invoke mingle client %s BeforeHandle failed %+v", cli.GetClusterCfgInfo().GetName(), err)
}
}
go func() {
err = cli.Start(ctx)
if err != nil {
klog.ErrorS(err, "start mingle client failed", "clusterName", cli.GetClusterCfgInfo().GetName())
}
}()
return nil
}
func (mc *multiClient) stopCluster(cli api.MingleClient) {
if len(mc.clusterEventHandlerList) > 0 {
for _, handler := range mc.clusterEventHandlerList {
handler.OnDelete(mc.ctx, cli)
}
}
cli.Stop()
}
func BuildNormalClient(clsInfo api.ClusterCfgInfo, opts *Options) (api.MingleClient, error) {
return NewMingleClient(clsInfo, opts)
}