-
Notifications
You must be signed in to change notification settings - Fork 684
/
aggregator.go
260 lines (225 loc) · 6.76 KB
/
aggregator.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
package main
import (
"encoding/json"
"os/exec"
"strings"
"sync"
"time"
"github.com/datawire/ambassador/pkg/consulwatch"
"github.com/datawire/ambassador/pkg/limiter"
"github.com/datawire/ambassador/pkg/watt"
"github.com/datawire/ambassador/pkg/k8s"
"github.com/datawire/ambassador/pkg/supervisor"
)
type WatchHook func(p *supervisor.Process, snapshot string) WatchSet
type aggregator struct {
// Input channel used to tell us about kubernetes state.
KubernetesEvents chan k8sEvent
// Input channel used to tell us about consul endpoints.
ConsulEvents chan consulEvent
// Output channel used to communicate with the k8s watch manager.
k8sWatches chan<- []KubernetesWatchSpec
// Output channel used to communicate with the consul watch manager.
consulWatches chan<- []ConsulWatchSpec
// Output channel used to communicate with the invoker.
snapshots chan<- string
// We won't consider ourselves "bootstrapped" until we hear
// about all these kinds.
requiredKinds []string
watchHook WatchHook
limiter limiter.Limiter
ids map[string]bool
kubernetesResources map[string]map[string][]k8s.Resource
consulEndpoints map[string]consulwatch.Endpoints
bootstrapped bool
notifyMux sync.Mutex
}
func NewAggregator(snapshots chan<- string, k8sWatches chan<- []KubernetesWatchSpec, consulWatches chan<- []ConsulWatchSpec,
requiredKinds []string, watchHook WatchHook, limiter limiter.Limiter) *aggregator {
return &aggregator{
KubernetesEvents: make(chan k8sEvent),
ConsulEvents: make(chan consulEvent),
k8sWatches: k8sWatches,
consulWatches: consulWatches,
snapshots: snapshots,
requiredKinds: requiredKinds,
watchHook: watchHook,
limiter: limiter,
ids: make(map[string]bool),
kubernetesResources: make(map[string]map[string][]k8s.Resource),
consulEndpoints: make(map[string]consulwatch.Endpoints),
}
}
func (a *aggregator) Work(p *supervisor.Process) error {
p.Ready()
for {
select {
case event := <-a.KubernetesEvents:
a.setKubernetesResources(event)
a.maybeNotify(p)
case event := <-a.ConsulEvents:
a.updateConsulResources(event)
a.maybeNotify(p)
case <-p.Shutdown():
return nil
}
}
}
func (a *aggregator) updateConsulResources(event consulEvent) {
a.ids[event.WatchId] = true
a.consulEndpoints[event.Endpoints.Service] = event.Endpoints
}
func (a *aggregator) setKubernetesResources(event k8sEvent) {
a.ids[event.watchId] = true
submap, ok := a.kubernetesResources[event.watchId]
if !ok {
submap = make(map[string][]k8s.Resource)
a.kubernetesResources[event.watchId] = submap
}
submap[event.kind] = event.resources
}
func (a *aggregator) generateSnapshot() (string, error) {
k8sResources := make(map[string][]k8s.Resource)
for _, submap := range a.kubernetesResources {
for k, v := range submap {
k8sResources[k] = append(k8sResources[k], v...)
}
}
s := watt.Snapshot{
Consul: watt.ConsulSnapshot{Endpoints: a.consulEndpoints},
Kubernetes: k8sResources,
}
jsonBytes, err := json.MarshalIndent(s, "", " ")
if err != nil {
return "{}", err
}
return string(jsonBytes), nil
}
func (a *aggregator) isKubernetesBootstrapped(p *supervisor.Process) bool {
submap, sok := a.kubernetesResources[""]
if !sok {
return false
}
for _, k := range a.requiredKinds {
_, ok := submap[k]
if !ok {
return false
}
}
return true
}
// Returns true if the current state of the world is complete. The
// kubernetes state of the world is always complete by definition
// because the kubernetes client provides that guarantee. The
// aggregate state of the world is complete when any consul services
// referenced by kubernetes have populated endpoint information (even
// if the value of the populated info is an empty set of endpoints).
func (a *aggregator) isComplete(p *supervisor.Process, watchset WatchSet) bool {
complete := true
for _, w := range watchset.KubernetesWatches {
if _, ok := a.ids[w.WatchId()]; ok {
p.Logf("initialized k8s watch: %s", w.WatchId())
} else {
complete = false
p.Logf("waiting for k8s watch: %s", w.WatchId())
}
}
for _, w := range watchset.ConsulWatches {
if _, ok := a.ids[w.WatchId()]; ok {
p.Logf("initialized k8s watch: %s", w.WatchId())
} else {
complete = false
p.Logf("waiting for consul watch: %s", w.WatchId())
}
}
return complete
}
func (a *aggregator) maybeNotify(p *supervisor.Process) {
now := time.Now()
delay := a.limiter.Limit(now)
if delay == 0 {
a.notify(p)
} else if delay > 0 {
time.AfterFunc(delay, func() {
a.notify(p)
})
}
}
func (a *aggregator) notify(p *supervisor.Process) {
a.notifyMux.Lock()
defer a.notifyMux.Unlock()
if !a.isKubernetesBootstrapped(p) {
return
}
watchset := a.getWatches(p)
p.Logf("found %d kubernetes watches", len(watchset.KubernetesWatches))
p.Logf("found %d consul watches", len(watchset.ConsulWatches))
a.k8sWatches <- watchset.KubernetesWatches
a.consulWatches <- watchset.ConsulWatches
if !a.bootstrapped && a.isComplete(p, watchset) {
p.Logf("bootstrapped!")
a.bootstrapped = true
}
if a.bootstrapped {
snapshot, err := a.generateSnapshot()
if err != nil {
p.Logf("generate snapshot failed %v", err)
return
}
a.snapshots <- snapshot
}
}
func (a *aggregator) getWatches(p *supervisor.Process) WatchSet {
snapshot, err := a.generateSnapshot()
if err != nil {
p.Logf("generate snapshot failed %v", err)
return WatchSet{}
}
result := a.watchHook(p, snapshot)
return result.interpolate()
}
func ExecWatchHook(watchHooks []string) WatchHook {
return func(p *supervisor.Process, snapshot string) WatchSet {
result := WatchSet{}
for _, hook := range watchHooks {
ws := invokeHook(p, hook, snapshot)
result.KubernetesWatches = append(result.KubernetesWatches, ws.KubernetesWatches...)
result.ConsulWatches = append(result.ConsulWatches, ws.ConsulWatches...)
}
return result
}
}
func lines(st string) []string {
return strings.Split(st, "\n")
}
func invokeHook(p *supervisor.Process, hook, snapshot string) WatchSet {
cmd := exec.Command(hook)
cmd.Stdin = strings.NewReader(snapshot)
var watches, errors strings.Builder
cmd.Stdout = &watches
cmd.Stderr = &errors
err := cmd.Run()
stderr := errors.String()
if stderr != "" {
for _, line := range lines(stderr) {
p.Logf("watch hook stderr: %s", line)
}
}
if err != nil {
p.Logf("watch hook failed: %v", err)
return WatchSet{}
}
encoded := watches.String()
decoder := json.NewDecoder(strings.NewReader(encoded))
decoder.DisallowUnknownFields()
result := WatchSet{}
err = decoder.Decode(&result)
if err != nil {
for _, line := range lines(encoded) {
p.Logf("watch hook: %s", line)
}
p.Logf("watchset decode failed: %v", err)
return WatchSet{}
}
return result
}