-
Notifications
You must be signed in to change notification settings - Fork 71
/
controller.go
343 lines (303 loc) · 8.51 KB
/
controller.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
package flow
import (
"log"
"os"
"strings"
"sync"
"time"
"github.com/skupperproject/skupper/api/types"
"github.com/skupperproject/skupper/pkg/config"
"github.com/skupperproject/skupper/pkg/messaging"
)
const (
FlowControllerEvent string = "FlowControllerEvent"
)
type FlowController struct {
origin string
connectionFactory messaging.ConnectionFactory
beaconOutgoing chan interface{}
heartbeatOutgoing chan interface{}
recordOutgoing chan interface{}
flushIncoming chan []interface{}
processOutgoing chan *ProcessRecord
processRecords map[string]*ProcessRecord
hostRecords map[string]*HostRecord
hostOutgoing chan *HostRecord
siteRecordController siteRecordController
startTime int64
beaconInterval time.Duration // test hooks
heartbeatInterval time.Duration
}
type PolicyEvaluator interface {
Enabled() bool
}
const WithPolicyDisabled = policyEnabledConst(false)
func NewFlowController(origin string, version string, creationTime uint64, connectionFactory messaging.ConnectionFactory, policyEvaluator PolicyEvaluator) *FlowController {
fc := &FlowController{
origin: origin,
connectionFactory: connectionFactory,
beaconOutgoing: make(chan interface{}, 10),
heartbeatOutgoing: make(chan interface{}, 10),
recordOutgoing: make(chan interface{}, 10),
flushIncoming: make(chan []interface{}, 10),
processOutgoing: make(chan *ProcessRecord, 10),
processRecords: make(map[string]*ProcessRecord),
hostRecords: make(map[string]*HostRecord),
hostOutgoing: make(chan *HostRecord, 10),
siteRecordController: newSiteRecordController(creationTime, version, policyEvaluator),
startTime: time.Now().Unix(),
}
return fc
}
func UpdateProcess(c *FlowController, deleted bool, name string, process *ProcessRecord) error {
// wait for update where host is assigned
if !deleted && process != nil && process.SourceHost != nil {
process.RecType = recordNames[Process]
if _, ok := c.processRecords[process.Identity]; !ok {
c.processRecords[process.Identity] = process
}
c.processOutgoing <- process
} else {
// deleted key may have ns prefix, check parts len
match := name
parts := strings.Split(name, "/")
if len(parts) > 1 {
match = parts[1]
}
t := time.Now()
for id, record := range c.processRecords {
if *record.Name == match {
process = record
process.EndTime = uint64(t.UnixNano()) / uint64(time.Microsecond)
delete(c.processRecords, id)
c.processOutgoing <- process
break
}
}
}
return nil
}
func UpdateHost(c *FlowController, deleted bool, name string, host *HostRecord) error {
if deleted {
for id, host := range c.hostRecords {
if *host.Name == name {
host.EndTime = uint64(time.Now().UnixNano()) / uint64(time.Microsecond)
delete(c.hostRecords, id)
break
}
}
} else if host != nil {
host.RecType = recordNames[Host]
if _, ok := c.hostRecords[host.Identity]; !ok {
c.hostRecords[host.Identity] = host
}
c.hostOutgoing <- host
}
return nil
}
func (c *FlowController) updateBeacon(stopCh <-chan struct{}) {
beaconInterval := c.beaconInterval
if beaconInterval == 0 {
beaconInterval = 10 * time.Second
}
beaconTimer := time.NewTicker(beaconInterval)
defer beaconTimer.Stop()
identity := c.origin
beacon := &BeaconRecord{
Version: 1,
SourceType: "CONTROLLER",
Address: RecordPrefix + c.origin,
Direct: "sfe." + c.origin,
Identity: identity,
}
c.beaconOutgoing <- beacon
for {
select {
case <-beaconTimer.C:
c.beaconOutgoing <- beacon
case <-stopCh:
return
}
}
}
func (c *FlowController) updateHeartbeats(stopCh <-chan struct{}) {
heartbeatInterval := c.heartbeatInterval
if heartbeatInterval == 0 {
heartbeatInterval = 2 * time.Second
}
heartbeatTimer := time.NewTicker(heartbeatInterval)
defer heartbeatTimer.Stop()
heartbeat := &HeartbeatRecord{
Version: 1,
Identity: os.Getenv("SKUPPER_SITE_ID"),
Source: "sfe." + c.origin,
}
heartbeat.Now = uint64(time.Now().UnixNano()) / uint64(time.Microsecond)
c.heartbeatOutgoing <- heartbeat
for {
select {
case <-heartbeatTimer.C:
heartbeat.Now = uint64(time.Now().UnixNano()) / uint64(time.Microsecond)
c.heartbeatOutgoing <- heartbeat
case <-stopCh:
return
}
}
}
func (c *FlowController) updateRecords(stopCh <-chan struct{}, siteRecordsIncoming <-chan *SiteRecord) {
tickerAge := time.NewTicker(10 * time.Minute)
defer tickerAge.Stop()
for {
select {
case process := <-c.processOutgoing:
c.recordOutgoing <- process
case site, ok := <-siteRecordsIncoming:
if !ok {
continue
}
c.recordOutgoing <- site
case host := <-c.hostOutgoing:
c.recordOutgoing <- host
case flushUpdates := <-c.flushIncoming:
for _, flushUpdate := range flushUpdates {
_, ok := flushUpdate.(FlushRecord)
if !ok {
log.Println("Unable to convert interface to flush")
}
}
c.recordOutgoing <- c.siteRecordController.Record()
for _, process := range c.processRecords {
c.recordOutgoing <- process
}
for _, host := range c.hostRecords {
c.recordOutgoing <- host
}
case <-tickerAge.C:
case <-stopCh:
return
}
}
}
func (c *FlowController) Start(stopCh <-chan struct{}) {
go c.run(stopCh)
}
func (c *FlowController) run(stopCh <-chan struct{}) {
beaconSender := newSender(c.connectionFactory, BeaconAddress, true, c.beaconOutgoing)
heartbeatSender := newSender(c.connectionFactory, RecordPrefix+c.origin+".heartbeats", true, c.heartbeatOutgoing)
recordSender := newSender(c.connectionFactory, RecordPrefix+c.origin, false, c.recordOutgoing)
flushReceiver := newReceiver(c.connectionFactory, DirectPrefix+c.origin, c.flushIncoming)
beaconSender.start()
heartbeatSender.start()
recordSender.start()
flushReceiver.start()
go c.updateBeacon(stopCh)
go c.updateHeartbeats(stopCh)
go c.updateRecords(stopCh, c.siteRecordController.Start(stopCh))
<-stopCh
beaconSender.stop()
heartbeatSender.stop()
recordSender.stop()
flushReceiver.stop()
}
type siteRecordController struct {
mu sync.Mutex
Identity string
CreatedAt uint64
Version string
Name string
Namespace string
Platform string
PolicyEnabled bool
policyEvaluator PolicyEvaluator
pollInterval time.Duration
}
func newSiteRecordController(createdAt uint64, version string, policyEvaluator PolicyEvaluator) siteRecordController {
var policy bool
var platformStr string
platform := config.GetPlatform()
if platform == "" || platform == types.PlatformKubernetes {
platformStr = string(types.PlatformKubernetes)
policy = policyEvaluator.Enabled()
} else if platform == types.PlatformPodman {
platformStr = string(types.PlatformPodman)
}
return siteRecordController{
Identity: os.Getenv("SKUPPER_SITE_ID"),
CreatedAt: createdAt,
Version: version,
Name: os.Getenv("SKUPPER_SITE_NAME"),
Namespace: os.Getenv("SKUPPER_NAMESPACE"),
Platform: platformStr,
PolicyEnabled: policy,
policyEvaluator: policyEvaluator,
}
}
func (c *siteRecordController) Start(stopCh <-chan struct{}) <-chan *SiteRecord {
updates := make(chan *SiteRecord, 1)
go func() {
updates <- c.Record()
if c.Platform != string(types.PlatformKubernetes) {
return
}
// watch for changes to policy enabled
pollInterval := c.pollInterval
if pollInterval <= 0 {
pollInterval = time.Second * 30
}
ticker := time.NewTicker(pollInterval)
defer ticker.Stop()
for {
select {
case <-stopCh:
return
case <-ticker.C:
if update := c.updatePolicy(); update != nil {
updates <- update
}
}
}
}()
return updates
}
func (c *siteRecordController) updatePolicy() *SiteRecord {
c.mu.Lock()
defer c.mu.Unlock()
enabled := c.policyEvaluator.Enabled()
if enabled == c.PolicyEnabled {
return nil
}
c.PolicyEnabled = enabled
policy := Disabled
if enabled {
policy = Enabled
}
return &SiteRecord{
Base: Base{
RecType: recordNames[Site],
Identity: c.Identity,
},
Policy: &policy,
}
}
func (c *siteRecordController) Record() *SiteRecord {
c.mu.Lock()
defer c.mu.Unlock()
policy := Disabled
if c.PolicyEnabled {
policy = Enabled
}
return &SiteRecord{
Base: Base{
RecType: recordNames[Site],
Identity: c.Identity,
StartTime: c.CreatedAt,
},
Name: &c.Name,
NameSpace: &c.Namespace,
Platform: &c.Platform,
Version: &c.Version,
Policy: &policy,
}
}
type policyEnabledConst bool
func (c policyEnabledConst) Enabled() bool { return bool(c) }