forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
k8s_watcher.go
925 lines (784 loc) · 23.6 KB
/
k8s_watcher.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
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
// 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 main
import (
"fmt"
"net"
"os"
"reflect"
"strings"
"sync"
"time"
"github.com/cilium/cilium/common/types"
"github.com/cilium/cilium/pkg/k8s"
"github.com/cilium/cilium/pkg/labels"
"github.com/cilium/cilium/pkg/node"
log "github.com/Sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/pkg/apis/extensions/v1beta1"
networkingv1 "k8s.io/client-go/pkg/apis/networking/v1"
"k8s.io/client-go/tools/cache"
)
const (
k8sErrLogTimeout = time.Minute
)
var (
k8sLogMessagesTimer = time.NewTimer(k8sErrLogTimeout)
firstK8sErrorLogMessage sync.Once
)
func init() {
// Replace error handler with our own
runtime.ErrorHandlers = []func(error){
k8sErrorHandler,
}
}
// k8sErrorHandler handles the error messages on a non verbose way by omitting
// same error messages for a timeout defined with k8sErrLogTimeout.
func k8sErrorHandler(e error) {
if e == nil {
return
}
// Omitting the 'connection refused' common messages
if strings.Contains(e.Error(), "connection refused") {
firstK8sErrorLogMessage.Do(func() {
// Reset the timer for the first message
log.Error(e)
k8sLogMessagesTimer.Reset(k8sErrLogTimeout)
})
select {
case <-k8sLogMessagesTimer.C:
log.Error(e)
k8sLogMessagesTimer.Reset(k8sErrLogTimeout)
default:
}
return
}
// Still log other error messages
log.Error(e)
}
func (d *Daemon) createThirdPartyResources() error {
// TODO: Retry a couple of times
res := &v1beta1.ThirdPartyResource{
ObjectMeta: metav1.ObjectMeta{
Name: "cilium-network-policy." + k8s.ThirdPartyResourceGroup,
},
Description: "Cilium network policy rule",
Versions: []v1beta1.APIVersion{
{Name: k8s.ThirdPartyResourceVersion},
},
}
_, err := d.k8sClient.ExtensionsV1beta1().ThirdPartyResources().Create(res)
if err != nil && !errors.IsAlreadyExists(err) {
return err
}
return nil
}
// EnableK8sWatcher watches for policy, services and endpoint changes on the Kubernetes
// api server defined in the receiver's daemon k8sClient. Re-syncs all state from the
// Kubernetes api server at the given reSyncPeriod duration.
func (d *Daemon) EnableK8sWatcher(reSyncPeriod time.Duration) error {
if !d.conf.IsK8sEnabled() {
return nil
}
if err := d.createThirdPartyResources(); err != nil {
return fmt.Errorf("Unable to create third party resource: %s", err)
}
restConfig, err := k8s.CreateConfig(d.conf.K8sEndpoint, d.conf.K8sCfgPath)
if err != nil {
return fmt.Errorf("Unable to create rest configuration: %s", err)
}
tprClient, err := k8s.CreateTPRClient(restConfig)
if err != nil {
return fmt.Errorf("Unable to create third party resource client: %s", err)
}
_, policyController := cache.NewInformer(
cache.NewListWatchFromClient(d.k8sClient.NetworkingV1().RESTClient(),
"networkpolicies", v1.NamespaceAll, fields.Everything()),
&networkingv1.NetworkPolicy{},
reSyncPeriod,
cache.ResourceEventHandlerFuncs{
AddFunc: d.addK8sNetworkPolicy,
UpdateFunc: d.updateK8sNetworkPolicy,
DeleteFunc: d.deleteK8sNetworkPolicy,
},
)
go policyController.Run(wait.NeverStop)
_, svcController := cache.NewInformer(
cache.NewListWatchFromClient(d.k8sClient.CoreV1().RESTClient(),
"services", v1.NamespaceAll, fields.Everything()),
&v1.Service{},
reSyncPeriod,
cache.ResourceEventHandlerFuncs{
AddFunc: d.serviceAddFn,
UpdateFunc: d.serviceModFn,
DeleteFunc: d.serviceDelFn,
},
)
go svcController.Run(wait.NeverStop)
_, endpointController := cache.NewInformer(
cache.NewListWatchFromClient(d.k8sClient.CoreV1().RESTClient(),
"endpoints", v1.NamespaceAll, fields.Everything()),
&v1.Endpoints{},
reSyncPeriod,
cache.ResourceEventHandlerFuncs{
AddFunc: d.endpointAddFn,
UpdateFunc: d.endpointModFn,
DeleteFunc: d.endpointDelFn,
},
)
go endpointController.Run(wait.NeverStop)
_, ingressController := cache.NewInformer(
cache.NewListWatchFromClient(d.k8sClient.ExtensionsV1beta1().RESTClient(),
"ingresses", v1.NamespaceAll, fields.Everything()),
&v1beta1.Ingress{},
reSyncPeriod,
cache.ResourceEventHandlerFuncs{
AddFunc: d.ingressAddFn,
UpdateFunc: d.ingressModFn,
DeleteFunc: d.ingressDelFn,
},
)
go ingressController.Run(wait.NeverStop)
_, ciliumRulesController := cache.NewInformer(
cache.NewListWatchFromClient(tprClient, "ciliumnetworkpolicies",
v1.NamespaceAll, fields.Everything()),
&k8s.CiliumNetworkPolicy{},
reSyncPeriod,
cache.ResourceEventHandlerFuncs{
AddFunc: d.addCiliumNetworkPolicy,
UpdateFunc: d.updateCiliumNetworkPolicy,
DeleteFunc: d.deleteCiliumNetworkPolicy,
},
)
go ciliumRulesController.Run(wait.NeverStop)
_, nodesController := cache.NewInformer(
cache.NewListWatchFromClient(d.k8sClient.CoreV1().RESTClient(),
"nodes", v1.NamespaceAll, fields.Everything()),
&v1.Node{},
reSyncPeriod,
cache.ResourceEventHandlerFuncs{
AddFunc: d.nodesAddFn,
UpdateFunc: d.nodesModFn,
DeleteFunc: d.nodesDelFn,
},
)
go nodesController.Run(wait.NeverStop)
return nil
}
func (d *Daemon) addK8sNetworkPolicy(obj interface{}) {
k8sNP, ok := obj.(*networkingv1.NetworkPolicy)
if !ok {
log.Errorf("Ignoring invalid k8s NetworkPolicy addition")
return
}
rules, err := k8s.ParseNetworkPolicy(k8sNP)
if err != nil {
log.Errorf("Error while parsing kubernetes network policy %+v: %s", obj, err)
return
}
opts := AddOptions{Replace: true}
if _, err := d.PolicyAdd(rules, &opts); err != nil {
log.Errorf("Error while adding kubernetes network policy %+v: %s", rules, err)
return
}
log.Infof("Kubernetes network policy '%s' successfully add", k8sNP.Name)
}
func (d *Daemon) updateK8sNetworkPolicy(oldObj interface{}, newObj interface{}) {
log.Debugf("Modified policy %+v->%+v", oldObj, newObj)
d.addK8sNetworkPolicy(newObj)
}
func (d *Daemon) deleteK8sNetworkPolicy(obj interface{}) {
k8sNP, ok := obj.(*networkingv1.NetworkPolicy)
if !ok {
log.Errorf("Ignoring invalid k8s NetworkPolicy deletion")
return
}
labels := labels.ParseSelectLabelArray(k8s.ExtractPolicyName(k8sNP))
if _, err := d.PolicyDelete(labels); err != nil {
log.Errorf("Error while deleting kubernetes network policy %+v: %s", labels, err)
} else {
log.Infof("Kubernetes network policy '%s' successfully removed", k8sNP.Name)
}
}
func (d *Daemon) serviceAddFn(obj interface{}) {
svc, ok := obj.(*v1.Service)
if !ok {
return
}
switch svc.Spec.Type {
case v1.ServiceTypeClusterIP, v1.ServiceTypeNodePort, v1.ServiceTypeLoadBalancer:
break
case v1.ServiceTypeExternalName:
// External-name services must be ignored
return
default:
log.Warningf("Ignoring k8s service %s/%s, reason unsupported type %s",
svc.Namespace, svc.Name, svc.Spec.Type)
return
}
if strings.ToLower(svc.Spec.ClusterIP) == "none" || svc.Spec.ClusterIP == "" {
log.Infof("Ignoring k8s service %s/%s, reason: headless",
svc.Namespace, svc.Name, svc.Spec.Type)
return
}
svcns := types.K8sServiceNamespace{
Service: svc.Name,
Namespace: svc.Namespace,
}
clusterIP := net.ParseIP(svc.Spec.ClusterIP)
newSI := types.NewK8sServiceInfo(clusterIP)
// FIXME: Add support for
// - NodePort
for _, port := range svc.Spec.Ports {
p, err := types.NewFEPort(types.L4Type(port.Protocol), uint16(port.Port))
if err != nil {
log.Errorf("Unable to add service port %v: %s", port, err)
continue
}
if _, ok := newSI.Ports[types.FEPortName(port.Name)]; !ok {
newSI.Ports[types.FEPortName(port.Name)] = p
}
}
d.loadBalancer.K8sMU.Lock()
defer d.loadBalancer.K8sMU.Unlock()
d.loadBalancer.K8sServices[svcns] = newSI
d.syncLB(&svcns, nil, nil)
}
func (d *Daemon) serviceModFn(_ interface{}, newObj interface{}) {
newSvc, ok := newObj.(*v1.Service)
if !ok {
return
}
log.Debugf("Service %+v", newSvc)
d.serviceAddFn(newObj)
}
func (d *Daemon) serviceDelFn(obj interface{}) {
svc, ok := obj.(*v1.Service)
if !ok {
return
}
log.Debugf("Service %+v", svc)
svcns := &types.K8sServiceNamespace{
Service: svc.Name,
Namespace: svc.Namespace,
}
d.loadBalancer.K8sMU.Lock()
defer d.loadBalancer.K8sMU.Unlock()
d.syncLB(nil, nil, svcns)
}
func (d *Daemon) endpointAddFn(obj interface{}) {
ep, ok := obj.(*v1.Endpoints)
if !ok {
return
}
svcns := types.K8sServiceNamespace{
Service: ep.Name,
Namespace: ep.Namespace,
}
newSvcEP := types.NewK8sServiceEndpoint()
for _, sub := range ep.Subsets {
for _, addr := range sub.Addresses {
newSvcEP.BEIPs[addr.IP] = true
}
for _, port := range sub.Ports {
lbPort, err := types.NewL4Addr(types.L4Type(port.Protocol), uint16(port.Port))
if err != nil {
log.Errorf("Error while creating a new LB Port: %s", err)
continue
}
newSvcEP.Ports[types.FEPortName(port.Name)] = lbPort
}
}
d.loadBalancer.K8sMU.Lock()
defer d.loadBalancer.K8sMU.Unlock()
d.loadBalancer.K8sEndpoints[svcns] = newSvcEP
d.syncLB(&svcns, nil, nil)
if d.conf.IsLBEnabled() {
if err := d.syncExternalLB(&svcns, nil, nil); err != nil {
log.Errorf("Unable to add endpoints on ingress service %s: %s", svcns, err)
return
}
}
}
func (d *Daemon) endpointModFn(_ interface{}, newObj interface{}) {
_, ok := newObj.(*v1.Endpoints)
if !ok {
return
}
d.endpointAddFn(newObj)
}
func (d *Daemon) endpointDelFn(obj interface{}) {
ep, ok := obj.(*v1.Endpoints)
if !ok {
return
}
svcns := &types.K8sServiceNamespace{
Service: ep.Name,
Namespace: ep.Namespace,
}
d.loadBalancer.K8sMU.Lock()
defer d.loadBalancer.K8sMU.Unlock()
d.syncLB(nil, nil, svcns)
if d.conf.IsLBEnabled() {
if err := d.syncExternalLB(nil, nil, svcns); err != nil {
log.Errorf("Unable to remove endpoints on ingress service %s: %s", svcns, err)
return
}
}
}
func areIPsConsistent(ipv4Enabled, isSvcIPv4 bool, svc types.K8sServiceNamespace, se *types.K8sServiceEndpoint) error {
if isSvcIPv4 {
if !ipv4Enabled {
return fmt.Errorf("Received an IPv4 kubernetes service but IPv4 is "+
"disabled in the cilium daemon. Ignoring service %+v", svc)
}
for epIP := range se.BEIPs {
//is IPv6?
if net.ParseIP(epIP).To4() == nil {
return fmt.Errorf("Not all endpoints IPs are IPv4. Ignoring IPv4 service %+v", svc)
}
}
} else {
for epIP := range se.BEIPs {
//is IPv4?
if net.ParseIP(epIP).To4() != nil {
return fmt.Errorf("Not all endpoints IPs are IPv6. Ignoring IPv6 service %+v", svc)
}
}
}
return nil
}
func getUniqPorts(svcPorts map[types.FEPortName]*types.FEPort) map[uint16]bool {
// We are not discriminating the different L4 protocols on the same L4
// port so we create the number of unique sets of service IP + service
// port.
uniqPorts := map[uint16]bool{}
for _, svcPort := range svcPorts {
uniqPorts[svcPort.Port] = true
}
return uniqPorts
}
func (d *Daemon) delK8sSVCs(svc types.K8sServiceNamespace, svcInfo *types.K8sServiceInfo, se *types.K8sServiceEndpoint) error {
isSvcIPv4 := svcInfo.FEIP.To4() != nil
if err := areIPsConsistent(!d.conf.IPv4Disabled, isSvcIPv4, svc, se); err != nil {
return err
}
repPorts := getUniqPorts(svcInfo.Ports)
for _, svcPort := range svcInfo.Ports {
if !repPorts[svcPort.Port] {
continue
}
repPorts[svcPort.Port] = false
if svcPort.ID != 0 {
if err := DeleteL3n4AddrIDByUUID(uint32(svcPort.ID)); err != nil {
log.Warningf("Error while cleaning service ID: %s", err)
}
}
fe, err := types.NewL3n4Addr(svcPort.Protocol, svcInfo.FEIP, svcPort.Port)
if err != nil {
log.Errorf("Error while creating a New L3n4AddrID: %s. Ignoring service %v...", err, svcInfo)
continue
}
if err := d.svcDeleteByFrontend(fe); err != nil {
log.Warningf("Error deleting service %+v, %s", fe, err)
} else {
log.Debugf("# cilium lb delete-service %s %d 0", svcInfo.FEIP, svcPort.Port)
}
if err := d.RevNATDelete(svcPort.ID); err != nil {
log.Warningf("Error deleting reverse NAT %+v, %s", svcPort.ID, err)
} else {
log.Debugf("# cilium lb delete-rev-nat %d", svcPort.ID)
}
}
return nil
}
func (d *Daemon) addK8sSVCs(svc types.K8sServiceNamespace, svcInfo *types.K8sServiceInfo, se *types.K8sServiceEndpoint) error {
isSvcIPv4 := svcInfo.FEIP.To4() != nil
if err := areIPsConsistent(!d.conf.IPv4Disabled, isSvcIPv4, svc, se); err != nil {
return err
}
uniqPorts := getUniqPorts(svcInfo.Ports)
for fePortName, fePort := range svcInfo.Ports {
if !uniqPorts[fePort.Port] {
continue
}
k8sBEPort := se.Ports[fePortName]
uniqPorts[fePort.Port] = false
if fePort.ID == 0 {
feAddr, err := types.NewL3n4Addr(fePort.Protocol, svcInfo.FEIP, fePort.Port)
if err != nil {
log.Errorf("Error while creating a new L3n4Addr: %s. Ignoring service...", err)
continue
}
feAddrID, err := PutL3n4Addr(*feAddr, 0)
if err != nil {
log.Errorf("Error while getting a new service ID: %s. Ignoring service %v...", err, feAddr)
continue
}
log.Debugf("Got feAddr ID %d for service %+v", feAddrID.ID, svc)
fePort.ID = feAddrID.ID
}
besValues := []types.LBBackEnd{}
if k8sBEPort != nil {
for epIP := range se.BEIPs {
bePort := types.LBBackEnd{
L3n4Addr: types.L3n4Addr{IP: net.ParseIP(epIP), L4Addr: *k8sBEPort},
Weight: 0,
}
besValues = append(besValues, bePort)
}
}
fe, err := types.NewL3n4AddrID(fePort.Protocol, svcInfo.FEIP, fePort.Port, fePort.ID)
if err != nil {
log.Errorf("Error while creating a New L3n4AddrID: %s. Ignoring service %v...", err, svcInfo)
continue
}
if _, err := d.svcAdd(*fe, besValues, true); err != nil {
log.Errorf("Error while inserting service in LB map: %s", err)
}
}
return nil
}
func (d *Daemon) syncLB(newSN, modSN, delSN *types.K8sServiceNamespace) {
deleteSN := func(delSN types.K8sServiceNamespace) {
svc, ok := d.loadBalancer.K8sServices[delSN]
if !ok {
delete(d.loadBalancer.K8sEndpoints, delSN)
return
}
endpoint, ok := d.loadBalancer.K8sEndpoints[delSN]
if !ok {
delete(d.loadBalancer.K8sServices, delSN)
return
}
if err := d.delK8sSVCs(delSN, svc, endpoint); err != nil {
log.Errorf("Unable to delete k8s service: %s", err)
return
}
delete(d.loadBalancer.K8sServices, delSN)
delete(d.loadBalancer.K8sEndpoints, delSN)
}
addSN := func(addSN types.K8sServiceNamespace) {
svcInfo, ok := d.loadBalancer.K8sServices[addSN]
if !ok {
return
}
endpoint, ok := d.loadBalancer.K8sEndpoints[addSN]
if !ok {
return
}
if err := d.addK8sSVCs(addSN, svcInfo, endpoint); err != nil {
log.Errorf("Unable to add K8s service: %s", err)
}
}
if delSN != nil {
// Clean old services
deleteSN(*delSN)
}
if modSN != nil {
// Re-add modified services
addSN(*modSN)
}
if newSN != nil {
// Add new services
addSN(*newSN)
}
}
func (d *Daemon) ingressAddFn(obj interface{}) {
if !d.conf.IsLBEnabled() {
// Add operations don't matter to non-LB nodes.
return
}
ingress, ok := obj.(*v1beta1.Ingress)
if !ok {
return
}
if ingress.Spec.Backend == nil {
// We only support Single Service Ingress for now
return
}
svcName := types.K8sServiceNamespace{
Service: ingress.Spec.Backend.ServiceName,
Namespace: ingress.Namespace,
}
ingressPort := ingress.Spec.Backend.ServicePort.IntValue()
fePort, err := types.NewFEPort(types.TCP, uint16(ingressPort))
if err != nil {
return
}
var host net.IP
if d.conf.IPv4Disabled {
host = d.conf.HostV6Addr
} else {
host = d.conf.HostV4Addr
}
ingressSvcInfo := types.NewK8sServiceInfo(host)
ingressSvcInfo.Ports[types.FEPortName(ingress.Spec.Backend.ServicePort.StrVal)] = fePort
syncIngress := func(ingressSvcInfo *types.K8sServiceInfo) error {
d.loadBalancer.K8sIngress[svcName] = ingressSvcInfo
if err := d.syncExternalLB(&svcName, nil, nil); err != nil {
return fmt.Errorf("Unable to add ingress service %s: %s", svcName, err)
}
return nil
}
d.loadBalancer.K8sMU.Lock()
err = syncIngress(ingressSvcInfo)
d.loadBalancer.K8sMU.Unlock()
if err != nil {
log.Errorf("%s", err)
return
}
hostname, _ := os.Hostname()
ingress.Status.LoadBalancer.Ingress = []v1.LoadBalancerIngress{
{
IP: host.String(),
Hostname: hostname,
},
}
_, err = d.k8sClient.Extensions().Ingresses(ingress.Namespace).UpdateStatus(ingress)
if err != nil {
log.Errorf("Unable to update status of ingress %s: %s", ingress.Name, err)
return
}
}
func (d *Daemon) ingressModFn(oldObj interface{}, newObj interface{}) {
oldIngress, ok := oldObj.(*v1beta1.Ingress)
if !ok {
return
}
newIngress, ok := newObj.(*v1beta1.Ingress)
if !ok {
return
}
if oldIngress.Spec.Backend == nil || newIngress.Spec.Backend == nil {
// We only support Single Service Ingress for now
return
}
// Add RevNAT to the BPF Map for non-LB nodes when a LB node update the
// ingress status with its address.
if !d.conf.IsLBEnabled() {
port := newIngress.Spec.Backend.ServicePort.IntValue()
for _, loadbalancer := range newIngress.Status.LoadBalancer.Ingress {
ingressIP := net.ParseIP(loadbalancer.IP)
if ingressIP == nil {
continue
}
feAddr, err := types.NewL3n4Addr(types.TCP, ingressIP, uint16(port))
if err != nil {
log.Errorf("Error while creating a new L3n4Addr: %s. Ignoring ingress %s/%s...", err, newIngress.Namespace, newIngress.Name)
continue
}
feAddrID, err := PutL3n4Addr(*feAddr, 0)
if err != nil {
log.Errorf("Error while getting a new service ID: %s. Ignoring ingress %s/%s...", err, newIngress.Namespace, newIngress.Name)
continue
}
log.Debugf("Got service ID %d for ingress %s/%s", feAddrID.ID, newIngress.Namespace, newIngress.Name)
if err := d.RevNATAdd(feAddrID.ID, feAddrID.L3n4Addr); err != nil {
log.Errorf("Unable to add reverse NAT ID for ingress %s/%s: %s", newIngress.Namespace, newIngress.Name, err)
}
}
return
}
if oldIngress.Spec.Backend.ServiceName == newIngress.Spec.Backend.ServiceName &&
oldIngress.Spec.Backend.ServicePort == newIngress.Spec.Backend.ServicePort {
return
}
d.ingressAddFn(newObj)
}
func (d *Daemon) ingressDelFn(obj interface{}) {
ing, ok := obj.(*v1beta1.Ingress)
if !ok {
return
}
if ing.Spec.Backend == nil {
// We only support Single Service Ingress for now
return
}
svcName := types.K8sServiceNamespace{
Service: ing.Spec.Backend.ServiceName,
Namespace: ing.Namespace,
}
// Remove RevNAT from the BPF Map for non-LB nodes.
if !d.conf.IsLBEnabled() {
port := ing.Spec.Backend.ServicePort.IntValue()
for _, loadbalancer := range ing.Status.LoadBalancer.Ingress {
ingressIP := net.ParseIP(loadbalancer.IP)
if ingressIP == nil {
continue
}
feAddr, err := types.NewL3n4Addr(types.TCP, ingressIP, uint16(port))
if err != nil {
log.Errorf("Error while creating a new L3n4Addr: %s. Ignoring ingress %s/%s...", err, ing.Namespace, ing.Name)
continue
}
// This is the only way that we can get the service's ID
// without accessing the KVStore.
svc := d.svcGetBySHA256Sum(feAddr.SHA256Sum())
if svc != nil {
if err := d.RevNATDelete(svc.FE.ID); err != nil {
log.Errorf("Error while removing RevNAT for ID %d for ingress %s/%s: %s", svc.FE.ID, ing.Namespace, ing.Name, err)
}
}
}
return
}
d.loadBalancer.K8sMU.Lock()
defer d.loadBalancer.K8sMU.Unlock()
ingressSvcInfo, ok := d.loadBalancer.K8sIngress[svcName]
if !ok {
return
}
// Get all active endpoints for the service specified in ingress
k8sEP, ok := d.loadBalancer.K8sEndpoints[svcName]
if !ok {
return
}
err := d.delK8sSVCs(svcName, ingressSvcInfo, k8sEP)
if err != nil {
log.Errorf("Unable to delete K8s ingress: %s", err)
return
}
delete(d.loadBalancer.K8sIngress, svcName)
}
func (d *Daemon) syncExternalLB(newSN, modSN, delSN *types.K8sServiceNamespace) error {
deleteSN := func(delSN types.K8sServiceNamespace) error {
ingSvc, ok := d.loadBalancer.K8sIngress[delSN]
if !ok {
return nil
}
endpoint, ok := d.loadBalancer.K8sEndpoints[delSN]
if !ok {
return nil
}
if err := d.delK8sSVCs(delSN, ingSvc, endpoint); err != nil {
return err
}
delete(d.loadBalancer.K8sServices, delSN)
return nil
}
addSN := func(addSN types.K8sServiceNamespace) error {
ingressSvcInfo, ok := d.loadBalancer.K8sIngress[addSN]
if !ok {
return nil
}
k8sEP, ok := d.loadBalancer.K8sEndpoints[addSN]
if !ok {
return nil
}
err := d.addK8sSVCs(addSN, ingressSvcInfo, k8sEP)
if err != nil {
return err
}
return nil
}
if delSN != nil {
// Clean old services
return deleteSN(*delSN)
}
if modSN != nil {
// Re-add modified services
return addSN(*modSN)
}
if newSN != nil {
// Add new services
return addSN(*newSN)
}
return nil
}
func (d *Daemon) addCiliumNetworkPolicy(obj interface{}) {
rule, ok := obj.(*k8s.CiliumNetworkPolicy)
if !ok {
log.Warningf("Invalid third-party objected, expected CiliumNetworkPolicy, got %+v", obj)
return
}
log.Debugf("Adding k8s TPR CiliumNetworkPolicy %+v", rule)
rules, err := rule.Parse()
if err != nil {
log.Warningf("Ignoring invalid third-party policy rule: %s", err)
return
}
opts := AddOptions{Replace: true}
if _, err := d.PolicyAdd(rules, &opts); err != nil {
log.Warningf("Error while adding kubernetes network policy %+v: %s", rules, err)
return
}
log.Infof("Imported third-party policy rule '%s'", rule.Metadata.Name)
}
func (d *Daemon) deleteCiliumNetworkPolicy(obj interface{}) {
rule, ok := obj.(*k8s.CiliumNetworkPolicy)
if !ok {
log.Warningf("Invalid third-party objected, expected CiliumNetworkPolicy, got %+v", obj)
return
}
log.Debugf("Deleting k8s TPR CiliumNetworkPolicy %+v", rule)
rules, err := rule.Parse()
if err != nil {
log.Warningf("Ignoring invalid third-party policy rule: %s", err)
return
}
if _, err := d.PolicyDelete(rules[0].Labels); err != nil {
log.Warningf("Error while adding kubernetes network policy %+v: %s", rules, err)
return
}
log.Infof("Deleted third-party policy rule '%s'", rule.Metadata.Name)
}
func (d *Daemon) updateCiliumNetworkPolicy(oldObj interface{}, newObj interface{}) {
// FIXME
d.deleteCiliumNetworkPolicy(oldObj)
d.addCiliumNetworkPolicy(newObj)
}
func (d *Daemon) nodesAddFn(obj interface{}) {
k8sNode, ok := obj.(*v1.Node)
if !ok {
log.Warningf("Invalid objected, expected v1.Node, got %+v", obj)
return
}
ni := node.Identity{Name: k8sNode.Name}
n := k8s.ParseNode(k8sNode)
node.UpdateNode(ni, n)
log.Debugf("Added node %s: %+v", ni, n)
}
func (d *Daemon) nodesModFn(oldObj interface{}, newObj interface{}) {
k8sNode, ok := newObj.(*v1.Node)
if !ok {
log.Warningf("Invalid objected, expected v1.Node, got %+v", newObj)
return
}
newNode := k8s.ParseNode(k8sNode)
ni := node.Identity{Name: k8sNode.Name}
oldNode := node.GetNode(ni)
// If node is the same don't even change it on the map
// TODO: Run the DeepEqual only for the metadata that we care about?
if reflect.DeepEqual(oldNode, newNode) {
return
}
node.UpdateNode(ni, newNode)
log.Debugf("k8s: Updated node %s to %+v", ni, newNode)
}
func (d *Daemon) nodesDelFn(obj interface{}) {
k8sNode, ok := obj.(*v1.Node)
if !ok {
log.Warningf("Invalid objected, expected v1.Node, got %+v", obj)
return
}
ni := node.Identity{Name: k8sNode.Name}
node.DeleteNode(ni)
log.Debugf("k8s: Removed node %s", ni)
}