-
Notifications
You must be signed in to change notification settings - Fork 13
/
snapshot_emitter_template.go
356 lines (307 loc) · 10.8 KB
/
snapshot_emitter_template.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
package templates
import (
"text/template"
)
var ResourceGroupEmitterTemplate = template.Must(template.New("resource_group_emitter").Funcs(Funcs).Parse(
`package {{ .Project.ProjectConfig.Version }}
{{- $client_declarations := new_str_slice }}
{{- $clients := new_str_slice }}
{{- range .Resources}}
{{- $client_declarations := (append_str_slice $client_declarations (printf "%vClient %v%vClient" (lower_camel .Name) .ImportPrefix .Name)) }}
{{- $clients := (append_str_slice $clients (printf "%vClient" (lower_camel .Name))) }}
{{- end}}
{{- $client_declarations := (join_str_slice $client_declarations ", ") }}
{{- $clients := (join_str_slice $clients ", ") }}
import (
"fmt"
"sync"
"time"
{{ .Imports }}
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
"go.uber.org/zap"
"github.com/solo-io/solo-kit/pkg/api/v1/clients"
"github.com/solo-io/solo-kit/pkg/errors"
skstats "github.com/solo-io/solo-kit/pkg/stats"
"github.com/solo-io/go-utils/errutils"
"github.com/solo-io/go-utils/contextutils"
)
{{ $emitter_prefix := (print (snake .Name) "/emitter") }}
{{ $resource_group := upper_camel .GoName }}
var (
// Deprecated. See m{{ $resource_group }}ResourcesIn
m{{ $resource_group }}SnapshotIn = stats.Int64("{{ $emitter_prefix }}/snap_in", "Deprecated. Use {{ $emitter_prefix }}/resources_in. The number of snapshots in", "1")
// metrics for emitter
m{{ $resource_group }}ResourcesIn = stats.Int64("{{ $emitter_prefix }}/resources_in", "The number of resource lists received on open watch channels", "1")
m{{ $resource_group }}SnapshotOut = stats.Int64("{{ $emitter_prefix }}/snap_out", "The number of snapshots out", "1")
m{{ $resource_group }}SnapshotMissed = stats.Int64("{{ $emitter_prefix }}/snap_missed", "The number of snapshots missed", "1")
// views for emitter
// deprecated: see {{ lower_camel .GoName }}ResourcesInView
{{ lower_camel .GoName }}snapshotInView = &view.View{
Name: "{{ $emitter_prefix }}/snap_in",
Measure: m{{ $resource_group }}SnapshotIn,
Description: "Deprecated. Use {{ $emitter_prefix }}/resources_in. The number of snapshots updates coming in.",
Aggregation: view.Count(),
TagKeys: []tag.Key{
},
}
{{ lower_camel .GoName }}ResourcesInView = &view.View{
Name: "{{ $emitter_prefix }}/resources_in",
Measure: m{{ $resource_group }}ResourcesIn,
Description: "The number of resource lists received on open watch channels",
Aggregation: view.Count(),
TagKeys: []tag.Key{
skstats.NamespaceKey,
skstats.ResourceKey,
},
}
{{ lower_camel .GoName }}snapshotOutView = &view.View{
Name: "{{ $emitter_prefix }}/snap_out",
Measure: m{{ $resource_group }}SnapshotOut,
Description: "The number of snapshots updates going out",
Aggregation: view.Count(),
TagKeys: []tag.Key{
},
}
{{ lower_camel .GoName }}snapshotMissedView = &view.View{
Name: "{{ $emitter_prefix }}/snap_missed",
Measure: m{{ $resource_group }}SnapshotMissed,
Description: "The number of snapshots updates going missed. this can happen in heavy load. missed snapshot will be re-tried after a second.",
Aggregation: view.Count(),
TagKeys: []tag.Key{
},
}
)
func init() {
view.Register(
{{ lower_camel .GoName }}snapshotInView,
{{ lower_camel .GoName }}snapshotOutView,
{{ lower_camel .GoName }}snapshotMissedView,
{{ lower_camel .GoName }}ResourcesInView,
)
}
type {{ .GoName }}SnapshotEmitter interface {
Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *{{ .GoName }}Snapshot, <-chan error, error)
}
type {{ .GoName }}Emitter interface {
{{ .GoName }}SnapshotEmitter
Register() error
{{- range .Resources}}
{{ .Name }}() {{ .ImportPrefix }}{{ .Name }}Client
{{- end}}
}
func New{{ .GoName }}Emitter({{ $client_declarations }}) {{ .GoName }}Emitter {
return New{{ .GoName }}EmitterWithEmit({{ $clients }}, make(chan struct{}))
}
func New{{ .GoName }}EmitterWithEmit({{ $client_declarations }}, emit <-chan struct{}) {{ .GoName }}Emitter {
return &{{ lower_camel .GoName }}Emitter{
{{- range .Resources}}
{{ lower_camel .Name }}:{{ lower_camel .Name }}Client,
{{- end}}
forceEmit: emit,
}
}
type {{ lower_camel .GoName }}Emitter struct {
forceEmit <- chan struct{}
{{- range .Resources}}
{{ lower_camel .Name }} {{ .ImportPrefix }}{{ .Name }}Client
{{- end}}
}
func (c *{{ lower_camel .GoName }}Emitter) Register() error {
{{- range .Resources}}
if err := c.{{ lower_camel .Name }}.Register(); err != nil {
return err
}
{{- end}}
return nil
}
{{- range .Resources}}
func (c *{{ lower_camel $.GoName }}Emitter) {{ .Name }}() {{ .ImportPrefix }}{{ .Name }}Client {
return c.{{ lower_camel .Name }}
}
{{- end}}
func (c *{{ lower_camel .GoName }}Emitter) Snapshots(watchNamespaces []string, opts clients.WatchOpts) (<-chan *{{ .GoName }}Snapshot, <-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
{{- range .Resources}}
/* Create channel for {{ .Name }} */
{{- if (not .ClusterScoped) }}
type {{ lower_camel .Name }}ListWithNamespace struct {
list {{ .ImportPrefix }}{{ .Name }}List
namespace string
}
{{ lower_camel .Name }}Chan := make(chan {{ lower_camel .Name }}ListWithNamespace)
var initial{{ upper_camel .Name }}List {{ .ImportPrefix }}{{ .Name }}List{{- end }}
{{- end}}
currentSnapshot := {{ .GoName }}Snapshot{}
{{- range .Resources}}
{{- if not .ClusterScoped }}
{{ lower_camel .PluralName }}ByNamespace := make(map[string]{{ .ImportPrefix }}{{ .Name }}List)
{{- end }}
{{- end }}
for _, namespace := range watchNamespaces {
{{- range .Resources}}
{{- if (not .ClusterScoped) }}
/* Setup namespaced watch for {{ .Name }} */
{
{{ lower_camel .PluralName }}, err := c.{{ lower_camel .Name }}.List(namespace, clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector})
if err != nil {
return nil, nil, errors.Wrapf(err, "initial {{ .Name }} list")
}
initial{{ upper_camel .Name }}List = append(initial{{ upper_camel .Name }}List, {{ lower_camel .PluralName }}...)
{{ lower_camel .PluralName }}ByNamespace[namespace] = {{ lower_camel .PluralName }}
}
{{ lower_camel .Name }}NamespacesChan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(namespace, opts)
if err != nil {
return nil, nil, errors.Wrapf(err, "starting {{ .Name }} watch")
}
done.Add(1)
go func(namespace string) {
defer done.Done()
errutils.AggregateErrs(ctx, errs, {{ lower_camel .Name }}Errs, namespace+"-{{ lower_camel .PluralName }}")
}(namespace)
{{- end }}
{{- end}}
/* Watch for changes and update snapshot */
go func(namespace string) {
for {
select {
case <-ctx.Done():
return
{{- range .Resources}}
{{- if (not .ClusterScoped) }}
case {{ lower_camel .Name }}List, ok := <- {{ lower_camel .Name }}NamespacesChan:
if !ok {
return
}
select {
case <-ctx.Done():
return
case {{ lower_camel .Name }}Chan <- {{ lower_camel .Name }}ListWithNamespace{list:{{ lower_camel .Name }}List, namespace:namespace}:
}
{{- end }}
{{- end}}
}
}
}(namespace)
}
{{- range .Resources}}
{{- if .ClusterScoped }}
/* Setup cluster-wide watch for {{ .Name }} */
var err error
currentSnapshot.{{ upper_camel .PluralName }},err = c.{{ lower_camel .Name }}.List(clients.ListOpts{Ctx: opts.Ctx, Selector: opts.Selector})
if err != nil {
return nil, nil, errors.Wrapf(err, "initial {{ .Name }} list")
}
{{ lower_camel .Name }}Chan, {{ lower_camel .Name }}Errs, err := c.{{ lower_camel .Name }}.Watch(opts)
if err != nil {
return nil, nil, errors.Wrapf(err, "starting {{ .Name }} watch")
}
done.Add(1)
go func() {
defer done.Done()
errutils.AggregateErrs(ctx, errs, {{ lower_camel .Name }}Errs, "{{ lower_camel .PluralName }}")
}()
{{- else }}
/* Initialize snapshot for {{ upper_camel .PluralName }} */
currentSnapshot.{{ upper_camel .PluralName }} = initial{{ upper_camel .Name }}List.Sort()
{{- end }}
{{- end}}
snapshots := make(chan *{{ .GoName }}Snapshot)
go func() {
// sent initial snapshot to kick off the watch
initialSnapshot := currentSnapshot.Clone()
snapshots <- &initialSnapshot
timer := time.NewTicker(time.Second * 1)
previousHash, err := currentSnapshot.Hash(nil)
if err != nil {
contextutils.LoggerFrom(ctx).Panicw("error while hashing, this should never happen", zap.Error(err))
}
sync := func() {
currentHash, err := currentSnapshot.Hash(nil)
// this should never happen, so panic if it does
if err != nil {
contextutils.LoggerFrom(ctx).Panicw("error while hashing, this should never happen", zap.Error(err))
}
if previousHash == currentHash {
return
}
sentSnapshot := currentSnapshot.Clone()
select {
case snapshots <- &sentSnapshot:
stats.Record(ctx, m{{ $resource_group }}SnapshotOut.M(1))
previousHash = currentHash
default:
stats.Record(ctx, m{{ $resource_group }}SnapshotMissed.M(1))
}
}
defer func() {
close(snapshots)
// we must wait for done before closing the error chan,
// to avoid sending on close channel.
done.Wait()
close(errs)
}()
for {
record := func(){stats.Record(ctx, m{{ $resource_group }}SnapshotIn.M(1))}
select {
case <-timer.C:
sync()
case <-ctx.Done():
return
case <-c.forceEmit:
sentSnapshot := currentSnapshot.Clone()
snapshots <- &sentSnapshot
{{- range .Resources}}
{{- if .ClusterScoped }}
case {{ lower_camel .Name }}List, ok := <- {{ lower_camel .Name }}Chan:
if !ok {
return
}
record()
skstats.IncrementResourceCount(
ctx,
"<all>",
"{{ snake .Name }}",
m{{ $resource_group }}ResourcesIn,
)
currentSnapshot.{{ upper_camel .PluralName }} = {{ lower_camel .Name }}List
{{- else }}
case {{ lower_camel .Name }}NamespacedList, ok := <- {{ lower_camel .Name }}Chan:
if !ok {
return
}
record()
namespace := {{ lower_camel .Name }}NamespacedList.namespace
skstats.IncrementResourceCount(
ctx,
namespace,
"{{ snake .Name }}",
m{{ $resource_group }}ResourcesIn,
)
// merge lists by namespace
{{ lower_camel .PluralName }}ByNamespace[namespace] = {{ lower_camel .Name }}NamespacedList.list
var {{ lower_camel .Name }}List {{ .ImportPrefix }}{{ .Name }}List
for _, {{ lower_camel .PluralName }} := range {{ lower_camel .PluralName }}ByNamespace {
{{ lower_camel .Name }}List = append({{ lower_camel .Name }}List, {{ lower_camel .PluralName }}...)
}
currentSnapshot.{{ upper_camel .PluralName }} = {{ lower_camel .Name }}List.Sort()
{{- end }}
{{- end}}
}
}
}()
return snapshots, errs, nil
}
`))