forked from joeholley/supergloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistration_snapshot_emitter.sk.go
221 lines (191 loc) · 6.22 KB
/
registration_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
// Code generated by solo-kit. DO NOT EDIT.
package v1
import (
"sync"
"time"
"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 (
mRegistrationSnapshotIn = stats.Int64("registration.supergloo.solo.io/snap_emitter/snap_in", "The number of snapshots in", "1")
mRegistrationSnapshotOut = stats.Int64("registration.supergloo.solo.io/snap_emitter/snap_out", "The number of snapshots out", "1")
registrationsnapshotInView = &view.View{
Name: "registration.supergloo.solo.io_snap_emitter/snap_in",
Measure: mRegistrationSnapshotIn,
Description: "The number of snapshots updates coming in",
Aggregation: view.Count(),
TagKeys: []tag.Key{},
}
registrationsnapshotOutView = &view.View{
Name: "registration.supergloo.solo.io/snap_emitter/snap_out",
Measure: mRegistrationSnapshotOut,
Description: "The number of snapshots updates going out",
Aggregation: view.Count(),
TagKeys: []tag.Key{},
}
)
func init() {
view.Register(registrationsnapshotInView, registrationsnapshotOutView)
}
type RegistrationEmitter interface {
Register() error
Mesh() MeshClient
MeshIngress() MeshIngressClient
Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *RegistrationSnapshot, <-chan error, error)
}
func NewRegistrationEmitter(meshClient MeshClient, meshIngressClient MeshIngressClient) RegistrationEmitter {
return NewRegistrationEmitterWithEmit(meshClient, meshIngressClient, make(chan struct{}))
}
func NewRegistrationEmitterWithEmit(meshClient MeshClient, meshIngressClient MeshIngressClient, emit <-chan struct{}) RegistrationEmitter {
return ®istrationEmitter{
mesh: meshClient,
meshIngress: meshIngressClient,
forceEmit: emit,
}
}
type registrationEmitter struct {
forceEmit <-chan struct{}
mesh MeshClient
meshIngress MeshIngressClient
}
func (c *registrationEmitter) Register() error {
if err := c.mesh.Register(); err != nil {
return err
}
if err := c.meshIngress.Register(); err != nil {
return err
}
return nil
}
func (c *registrationEmitter) Mesh() MeshClient {
return c.mesh
}
func (c *registrationEmitter) MeshIngress() MeshIngressClient {
return c.meshIngress
}
func (c *registrationEmitter) Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *RegistrationSnapshot, <-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)
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)
/* 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}:
}
}
}
}(namespace)
}
snapshots := make(chan *RegistrationSnapshot)
go func() {
originalSnapshot := RegistrationSnapshot{}
currentSnapshot := originalSnapshot.Clone()
timer := time.NewTicker(time.Second * 1)
sync := func() {
if originalSnapshot.Hash() == currentSnapshot.Hash() {
return
}
stats.Record(ctx, mRegistrationSnapshotOut.M(1))
originalSnapshot = currentSnapshot.Clone()
sentSnapshot := currentSnapshot.Clone()
snapshots <- &sentSnapshot
}
meshesByNamespace := make(map[string]MeshList)
meshingressesByNamespace := make(map[string]MeshIngressList)
for {
record := func() { stats.Record(ctx, mRegistrationSnapshotIn.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()
}
}
}()
return snapshots, errs, nil
}