forked from meshery/meshery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeshsync_events.go
219 lines (187 loc) · 5.97 KB
/
meshsync_events.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
package models
import (
"github.com/layer5io/meshkit/broker"
"github.com/layer5io/meshkit/database"
"github.com/layer5io/meshkit/logger"
"github.com/layer5io/meshkit/utils"
meshsyncmodel "github.com/layer5io/meshsync/pkg/model"
"gorm.io/gorm"
)
const (
MeshsyncStoreUpdatesSubject = "meshery-server.meshsync.store"
MeshsyncRequestSubject = "meshery.meshsync.request"
)
// TODO: Create proper error codes for the functionalities this struct implements
type MeshsyncDataHandler struct {
broker broker.Handler
dbHandler database.Handler
log logger.Handler
}
func NewMeshsyncDataHandler(broker broker.Handler, dbHandler database.Handler, log logger.Handler) *MeshsyncDataHandler {
return &MeshsyncDataHandler{
broker: broker,
dbHandler: dbHandler,
log: log,
}
}
func (mh *MeshsyncDataHandler) Run() error {
storeSubscriptionStatusChan := make(chan bool)
// this subscription is independent of whether or not the stale data in the database have been cleaned up
go mh.subscribeToMeshsyncEvents()
go mh.subsribeToStoreUpdates(storeSubscriptionStatusChan)
// to make sure that we don't ask for data before we start listening
if <-storeSubscriptionStatusChan {
// err := mh.removeStaleObjects()
// if err != nil {
// return err
// }
err := mh.requestMeshsyncStore()
if err != nil {
return err
}
}
return nil
}
func (mh *MeshsyncDataHandler) subscribeToMeshsyncEvents() {
eventsChan := make(chan *broker.Message)
err := mh.ListenToMeshSyncEvents(eventsChan)
if err != nil {
mh.log.Error(ErrBrokerSubscription(err))
return
}
mh.log.Info("subscribed to meshery broker for meshsync events")
for event := range eventsChan {
if event.EventType == broker.ErrorEvent {
// TODO: Handle errors accordingly
mh.log.Error(event.Object.(error))
continue
}
// handle the events
err := mh.meshsyncEventsAccumulator(event)
if err != nil {
mh.log.Error(err)
continue
}
}
}
func (mh *MeshsyncDataHandler) ListenToMeshSyncEvents(out chan *broker.Message) error {
err := mh.broker.SubscribeWithChannel("meshery.meshsync.core", "", out)
if err != nil {
return err
}
return nil
}
func (mh *MeshsyncDataHandler) subsribeToStoreUpdates(statusChan chan bool) {
storeChan := make(chan *broker.Message)
mh.log.Info("subscribing to store updates from meshsync on NATS subject: ", MeshsyncStoreUpdatesSubject)
err := mh.broker.SubscribeWithChannel(MeshsyncStoreUpdatesSubject, "", storeChan)
if err != nil {
mh.log.Error(ErrBrokerSubscription(err))
statusChan <- false
return
}
statusChan <- true
for storeUpdate := range storeChan {
if storeUpdate.EventType == broker.ErrorEvent {
mh.log.Error(storeUpdate.Object.(error))
continue
}
objectsSlice := storeUpdate.Object.([]interface{})
for _, object := range objectsSlice {
objectJSON, _ := utils.Marshal(object)
obj := meshsyncmodel.KubernetesResource{}
err := utils.Unmarshal(objectJSON, &obj)
if err != nil {
mh.log.Error(ErrUnmarshal(err, objectJSON))
continue
}
err = mh.persistStoreUpdate(&obj)
if err != nil {
mh.log.Error(err)
continue
}
}
}
}
// derives the state of the cluster from the events and persists it in the database
func (mh *MeshsyncDataHandler) meshsyncEventsAccumulator(event *broker.Message) error {
mh.dbHandler.Lock()
defer mh.dbHandler.Unlock()
objectJSON, _ := utils.Marshal(event.Object)
obj := meshsyncmodel.KubernetesResource{}
err := utils.Unmarshal(objectJSON, &obj)
if err != nil {
return ErrUnmarshal(err, objectJSON)
}
switch event.EventType {
case broker.Add, broker.Update:
result := mh.dbHandler.Create(&obj)
if result.Error != nil {
result = mh.dbHandler.Session(&gorm.Session{FullSaveAssociations: true}).Updates(&obj)
if result.Error != nil {
return ErrDBPut(result.Error)
}
return nil
}
case broker.Delete:
result := mh.dbHandler.Delete(&obj)
if result.Error != nil {
return ErrDBDelete(result.Error, "")
}
}
mh.log.Info("Updated database in response to ", event.EventType, " event of object: ", obj.KubernetesResourceMeta.Name, " in namespace: ", obj.KubernetesResourceMeta.Namespace, " of kind: ", obj.Kind)
return nil
}
func (mh *MeshsyncDataHandler) persistStoreUpdate(object *meshsyncmodel.KubernetesResource) error {
mh.dbHandler.Lock()
defer mh.dbHandler.Unlock()
result := mh.dbHandler.Create(object)
if result.Error != nil {
result = mh.dbHandler.Session(&gorm.Session{FullSaveAssociations: true}).Updates(object)
if result.Error != nil {
return ErrDBPut(result.Error)
}
mh.log.Info("Updated object: ", object.KubernetesResourceMeta.Name, "/", object.KubernetesResourceMeta.Namespace, " of kind: ", object.Kind, " in the database")
return nil
}
mh.log.Info("Added object: ", object.KubernetesResourceMeta.Name, "/", object.KubernetesResourceMeta.Namespace, " of kind: ", object.Kind, " to the database")
return nil
}
func RemoveStaleObjects(dbHandler database.Handler) error {
dbHandler.Lock()
defer dbHandler.Unlock()
// Clear stale meshsync data
err := dbHandler.Migrator().DropTable(
&meshsyncmodel.KubernetesKeyValue{},
&meshsyncmodel.KubernetesResource{},
&meshsyncmodel.KubernetesResourceSpec{},
&meshsyncmodel.KubernetesResourceStatus{},
&meshsyncmodel.KubernetesResourceObjectMeta{},
)
if err != nil {
return err
}
err = dbHandler.Migrator().CreateTable(
&meshsyncmodel.KubernetesKeyValue{},
&meshsyncmodel.KubernetesResource{},
&meshsyncmodel.KubernetesResourceSpec{},
&meshsyncmodel.KubernetesResourceStatus{},
&meshsyncmodel.KubernetesResourceObjectMeta{},
)
if err != nil {
return err
}
return nil
}
func (mh *MeshsyncDataHandler) requestMeshsyncStore() error {
err := mh.broker.Publish(MeshsyncRequestSubject, &broker.Message{
Request: &broker.RequestObject{
Entity: "informer-store",
// TODO: Name of the Reply subject should be taken from some sort of configuration
Payload: struct{ Reply string }{Reply: MeshsyncStoreUpdatesSubject},
}})
if err != nil {
return ErrRequestMeshsyncStore(err)
}
return nil
}