-
Notifications
You must be signed in to change notification settings - Fork 248
/
eventprocessor.go
418 lines (380 loc) · 13.8 KB
/
eventprocessor.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package app
import (
"fmt"
"os"
"reflect"
"strconv"
"time"
"github.com/Tencent/bk-bcs/bcs-common/common/blog"
loadbalance "github.com/Tencent/bk-bcs/bcs-common/pkg/loadbalance/v2"
"github.com/Tencent/bk-bcs/bcs-runtime/bcs-mesos/bcs-loadbalance/clear"
"github.com/Tencent/bk-bcs/bcs-runtime/bcs-mesos/bcs-loadbalance/monitor"
bcsprometheus "github.com/Tencent/bk-bcs/bcs-runtime/bcs-mesos/bcs-loadbalance/monitor/prometheus"
"github.com/Tencent/bk-bcs/bcs-runtime/bcs-mesos/bcs-loadbalance/monitor/status"
"github.com/Tencent/bk-bcs/bcs-runtime/bcs-mesos/bcs-loadbalance/option"
"github.com/Tencent/bk-bcs/bcs-runtime/bcs-mesos/bcs-loadbalance/rdiscover"
"github.com/Tencent/bk-bcs/bcs-runtime/bcs-mesos/bcs-loadbalance/template"
"github.com/Tencent/bk-bcs/bcs-runtime/bcs-mesos/bcs-loadbalance/template/haproxy"
"github.com/Tencent/bk-bcs/bcs-runtime/bcs-mesos/bcs-loadbalance/template/nginx"
"github.com/Tencent/bk-bcs/bcs-runtime/bcs-mesos/bcs-loadbalance/types"
"github.com/prometheus/client_golang/prometheus"
)
// EventHandler is event interface when
type EventHandler interface {
OnAdd(obj interface{})
OnDelete(obj interface{})
OnUpdate(oldObj, newObj interface{})
}
// InitLogger init app logger
func InitLogger(config *option.LBConfig) {
blog.InitLogs(config.LogConfig)
}
// CloseLogger close logger
func CloseLogger() {
blog.CloseLogs()
}
// NewEventProcessor create EventProcessor with LBConfig
func NewEventProcessor(config *option.LBConfig) *LBEventProcessor {
var err error
processor := &LBEventProcessor{
update: false,
generate: false,
reload: false,
signals: make(chan os.Signal, 10),
exit: make(chan struct{}),
config: config,
clearManager: clear.NewClearManager(),
}
// register both service zookeeper and cluster zookeeper
// service zookeeper for health check, service register
// cluster zookeeper for prometheus metrics collector
zkSubRegPath := config.ClusterID + "/" + config.Group
processor.rd = rdiscover.NewRDiscover(config.BcsZkAddr, zkSubRegPath, config.ClusterID, config.Proxy, config.Address,
uint(config.MetricPort))
if len(config.ClusterZk) != 0 {
processor.clusterRd = rdiscover.NewRDiscover(config.ClusterZk, config.Group, config.ClusterID, config.Proxy,
config.Address, uint(config.MetricPort))
}
processor.reflector = NewReflector(config, processor)
lbMonitor := monitor.NewMonitor(config.Address, int(config.MetricPort))
newMetricResource := bcsprometheus.NewPromMetric()
if config.Proxy == option.ProxyHaproxy {
blog.Infof("use haproxy transmit")
processor.cfgManager, err = haproxy.NewManager(
config.Name,
config.BinPath,
config.CfgPath,
config.GeneratingDir,
config.CfgBackupDir,
config.TemplateDir,
config.StatusFetchPeriod,
)
if err != nil {
blog.Infof("failed to create haproxy manager wiith config %v, err %s", config, err.Error())
}
} else {
blog.Infof("use nginx transmit")
processor.cfgManager = nginx.NewManager(
config.BinPath,
config.CfgPath,
config.GeneratingDir,
config.CfgBackupDir,
config.TemplateDir)
}
// add manager to promethes
prometheus.MustRegister(processor.cfgManager)
// register metric
prometheus.Register(LoadbalanceZookeeperStateMetric)
prometheus.Register(LoadbalanceZookeeperEventAddMetric)
prometheus.Register(LoadbalanceZookeeperEventUpdateMetric)
prometheus.Register(LoadbalanceZookeeperEventDeleteMetric)
prometheus.Register(LoadbalanceServiceConflictMetric)
LoadbalanceZookeeperStateMetric.WithLabelValues(config.Name).Set(1)
newStatusResource := status.NewStatus(processor.cfgManager.GetStatusFunction())
lbMonitor.RegisterResource(newMetricResource)
lbMonitor.RegisterResource(newStatusResource)
processor.monitor = lbMonitor
return processor
}
// LBEventProcessor event loop for handling data change event.
type LBEventProcessor struct {
update bool // update flag
generate bool // flag for resetting HAProxy configuration
reload bool // flag for reloading HAProxy
signals chan os.Signal // handle all signal we need, reserved
exit chan struct{} // flag for processor exit
config *option.LBConfig // config item from config file
reflector DataReflector // data cache holder
cfgManager template.Manager // template manager
rd *rdiscover.RDiscover // bcs zookeeper register
clusterRd *rdiscover.RDiscover // cluster zookeeper register
clearManager *clear.Manager // timer to clear template file
monitor *monitor.Monitor // monitor to support metric and status api
}
// Start starting point for event processing
// 1. start reflector to cache data from storage
// 2. start template manager for Create/Reload config for haproxy.cfg
// 3. start local logic loop for check data changed
func (lp *LBEventProcessor) Start() error {
go func() {
if err := lp.monitor.Run(); err != nil {
blog.Errorf("run lb monitor failed, err %s", err.Error())
}
}()
blog.Infof("run lb monitor")
go func() {
if err := lp.rd.Start(); err != nil {
blog.Errorf("start register zookeeper error: %s", err.Error())
// should go ahead to work event if register zookeeper failed
}
}()
blog.Infof("start register success")
if len(lp.config.ClusterZk) != 0 {
go func() {
if err := lp.clusterRd.Start(); err != nil {
blog.Errorf("start register cluster zookeeper error: %s", err.Error())
}
}()
blog.Infof("start cluster register success")
}
if err := lp.reflector.Start(); err != nil {
blog.Errorf("start Reflector error: %s", err.Error())
return err
}
blog.Infof("start reflector success")
if err := lp.cfgManager.Start(); err != nil {
blog.Errorf("start ConfigManager error: %s", err.Error())
return err
}
blog.Infof("start config manager successfully")
lp.clearManager.Start()
go lp.run()
return nil
}
// run main loop
func (lp *LBEventProcessor) run() {
updateTick := time.NewTicker(time.Second * time.Duration(int64(lp.config.CfgCheckPeriod)))
defer updateTick.Stop()
syncTick := time.NewTicker(time.Second * time.Duration(int64(lp.config.SyncPeriod)))
defer syncTick.Stop()
for {
select {
case <-lp.exit:
blog.Infof("EeventProcessor Get close event, return")
return
case <-updateTick.C:
// ready to check update event
if !lp.update {
continue
}
if lp.reload {
blog.Infof("configuration is under reloading, skip operation.")
continue
}
lp.update = false
lp.configHandle()
case <-syncTick.C:
if lp.reload {
blog.Infof("configuration is under reloading, skip operation.")
continue
}
lp.update = false
lp.configHandle()
}
}
}
// configHandle Get all data from reflector, export to template
// to generating haproxy.cfg
func (lp *LBEventProcessor) configHandle() {
lp.reload = true
defer func() {
lp.reload = false
}()
// Get all data from ServiceReflector
tData := new(types.TemplateData)
tData.HTTP, tData.HTTPS, tData.TCP, tData.UDP = lp.reflector.Lister()
if len(tData.HTTP) == 0 && len(tData.HTTPS) == 0 && len(tData.TCP) == 0 && len(tData.UDP) == 0 {
blog.Warnf("No any service in Reflector, try reload if changed")
}
tData.LogFlag = true
tData.SSLCert = ""
// find conflicts
if findConflict, Msg := lp.findConficts(tData); findConflict {
blog.Errorf("[CONFLICTS] msg: %s", Msg)
return
}
// haproxy reload
if !lp.doReload(tData) {
blog.Errorf("Do proxy reloading failed, wait for next tick")
return
}
}
// findConficts xxx
// detectConflicts detect port conflict
// true for conflicts found
func (lp *LBEventProcessor) findConficts(data *types.TemplateData) (bool, string) {
layer7Map := make(map[string]string)
layer4Map := make(map[int]string)
for _, http := range data.HTTP {
domainPortStr := http.BCSVHost + "," + strconv.Itoa(http.ServicePort)
for _, backend := range http.Backends {
if serviceKey, isConflict := layer7Map[domainPortStr+","+backend.Path]; isConflict {
LoadbalanceServiceConflictMetric.WithLabelValues(lp.config.Name, http.Name).Inc()
return true, fmt.Sprintf("%s is conflict with %s", http.Name, serviceKey)
}
layer7Map[domainPortStr+","+backend.Path] = http.Name
}
layer4Map[http.ServicePort] = http.Name
}
for _, https := range data.HTTPS {
domainPortStr := https.BCSVHost + "," + strconv.Itoa(https.ServicePort)
for _, backend := range https.Backends {
if serviceKey, isConflict := layer7Map[domainPortStr+","+backend.Path]; isConflict {
LoadbalanceServiceConflictMetric.WithLabelValues(lp.config.Name, https.Name).Inc()
return true, fmt.Sprintf("%s is conflict with %s", https.Name, serviceKey)
}
layer7Map[domainPortStr+","+backend.Path] = https.Name
}
layer4Map[https.ServicePort] = https.Name
}
for _, tcp := range data.TCP {
if serviceKey, isConfict := layer4Map[tcp.ServicePort]; isConfict {
LoadbalanceServiceConflictMetric.WithLabelValues(lp.config.Name, tcp.Name).Inc()
return true, fmt.Sprintf("%s is conflict with %s", tcp.Name, serviceKey)
}
layer4Map[tcp.ServicePort] = tcp.Name
}
for _, udp := range data.UDP {
if serviceKey, isConflict := layer4Map[udp.ServicePort]; isConflict {
LoadbalanceServiceConflictMetric.WithLabelValues(lp.config.Name, udp.Name).Inc()
return true, fmt.Sprintf("%s is conflict with %s", udp.Name, serviceKey)
}
layer4Map[udp.ServicePort] = udp.Name
}
return false, ""
}
// doReload reset HAproy configuration
func (lp *LBEventProcessor) doReload(data *types.TemplateData) bool {
// do config check and try update without reload
if !lp.cfgManager.TryUpdateWithoutReload(data) {
blog.Infof("try update successfully, no need reload")
return true
}
// create configuration
newFile, creatErr := lp.cfgManager.Create(data)
if creatErr != nil {
blog.Errorf("Create proxy with template data faield: %s", creatErr.Error())
return false
}
// check difference between new file and old file
if !lp.cfgManager.CheckDifference(lp.config.CfgPath, newFile) {
blog.Warnf("No difference in new configuration file")
return true
}
// use check command validate correct of configuration
if !lp.cfgManager.Validate(newFile) {
template.LoadbalanceConfigRenderTotal.WithLabelValues("fail").Inc()
blog.Errorf("Validate %s with proxy command failed", newFile)
return false
}
template.LoadbalanceConfigRenderTotal.WithLabelValues("success").Inc()
blog.Infof("Generation config file %s success", newFile)
// replace new file, backup old one
err := lp.cfgManager.Replace(lp.config.CfgPath, newFile)
if err != nil {
template.LoadbalanceConfigRefreshTotal.WithLabelValues("fail").Inc()
blog.Errorf("Replace config with %s and backup failed", newFile)
return false
}
template.LoadbalanceConfigRefreshTotal.WithLabelValues("success").Inc()
// reload with haproxy command
if err := lp.cfgManager.Reload(lp.config.CfgPath); err != nil {
template.LoadbalanceProxyReloadTotal.WithLabelValues("fail").Inc()
return false
}
template.LoadbalanceProxyReloadTotal.WithLabelValues("success").Inc()
blog.Infof("Reload proxy config %s success.", lp.config.CfgPath)
return true
}
// Stop stop processor all worker gracefully
func (lp *LBEventProcessor) Stop() {
lp.reflector.Stop()
lp.cfgManager.Stop()
if err := lp.rd.Stop(); err != nil {
blog.Warnf("register stop failed, err %s", err.Error())
}
if len(lp.config.ClusterZk) != 0 {
if err := lp.clusterRd.Stop(); err != nil {
blog.Warnf("cluster zk register stop failed, err %s", err.Error())
}
}
lp.clearManager.Stop()
close(lp.exit)
}
// HandleSignal interface for handle signal from system/User
func (lp *LBEventProcessor) HandleSignal(signalChan <-chan os.Signal) {
for {
select {
case <-lp.exit:
blog.Info("EventProcessor Signal Handler exit")
return
case <-signalChan:
blog.Infof("Get signal from system. Exit")
lp.Stop()
return
}
}
}
// OnAdd receive data Add event
func (lp *LBEventProcessor) OnAdd(obj interface{}) {
svr, ok := obj.(*loadbalance.ExportService)
if !ok {
blog.Errorf("%v is not type ExportService", obj)
return
}
blog.Infof("Service %s added, ready to refresh", svr.ServiceName)
LoadbalanceZookeeperEventAddMetric.WithLabelValues(lp.config.Name).Inc()
lp.update = true
}
// OnDelete receive data Delete event
func (lp *LBEventProcessor) OnDelete(obj interface{}) {
svr, ok := obj.(*loadbalance.ExportService)
if !ok {
blog.Errorf("%v is not type ExportService", obj)
return
}
blog.Infof("Service %s deleted, ready to refresh", svr.ServiceName)
LoadbalanceZookeeperEventDeleteMetric.WithLabelValues(lp.config.Name).Inc()
lp.update = true
}
// OnUpdate receive data Update event
func (lp *LBEventProcessor) OnUpdate(oldObj, newObj interface{}) {
newSvr, ok := newObj.(*loadbalance.ExportService)
if !ok {
blog.Errorf("new obj %v is not type ExportService", newObj)
return
}
oldSvc, ok := oldObj.(*loadbalance.ExportService)
if !ok {
blog.Errorf("old obj %v is not type ExportService", oldObj)
}
if reflect.DeepEqual(oldSvc, newSvr) {
blog.Infof(fmt.Sprintf("Service %s No changed, skip update event", newSvr.ServiceName))
return
}
blog.Infof("Service %s update, ready to refresh", newSvr.ServiceName)
LoadbalanceZookeeperEventUpdateMetric.WithLabelValues(lp.config.Name).Inc()
lp.update = true
}