forked from joeholley/supergloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_snapshot_emitter.sk.go
546 lines (487 loc) · 17.9 KB
/
config_snapshot_emitter.sk.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
// Code generated by solo-kit. DO NOT EDIT.
package v1
import (
"sync"
"time"
gloo_solo_io "github.com/solo-io/gloo/projects/gloo/pkg/api/v1"
github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes "github.com/solo-io/solo-kit/pkg/api/v1/resources/common/kubernetes"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
"github.com/solo-io/go-utils/errutils"
"github.com/solo-io/solo-kit/pkg/api/v1/clients"
"github.com/solo-io/solo-kit/pkg/errors"
)
var (
mConfigSnapshotIn = stats.Int64("config.supergloo.solo.io/snap_emitter/snap_in", "The number of snapshots in", "1")
mConfigSnapshotOut = stats.Int64("config.supergloo.solo.io/snap_emitter/snap_out", "The number of snapshots out", "1")
configsnapshotInView = &view.View{
Name: "config.supergloo.solo.io_snap_emitter/snap_in",
Measure: mConfigSnapshotIn,
Description: "The number of snapshots updates coming in",
Aggregation: view.Count(),
TagKeys: []tag.Key{},
}
configsnapshotOutView = &view.View{
Name: "config.supergloo.solo.io/snap_emitter/snap_out",
Measure: mConfigSnapshotOut,
Description: "The number of snapshots updates going out",
Aggregation: view.Count(),
TagKeys: []tag.Key{},
}
)
func init() {
view.Register(configsnapshotInView, configsnapshotOutView)
}
type ConfigEmitter interface {
Register() error
Mesh() MeshClient
MeshIngress() MeshIngressClient
MeshGroup() MeshGroupClient
RoutingRule() RoutingRuleClient
SecurityRule() SecurityRuleClient
TlsSecret() TlsSecretClient
Upstream() gloo_solo_io.UpstreamClient
Pod() github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodClient
Service() github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.ServiceClient
Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *ConfigSnapshot, <-chan error, error)
}
func NewConfigEmitter(meshClient MeshClient, meshIngressClient MeshIngressClient, meshGroupClient MeshGroupClient, routingRuleClient RoutingRuleClient, securityRuleClient SecurityRuleClient, tlsSecretClient TlsSecretClient, upstreamClient gloo_solo_io.UpstreamClient, podClient github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodClient, serviceClient github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.ServiceClient) ConfigEmitter {
return NewConfigEmitterWithEmit(meshClient, meshIngressClient, meshGroupClient, routingRuleClient, securityRuleClient, tlsSecretClient, upstreamClient, podClient, serviceClient, make(chan struct{}))
}
func NewConfigEmitterWithEmit(meshClient MeshClient, meshIngressClient MeshIngressClient, meshGroupClient MeshGroupClient, routingRuleClient RoutingRuleClient, securityRuleClient SecurityRuleClient, tlsSecretClient TlsSecretClient, upstreamClient gloo_solo_io.UpstreamClient, podClient github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodClient, serviceClient github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.ServiceClient, emit <-chan struct{}) ConfigEmitter {
return &configEmitter{
mesh: meshClient,
meshIngress: meshIngressClient,
meshGroup: meshGroupClient,
routingRule: routingRuleClient,
securityRule: securityRuleClient,
tlsSecret: tlsSecretClient,
upstream: upstreamClient,
pod: podClient,
service: serviceClient,
forceEmit: emit,
}
}
type configEmitter struct {
forceEmit <-chan struct{}
mesh MeshClient
meshIngress MeshIngressClient
meshGroup MeshGroupClient
routingRule RoutingRuleClient
securityRule SecurityRuleClient
tlsSecret TlsSecretClient
upstream gloo_solo_io.UpstreamClient
pod github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodClient
service github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.ServiceClient
}
func (c *configEmitter) Register() error {
if err := c.mesh.Register(); err != nil {
return err
}
if err := c.meshIngress.Register(); err != nil {
return err
}
if err := c.meshGroup.Register(); err != nil {
return err
}
if err := c.routingRule.Register(); err != nil {
return err
}
if err := c.securityRule.Register(); err != nil {
return err
}
if err := c.tlsSecret.Register(); err != nil {
return err
}
if err := c.upstream.Register(); err != nil {
return err
}
if err := c.pod.Register(); err != nil {
return err
}
if err := c.service.Register(); err != nil {
return err
}
return nil
}
func (c *configEmitter) Mesh() MeshClient {
return c.mesh
}
func (c *configEmitter) MeshIngress() MeshIngressClient {
return c.meshIngress
}
func (c *configEmitter) MeshGroup() MeshGroupClient {
return c.meshGroup
}
func (c *configEmitter) RoutingRule() RoutingRuleClient {
return c.routingRule
}
func (c *configEmitter) SecurityRule() SecurityRuleClient {
return c.securityRule
}
func (c *configEmitter) TlsSecret() TlsSecretClient {
return c.tlsSecret
}
func (c *configEmitter) Upstream() gloo_solo_io.UpstreamClient {
return c.upstream
}
func (c *configEmitter) Pod() github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodClient {
return c.pod
}
func (c *configEmitter) Service() github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.ServiceClient {
return c.service
}
func (c *configEmitter) Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *ConfigSnapshot, <-chan error, error) {
if len(watchNamespaces) == 0 {
watchNamespaces = []string{""}
}
for _, ns := range watchNamespaces {
if ns == "" && len(watchNamespaces) > 1 {
return nil, nil, errors.Errorf("the \"\" namespace is used to watch all namespaces. Snapshots can either be tracked for " +
"specific namespaces or \"\" AllNamespaces, but not both.")
}
}
errs := make(chan error)
var done sync.WaitGroup
ctx := opts.Ctx
/* Create channel for Mesh */
type meshListWithNamespace struct {
list MeshList
namespace string
}
meshChan := make(chan meshListWithNamespace)
/* Create channel for MeshIngress */
type meshIngressListWithNamespace struct {
list MeshIngressList
namespace string
}
meshIngressChan := make(chan meshIngressListWithNamespace)
/* Create channel for MeshGroup */
type meshGroupListWithNamespace struct {
list MeshGroupList
namespace string
}
meshGroupChan := make(chan meshGroupListWithNamespace)
/* Create channel for RoutingRule */
type routingRuleListWithNamespace struct {
list RoutingRuleList
namespace string
}
routingRuleChan := make(chan routingRuleListWithNamespace)
/* Create channel for SecurityRule */
type securityRuleListWithNamespace struct {
list SecurityRuleList
namespace string
}
securityRuleChan := make(chan securityRuleListWithNamespace)
/* Create channel for TlsSecret */
type tlsSecretListWithNamespace struct {
list TlsSecretList
namespace string
}
tlsSecretChan := make(chan tlsSecretListWithNamespace)
/* Create channel for Upstream */
type upstreamListWithNamespace struct {
list gloo_solo_io.UpstreamList
namespace string
}
upstreamChan := make(chan upstreamListWithNamespace)
/* Create channel for Pod */
type podListWithNamespace struct {
list github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList
namespace string
}
podChan := make(chan podListWithNamespace)
/* Create channel for Service */
type serviceListWithNamespace struct {
list github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.ServiceList
namespace string
}
serviceChan := make(chan serviceListWithNamespace)
for _, namespace := range watchNamespaces {
/* Setup namespaced watch for Mesh */
meshNamespacesChan, meshErrs, err := c.mesh.Watch(namespace, opts)
if err != nil {
return nil, nil, errors.Wrapf(err, "starting Mesh watch")
}
done.Add(1)
go func(namespace string) {
defer done.Done()
errutils.AggregateErrs(ctx, errs, meshErrs, namespace+"-meshes")
}(namespace)
/* Setup namespaced watch for MeshIngress */
meshIngressNamespacesChan, meshIngressErrs, err := c.meshIngress.Watch(namespace, opts)
if err != nil {
return nil, nil, errors.Wrapf(err, "starting MeshIngress watch")
}
done.Add(1)
go func(namespace string) {
defer done.Done()
errutils.AggregateErrs(ctx, errs, meshIngressErrs, namespace+"-meshingresses")
}(namespace)
/* Setup namespaced watch for MeshGroup */
meshGroupNamespacesChan, meshGroupErrs, err := c.meshGroup.Watch(namespace, opts)
if err != nil {
return nil, nil, errors.Wrapf(err, "starting MeshGroup watch")
}
done.Add(1)
go func(namespace string) {
defer done.Done()
errutils.AggregateErrs(ctx, errs, meshGroupErrs, namespace+"-meshgroups")
}(namespace)
/* Setup namespaced watch for RoutingRule */
routingRuleNamespacesChan, routingRuleErrs, err := c.routingRule.Watch(namespace, opts)
if err != nil {
return nil, nil, errors.Wrapf(err, "starting RoutingRule watch")
}
done.Add(1)
go func(namespace string) {
defer done.Done()
errutils.AggregateErrs(ctx, errs, routingRuleErrs, namespace+"-routingrules")
}(namespace)
/* Setup namespaced watch for SecurityRule */
securityRuleNamespacesChan, securityRuleErrs, err := c.securityRule.Watch(namespace, opts)
if err != nil {
return nil, nil, errors.Wrapf(err, "starting SecurityRule watch")
}
done.Add(1)
go func(namespace string) {
defer done.Done()
errutils.AggregateErrs(ctx, errs, securityRuleErrs, namespace+"-securityrules")
}(namespace)
/* Setup namespaced watch for TlsSecret */
tlsSecretNamespacesChan, tlsSecretErrs, err := c.tlsSecret.Watch(namespace, opts)
if err != nil {
return nil, nil, errors.Wrapf(err, "starting TlsSecret watch")
}
done.Add(1)
go func(namespace string) {
defer done.Done()
errutils.AggregateErrs(ctx, errs, tlsSecretErrs, namespace+"-tlssecrets")
}(namespace)
/* Setup namespaced watch for Upstream */
upstreamNamespacesChan, upstreamErrs, err := c.upstream.Watch(namespace, opts)
if err != nil {
return nil, nil, errors.Wrapf(err, "starting Upstream watch")
}
done.Add(1)
go func(namespace string) {
defer done.Done()
errutils.AggregateErrs(ctx, errs, upstreamErrs, namespace+"-upstreams")
}(namespace)
/* Setup namespaced watch for Pod */
podNamespacesChan, podErrs, err := c.pod.Watch(namespace, opts)
if err != nil {
return nil, nil, errors.Wrapf(err, "starting Pod watch")
}
done.Add(1)
go func(namespace string) {
defer done.Done()
errutils.AggregateErrs(ctx, errs, podErrs, namespace+"-pods")
}(namespace)
/* Setup namespaced watch for Service */
serviceNamespacesChan, serviceErrs, err := c.service.Watch(namespace, opts)
if err != nil {
return nil, nil, errors.Wrapf(err, "starting Service watch")
}
done.Add(1)
go func(namespace string) {
defer done.Done()
errutils.AggregateErrs(ctx, errs, serviceErrs, namespace+"-services")
}(namespace)
/* Watch for changes and update snapshot */
go func(namespace string) {
for {
select {
case <-ctx.Done():
return
case meshList := <-meshNamespacesChan:
select {
case <-ctx.Done():
return
case meshChan <- meshListWithNamespace{list: meshList, namespace: namespace}:
}
case meshIngressList := <-meshIngressNamespacesChan:
select {
case <-ctx.Done():
return
case meshIngressChan <- meshIngressListWithNamespace{list: meshIngressList, namespace: namespace}:
}
case meshGroupList := <-meshGroupNamespacesChan:
select {
case <-ctx.Done():
return
case meshGroupChan <- meshGroupListWithNamespace{list: meshGroupList, namespace: namespace}:
}
case routingRuleList := <-routingRuleNamespacesChan:
select {
case <-ctx.Done():
return
case routingRuleChan <- routingRuleListWithNamespace{list: routingRuleList, namespace: namespace}:
}
case securityRuleList := <-securityRuleNamespacesChan:
select {
case <-ctx.Done():
return
case securityRuleChan <- securityRuleListWithNamespace{list: securityRuleList, namespace: namespace}:
}
case tlsSecretList := <-tlsSecretNamespacesChan:
select {
case <-ctx.Done():
return
case tlsSecretChan <- tlsSecretListWithNamespace{list: tlsSecretList, namespace: namespace}:
}
case upstreamList := <-upstreamNamespacesChan:
select {
case <-ctx.Done():
return
case upstreamChan <- upstreamListWithNamespace{list: upstreamList, namespace: namespace}:
}
case podList := <-podNamespacesChan:
select {
case <-ctx.Done():
return
case podChan <- podListWithNamespace{list: podList, namespace: namespace}:
}
case serviceList := <-serviceNamespacesChan:
select {
case <-ctx.Done():
return
case serviceChan <- serviceListWithNamespace{list: serviceList, namespace: namespace}:
}
}
}
}(namespace)
}
snapshots := make(chan *ConfigSnapshot)
go func() {
originalSnapshot := ConfigSnapshot{}
currentSnapshot := originalSnapshot.Clone()
timer := time.NewTicker(time.Second * 1)
sync := func() {
if originalSnapshot.Hash() == currentSnapshot.Hash() {
return
}
stats.Record(ctx, mConfigSnapshotOut.M(1))
originalSnapshot = currentSnapshot.Clone()
sentSnapshot := currentSnapshot.Clone()
snapshots <- &sentSnapshot
}
meshesByNamespace := make(map[string]MeshList)
meshingressesByNamespace := make(map[string]MeshIngressList)
meshgroupsByNamespace := make(map[string]MeshGroupList)
routingrulesByNamespace := make(map[string]RoutingRuleList)
securityrulesByNamespace := make(map[string]SecurityRuleList)
tlssecretsByNamespace := make(map[string]TlsSecretList)
upstreamsByNamespace := make(map[string]gloo_solo_io.UpstreamList)
podsByNamespace := make(map[string]github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList)
servicesByNamespace := make(map[string]github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.ServiceList)
for {
record := func() { stats.Record(ctx, mConfigSnapshotIn.M(1)) }
select {
case <-timer.C:
sync()
case <-ctx.Done():
close(snapshots)
done.Wait()
close(errs)
return
case <-c.forceEmit:
sentSnapshot := currentSnapshot.Clone()
snapshots <- &sentSnapshot
case meshNamespacedList := <-meshChan:
record()
namespace := meshNamespacedList.namespace
// merge lists by namespace
meshesByNamespace[namespace] = meshNamespacedList.list
var meshList MeshList
for _, meshes := range meshesByNamespace {
meshList = append(meshList, meshes...)
}
currentSnapshot.Meshes = meshList.Sort()
case meshIngressNamespacedList := <-meshIngressChan:
record()
namespace := meshIngressNamespacedList.namespace
// merge lists by namespace
meshingressesByNamespace[namespace] = meshIngressNamespacedList.list
var meshIngressList MeshIngressList
for _, meshingresses := range meshingressesByNamespace {
meshIngressList = append(meshIngressList, meshingresses...)
}
currentSnapshot.Meshingresses = meshIngressList.Sort()
case meshGroupNamespacedList := <-meshGroupChan:
record()
namespace := meshGroupNamespacedList.namespace
// merge lists by namespace
meshgroupsByNamespace[namespace] = meshGroupNamespacedList.list
var meshGroupList MeshGroupList
for _, meshgroups := range meshgroupsByNamespace {
meshGroupList = append(meshGroupList, meshgroups...)
}
currentSnapshot.Meshgroups = meshGroupList.Sort()
case routingRuleNamespacedList := <-routingRuleChan:
record()
namespace := routingRuleNamespacedList.namespace
// merge lists by namespace
routingrulesByNamespace[namespace] = routingRuleNamespacedList.list
var routingRuleList RoutingRuleList
for _, routingrules := range routingrulesByNamespace {
routingRuleList = append(routingRuleList, routingrules...)
}
currentSnapshot.Routingrules = routingRuleList.Sort()
case securityRuleNamespacedList := <-securityRuleChan:
record()
namespace := securityRuleNamespacedList.namespace
// merge lists by namespace
securityrulesByNamespace[namespace] = securityRuleNamespacedList.list
var securityRuleList SecurityRuleList
for _, securityrules := range securityrulesByNamespace {
securityRuleList = append(securityRuleList, securityrules...)
}
currentSnapshot.Securityrules = securityRuleList.Sort()
case tlsSecretNamespacedList := <-tlsSecretChan:
record()
namespace := tlsSecretNamespacedList.namespace
// merge lists by namespace
tlssecretsByNamespace[namespace] = tlsSecretNamespacedList.list
var tlsSecretList TlsSecretList
for _, tlssecrets := range tlssecretsByNamespace {
tlsSecretList = append(tlsSecretList, tlssecrets...)
}
currentSnapshot.Tlssecrets = tlsSecretList.Sort()
case upstreamNamespacedList := <-upstreamChan:
record()
namespace := upstreamNamespacedList.namespace
// merge lists by namespace
upstreamsByNamespace[namespace] = upstreamNamespacedList.list
var upstreamList gloo_solo_io.UpstreamList
for _, upstreams := range upstreamsByNamespace {
upstreamList = append(upstreamList, upstreams...)
}
currentSnapshot.Upstreams = upstreamList.Sort()
case podNamespacedList := <-podChan:
record()
namespace := podNamespacedList.namespace
// merge lists by namespace
podsByNamespace[namespace] = podNamespacedList.list
var podList github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.PodList
for _, pods := range podsByNamespace {
podList = append(podList, pods...)
}
currentSnapshot.Pods = podList.Sort()
case serviceNamespacedList := <-serviceChan:
record()
namespace := serviceNamespacedList.namespace
// merge lists by namespace
servicesByNamespace[namespace] = serviceNamespacedList.list
var serviceList github_com_solo_io_solo_kit_pkg_api_v1_resources_common_kubernetes.ServiceList
for _, services := range servicesByNamespace {
serviceList = append(serviceList, services...)
}
currentSnapshot.Services = serviceList.Sort()
}
}
}()
return snapshots, errs, nil
}