forked from joeholley/supergloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreconciler_helper.go
366 lines (317 loc) · 9.68 KB
/
reconciler_helper.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
package appmesh
import (
"context"
"sync"
"github.com/aws/aws-sdk-go/service/appmesh"
"github.com/hashicorp/go-multierror"
"github.com/solo-io/go-utils/errors"
translator "github.com/solo-io/supergloo/pkg/translator/appmesh"
)
func newHelper(ctx context.Context, client Client, snapshot *translator.ResourceSnapshot) *reconcileHelper {
return &reconcileHelper{
ctx: ctx,
client: client,
snapshot: snapshot,
}
}
type reconcileHelper struct {
ctx context.Context
client Client
snapshot *translator.ResourceSnapshot
}
func (h *reconcileHelper) createAll() error {
if _, err := h.client.CreateMesh(h.ctx, h.snapshot.MeshName); err != nil {
return err
}
// Step 1:
// Create Virtual Nodes and Virtual Routers in parallel, as these do not depend on other resources.
// Virtual nodes reference Virtual Services, but the referential integrity is not enforced by the API.
errorChan1 := make(chan error, 10)
wg1 := sync.WaitGroup{}
wg1.Add(len(h.snapshot.VirtualNodes) + len(h.snapshot.VirtualRouters))
for _, vn := range h.snapshot.VirtualNodes {
go func(data appmesh.VirtualNodeData) {
defer wg1.Done()
if _, err := h.client.CreateVirtualNode(h.ctx, &data); err != nil {
errorChan1 <- err
}
}(*vn)
}
for _, vr := range h.snapshot.VirtualRouters {
go func(data appmesh.VirtualRouterData) {
defer wg1.Done()
if _, err := h.client.CreateVirtualRouter(h.ctx, &data); err != nil {
errorChan1 <- err
}
}(*vr)
}
// Close in separate go routine so we can start consuming the errors, the channel might fill up and we wait forever
go func() {
wg1.Wait()
close(errorChan1)
}()
// Blocks until channel is closed
if err := drainErrorChannel(errorChan1); err != nil {
return errors.Wrapf(err, "failed to create resources for mesh %s", h.snapshot.MeshName)
}
// Step 2:
// Create Routes and Virtual Services in parallel
errorChan2 := make(chan error)
wg2 := sync.WaitGroup{}
wg2.Add(len(h.snapshot.Routes) + len(h.snapshot.VirtualServices))
for _, route := range h.snapshot.Routes {
go func(data appmesh.RouteData) {
defer wg2.Done()
if _, err := h.client.CreateRoute(h.ctx, &data); err != nil {
errorChan2 <- err
}
}(*route)
}
for _, vs := range h.snapshot.VirtualServices {
go func(data appmesh.VirtualServiceData) {
defer wg2.Done()
if _, err := h.client.CreateVirtualService(h.ctx, &data); err != nil {
errorChan2 <- err
}
}(*vs)
}
go func() {
wg2.Wait()
close(errorChan2)
}()
if err := drainErrorChannel(errorChan2); err != nil {
return errors.Wrapf(err, "failed to create resources for mesh %s", h.snapshot.MeshName)
}
return nil
}
func (h *reconcileHelper) reconcile() error {
// List the existing resources of each type
allResources, err := ListAllForMesh(h.ctx, h.client, h.snapshot.MeshName)
if err != nil {
return err
}
markedResources, err := h.writeAndMarkResources(allResources)
if err != nil {
return err
}
// For each of the existing resources, if it has no correspondent resource in the snapshot, then delete it
if err := h.deleteUnmarkedResources(markedResources, allResources); err != nil {
return err
}
return nil
}
// For each of the resources in the snapshot:
// - if it does not exist, create it
// - if it does already exist, overwrite the existing one
// Return information about which existing resources are present in the snapshot
func (h *reconcileHelper) writeAndMarkResources(resources *Resources) (*markedResources, error) {
snap := h.snapshot
existing := buildMarkedResourcesMap(resources)
// 1. Virtual Nodes & Virtual Routers
errorChan1 := make(chan error, 50)
wg1 := sync.WaitGroup{}
wg1.Add(len(snap.VirtualNodes) + len(snap.VirtualRouters))
for name, vn := range snap.VirtualNodes {
go func(name string, data appmesh.VirtualNodeData) {
defer wg1.Done()
if _, exists := existing.VirtualNodes.Load(name); exists {
if _, err := h.client.UpdateVirtualNode(h.ctx, &data); err != nil {
errorChan1 <- err
}
// Mark existing resource
existing.VirtualNodes.Store(name, true)
} else {
if _, err := h.client.CreateVirtualNode(h.ctx, &data); err != nil {
errorChan1 <- err
}
}
}(name, *vn)
}
for name, vr := range snap.VirtualRouters {
go func(name string, data appmesh.VirtualRouterData) {
defer wg1.Done()
if _, exists := existing.VirtualRouters.Load(name); exists {
if _, err := h.client.UpdateVirtualRouter(h.ctx, &data); err != nil {
errorChan1 <- err
}
// Mark existing resource
existing.VirtualRouters.Store(name, true)
} else {
if _, err := h.client.CreateVirtualRouter(h.ctx, &data); err != nil {
errorChan1 <- err
}
}
}(name, *vr)
}
// Close in separate go routine so we can start consuming the errors, the channel might fill up and we wait forever
go func() {
wg1.Wait()
close(errorChan1)
}()
if err := drainErrorChannel(errorChan1); err != nil {
return nil, errors.Wrapf(err, "failed to write resources for mesh %s", snap.MeshName)
}
// 2. Routes (reference a VR) & Virtual Services (can reference either a VN or a VR)
errorChan2 := make(chan error)
wg2 := sync.WaitGroup{}
wg2.Add(len(snap.Routes) + len(snap.VirtualServices))
for name, route := range snap.Routes {
go func(name string, data appmesh.RouteData) {
defer wg2.Done()
if _, exists := existing.Routes.Load(name); exists {
if _, err := h.client.UpdateRoute(h.ctx, &data); err != nil {
errorChan2 <- err
}
// Mark existing resource
existing.Routes.Store(name, true)
} else {
if _, err := h.client.CreateRoute(h.ctx, &data); err != nil {
errorChan2 <- err
}
}
}(name, *route)
}
for name, vs := range snap.VirtualServices {
go func(name string, data appmesh.VirtualServiceData) {
defer wg2.Done()
if _, exists := existing.VirtualServices.Load(name); exists {
if _, err := h.client.UpdateVirtualService(h.ctx, &data); err != nil {
errorChan2 <- err
}
// Mark existing resource
existing.VirtualServices.Store(name, true)
} else {
if _, err := h.client.CreateVirtualService(h.ctx, &data); err != nil {
errorChan2 <- err
}
}
}(name, *vs)
}
go func() {
wg2.Wait()
close(errorChan2)
}()
if err := drainErrorChannel(errorChan2); err != nil {
return nil, errors.Wrapf(err, "failed to write resources for mesh %s", snap.MeshName)
}
return existing, nil
}
func (h *reconcileHelper) deleteUnmarkedResources(marked *markedResources, all *Resources) error {
// 1. Virtual Services
errorChan := make(chan error, 10)
vsWg := sync.WaitGroup{}
marked.VirtualServices.Range(func(key, value interface{}) bool {
if name, required := key.(string), value.(bool); !required {
vsWg.Add(1)
go func() {
defer vsWg.Done()
if err := h.client.DeleteVirtualService(h.ctx, h.snapshot.MeshName, name); err != nil {
errorChan <- err
}
}()
}
return true
})
go func() {
vsWg.Wait()
close(errorChan)
}()
if err := drainErrorChannel(errorChan); err != nil {
return err
}
// 2. Virtual Routers + Routes
errorChan = make(chan error, 10)
vrWg := sync.WaitGroup{}
marked.VirtualRouters.Range(func(key, value interface{}) bool {
vrName, required := key.(string), value.(bool)
vrWg.Add(1)
go func(vrName string, required bool) {
defer vrWg.Done()
// Delete the routes linked to this router
for _, routeName := range all.VirtualRouters[vrName] {
// If it is marked as required, the route has been assigned to a different VR, so don't delete it
if required, _ := marked.Routes.Load(routeName); !required.(bool) {
vrWg.Add(1)
go func(rName, vrName string) {
defer vrWg.Done()
if err := h.client.DeleteRoute(h.ctx, h.snapshot.MeshName, vrName, rName); err != nil {
errorChan <- err
}
}(routeName, vrName)
}
}
if !required {
if err := h.client.DeleteVirtualRouter(h.ctx, h.snapshot.MeshName, vrName); err != nil {
errorChan <- err
}
}
}(vrName, required)
return true
})
go func() {
vrWg.Wait()
close(errorChan)
}()
if err := drainErrorChannel(errorChan); err != nil {
return err
}
// 3. Virtual Nodes
errorChan = make(chan error, 10)
vnWg := sync.WaitGroup{}
marked.VirtualNodes.Range(func(key, value interface{}) bool {
if name, required := key.(string), value.(bool); !required {
vnWg.Add(1)
go func() {
defer vnWg.Done()
if err := h.client.DeleteVirtualNode(h.ctx, h.snapshot.MeshName, name); err != nil {
errorChan <- err
}
}()
}
return true
})
go func() {
vnWg.Wait()
close(errorChan)
}()
if err := drainErrorChannel(errorChan); err != nil {
return err
}
return nil
}
// Maps the names of all the existing resources to a boolean flag, initially set to false.
// During reconciliation, the flags is set to true if a matching resource is present in the snapshot.
// At the end of the process, the resources with a false flag will be deleted.
type markedResources struct {
VirtualNodes, VirtualServices, VirtualRouters, Routes sync.Map
}
func buildMarkedResourcesMap(r *Resources) *markedResources {
result := markedResources{
VirtualNodes: sync.Map{},
VirtualServices: sync.Map{},
VirtualRouters: sync.Map{},
Routes: sync.Map{},
}
for _, vn := range r.VirtualNodes {
result.VirtualNodes.Store(vn, false)
}
for _, vs := range r.VirtualServices {
result.VirtualServices.Store(vs, false)
}
for vr, routes := range r.VirtualRouters {
result.VirtualRouters.Store(vr, false)
for _, route := range routes {
result.Routes.Store(route, false)
}
}
return &result
}
// Blocks until channel is closed
func drainErrorChannel(errorChan chan error) error {
// Collect errors
var err *multierror.Error
for e := range errorChan {
err = multierror.Append(err, e)
}
return err.ErrorOrNil()
}