forked from alauda/felix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc_graph.go
396 lines (364 loc) · 12.6 KB
/
calc_graph.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
// Copyright (c) 2016-2018 Tigera, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package calc
import (
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"github.com/projectcalico/felix/config"
"github.com/projectcalico/felix/dispatcher"
"github.com/projectcalico/felix/labelindex"
"github.com/projectcalico/felix/proto"
"github.com/projectcalico/libcalico-go/lib/backend/api"
"github.com/projectcalico/libcalico-go/lib/backend/model"
"github.com/projectcalico/libcalico-go/lib/net"
)
var (
gaugeNumActiveSelectors = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "felix_active_local_selectors",
Help: "Number of active selectors on this host.",
})
gaugeNumActiveTags = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "felix_active_local_tags",
Help: "Number of active tags on this host.",
})
)
func init() {
prometheus.MustRegister(gaugeNumActiveTags)
prometheus.MustRegister(gaugeNumActiveSelectors)
}
type ipSetUpdateCallbacks interface {
OnIPSetAdded(setID string, ipSetType proto.IPSetUpdate_IPSetType)
OnIPSetMemberAdded(setID string, ip labelindex.IPSetMember)
OnIPSetMemberRemoved(setID string, ip labelindex.IPSetMember)
OnIPSetRemoved(setID string)
}
type rulesUpdateCallbacks interface {
OnPolicyActive(model.PolicyKey, *ParsedRules)
OnPolicyInactive(model.PolicyKey)
OnProfileActive(model.ProfileRulesKey, *ParsedRules)
OnProfileInactive(model.ProfileRulesKey)
}
type endpointCallbacks interface {
OnEndpointTierUpdate(endpointKey model.Key,
endpoint interface{},
filteredTiers []tierInfo)
}
type configCallbacks interface {
OnConfigUpdate(globalConfig, hostConfig map[string]string)
OnDatastoreNotReady()
}
type passthruCallbacks interface {
OnHostIPUpdate(hostname string, ip *net.IP)
OnHostIPRemove(hostname string)
OnIPPoolUpdate(model.IPPoolKey, *model.IPPool)
OnIPPoolRemove(model.IPPoolKey)
OnServiceAccountUpdate(*proto.ServiceAccountUpdate)
OnServiceAccountRemove(proto.ServiceAccountID)
OnNamespaceUpdate(*proto.NamespaceUpdate)
OnNamespaceRemove(proto.NamespaceID)
}
type routeCallbacks interface {
OnRouteUpdate(update *proto.RouteUpdate)
OnRouteRemove(dst string)
}
type vxlanCallbacks interface {
OnVTEPUpdate(update *proto.VXLANTunnelEndpointUpdate)
OnVTEPRemove(node string)
}
type PipelineCallbacks interface {
ipSetUpdateCallbacks
rulesUpdateCallbacks
endpointCallbacks
configCallbacks
passthruCallbacks
routeCallbacks
vxlanCallbacks
}
type CalcGraph struct {
// AllUpdDispatcher is the input node to the calculation graph.
AllUpdDispatcher *dispatcher.Dispatcher
activeRulesCalculator *ActiveRulesCalculator
}
func NewCalculationGraph(callbacks PipelineCallbacks, conf *config.Config) *CalcGraph {
hostname := conf.FelixHostname
log.Infof("Creating calculation graph, filtered to hostname %v", hostname)
// The source of the processing graph, this dispatcher will be fed all the updates from the
// datastore, fanning them out to the registered receivers.
//
// Syncer
// ||
// || All updates
// \/
// Dispatcher (all updates)
// / | \
// / | \ Updates filtered by type
// / | \
// receiver_1 ... receiver_n
//
allUpdDispatcher := dispatcher.NewDispatcher()
// Some of the receivers only need to know about local endpoints. Create a second dispatcher
// that will filter out non-local endpoints.
//
// ...
// Dispatcher (all updates)
// ... \
// \ All Host/Workload Endpoints
// \
// Dispatcher (local updates)
// <filter>
// / | \
// / | \ Local Host/Workload Endpoints only
// / | \
// receiver_1 ... receiver_n
//
localEndpointDispatcher := dispatcher.NewDispatcher()
(*localEndpointDispatcherReg)(localEndpointDispatcher).RegisterWith(allUpdDispatcher)
localEndpointFilter := &endpointHostnameFilter{hostname: hostname}
localEndpointFilter.RegisterWith(localEndpointDispatcher)
// The active rules calculator matches local endpoints against policies and profiles to figure
// out which policies/profiles are active on this host. Limiting to policies that apply to
// local endpoints significantly cuts down the number of policies that Felix has to
// render into the dataplane.
//
// ...
// Dispatcher (all updates)
// / \
// / \ All Host/Workload Endpoints
// / \
// / Dispatcher (local updates)
// / |
// | Policies | Local Host/Workload Endpoints only
// | Profiles |
// | |
// Active Rules Calculator
// |
// | Locally active policies/profiles
// ...
//
activeRulesCalc := NewActiveRulesCalculator()
activeRulesCalc.RegisterWith(localEndpointDispatcher, allUpdDispatcher)
// The active rules calculator only figures out which rules are active, it doesn't extract
// any information from the rules. The rule scanner takes the output from the active rules
// calculator and scans the individual rules for selectors, tags, and named ports. It
// generates events when a new selector/tag/named port starts/stops being used.
//
// ...
// Active Rules Calculator
// |
// | Locally active policies/profiles
// |
// Rule scanner
// | \
// | \ Locally active tags/selectors/named ports
// | \
// | ...
// |
// | IP set active/inactive
// |
// <dataplane>
//
ruleScanner := NewRuleScanner()
// Wire up the rule scanner's inputs.
activeRulesCalc.RuleScanner = ruleScanner
// Send IP set added/removed events to the dataplane. We'll hook up the other outputs
// below.
ruleScanner.RulesUpdateCallbacks = callbacks
// The rule scanner only goes as far as figuring out which tags/selectors/named ports are
// active. Next we need to figure out which endpoints (and hence which IP addresses/ports) are
// in each tag/selector/named port. The IP set member index calculates the set of IPs and named
// ports that should be in each IP set. To do that, it matches the active selectors/tags/named
// ports extracted by the rule scanner against all the endpoints.
//
// ...
// Dispatcher (all updates)
// |
// | All endpoints
// |
// | ...
// | Rule scanner
// | | \
// | ... \ Locally active tags/selectors/named ports
// \ |
// \_____ |
// \ |
// IP set member index
// |
// | IP set member added/removed
// |
// <dataplane>
//
ipsetMemberIndex := labelindex.NewSelectorAndNamedPortIndex()
// Wire up the inputs to the IP set member index.
ipsetMemberIndex.RegisterWith(allUpdDispatcher)
ruleScanner.OnIPSetActive = func(ipSet *IPSetData) {
log.WithField("ipSet", ipSet).Info("IPSet now active")
callbacks.OnIPSetAdded(ipSet.UniqueID(), ipSet.DataplaneProtocolType())
ipsetMemberIndex.UpdateIPSet(ipSet.UniqueID(), ipSet.Selector, ipSet.NamedPortProtocol, ipSet.NamedPort)
gaugeNumActiveSelectors.Inc()
}
ruleScanner.OnIPSetInactive = func(ipSet *IPSetData) {
log.WithField("ipSet", ipSet).Info("IPSet now inactive")
ipsetMemberIndex.DeleteIPSet(ipSet.UniqueID())
callbacks.OnIPSetRemoved(ipSet.UniqueID())
gaugeNumActiveSelectors.Dec()
}
// Send the IP set member index's outputs to the dataplane.
ipsetMemberIndex.OnMemberAdded = func(ipSetID string, member labelindex.IPSetMember) {
if log.GetLevel() >= log.DebugLevel {
log.WithFields(log.Fields{
"ipSetID": ipSetID,
"member": member,
}).Debug("Member added to IP set.")
}
callbacks.OnIPSetMemberAdded(ipSetID, member)
}
ipsetMemberIndex.OnMemberRemoved = func(ipSetID string, member labelindex.IPSetMember) {
if log.GetLevel() >= log.DebugLevel {
log.WithFields(log.Fields{
"ipSetID": ipSetID,
"member": member,
}).Debug("Member removed from IP set.")
}
callbacks.OnIPSetMemberRemoved(ipSetID, member)
}
// The endpoint policy resolver marries up the active policies with local endpoints and
// calculates the complete, ordered set of policies that apply to each endpoint.
//
// ...
// Dispatcher (all updates)
// |
// | All policies
// |
// | ...
// \ Active rules calculator
// \ \
// \ \
// \ | Policy X matches endpoint Y
// \ | Policy Z matches endpoint Y
// \ |
// Policy resolver
// |
// | Endpoint Y has policies [Z, X] in that order
// |
// <dataplane>
//
polResolver := NewPolicyResolver()
// Hook up the inputs to the policy resolver.
activeRulesCalc.PolicyMatchListener = polResolver
polResolver.RegisterWith(allUpdDispatcher, localEndpointDispatcher)
// And hook its output to the callbacks.
polResolver.Callbacks = callbacks
// Register for host IP updates.
//
// ...
// Dispatcher (all updates)
// |
// | host IPs
// |
// passthru
// |
// |
// |
// <dataplane>
//
hostIPPassthru := NewDataplanePassthru(callbacks)
hostIPPassthru.RegisterWith(allUpdDispatcher)
// Calculate VXLAN routes.
// ...
// Dispatcher (all updates)
// |
// | host IPs, host config, IP pools, IPAM blocks
// |
// vxlan resolver
// |
// | VTEPs, routes
// |
// <dataplane>
//
if conf.VXLANEnabled {
vxlanResolver := NewVXLANResolver(hostname, callbacks)
vxlanResolver.RegisterWith(allUpdDispatcher)
}
// Register for config updates.
//
// ...
// Dispatcher (all updates)
// |
// | separate config updates foo=bar, baz=biff
// |
// config batcher
// |
// | combined config {foo=bar, bax=biff}
// |
// <dataplane>
//
configBatcher := NewConfigBatcher(hostname, callbacks)
configBatcher.RegisterWith(allUpdDispatcher)
// The profile decoder identifies objects with special dataplane significance which have
// been encoded as profiles by libcalico-go. At present this includes Kubernetes Service
// Accounts and Kubernetes Namespaces.
// ...
// Dispatcher (all updates)
// |
// | Profiles
// |
// profile decoder
// |
// |
// |
// <dataplane>
//
profileDecoder := NewProfileDecoder(callbacks)
profileDecoder.RegisterWith(allUpdDispatcher)
return &CalcGraph{
AllUpdDispatcher: allUpdDispatcher,
activeRulesCalculator: activeRulesCalc,
}
}
type localEndpointDispatcherReg dispatcher.Dispatcher
func (l *localEndpointDispatcherReg) RegisterWith(disp *dispatcher.Dispatcher) {
led := (*dispatcher.Dispatcher)(l)
disp.Register(model.WorkloadEndpointKey{}, led.OnUpdate)
disp.Register(model.HostEndpointKey{}, led.OnUpdate)
disp.RegisterStatusHandler(led.OnDatamodelStatus)
}
// endpointHostnameFilter provides an UpdateHandler that filters out endpoints
// that are not on the given host.
type endpointHostnameFilter struct {
hostname string
}
func (f *endpointHostnameFilter) RegisterWith(localEndpointDisp *dispatcher.Dispatcher) {
localEndpointDisp.Register(model.WorkloadEndpointKey{}, f.OnUpdate)
localEndpointDisp.Register(model.HostEndpointKey{}, f.OnUpdate)
}
func (f *endpointHostnameFilter) OnUpdate(update api.Update) (filterOut bool) {
switch key := update.Key.(type) {
case model.WorkloadEndpointKey:
if key.Hostname != f.hostname {
filterOut = true
}
case model.HostEndpointKey:
if key.Hostname != f.hostname {
filterOut = true
}
}
if !filterOut {
// To keep log spam down, log only for local endpoints.
if update.Value == nil {
log.WithField("id", update.Key).Info("Local endpoint deleted")
} else {
log.WithField("id", update.Key).Info("Local endpoint updated")
}
}
return
}