forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
loadbalancer.go
442 lines (377 loc) · 10.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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// Copyright 2016-2017 Authors of Cilium
//
// 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 types
import (
"crypto/sha512"
"fmt"
"net"
"strings"
"sync"
"github.com/cilium/cilium/api/v1/models"
)
const (
NONE = L4Type("NONE")
// TCP type.
TCP = L4Type("TCP")
// UDP type.
UDP = L4Type("UDP")
)
// L4Type name.
type L4Type string
// FEPortName is the name of the frontend's port.
type FEPortName string
// ServiceID is the service's ID.
type ServiceID uint16
// LBBackEnd represents load balancer backend.
type LBBackEnd struct {
L3n4Addr
Weight uint16
}
// LBSVC is essentially used for the REST API.
type LBSVC struct {
Sha256 string
FE L3n4AddrID
BES []LBBackEnd
}
func (s *LBSVC) GetModel() *models.Service {
if s == nil {
return nil
}
id := int64(s.FE.ID)
svc := &models.Service{
ID: id,
FrontendAddress: s.FE.GetModel(),
BackendAddresses: make([]*models.BackendAddress, len(s.BES)),
}
for i, be := range s.BES {
svc.BackendAddresses[i] = be.GetBackendModel()
}
return svc
}
// SVCMap is a map of the daemon's services. The key is the sha256sum of the LBSVC's FE
// and the value the LBSVC.
type SVCMap map[string]LBSVC
// SVCMapID maps service IDs to service structures.
type SVCMapID map[ServiceID]*LBSVC
// RevNATMap is a map of the daemon's RevNATs.
type RevNATMap map[ServiceID]L3n4Addr
// LoadBalancer is the internal representation of the loadbalancer in the local cilium
// daemon.
type LoadBalancer struct {
BPFMapMU sync.RWMutex
SVCMap SVCMap
SVCMapID SVCMapID
RevNATMap RevNATMap
K8sMU sync.Mutex
K8sServices map[K8sServiceNamespace]*K8sServiceInfo
K8sEndpoints map[K8sServiceNamespace]*K8sServiceEndpoint
K8sIngress map[K8sServiceNamespace]*K8sServiceInfo
}
// AddService adds a service to list of loadbalancers and returns true if created.
func (lb *LoadBalancer) AddService(svc LBSVC) bool {
oldSvc, ok := lb.SVCMapID[svc.FE.ID]
if ok {
// If service already existed, remove old entry from map
delete(lb.SVCMap, oldSvc.Sha256)
}
lb.SVCMap[svc.Sha256] = svc
lb.SVCMapID[svc.FE.ID] = &svc
return !ok
}
func (lb *LoadBalancer) DeleteService(svc *LBSVC) {
delete(lb.SVCMap, svc.Sha256)
delete(lb.SVCMapID, svc.FE.ID)
}
func NewL4Type(name string) (L4Type, error) {
switch strings.ToLower(name) {
case "tcp":
return TCP, nil
case "udp":
return UDP, nil
default:
return "", fmt.Errorf("Unknown L4 protocol")
}
}
// NewLoadBalancer returns a LoadBalancer with all maps initialized.
func NewLoadBalancer() *LoadBalancer {
return &LoadBalancer{
SVCMap: SVCMap{},
SVCMapID: SVCMapID{},
RevNATMap: RevNATMap{},
K8sServices: map[K8sServiceNamespace]*K8sServiceInfo{},
K8sEndpoints: map[K8sServiceNamespace]*K8sServiceEndpoint{},
K8sIngress: map[K8sServiceNamespace]*K8sServiceInfo{},
}
}
// K8sServiceNamespace is an abstraction for the k8s service + namespace types.
type K8sServiceNamespace struct {
Service string
Namespace string
}
// K8sServiceInfo is an abstraction for a k8s service that is composed by the frontend IP
// address (FEIP) and the map of the frontend ports (Ports).
type K8sServiceInfo struct {
FEIP net.IP
Ports map[FEPortName]*FEPort
}
// NewK8sServiceInfo creates a new K8sServiceInfo with the Ports map initialized.
func NewK8sServiceInfo(ip net.IP) *K8sServiceInfo {
return &K8sServiceInfo{
FEIP: ip,
Ports: map[FEPortName]*FEPort{},
}
}
// K8sServiceEndpoint is an abstraction for the k8s endpoint object. Each service is
// composed by a map of backend IPs (BEIPs) and a map of Ports (Ports). Each k8s endpoint
// present in BEIPs share the same list of Ports open.
type K8sServiceEndpoint struct {
// TODO: Replace bool for time.Time so we know last time the service endpoint was seen?
BEIPs map[string]bool
Ports map[FEPortName]*L4Addr
}
// NewK8sServiceEndpoint creates a new K8sServiceEndpoint with the backend BEIPs map and
// Ports map initialized.
func NewK8sServiceEndpoint() *K8sServiceEndpoint {
return &K8sServiceEndpoint{
BEIPs: map[string]bool{},
Ports: map[FEPortName]*L4Addr{},
}
}
// L4Addr is an abstraction for the backend port with a L4Type, usually tcp or udp, and
// the Port number.
type L4Addr struct {
Protocol L4Type
Port uint16
}
// NewL4Addr creates a new L4Addr. Returns an error if protocol is not recognized.
func NewL4Addr(protocol L4Type, number uint16) (*L4Addr, error) {
switch protocol {
case TCP, UDP, NONE:
default:
return nil, fmt.Errorf("unknown protocol type %s", protocol)
}
return &L4Addr{Protocol: protocol, Port: number}, nil
}
// DeepCopy returns a DeepCopy of the given L4Addr.
func (l *L4Addr) DeepCopy() *L4Addr {
return &L4Addr{
Port: l.Port,
Protocol: l.Protocol,
}
}
// FEPort represents a frontend port with its ID and the L4Addr's inheritance.
type FEPort struct {
*L4Addr
ID ServiceID
}
// NewFEPort creates a new FEPort with the ID set to 0.
func NewFEPort(protocol L4Type, portNumber uint16) (*FEPort, error) {
lbport, err := NewL4Addr(protocol, portNumber)
return &FEPort{L4Addr: lbport}, err
}
// L3n4Addr is used to store, as an unique L3+L4 address in the KVStore.
type L3n4Addr struct {
IP net.IP
L4Addr
}
// NewL3n4Addr creates a new L3n4Addr.
func NewL3n4Addr(protocol L4Type, ip net.IP, portNumber uint16) (*L3n4Addr, error) {
lbport, err := NewL4Addr(protocol, portNumber)
if err != nil {
return nil, err
}
return &L3n4Addr{IP: ip, L4Addr: *lbport}, nil
}
func NewL3n4AddrFromModel(base *models.FrontendAddress) (*L3n4Addr, error) {
if base == nil {
return nil, nil
}
if base.IP == "" {
return nil, fmt.Errorf("Missing IP address")
}
proto, err := NewL4Type(base.Protocol)
if err != nil {
return nil, err
}
l4addr, err := NewL4Addr(proto, base.Port)
if err != nil {
return nil, err
}
ip := net.ParseIP(base.IP)
if ip == nil {
return nil, fmt.Errorf("Invalid IP address \"%s\"", base.IP)
}
return &L3n4Addr{IP: ip, L4Addr: *l4addr}, nil
}
func NewLBBackEnd(protocol L4Type, ip net.IP, portNumber uint16, weight uint16) (*LBBackEnd, error) {
lbport, err := NewL4Addr(protocol, portNumber)
if err != nil {
return nil, err
}
return &LBBackEnd{
L3n4Addr: L3n4Addr{IP: ip, L4Addr: *lbport},
Weight: weight,
}, nil
}
func NewLBBackEndFromBackendModel(base *models.BackendAddress) (*LBBackEnd, error) {
if base.IP == nil {
return nil, fmt.Errorf("Missing IP address")
}
// FIXME: Should this be NONE ?
l4addr, err := NewL4Addr(NONE, base.Port)
if err != nil {
return nil, err
}
ip := net.ParseIP(*base.IP)
if ip == nil {
return nil, fmt.Errorf("Invalid IP address \"%s\"", *base.IP)
}
return &LBBackEnd{
L3n4Addr: L3n4Addr{IP: ip, L4Addr: *l4addr},
Weight: base.Weight,
}, nil
}
func NewL3n4AddrFromBackendModel(base *models.BackendAddress) (*L3n4Addr, error) {
if base.IP == nil {
return nil, fmt.Errorf("Missing IP address")
}
// FIXME: Should this be NONE ?
l4addr, err := NewL4Addr(NONE, base.Port)
if err != nil {
return nil, err
}
ip := net.ParseIP(*base.IP)
if ip == nil {
return nil, fmt.Errorf("Invalid IP address \"%s\"", *base.IP)
}
return &L3n4Addr{IP: ip, L4Addr: *l4addr}, nil
}
func (a *L3n4Addr) GetModel() *models.FrontendAddress {
if a == nil {
return nil
}
return &models.FrontendAddress{
IP: a.IP.String(),
Protocol: string(a.Protocol),
Port: a.Port,
}
}
func (b *LBBackEnd) GetBackendModel() *models.BackendAddress {
if b == nil {
return nil
}
ip := b.IP.String()
return &models.BackendAddress{
IP: &ip,
Port: b.Port,
Weight: b.Weight,
}
}
// String returns the L3n4Addr in the "IPv4:Port" format for IPv4 and "[IPv6]:Port" format
// for IPv6.
func (a *L3n4Addr) String() string {
if a.IsIPv6() {
return fmt.Sprintf("[%s]:%d", a.IP.String(), a.Port)
}
return fmt.Sprintf("%s:%d", a.IP.String(), a.Port)
}
// DeepCopy returns a DeepCopy of the given L3n4Addr.
func (a *L3n4Addr) DeepCopy() *L3n4Addr {
copyIP := make(net.IP, len(a.IP))
copy(copyIP, a.IP)
return &L3n4Addr{
IP: copyIP,
L4Addr: *a.L4Addr.DeepCopy(),
}
}
// SHA256Sum calculates L3n4Addr's internal SHA256Sum.
func (a L3n4Addr) SHA256Sum() string {
// FIXME: Remove Protocol's omission once we care about protocols.
protoBak := a.Protocol
a.Protocol = ""
defer func() {
a.Protocol = protoBak
}()
str := []byte(fmt.Sprintf("%+v", a))
return fmt.Sprintf("%x", sha512.New512_256().Sum(str))
}
// IsIPv6 returns true if the IP address in the given L3n4Addr is IPv6 or not.
func (a *L3n4Addr) IsIPv6() bool {
return a.IP.To4() == nil
}
// L3n4AddrID is used to store, as an unique L3+L4 plus the assigned ID, in the
// KVStore.
type L3n4AddrID struct {
L3n4Addr
ID ServiceID
}
// NewL3n4AddrID creates a new L3n4AddrID.
func NewL3n4AddrID(protocol L4Type, ip net.IP, portNumber uint16, id ServiceID) (*L3n4AddrID, error) {
l3n4Addr, err := NewL3n4Addr(protocol, ip, portNumber)
if err != nil {
return nil, err
}
return &L3n4AddrID{L3n4Addr: *l3n4Addr, ID: id}, nil
}
// DeepCopy returns a DeepCopy of the given L3n4AddrID.
func (l *L3n4AddrID) DeepCopy() *L3n4AddrID {
return &L3n4AddrID{
L3n4Addr: *l.L3n4Addr.DeepCopy(),
ID: l.ID,
}
}
// IsIPv6 returns true if the IP address in L3n4Addr's L3n4AddrID is IPv6 or not.
func (l *L3n4AddrID) IsIPv6() bool {
return l.L3n4Addr.IsIPv6()
}
// AddFEnBE adds the given 'fe' and 'be' to the SVCMap. If 'fe' exists and beIndex is 0,
// the new 'be' will be appended to the list of existing backends. If beIndex is bigger
// than the size of existing backends slice, it will be created a new array with size of
// beIndex and the new 'be' will be inserted on index beIndex-1 of that new array. All
// remaining be elements will be kept on the same index and, in case the new array is
// larger than the number of backends, some elements will be empty.
func (svcs SVCMap) AddFEnBE(fe *L3n4AddrID, be *LBBackEnd, beIndex int) {
sha := fe.SHA256Sum()
var lbsvc LBSVC
lbsvc, ok := svcs[sha]
if !ok {
var bes []LBBackEnd
if beIndex == 0 {
bes = make([]LBBackEnd, 1)
bes[0] = *be
} else {
bes = make([]LBBackEnd, beIndex)
bes[beIndex-1] = *be
}
lbsvc = LBSVC{
FE: *fe,
BES: bes,
}
} else {
var bes []LBBackEnd
if len(lbsvc.BES) < beIndex {
bes = make([]LBBackEnd, beIndex)
for i, lbsvcBE := range lbsvc.BES {
bes[i] = lbsvcBE
}
lbsvc.BES = bes
}
if beIndex == 0 {
lbsvc.BES = append(lbsvc.BES, *be)
} else {
lbsvc.BES[beIndex-1] = *be
}
}
svcs[sha] = lbsvc
}