-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
process.go
683 lines (624 loc) · 20 KB
/
process.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
package metamanager
import (
"encoding/json"
"fmt"
"github.com/kubeedge/kubeedge/edge/pkg/metamanager/metaserver/kubernetes/storage/sqlite/imitator"
"strings"
"time"
v1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
"github.com/kubeedge/beehive/pkg/common/util"
beehiveContext "github.com/kubeedge/beehive/pkg/core/context"
"github.com/kubeedge/beehive/pkg/core/model"
cloudmodules "github.com/kubeedge/kubeedge/cloud/pkg/common/modules"
"github.com/kubeedge/kubeedge/common/constants"
connect "github.com/kubeedge/kubeedge/edge/pkg/common/cloudconnection"
messagepkg "github.com/kubeedge/kubeedge/edge/pkg/common/message"
"github.com/kubeedge/kubeedge/edge/pkg/common/modules"
metaManagerConfig "github.com/kubeedge/kubeedge/edge/pkg/metamanager/config"
"github.com/kubeedge/kubeedge/edge/pkg/metamanager/dao"
)
//Constants to check metamanager processes
const (
OK = "OK"
GroupResource = "resource"
OperationMetaSync = "meta-internal-sync"
OperationFunctionAction = "action"
OperationFunctionActionResult = "action_result"
EdgeFunctionModel = "edgefunction"
CloudFunctionModel = "funcmgr"
CloudControlerModel = "edgecontroller"
)
func feedbackError(err error, info string, request model.Message) {
errInfo := "Something wrong"
if err != nil {
errInfo = fmt.Sprintf(info+": %v", err)
}
errResponse := model.NewErrorMessage(&request, errInfo).SetRoute(MetaManagerModuleName, request.GetGroup())
if request.GetSource() == modules.EdgedModuleName {
sendToEdged(errResponse, request.IsSync())
} else {
sendToCloud(errResponse)
}
}
func sendToEdged(message *model.Message, sync bool) {
if sync {
beehiveContext.SendResp(*message)
} else {
beehiveContext.Send(modules.EdgedModuleName, *message)
}
}
func sendToEdgeMesh(message *model.Message, sync bool) {
//[metaserver]help for test
return
if sync {
beehiveContext.SendResp(*message)
} else {
beehiveContext.Send(modules.EdgeMeshModuleName, *message)
}
}
func sendToCloud(message *model.Message) {
beehiveContext.SendToGroup(string(metaManagerConfig.Config.ContextSendGroup), *message)
}
// Resource format: <namespace>/<restype>[/resid]
// return <reskey, restype, resid>
func parseResource(resource string) (string, string, string) {
tokens := strings.Split(resource, constants.ResourceSep)
resType := ""
resID := ""
switch len(tokens) {
case 2:
resType = tokens[len(tokens)-1]
case 3:
resType = tokens[len(tokens)-2]
resID = tokens[len(tokens)-1]
default:
}
return resource, resType, resID
}
// is resource type require remote query
func requireRemoteQuery(resType string) bool {
return resType == model.ResourceTypeConfigmap ||
resType == model.ResourceTypeSecret ||
resType == constants.ResourceTypeEndpoints ||
resType == constants.ResourceTypePersistentVolume ||
resType == constants.ResourceTypePersistentVolumeClaim ||
resType == constants.ResourceTypeVolumeAttachment ||
resType == model.ResourceTypeNode
}
// if resource type is EdgeMesh related
func isEdgeMeshResource(resType string) bool {
return resType == constants.ResourceTypeService ||
resType == constants.ResourceTypeServiceList ||
resType == constants.ResourceTypeEndpoints ||
resType == model.ResourceTypePodlist
}
func isConnected() bool {
return metaManagerConfig.Connected
}
func msgDebugInfo(message *model.Message) string {
return fmt.Sprintf("msgID[%s] resource[%s]", message.GetID(), message.GetResource())
}
func resourceUnchanged(resType string, resKey string, content []byte) bool {
if resType == model.ResourceTypePodStatus {
dbRecord, err := dao.QueryMeta("key", resKey)
if err == nil && len(*dbRecord) > 0 && string(content) == (*dbRecord)[0] {
return true
}
}
return false
}
func (m *metaManager) processInsert(message model.Message) {
var err error
var content []byte
switch message.GetContent().(type) {
case []uint8:
content = message.GetContent().([]byte)
default:
content, err = json.Marshal(message.GetContent())
if err != nil {
klog.Errorf("marshal update message content failed, %s", msgDebugInfo(&message))
feedbackError(err, "Error to marshal message content", message)
return
}
}
imitator.DefaultV2Client.Inject(message)
resKey, resType, _ := parseResource(message.GetResource())
switch resType {
case constants.ResourceTypeServiceList:
var svcList []v1.Service
err = json.Unmarshal(content, &svcList)
if err != nil {
klog.Errorf("Unmarshal insert message content failed, %s", msgDebugInfo(&message))
feedbackError(err, "Error to unmarshal", message)
return
}
for _, svc := range svcList {
data, err := json.Marshal(svc)
if err != nil {
klog.Errorf("Marshal service content failed, %v", svc)
continue
}
meta := &dao.Meta{
Key: fmt.Sprintf("%s/%s/%s", svc.Namespace, constants.ResourceTypeService, svc.Name),
Type: constants.ResourceTypeService,
Value: string(data)}
err = dao.SaveMeta(meta)
if err != nil {
klog.Errorf("Save meta %s failed, svc: %v, err: %v", string(data), svc, err)
feedbackError(err, "Error to save meta to DB", message)
return
}
}
default:
meta := &dao.Meta{
Key: resKey,
Type: resType,
Value: string(content)}
err = dao.SaveMeta(meta)
if err != nil {
klog.Errorf("save meta failed, %s: %v", msgDebugInfo(&message), err)
feedbackError(err, "Error to save meta to DB", message)
return
}
}
if resType == constants.ResourceTypeListener {
// Notify edgemesh only
resp := message.NewRespByMessage(&message, nil)
sendToEdgeMesh(resp, true)
return
}
if isEdgeMeshResource(resType) {
// Notify edgemesh
sendToEdgeMesh(&message, false)
} else {
// Notify edged
sendToEdged(&message, false)
}
resp := message.NewRespByMessage(&message, OK)
sendToCloud(resp)
}
func (m *metaManager) processUpdate(message model.Message) {
var err error
var content []byte
switch message.GetContent().(type) {
case []uint8:
content = message.GetContent().([]byte)
default:
content, err = json.Marshal(message.GetContent())
if err != nil {
klog.Errorf("marshal update message content failed, %s", msgDebugInfo(&message))
feedbackError(err, "Error to marshal message content", message)
return
}
}
imitator.DefaultV2Client.Inject(message)
resKey, resType, _ := parseResource(message.GetResource())
if resType == constants.ResourceTypeServiceList || resType == constants.ResourceTypeEndpointsList || resType == model.ResourceTypePodlist {
switch resType {
case constants.ResourceTypeEndpointsList:
var epsList []v1.Endpoints
err = json.Unmarshal(content, &epsList)
if err != nil {
klog.Errorf("Unmarshal update message content failed, %s", msgDebugInfo(&message))
feedbackError(err, "Error to unmarshal", message)
return
}
for _, eps := range epsList {
data, err := json.Marshal(eps)
if err != nil {
klog.Errorf("Marshal endpoints content failed, %v", eps)
continue
}
meta := &dao.Meta{
Key: fmt.Sprintf("%s/%s/%s", eps.Namespace, constants.ResourceTypeEndpoints, eps.Name),
Type: constants.ResourceTypeEndpoints,
Value: string(data)}
err = dao.InsertOrUpdate(meta)
if err != nil {
klog.Errorf("Update meta failed, %v", eps)
continue
}
}
sendToEdgeMesh(&message, false)
resp := message.NewRespByMessage(&message, OK)
sendToCloud(resp)
return
case constants.ResourceTypeServiceList:
var svcList []v1.Service
err = json.Unmarshal(content, &svcList)
if err != nil {
klog.Errorf("Unmarshal update message content failed, %s", msgDebugInfo(&message))
feedbackError(err, "Error to unmarshal", message)
return
}
for _, svc := range svcList {
data, err := json.Marshal(svc)
if err != nil {
klog.Errorf("Marshal service content failed, %v", svc)
continue
}
meta := &dao.Meta{
Key: fmt.Sprintf("%s/%s/%s", svc.Namespace, constants.ResourceTypeService, svc.Name),
Type: constants.ResourceTypeService,
Value: string(data)}
err = dao.InsertOrUpdate(meta)
if err != nil {
klog.Errorf("Update meta failed, %v", svc)
continue
}
}
sendToEdgeMesh(&message, false)
resp := message.NewRespByMessage(&message, OK)
sendToCloud(resp)
return
case model.ResourceTypePodlist:
meta := &dao.Meta{
Key: resKey,
Type: resType,
Value: string(content)}
err = dao.InsertOrUpdate(meta)
if err != nil {
klog.Errorf("Update meta failed, %s", msgDebugInfo(&message))
feedbackError(err, "Error to update meta to DB", message)
return
}
sendToEdgeMesh(&message, false)
resp := message.NewRespByMessage(&message, OK)
sendToCloud(resp)
return
default:
klog.Warningf("Resource type %s unknown", resType)
return
}
}
if resourceUnchanged(resType, resKey, content) {
resp := message.NewRespByMessage(&message, OK)
sendToEdged(resp, message.IsSync())
klog.Infof("resource[%s] unchanged, no notice", resKey)
return
}
meta := &dao.Meta{
Key: resKey,
Type: resType,
Value: string(content)}
err = dao.InsertOrUpdate(meta)
if err != nil {
klog.Errorf("update meta failed, %s", msgDebugInfo(&message))
feedbackError(err, "Error to update meta to DB", message)
return
}
msgSource := message.GetSource()
switch msgSource {
//case core.EdgedModuleName:
case modules.EdgedModuleName:
sendToCloud(&message)
resp := message.NewRespByMessage(&message, OK)
sendToEdged(resp, message.IsSync())
case cloudmodules.EdgeControllerModuleName,cloudmodules.DynamicControllerModuleName:
if isEdgeMeshResource(resType) {
sendToEdgeMesh(&message, message.IsSync())
} else {
sendToEdged(&message, message.IsSync())
}
resp := message.NewRespByMessage(&message, OK)
sendToCloud(resp)
case CloudFunctionModel:
beehiveContext.Send(EdgeFunctionModel, message)
case EdgeFunctionModel:
sendToCloud(&message)
default:
klog.Errorf("unsupport message source, %s", msgSource)
}
}
func (m *metaManager) processResponse(message model.Message) {
var err error
var content []byte
switch message.GetContent().(type) {
case []uint8:
content = message.GetContent().([]byte)
default:
content, err = json.Marshal(message.GetContent())
if err != nil {
klog.Errorf("marshal response message content failed, %s", msgDebugInfo(&message))
feedbackError(err, "Error to marshal message content", message)
return
}
}
resKey, resType, _ := parseResource(message.GetResource())
meta := &dao.Meta{
Key: resKey,
Type: resType,
Value: string(content)}
err = dao.InsertOrUpdate(meta)
if err != nil {
klog.Errorf("update meta failed, %s", msgDebugInfo(&message))
feedbackError(err, "Error to update meta to DB", message)
return
}
// Notify edged or edgemesh if the data is coming from cloud
if message.GetSource() == CloudControlerModel {
if resType == constants.ResourceTypeService || resType == constants.ResourceTypeEndpoints {
sendToEdgeMesh(&message, message.IsSync())
} else {
sendToEdged(&message, message.IsSync())
}
} else {
// Send to cloud if the update request is coming from edged
sendToCloud(&message)
}
}
func (m *metaManager) processDelete(message model.Message) {
imitator.DefaultV2Client.Inject(message)
err := dao.DeleteMetaByKey(message.GetResource())
if err != nil {
klog.Errorf("delete meta failed, %s", msgDebugInfo(&message))
feedbackError(err, "Error to delete meta to DB", message)
return
}
_, resType, _ := parseResource(message.GetResource())
if resType == constants.ResourceTypeService || resType == constants.ResourceTypeEndpoints {
// Notify edgemesh
sendToEdgeMesh(&message, false)
resp := message.NewRespByMessage(&message, OK)
sendToCloud(resp)
return
}
if resType == constants.ResourceTypeListener {
// Notify edgemesh only
resp := message.NewRespByMessage(&message, OK)
sendToEdgeMesh(resp, true)
return
}
if resType == model.ResourceTypePod && message.GetSource() == modules.EdgedModuleName {
sendToCloud(&message)
return
}
// Notify edged
sendToEdged(&message, false)
resp := message.NewRespByMessage(&message, OK)
sendToCloud(resp)
}
func (m *metaManager) processQuery(message model.Message) {
resKey, resType, resID := parseResource(message.GetResource())
var metas *[]string
var err error
if requireRemoteQuery(resType) && isConnected() {
metas, err = dao.QueryMeta("key", resKey)
if err != nil || len(*metas) == 0 || resType == model.ResourceTypeNode || resType == constants.ResourceTypeVolumeAttachment {
m.processRemoteQuery(message)
} else {
resp := message.NewRespByMessage(&message, *metas)
resp.SetRoute(MetaManagerModuleName, resp.GetGroup())
sendToEdged(resp, message.IsSync())
}
return
}
if resID == "" {
// Get specific type resources
metas, err = dao.QueryMeta("type", resType)
} else {
metas, err = dao.QueryMeta("key", resKey)
}
if err != nil {
klog.Errorf("query meta failed, %s", msgDebugInfo(&message))
feedbackError(err, "Error to query meta in DB", message)
} else {
resp := message.NewRespByMessage(&message, *metas)
resp.SetRoute(MetaManagerModuleName, resp.GetGroup())
if resType == constants.ResourceTypeService || resType == constants.ResourceTypeEndpoints || resType == constants.ResourceTypeListener {
sendToEdgeMesh(resp, message.IsSync())
} else {
sendToEdged(resp, message.IsSync())
}
}
}
func (m *metaManager) processRemoteQuery(message model.Message) {
go func() {
// TODO: retry
originalID := message.GetID()
message.UpdateID()
resp, err := beehiveContext.SendSync(
string(metaManagerConfig.Config.ContextSendModule),
message,
time.Duration(metaManagerConfig.Config.RemoteQueryTimeout)*time.Second)
klog.Infof("########## process get: req[%+v], resp[%+v], err[%+v]", message, resp, err)
if err != nil {
klog.Errorf("remote query failed: %v", err)
feedbackError(err, "Error to query meta in DB", message)
return
}
var content []byte
switch resp.GetContent().(type) {
case []uint8:
content = resp.GetContent().([]byte)
default:
content, err = json.Marshal(resp.GetContent())
if err != nil {
klog.Errorf("marshal remote query response content failed, %s", msgDebugInfo(&resp))
feedbackError(err, "Error to marshal message content", message)
return
}
}
resKey, resType, _ := parseResource(message.GetResource())
meta := &dao.Meta{
Key: resKey,
Type: resType,
Value: string(content)}
err = dao.InsertOrUpdate(meta)
if err != nil {
klog.Errorf("update meta failed, %s", msgDebugInfo(&resp))
}
resp.BuildHeader(resp.GetID(), originalID, resp.GetTimestamp())
if resType == constants.ResourceTypeService || resType == constants.ResourceTypeEndpoints {
sendToEdgeMesh(&resp, message.IsSync())
} else {
sendToEdged(&resp, message.IsSync())
}
respToCloud := message.NewRespByMessage(&resp, OK)
sendToCloud(respToCloud)
}()
}
func (m *metaManager) processNodeConnection(message model.Message) {
content, _ := message.GetContent().(string)
klog.Infof("node connection event occur: %s", content)
if content == connect.CloudConnected {
metaManagerConfig.Connected = true
} else if content == connect.CloudDisconnected {
metaManagerConfig.Connected = false
}
}
func (m *metaManager) processSync() {
m.syncPodStatus()
}
func (m *metaManager) syncPodStatus() {
klog.Infof("start to sync pod status in edge-store to cloud")
podStatusRecords, err := dao.QueryAllMeta("type", model.ResourceTypePodStatus)
if err != nil {
klog.Errorf("list pod status failed: %v", err)
return
}
if len(*podStatusRecords) <= 0 {
klog.Infof("list pod status, no record, skip sync")
return
}
contents := make(map[string][]interface{})
for _, v := range *podStatusRecords {
namespaceParsed, _, _, _ := util.ParseResourceEdge(v.Key, model.QueryOperation)
podKey := strings.Replace(v.Key, constants.ResourceSep+model.ResourceTypePodStatus+constants.ResourceSep, constants.ResourceSep+model.ResourceTypePod+constants.ResourceSep, 1)
podRecord, err := dao.QueryMeta("key", podKey)
if err != nil {
klog.Errorf("query pod[%s] failed: %v", podKey, err)
return
}
if len(*podRecord) <= 0 {
// pod already deleted, clear the corresponding podstatus record
err = dao.DeleteMetaByKey(v.Key)
klog.Infof("pod[%s] already deleted, clear podstatus record, result:%v", podKey, err)
continue
}
var podStatus interface{}
err = json.Unmarshal([]byte(v.Value), &podStatus)
if err != nil {
klog.Errorf("unmarshal podstatus[%s] failed, content[%s]: %v", v.Key, v.Value, err)
continue
}
contents[namespaceParsed] = append(contents[namespaceParsed], podStatus)
}
for namespace, content := range contents {
msg := model.NewMessage("").BuildRouter(MetaManagerModuleName, GroupResource, namespace+constants.ResourceSep+model.ResourceTypePodStatus, model.UpdateOperation).FillBody(content)
sendToCloud(msg)
klog.V(3).Infof("sync pod status successfully for namespaces %s, %s", namespace, msgDebugInfo(msg))
}
}
func (m *metaManager) processFunctionAction(message model.Message) {
var err error
var content []byte
switch message.GetContent().(type) {
case []uint8:
content = message.GetContent().([]byte)
default:
content, err = json.Marshal(message.GetContent())
if err != nil {
klog.Errorf("marshal save message content failed, %s: %v", msgDebugInfo(&message), err)
feedbackError(err, "Error to marshal message content", message)
return
}
}
resKey, resType, _ := parseResource(message.GetResource())
meta := &dao.Meta{
Key: resKey,
Type: resType,
Value: string(content)}
err = dao.SaveMeta(meta)
if err != nil {
klog.Errorf("save meta failed, %s: %v", msgDebugInfo(&message), err)
feedbackError(err, "Error to save meta to DB", message)
return
}
beehiveContext.Send(EdgeFunctionModel, message)
}
func (m *metaManager) processFunctionActionResult(message model.Message) {
var err error
var content []byte
switch message.GetContent().(type) {
case []uint8:
content = message.GetContent().([]byte)
default:
content, err = json.Marshal(message.GetContent())
if err != nil {
klog.Errorf("marshal save message content failed, %s: %v", msgDebugInfo(&message), err)
feedbackError(err, "Error to marshal message content", message)
return
}
}
resKey, resType, _ := parseResource(message.GetResource())
meta := &dao.Meta{
Key: resKey,
Type: resType,
Value: string(content)}
err = dao.SaveMeta(meta)
if err != nil {
klog.Errorf("save meta failed, %s: %v", msgDebugInfo(&message), err)
feedbackError(err, "Error to save meta to DB", message)
return
}
sendToCloud(&message)
}
func (m *metaManager) processVolume(message model.Message) {
klog.Info("process volume started")
back, err := beehiveContext.SendSync(modules.EdgedModuleName, message, constants.CSISyncMsgRespTimeout)
klog.Infof("process volume get: req[%+v], back[%+v], err[%+v]", message, back, err)
if err != nil {
klog.Errorf("process volume send to edged failed: %v", err)
}
resp := message.NewRespByMessage(&message, back.GetContent())
sendToCloud(resp)
klog.Infof("process volume send to cloud resp[%+v]", resp)
}
func (m *metaManager) process(message model.Message) {
operation := message.GetOperation()
switch operation {
case model.InsertOperation:
m.processInsert(message)
case model.UpdateOperation:
m.processUpdate(message)
case model.DeleteOperation:
m.processDelete(message)
case model.QueryOperation:
m.processQuery(message)
case model.ResponseOperation:
m.processResponse(message)
case messagepkg.OperationNodeConnection:
m.processNodeConnection(message)
case OperationMetaSync:
m.processSync()
case OperationFunctionAction:
m.processFunctionAction(message)
case OperationFunctionActionResult:
m.processFunctionActionResult(message)
case constants.CSIOperationTypeCreateVolume,
constants.CSIOperationTypeDeleteVolume,
constants.CSIOperationTypeControllerPublishVolume,
constants.CSIOperationTypeControllerUnpublishVolume:
m.processVolume(message)
}
}
func (m *metaManager) runMetaManager() {
go func() {
for {
select {
case <-beehiveContext.Done():
klog.Warning("MetaManager mainloop stop")
return
default:
}
if msg, err := beehiveContext.Receive(m.Name()); err == nil {
klog.V(2).Infof("get a message %+v", msg)
m.process(msg)
} else {
klog.Errorf("get a message %+v: %v", msg, err)
}
}
}()
}