/
loadbalancer.go
345 lines (303 loc) · 9.8 KB
/
loadbalancer.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
package loadbalancer
import (
"fmt"
"reflect"
"strings"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
libovsdbclient "github.com/ovn-org/libovsdb/client"
libovsdb "github.com/ovn-org/libovsdb/ovsdb"
"github.com/ovn-org/ovn-kubernetes/go-controller/pkg/libovsdbops"
"github.com/ovn-org/ovn-kubernetes/go-controller/pkg/metrics"
"github.com/ovn-org/ovn-kubernetes/go-controller/pkg/nbdb"
"github.com/ovn-org/ovn-kubernetes/go-controller/pkg/util"
"k8s.io/klog/v2"
)
// EnsureLBs provides a generic load-balancer reconciliation engine.
//
// It assures that, for a given set of ExternalIDs, only the configured
// list of load balancers exist. Existing load-balancers will be updated,
// new ones will be created as needed, and stale ones will be deleted.
//
// For example, you might want to ensure that service ns/foo has the
// correct set of load balancers. You would call it with something like
//
// EnsureLBs( { kind: Service, owner: ns/foo}, { {Name: Service_ns/foo_cluster_tcp, ...}})
//
// This will ensure that, for this example, only that one LB exists and
// has the desired configuration.
//
// It will commit all updates in a single transaction, so updates will be
// atomic and users should see no disruption. However, concurrent calls
// that modify the same externalIDs are not allowed.
//
// It is assumed that names are meaningful and somewhat stable, to minimize churn. This
// function doesn't work with Load_Balancers without a name.
func EnsureLBs(nbClient libovsdbclient.Client, service *corev1.Service, LBs []LB) error {
externalIDs := util.ExternalIDsForObject(service)
lbCache, err := GetLBCache(nbClient)
if err != nil {
return fmt.Errorf("failed initialize LBcache: %w", err)
}
existing := lbCache.Find(externalIDs)
existingByName := make(map[string]*CachedLB, len(existing))
toDelete := sets.NewString()
for _, lb := range existing {
existingByName[lb.Name] = lb
toDelete.Insert(lb.UUID)
}
lbs := make([]*nbdb.LoadBalancer, 0, len(LBs))
existinglbs := make([]*nbdb.LoadBalancer, 0, len(LBs))
newlbs := make([]*nbdb.LoadBalancer, 0, len(LBs))
addLBsToSwitch := map[string][]*nbdb.LoadBalancer{}
removeLBsFromSwitch := map[string][]*nbdb.LoadBalancer{}
addLBsToRouter := map[string][]*nbdb.LoadBalancer{}
removesLBsFromRouter := map[string][]*nbdb.LoadBalancer{}
addLBsToGroups := map[string][]*nbdb.LoadBalancer{}
removeLBsFromGroups := map[string][]*nbdb.LoadBalancer{}
wantedByName := make(map[string]*LB, len(LBs))
for i, lb := range LBs {
wantedByName[lb.Name] = &LBs[i]
blb := buildLB(&lb)
lbs = append(lbs, blb)
existingLB := existingByName[lb.Name]
existingRouters := sets.String{}
existingSwitches := sets.String{}
existingGroups := sets.String{}
if existingLB != nil {
blb.UUID = existingLB.UUID
existinglbs = append(existinglbs, blb)
toDelete.Delete(existingLB.UUID)
existingRouters = existingLB.Routers
existingSwitches = existingLB.Switches
existingGroups = existingLB.Groups
} else {
newlbs = append(newlbs, blb)
}
wantRouters := sets.NewString(lb.Routers...)
wantSwitches := sets.NewString(lb.Switches...)
wantGroups := sets.NewString(lb.Groups...)
mapLBDifferenceByKey(addLBsToSwitch, wantSwitches, existingSwitches, blb)
mapLBDifferenceByKey(removeLBsFromSwitch, existingSwitches, wantSwitches, blb)
mapLBDifferenceByKey(addLBsToRouter, wantRouters, existingRouters, blb)
mapLBDifferenceByKey(removesLBsFromRouter, existingRouters, wantRouters, blb)
mapLBDifferenceByKey(addLBsToGroups, wantGroups, existingGroups, blb)
mapLBDifferenceByKey(removeLBsFromGroups, existingGroups, wantGroups, blb)
}
ops, err := libovsdbops.CreateOrUpdateLoadBalancersOps(nbClient, nil, existinglbs...)
if err != nil {
return err
}
ops, err = libovsdbops.CreateLoadBalancersOps(nbClient, ops, newlbs...)
if err != nil {
return err
}
// cache switches for this round of ops
lswitches := map[string]*nbdb.LogicalSwitch{}
getSwitch := func(name string) *nbdb.LogicalSwitch {
var lswitch *nbdb.LogicalSwitch
var found bool
if lswitch, found = lswitches[name]; !found {
lswitch = &nbdb.LogicalSwitch{Name: name}
lswitches[name] = lswitch
}
return lswitch
}
for k, v := range addLBsToSwitch {
ops, err = libovsdbops.AddLoadBalancersToLogicalSwitchOps(nbClient, ops, getSwitch(k), v...)
if err != nil {
return err
}
}
for k, v := range removeLBsFromSwitch {
ops, err = libovsdbops.RemoveLoadBalancersFromLogicalSwitchOps(nbClient, ops, getSwitch(k), v...)
if err != nil {
return err
}
}
// cache routers for this round of ops
routers := map[string]*nbdb.LogicalRouter{}
getRouter := func(name string) *nbdb.LogicalRouter {
var router *nbdb.LogicalRouter
var found bool
if router, found = routers[name]; !found {
router = &nbdb.LogicalRouter{Name: name}
routers[name] = router
}
return router
}
for k, v := range addLBsToRouter {
ops, err = libovsdbops.AddLoadBalancersToLogicalRouterOps(nbClient, ops, getRouter(k), v...)
if err != nil {
return err
}
}
for k, v := range removesLBsFromRouter {
ops, err = libovsdbops.RemoveLoadBalancersFromLogicalRouterOps(nbClient, ops, getRouter(k), v...)
if err != nil {
return err
}
}
// cache groups for this round of ops
groups := map[string]*nbdb.LoadBalancerGroup{}
getGroup := func(name string) *nbdb.LoadBalancerGroup {
var group *nbdb.LoadBalancerGroup
var found bool
if group, found = groups[name]; !found {
group = &nbdb.LoadBalancerGroup{Name: name}
groups[name] = group
}
return group
}
for k, v := range addLBsToGroups {
ops, err = libovsdbops.AddLoadBalancersToGroupOps(nbClient, ops, getGroup(k), v...)
if err != nil {
return err
}
}
for k, v := range removeLBsFromGroups {
ops, err = libovsdbops.RemoveLoadBalancersFromGroupOps(nbClient, ops, getGroup(k), v...)
if err != nil {
return err
}
}
deleteLBs := make([]*nbdb.LoadBalancer, 0, len(toDelete))
for uuid := range toDelete {
deleteLBs = append(deleteLBs, &nbdb.LoadBalancer{UUID: uuid})
}
ops, err = libovsdbops.DeleteLoadBalancersOps(nbClient, ops, deleteLBs...)
if err != nil {
return err
}
recordOps, txOkCallBack, _, err := metrics.GetConfigDurationRecorder().AddOVN(nbClient, "service",
service.Namespace, service.Name)
if err != nil {
klog.Errorf("Failed to record config duration: %v", err)
}
ops = append(ops, recordOps...)
_, err = libovsdbops.TransactAndCheckAndSetUUIDs(nbClient, lbs, ops)
if err != nil {
return err
}
txOkCallBack()
for _, lb := range lbs {
wantedByName[lb.Name].UUID = lb.UUID
}
lbCache.update(LBs, toDelete.UnsortedList())
klog.V(5).Infof("Deleted %d stale LBs for %#v", len(toDelete), externalIDs)
return nil
}
// LoadBalancersEqualNoUUID compares load balancer objects excluding uuid
func LoadBalancersEqualNoUUID(lbs1, lbs2 []LB) bool {
if len(lbs1) != len(lbs2) {
return false
}
new1 := make([]LB, len(lbs1))
new2 := make([]LB, len(lbs2))
for _, lb := range lbs1 {
lb.UUID = ""
new1 = append(new1, lb)
}
for _, lb := range lbs2 {
lb.UUID = ""
new2 = append(new2, lb)
}
return reflect.DeepEqual(new1, new2)
}
func mapLBDifferenceByKey(keyMap map[string][]*nbdb.LoadBalancer, keyIn sets.String, keyNotIn sets.String, lb *nbdb.LoadBalancer) {
for _, k := range keyIn.Difference(keyNotIn).UnsortedList() {
l := keyMap[k]
if l == nil {
l = []*nbdb.LoadBalancer{}
}
l = append(l, lb)
keyMap[k] = l
}
}
func buildLB(lb *LB) *nbdb.LoadBalancer {
reject := "true"
event := "false"
if lb.Opts.Unidling {
reject = "false"
event = "true"
}
skipSNAT := "false"
if lb.Opts.SkipSNAT {
skipSNAT = "true"
}
options := map[string]string{
"reject": reject,
"event": event,
"skip_snat": skipSNAT,
}
// Session affinity
// If enabled, then bucket flows by 3-tuple (proto, srcip, dstip)
// otherwise, use default ovn value
selectionFields := []nbdb.LoadBalancerSelectionFields{}
if lb.Opts.Affinity {
selectionFields = []string{
nbdb.LoadBalancerSelectionFieldsIPSrc,
nbdb.LoadBalancerSelectionFieldsIPDst,
}
}
// vipMap
vips := buildVipMap(lb.Rules)
return libovsdbops.BuildLoadBalancer(lb.Name, strings.ToLower(lb.Protocol), selectionFields, vips, options, lb.ExternalIDs)
}
// buildVipMap returns a viups map from a set of rules
func buildVipMap(rules []LBRule) map[string]string {
vipMap := make(map[string]string, len(rules))
for _, r := range rules {
tgts := make([]string, 0, len(r.Targets))
for _, tgt := range r.Targets {
tgts = append(tgts, tgt.String())
}
vipMap[r.Source.String()] = strings.Join(tgts, ",")
}
return vipMap
}
// DeleteLBs deletes all load balancer uuids supplied
// Note: this also automatically removes them from the switches, routers, and the groups :-)
func DeleteLBs(nbClient libovsdbclient.Client, uuids []string) error {
if len(uuids) == 0 {
return nil
}
cache, err := GetLBCache(nbClient)
if err != nil {
return err
}
lbs := make([]*nbdb.LoadBalancer, 0, len(uuids))
for _, uuid := range uuids {
lbs = append(lbs, &nbdb.LoadBalancer{UUID: uuid})
}
err = libovsdbops.DeleteLoadBalancers(nbClient, lbs)
if err != nil {
return err
}
cache.update(nil, uuids)
return nil
}
type DeleteVIPEntry struct {
LBUUID string
VIPs []string // ip:string (or v6 equivalent)
}
// DeleteLoadBalancerVIPs removes VIPs from load-balancers in a single shot.
func DeleteLoadBalancerVIPs(nbClient libovsdbclient.Client, toRemove []DeleteVIPEntry) error {
lbCache, err := GetLBCache(nbClient)
if err != nil {
return err
}
var ops []libovsdb.Operation
for _, entry := range toRemove {
ops, err = libovsdbops.RemoveLoadBalancerVipsOps(nbClient, ops, &nbdb.LoadBalancer{UUID: entry.LBUUID}, entry.VIPs...)
if err != nil {
return err
}
}
_, err = libovsdbops.TransactAndCheck(nbClient, ops)
if err != nil {
return fmt.Errorf("failed to remove vips from load_balancer: %w", err)
}
lbCache.removeVips(toRemove)
return nil
}