-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontroller.go
More file actions
498 lines (429 loc) · 13.7 KB
/
controller.go
File metadata and controls
498 lines (429 loc) · 13.7 KB
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package main
import (
"errors"
"fmt"
"runtime"
"sort"
"strings"
"time"
"github.com/golang/glog"
"github.com/sostheim/lbex/annotations"
"github.com/sostheim/lbex/nginx"
"k8s.io/client-go/kubernetes"
v1 "k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/pkg/util/intstr"
"k8s.io/client-go/tools/cache"
)
var (
resyncPeriod = 30 * time.Second
)
// List Watch (lw) Controller (lwc)
type lwController struct {
controller *cache.Controller
stopCh chan struct{}
}
// External LB Controller (lbex)
type lbExController struct {
// Command line / environment supplied configuration values
cfg *config
clientset *kubernetes.Clientset
endpointsLWC *lwController
endpointStore cache.Store
endpointsQueue *TaskQueue
servicesLWC *lwController
servicesStore cache.Store
servicesQueue *TaskQueue
nodesLWC *lwController
nodesStore cache.Store
nodesQueue *TaskQueue
stopCh chan struct{}
cfgtor *nginx.Configurator
}
func newLbExController(clientset *kubernetes.Clientset, cfg *config) *lbExController {
// local testing -> no actual NGINX instance
cfgType := nginx.StreamCfg
if runtime.GOOS == "darwin" {
cfgType = nginx.LocalCfg
}
// Create and start the NGINX LoadBalancer
ngxc, _ := nginx.NewNginxController(cfgType, "/etc/nginx/", *cfg.healthCheck, *cfg.healthCheckPort)
ngxc.Start()
configtor := nginx.NewConfigurator(ngxc)
// create external loadbalancer controller struct
lbexc := lbExController{
clientset: clientset,
stopCh: make(chan struct{}),
cfg: cfg,
cfgtor: configtor,
}
lbexc.nodesQueue = NewTaskQueue(lbexc.syncNodes)
lbexc.nodesLWC = newNodesListWatchControllerForClientset(&lbexc)
lbexc.servicesQueue = NewTaskQueue(lbexc.syncServices)
lbexc.servicesLWC = newServicesListWatchControllerForClientset(&lbexc)
lbexc.endpointsQueue = NewTaskQueue(lbexc.syncEndpoints)
lbexc.endpointsLWC = newEndpointsListWatchControllerForClientset(&lbexc)
return &lbexc
}
func (lbex *lbExController) run() {
// run the controller and queue goroutines
go lbex.nodesLWC.controller.Run(lbex.stopCh)
go lbex.nodesQueue.Run(time.Second, lbex.stopCh)
go lbex.endpointsLWC.controller.Run(lbex.stopCh)
go lbex.endpointsQueue.Run(time.Second, lbex.stopCh)
// Allow time for the initial cache update for all nodes and endpoints to take place 1st
time.Sleep(5 * time.Second)
go lbex.servicesLWC.controller.Run(lbex.stopCh)
go lbex.servicesQueue.Run(time.Second, lbex.stopCh)
}
func (lbex *lbExController) enqueuServiceObjects(keys []string) {
for _, key := range keys {
obj, exists, err := lbex.servicesStore.GetByKey(key)
if err != nil || !exists {
continue
}
lbex.servicesQueue.Enqueue(obj)
}
}
func (lbex *lbExController) syncNodes(obj interface{}) error {
if lbex.nodesQueue.IsShuttingDown() {
return nil
}
key, ok := obj.(string)
if !ok {
return errors.New("type assertion faild for key string")
}
storeObj, exists, err := lbex.nodesStore.GetByKey(key)
if err != nil {
return err
}
affectedServices := []string{}
if !exists {
glog.V(2).Infof("deleting node: %v\n", key)
affectedServices = lbex.cfgtor.DeleteNode(key)
} else {
err = ValidateNodeObjectType(storeObj)
if err != nil {
glog.V(3).Infof("failed ValidateNodeObjectType(): err: %v", err)
return nil
}
addrs, err := GetNodeAddress(storeObj)
if err != nil {
glog.V(3).Infof("failed GetNodeAddress(): err: %v", err)
return nil
}
active := IsNodeScheduleable(storeObj)
node := nginx.Node{
Name: key,
Hostname: addrs.Hostname,
ExternalIP: addrs.ExternalIP,
InternalIP: addrs.InternalIP,
Active: active,
}
glog.V(3).Infof("add/update node: %s", key)
affectedServices = lbex.cfgtor.AddOrUpdateNode(node)
}
glog.V(4).Infof("queuing updates for affected services: %v", affectedServices)
// NOTE: This may be totally unnecessary, even if the node crashes unexpectedly.
// Conceptually, k8s should recognize the node failure and update the
// service and its' endpoints for any affected components. This has the
// potential to create a race between the k8s updates and this update.
lbex.enqueuServiceObjects(affectedServices)
return nil
}
func (lbex *lbExController) syncServices(obj interface{}) error {
if lbex.servicesQueue.IsShuttingDown() {
return nil
}
key, ok := obj.(string)
if !ok {
return errors.New("syncServices: type assertion failed for key string")
}
storeObj, exists, err := lbex.servicesStore.GetByKey(key)
if err != nil {
return err
}
// some-namespace/some-service -> some-namespace-some-service
conf := strings.Replace(key, "/", "-", -1)
if !exists {
glog.V(2).Infof("syncServices: deletion check for service: %v\n", key)
lbex.cfgtor.DeleteConfiguration(conf, nginx.StreamCfg)
} else {
err = ValidateServiceObjectType(storeObj)
if err != nil {
glog.V(3).Infof("syncServices: ValidateServiceObjectType(): err: %v", err)
return nil
}
service, _ := storeObj.(*v1.Service)
topo := lbex.getServiceNetworkTopo(key)
if topo == nil || len(topo) == 0 {
glog.V(4).Infof("syncServices: %s: not an lbex managed service", key)
return nil
}
val, _ := annotations.GetOptionalStringAnnotation(annotations.LBEXAlgorithmKey, service)
algo := nginx.ValidateAlgorithm(val)
val, _ = annotations.GetOptionalStringAnnotation(annotations.LBEXUpstreamType, service)
ups := nginx.ValidateUpstreamType(val)
svcSpec := &nginx.ServiceSpec{
Service: service,
Key: key,
Algorithm: algo,
ClusterIP: service.Spec.ClusterIP,
ConfigName: conf,
UpstreamType: ups,
}
for _, elem := range topo {
for _, ep := range elem.Endpoints {
svcTarget := nginx.Target{
ServicePort: ep.ServicePort,
NodeIP: ep.NodeIP,
NodeName: ep.NodeName,
NodePort: ep.NodePort,
PortName: ep.PortName,
PodIP: ep.PodIP,
PodPort: ep.PodPort,
Protocol: ep.Protocol,
}
svcSpec.Topology = append(svcSpec.Topology, svcTarget)
}
}
glog.V(3).Infof("syncServices: add/update service: %s", key)
lbex.cfgtor.AddOrUpdateService(svcSpec)
}
return nil
}
func (lbex *lbExController) syncEndpoints(obj interface{}) error {
if lbex.endpointsQueue.IsShuttingDown() {
return nil
}
key, ok := obj.(string)
if !ok {
return errors.New("syncEndpoints: key string type assertion failed")
}
_, exists, err := lbex.endpointStore.GetByKey(key)
if err != nil {
return err
}
if !exists {
glog.V(2).Infof("syncEndpoints: deleting removed endpoint: %v\n", key)
lbex.enqueuServiceObjects([]string{key})
} else {
topo := lbex.getServiceNetworkTopo(key)
if topo == nil || len(topo) == 0 {
glog.V(4).Info("syncEndpoints: not a lbex managed service endpoint")
} else {
glog.V(3).Infof("syncEndpoints: trigger service update managed service: %s, with topo:\n%v", key, topo)
lbex.enqueuServiceObjects([]string{key})
}
}
return nil
}
// getServiceEndpoints returns the endpoints v1 api object for the specified service name / namesapce.
func (lbex *lbExController) getServiceEndpoints(service *v1.Service) (endpoints v1.Endpoints, err error) {
for _, ep := range lbex.endpointStore.List() {
endpoints = *ep.(*v1.Endpoints)
if service.Name == endpoints.Name && service.Namespace == endpoints.Namespace {
return endpoints, nil
}
}
err = fmt.Errorf("could not find endpoints for service: %v", service.Name)
return
}
// getEndpoints returns a list endpoints from the set of addresses and ports
func (lbex *lbExController) getEndpoints(service *v1.Service, servicePort *v1.ServicePort) (endpoints []Endpoint) {
// https://kubernetes.io/docs/api-reference/v1.5/#endpointsubset-v1
// EndpointSubset is a group of addresses with a common set of ports.
// The expanded set of endpoints is the Cartesian product of:
// Addresses x Ports. For example, given:
// {
// Addresses: [
// {
// "ip": "10.10.1.1"
// },
// {
// "ip": "10.10.2.2"
// }
// ],
// Ports: [
// {
// "name": "a",
// "port": 8675
// },
// {
// "name": "b",
// "port": 309
// }
// ]
// }
// The resulting set of endpoints can be viewed as:
// a: [ 10.10.1.1:8675, 10.10.2.2:8675 ],
// b: [ 10.10.1.1:309, 10.10.2.2:309 ]
//
svcEndpoints, err := lbex.getServiceEndpoints(service)
if err != nil {
return
}
for _, subsets := range svcEndpoints.Subsets {
for _, epPort := range subsets.Ports {
// The servicePort.TargetPort serves to limit the endpoint set to
// just those endpoints for the current target.
var targetPort int
switch servicePort.TargetPort.Type {
case intstr.Int:
servicePortInt, err := GetServicePortTargetPortInt(servicePort)
if err != nil {
continue
}
if epPort.Port == int32(servicePortInt) {
targetPort = servicePortInt
}
case intstr.String:
if epPort.Name == servicePort.TargetPort.StrVal {
targetPort = int(epPort.Port)
}
}
if targetPort == 0 {
continue
}
for _, epAddress := range subsets.Addresses {
ep := Endpoint{
ServicePort: int(servicePort.Port),
NodeName: *epAddress.NodeName,
NodePort: int(servicePort.NodePort),
PortName: epPort.Name,
PodIP: epAddress.IP,
PodPort: targetPort,
Protocol: string(epPort.Protocol),
}
endpoints = append(endpoints, ep)
}
}
}
return
}
// getServices returns a list of TCP and UDP services
func (lbex *lbExController) getServices() (topo []Service) {
objects := lbex.servicesStore.List()
for _, obj := range objects {
if !lbex.baseCheck(obj) {
continue
}
namespace, err := GetServiceNamespace(obj)
if err != nil {
continue
}
serviceName, err := GetServiceName(obj)
if err != nil {
continue
}
topo = append(topo, lbex.getServiceNetworkTopo(namespace+"/"+serviceName)...)
}
sort.Sort(serviceByName(topo))
return
}
// getService returns a services and it's endpoints.
func (lbex *lbExController) getServiceNetworkTopo(key string) (targets []Service) {
obj, exists, err := lbex.servicesStore.GetByKey(key)
if err != nil || !exists {
return nil
}
if !lbex.baseCheck(obj) {
return nil
}
service, _ := obj.(*v1.Service)
serviceName, _ := GetServiceName(obj)
if *lbex.cfg.serviceName != "" && *lbex.cfg.serviceName != serviceName {
glog.V(3).Infof("getService: ignoring non-matching service name: %s", serviceName)
return nil
}
var pool string
if val, ok := annotations.GetOptionalStringAnnotation(annotations.LBEXPoolKey, service); ok {
pool = val
}
if lbex.checkAffinity(pool) == false {
glog.V(3).Infof("getService: service: %s, with pool selector: %s eliminate by affinity check",
serviceName, pool)
return nil
}
var host string
if val, ok := annotations.GetOptionalStringAnnotation(annotations.LBEXHostKey, service); ok {
host = val
}
endpoints := []Endpoint{}
for _, servicePort := range service.Spec.Ports {
endpoints = lbex.getEndpoints(service, &servicePort)
if len(endpoints) == 0 {
glog.V(3).Infof("getService: no endpoints found for service %s, port %+d", service.Name, servicePort)
continue
}
backendPort, _ := GetServicePortTargetPortInt(&servicePort)
newSvc := Service{
Name: GetServiceNameForLBRule(serviceName, int(servicePort.Port)),
Ep: []string{},
Endpoints: endpoints,
Host: host,
BackendPort: backendPort,
}
newSvc.FrontendPort = int(servicePort.Port)
targets = append(targets, newSvc)
glog.V(3).Infof("getService: lbex supported service: %+v", newSvc)
}
sort.Sort(serviceByName(targets))
return
}
// checkAffinity returns true or false depending on wether the affinity or
// anti-affinity rules are satisfied.
func (lbex *lbExController) checkAffinity(pool string) bool {
if *lbex.cfg.strictAffinity {
// intentionally checks for "" == "": strict affinity to no pool value
glog.V(4).Infof("strict affnity pool: %s, LBEX pool: %s", pool, *lbex.cfg.servicePool)
return pool == *lbex.cfg.servicePool
}
if *lbex.cfg.antiAffinity {
// intentionally checks for "" != "": anti-affinity to no pool value
glog.V(4).Infof("anti affnity pool: %s, LBEX pool: %s", pool, *lbex.cfg.servicePool)
return pool != *lbex.cfg.servicePool
}
if pool == "" {
return true
}
glog.V(4).Infof("pool: %s, LBEX pool: %s", pool, *lbex.cfg.servicePool)
return pool == *lbex.cfg.servicePool
}
// baseCheck returns true or false depending on wether service object selects
// lbex as it's load balancer, and (if required) validates that there is at least one
// port annotation present, and that all port annotation have valid values
func (lbex *lbExController) baseCheck(obj interface{}) bool {
retval := false
if !ValidateServiceObject(obj) {
// Normal case for non-LBEX services (e.g. other service type or no annotation)
glog.V(4).Info("can't validate service object key")
return false
}
if *lbex.cfg.requirePort {
service, _ := obj.(*v1.Service)
for _, servicePort := range service.Spec.Ports {
portAnnotation := annotations.LBEXPortAnnotationBase
if servicePort.Name != "" {
portAnnotation = portAnnotation + servicePort.Name
} else {
portAnnotation = portAnnotation + nginx.SingleDefaultPortName
}
glog.V(2).Infof("baseCheck: lbex checking annotation: %s", portAnnotation)
port, err := annotations.GetIntAnnotation(portAnnotation, obj)
if err != nil {
if annotations.IsMissingAnnotations(err) {
glog.Warningf("Annotation %s is not present", portAnnotation)
} else {
glog.V(2).Infof("unexpected error processing annotation, err: %v", err)
}
continue
}
if port <= 0 || port > 65535 {
glog.V(2).Infof("invalid port value specified for annotation %s: %v", portAnnotation, port)
return false
}
retval = true
}
}
return retval
}