@@ -28,7 +28,6 @@ import (
28
28
29
29
"github.com/go-logr/logr"
30
30
"github.com/netrisai/netris-operator/api/v1alpha1"
31
- k8sv1alpha1 "github.com/netrisai/netris-operator/api/v1alpha1"
32
31
"github.com/netrisai/netris-operator/calicowatcher/calico"
33
32
"github.com/netrisai/netris-operator/configloader"
34
33
"github.com/netrisai/netris-operator/netrisstorage"
@@ -70,8 +69,8 @@ type Watcher struct {
70
69
71
70
type data struct {
72
71
deleteMode bool
73
- generatedBGPs []* k8sv1alpha1 .BGP
74
- bgpList []* k8sv1alpha1 .BGP
72
+ generatedBGPs []* v1alpha1 .BGP
73
+ bgpList []* v1alpha1 .BGP
75
74
bgpConfs []* calico.BGPConfiguration
76
75
77
76
nodesMap map [string ]* nodeIP
@@ -98,7 +97,7 @@ type Options struct {
98
97
// NewWatcher is the main initialization function.
99
98
func NewWatcher (nStorage * netrisstorage.Storage , mgr manager.Manager , options Options ) (* Watcher , error ) {
100
99
if nStorage == nil {
101
- return nil , fmt .Errorf ("Please provide NStorage" )
100
+ return nil , fmt .Errorf ("please provide NStorage" )
102
101
}
103
102
104
103
watcher := & Watcher {
@@ -170,7 +169,7 @@ func (w *Watcher) Start() {
170
169
case <- w .stop :
171
170
ticker .Stop ()
172
171
close (w .stop )
173
- break
172
+ return
174
173
}
175
174
}
176
175
}
@@ -347,7 +346,7 @@ func (w *Watcher) deleteProcess() error {
347
346
return err
348
347
}
349
348
350
- w .data .generatedBGPs = []* k8sv1alpha1 .BGP {}
349
+ w .data .generatedBGPs = []* v1alpha1 .BGP {}
351
350
352
351
debugLogger .Info ("Geting BGPs from k8s" , "deleteMode" , w .data .deleteMode )
353
352
bgps , err := w .getBGPs ()
@@ -431,22 +430,29 @@ func (w *Watcher) updateBGPConfMesh(enabled bool) error {
431
430
}
432
431
433
432
func (w * Watcher ) generateBGPs () error {
434
- generatedBGPs := []* k8sv1alpha1 .BGP {}
433
+ generatedBGPs := []* v1alpha1 .BGP {}
435
434
436
435
nameReg , _ := regexp .Compile ("[^a-z0-9.]+" )
437
436
for name , node := range w .data .nodesMap {
438
437
asn , err := strconv .Atoi (node .ASN )
439
438
if err != nil {
440
439
return err
441
440
}
442
- PrefixListInboundList := []string {fmt .Sprintf ("permit %s/%d" , node .IPIP , w .data .blockSize )}
441
+
442
+ // Get the network address using node IP
443
+ _ , ipipNetAddr , err := net .ParseCIDR (fmt .Sprintf ("%s/%d" , node .IPIP , w .data .blockSize ))
444
+ if err != nil {
445
+ return fmt .Errorf ("node ip: %s" , err )
446
+ }
447
+
448
+ PrefixListInboundList := []string {fmt .Sprintf ("permit %s" , ipipNetAddr .String ())}
443
449
for _ , cidr := range w .data .serviceCIDRs {
444
450
PrefixListInboundList = append (PrefixListInboundList , fmt .Sprintf ("permit %s le %d" , cidr , 32 ))
445
451
}
446
452
447
453
name := fmt .Sprintf ("%s-%s" , name , strings .Split (node .IP , "/" )[0 ])
448
454
449
- bgp := & k8sv1alpha1 .BGP {
455
+ bgp := & v1alpha1 .BGP {
450
456
ObjectMeta : metav1.ObjectMeta {
451
457
Name : nameReg .ReplaceAllString (name , "-" ),
452
458
Namespace : "default" ,
@@ -455,7 +461,7 @@ func (w *Watcher) generateBGPs() error {
455
461
Kind : "BGP" ,
456
462
APIVersion : "k8s.netris.ai/v1alpha1" ,
457
463
},
458
- Spec : k8sv1alpha1 .BGPSpec {
464
+ Spec : v1alpha1 .BGPSpec {
459
465
Site : w .data .site .Name ,
460
466
NeighborAS : asn ,
461
467
Hardware : w .data .switchName ,
@@ -468,7 +474,7 @@ func (w *Watcher) generateBGPs() error {
468
474
PrefixListInbound : PrefixListInboundList ,
469
475
PrefixListOutbound : []string {
470
476
"permit 0.0.0.0/0" ,
471
- fmt .Sprintf ("deny %s/%d " , node . IPIP , w . data . blockSize ),
477
+ fmt .Sprintf ("deny %s" , ipipNetAddr . String () ),
472
478
fmt .Sprintf ("permit %s le %d" , w .data .clusterCIDR , w .data .blockSize ),
473
479
},
474
480
},
@@ -483,7 +489,7 @@ func (w *Watcher) generateBGPs() error {
483
489
return nil
484
490
}
485
491
486
- func (w * Watcher ) createBGPs (BGPs []* k8sv1alpha1 .BGP ) []error {
492
+ func (w * Watcher ) createBGPs (BGPs []* v1alpha1 .BGP ) []error {
487
493
var errors []error
488
494
for _ , bgp := range BGPs {
489
495
if err := w .createBGP (bgp ); err != nil {
@@ -493,13 +499,13 @@ func (w *Watcher) createBGPs(BGPs []*k8sv1alpha1.BGP) []error {
493
499
return errors
494
500
}
495
501
496
- func (w * Watcher ) createBGP (bgp * k8sv1alpha1 .BGP ) error {
502
+ func (w * Watcher ) createBGP (bgp * v1alpha1 .BGP ) error {
497
503
ctx , cancel := context .WithTimeout (cntxt , contextTimeout )
498
504
defer cancel ()
499
505
return w .client .Create (ctx , bgp .DeepCopyObject (), & client.CreateOptions {})
500
506
}
501
507
502
- func (w * Watcher ) updateBGPs (BGPs []* k8sv1alpha1 .BGP ) []error {
508
+ func (w * Watcher ) updateBGPs (BGPs []* v1alpha1 .BGP ) []error {
503
509
var errors []error
504
510
for _ , bgp := range BGPs {
505
511
if err := w .updateBGP (bgp ); err != nil {
@@ -509,13 +515,13 @@ func (w *Watcher) updateBGPs(BGPs []*k8sv1alpha1.BGP) []error {
509
515
return errors
510
516
}
511
517
512
- func (w * Watcher ) updateBGP (bgp * k8sv1alpha1 .BGP ) error {
518
+ func (w * Watcher ) updateBGP (bgp * v1alpha1 .BGP ) error {
513
519
ctx , cancel := context .WithTimeout (cntxt , contextTimeout )
514
520
defer cancel ()
515
521
return w .client .Update (ctx , bgp .DeepCopyObject (), & client.UpdateOptions {})
516
522
}
517
523
518
- func (w * Watcher ) deleteBGPs (BGPs []* k8sv1alpha1 .BGP ) []error {
524
+ func (w * Watcher ) deleteBGPs (BGPs []* v1alpha1 .BGP ) []error {
519
525
var errors []error
520
526
for _ , bgp := range BGPs {
521
527
if err := w .deleteBGP (bgp ); err != nil {
@@ -525,19 +531,19 @@ func (w *Watcher) deleteBGPs(BGPs []*k8sv1alpha1.BGP) []error {
525
531
return errors
526
532
}
527
533
528
- func (w * Watcher ) deleteBGP (bgp * k8sv1alpha1 .BGP ) error {
534
+ func (w * Watcher ) deleteBGP (bgp * v1alpha1 .BGP ) error {
529
535
ctx , cancel := context .WithTimeout (cntxt , contextTimeout )
530
536
defer cancel ()
531
537
return w .client .Delete (ctx , bgp .DeepCopyObject (), & client.DeleteAllOfOptions {})
532
538
}
533
539
534
- func (w * Watcher ) compareBGPs () ([]* k8sv1alpha1 .BGP , []* k8sv1alpha1 .BGP , []* k8sv1alpha1 .BGP ) {
535
- genBGPsMap := make (map [string ]* k8sv1alpha1 .BGP )
536
- BGPsMap := make (map [string ]* k8sv1alpha1 .BGP )
540
+ func (w * Watcher ) compareBGPs () ([]* v1alpha1 .BGP , []* v1alpha1 .BGP , []* v1alpha1 .BGP ) {
541
+ genBGPsMap := make (map [string ]* v1alpha1 .BGP )
542
+ BGPsMap := make (map [string ]* v1alpha1 .BGP )
537
543
538
- bgpsForCreate := []* k8sv1alpha1 .BGP {}
539
- bgpsForDelete := []* k8sv1alpha1 .BGP {}
540
- bgpsForUpdate := []* k8sv1alpha1 .BGP {}
544
+ bgpsForCreate := []* v1alpha1 .BGP {}
545
+ bgpsForDelete := []* v1alpha1 .BGP {}
546
+ bgpsForUpdate := []* v1alpha1 .BGP {}
541
547
542
548
for _ , bgp := range w .data .generatedBGPs {
543
549
genBGPsMap [bgp .Name ] = bgp
@@ -568,10 +574,10 @@ func (w *Watcher) compareBGPs() ([]*k8sv1alpha1.BGP, []*k8sv1alpha1.BGP, []*k8sv
568
574
return bgpsForCreate , bgpsForDelete , bgpsForUpdate
569
575
}
570
576
571
- func (w * Watcher ) getBGPs () (* k8sv1alpha1 .BGPList , error ) {
577
+ func (w * Watcher ) getBGPs () (* v1alpha1 .BGPList , error ) {
572
578
ctx , cancel := context .WithTimeout (cntxt , contextTimeout )
573
579
defer cancel ()
574
- bgps := & k8sv1alpha1 .BGPList {}
580
+ bgps := & v1alpha1 .BGPList {}
575
581
err := w .client .List (ctx , bgps , & client.ListOptions {})
576
582
if err != nil {
577
583
return nil , err
@@ -605,7 +611,7 @@ func (w *Watcher) getNodes() error {
605
611
}
606
612
607
613
if len (nodes .Items ) == 0 {
608
- return fmt .Errorf ("Nodes are missing" )
614
+ return fmt .Errorf ("nodes are missing" )
609
615
}
610
616
w .data .nodes = nodes
611
617
return nil
@@ -716,7 +722,7 @@ func (w *Watcher) nodesProcessing() error {
716
722
asn := ""
717
723
718
724
if _ , ok := anns ["projectcalico.org/ASNumber" ]; ! ok {
719
- return fmt .Errorf ("Couldn 't get as number for node %s" , node .Name )
725
+ return fmt .Errorf ("couldn 't get as number for node %s" , node .Name )
720
726
}
721
727
722
728
asn = anns ["projectcalico.org/ASNumber" ]
@@ -734,17 +740,30 @@ func (w *Watcher) nodesProcessing() error {
734
740
}
735
741
736
742
if siteName == "" {
737
- id , gateway , err := w . findSiteByIP ( ip )
743
+ sbnt , err := findIPAMByIP ( ip , w . NStorage . SubnetsStorage . GetAll () )
738
744
if err != nil {
739
745
fmt .Println (err )
740
746
continue
741
747
}
748
+
749
+ _ , ipNet , err := net .ParseCIDR (sbnt .Prefix )
750
+ if err != nil {
751
+ fmt .Println (err )
752
+ continue
753
+ }
754
+
755
+ subnet = ipNet .String ()
756
+ id := 0
757
+
758
+ if len (sbnt .Sites ) > 0 {
759
+ id = sbnt .Sites [0 ].ID
760
+ }
761
+
742
762
var ok bool
743
763
if site , ok = w .NStorage .SitesStorage .FindByID (id ); ok {
744
764
siteName = site .Name
745
765
}
746
- subnet = gateway
747
- if vn , ok := w .NStorage .VNetStorage .FindByGateway (gateway ); ok {
766
+ if vn , ok := w .NStorage .VNetStorage .FindByGateway (subnet ); ok {
748
767
vnet = vn
749
768
}
750
769
}
@@ -753,14 +772,14 @@ func (w *Watcher) nodesProcessing() error {
753
772
}
754
773
755
774
if siteName == "" {
756
- return fmt .Errorf ("Couldn 't find site" )
775
+ return fmt .Errorf ("couldn 't find site" )
757
776
}
758
777
759
778
if vnet == nil {
760
- return fmt .Errorf ("Couldn 't find vnet" )
779
+ return fmt .Errorf ("couldn 't find vnet" )
761
780
}
762
781
if vnet .ID == 0 {
763
- return fmt .Errorf ("Couldn 't find vnet" )
782
+ return fmt .Errorf ("couldn 't find vnet" )
764
783
}
765
784
766
785
vnetName = vnet .Name
@@ -816,7 +835,7 @@ func (w *Watcher) validateASNRange(asns string) (int, int, error) {
816
835
return a , b , nil
817
836
}
818
837
819
- // patchStringValue specifies a patch operation for a string.
838
+ // patchStringValue specifies a patch operation for a string.
820
839
type patchStringValue struct {
821
840
Op string `json:"op"`
822
841
Path string `json:"path"`
0 commit comments