@@ -16,6 +16,22 @@ import (
16
16
"github.com./cloudnativelabs/kube-router/pkg/utils"
17
17
)
18
18
19
+ const (
20
+ podCIDRSet = "podcidrdefinedset"
21
+
22
+ serviceVIPsSet = "servicevipsdefinedset"
23
+
24
+ allPeerSet = "allpeerset"
25
+ externalPeerSet = "externalpeerset"
26
+ iBGPPeerSet = "iBGPpeerset"
27
+
28
+ customImportRejectSet = "customimportrejectdefinedset"
29
+ defaultRouteSet = "defaultroutedefinedset"
30
+
31
+ kubeRouterExportPolicy = "kube_router_export"
32
+ kubeRouterImportPolicy = "kube_router_import"
33
+ )
34
+
19
35
// AddPolicies adds BGP import and export policies
20
36
func (nrc * NetworkRoutingController ) AddPolicies () error {
21
37
// we are rr server do not add export policies
@@ -75,7 +91,7 @@ func (nrc *NetworkRoutingController) AddPolicies() error {
75
91
func (nrc * NetworkRoutingController ) addPodCidrDefinedSet () error {
76
92
var currentDefinedSet * gobgpapi.DefinedSet
77
93
err := nrc .bgpServer .ListDefinedSet (context .Background (),
78
- & gobgpapi.ListDefinedSetRequest {DefinedType : gobgpapi .DefinedType_PREFIX , Name : "podcidrdefinedset" },
94
+ & gobgpapi.ListDefinedSetRequest {DefinedType : gobgpapi .DefinedType_PREFIX , Name : podCIDRSet },
79
95
func (ds * gobgpapi.DefinedSet ) {
80
96
currentDefinedSet = ds
81
97
})
@@ -89,7 +105,7 @@ func (nrc *NetworkRoutingController) addPodCidrDefinedSet() error {
89
105
}
90
106
podCidrDefinedSet := & gobgpapi.DefinedSet {
91
107
DefinedType : gobgpapi .DefinedType_PREFIX ,
92
- Name : "podcidrdefinedset" ,
108
+ Name : podCIDRSet ,
93
109
Prefixes : []* gobgpapi.Prefix {
94
110
{
95
111
IpPrefix : nrc .podCidr ,
@@ -108,7 +124,7 @@ func (nrc *NetworkRoutingController) addPodCidrDefinedSet() error {
108
124
func (nrc * NetworkRoutingController ) addServiceVIPsDefinedSet () error {
109
125
var currentDefinedSet * gobgpapi.DefinedSet
110
126
err := nrc .bgpServer .ListDefinedSet (context .Background (),
111
- & gobgpapi.ListDefinedSetRequest {DefinedType : gobgpapi .DefinedType_PREFIX , Name : "servicevipsdefinedset" },
127
+ & gobgpapi.ListDefinedSetRequest {DefinedType : gobgpapi .DefinedType_PREFIX , Name : serviceVIPsSet },
112
128
func (ds * gobgpapi.DefinedSet ) {
113
129
currentDefinedSet = ds
114
130
})
@@ -124,7 +140,7 @@ func (nrc *NetworkRoutingController) addServiceVIPsDefinedSet() error {
124
140
if currentDefinedSet == nil {
125
141
clusterIPPrefixSet := & gobgpapi.DefinedSet {
126
142
DefinedType : gobgpapi .DefinedType_PREFIX ,
127
- Name : "servicevipsdefinedset" ,
143
+ Name : serviceVIPsSet ,
128
144
Prefixes : advIPPrefixList ,
129
145
}
130
146
return nrc .bgpServer .AddDefinedSet (context .Background (),
@@ -167,7 +183,7 @@ func (nrc *NetworkRoutingController) addServiceVIPsDefinedSet() error {
167
183
}
168
184
clusterIPPrefixSet := & gobgpapi.DefinedSet {
169
185
DefinedType : gobgpapi .DefinedType_PREFIX ,
170
- Name : "servicevipsdefinedset" ,
186
+ Name : serviceVIPsSet ,
171
187
Prefixes : toAdd ,
172
188
}
173
189
err = nrc .bgpServer .AddDefinedSet (context .Background (),
@@ -177,7 +193,7 @@ func (nrc *NetworkRoutingController) addServiceVIPsDefinedSet() error {
177
193
}
178
194
clusterIPPrefixSet = & gobgpapi.DefinedSet {
179
195
DefinedType : gobgpapi .DefinedType_PREFIX ,
180
- Name : "servicevipsdefinedset" ,
196
+ Name : serviceVIPsSet ,
181
197
Prefixes : toDelete ,
182
198
}
183
199
err = nrc .bgpServer .DeleteDefinedSet (context .Background (),
@@ -193,7 +209,7 @@ func (nrc *NetworkRoutingController) addServiceVIPsDefinedSet() error {
193
209
func (nrc * NetworkRoutingController ) addDefaultRouteDefinedSet () error {
194
210
var currentDefinedSet * gobgpapi.DefinedSet
195
211
err := nrc .bgpServer .ListDefinedSet (context .Background (),
196
- & gobgpapi.ListDefinedSetRequest {DefinedType : gobgpapi .DefinedType_PREFIX , Name : "defaultroutedefinedset" },
212
+ & gobgpapi.ListDefinedSetRequest {DefinedType : gobgpapi .DefinedType_PREFIX , Name : defaultRouteSet },
197
213
func (ds * gobgpapi.DefinedSet ) {
198
214
currentDefinedSet = ds
199
215
})
@@ -204,7 +220,7 @@ func (nrc *NetworkRoutingController) addDefaultRouteDefinedSet() error {
204
220
cidrLen := 0
205
221
defaultRouteDefinedSet := & gobgpapi.DefinedSet {
206
222
DefinedType : gobgpapi .DefinedType_PREFIX ,
207
- Name : "defaultroutedefinedset" ,
223
+ Name : defaultRouteSet ,
208
224
Prefixes : []* gobgpapi.Prefix {
209
225
{
210
226
IpPrefix : "0.0.0.0/0" ,
@@ -223,7 +239,7 @@ func (nrc *NetworkRoutingController) addDefaultRouteDefinedSet() error {
223
239
func (nrc * NetworkRoutingController ) addCustomImportRejectDefinedSet () error {
224
240
var currentDefinedSet * gobgpapi.DefinedSet
225
241
err := nrc .bgpServer .ListDefinedSet (context .Background (),
226
- & gobgpapi.ListDefinedSetRequest {DefinedType : gobgpapi .DefinedType_PREFIX , Name : "customimportrejectdefinedset" },
242
+ & gobgpapi.ListDefinedSetRequest {DefinedType : gobgpapi .DefinedType_PREFIX , Name : customImportRejectSet },
227
243
func (ds * gobgpapi.DefinedSet ) {
228
244
currentDefinedSet = ds
229
245
})
@@ -242,7 +258,7 @@ func (nrc *NetworkRoutingController) addCustomImportRejectDefinedSet() error {
242
258
}
243
259
customImportRejectDefinedSet := & gobgpapi.DefinedSet {
244
260
DefinedType : gobgpapi .DefinedType_PREFIX ,
245
- Name : "customimportrejectdefinedset" ,
261
+ Name : customImportRejectSet ,
246
262
Prefixes : prefixes ,
247
263
}
248
264
return nrc .bgpServer .AddDefinedSet (context .Background (),
@@ -271,7 +287,7 @@ func (nrc *NetworkRoutingController) addiBGPPeersDefinedSet() ([]string, error)
271
287
272
288
var currentDefinedSet * gobgpapi.DefinedSet
273
289
err := nrc .bgpServer .ListDefinedSet (context .Background (),
274
- & gobgpapi.ListDefinedSetRequest {DefinedType : gobgpapi .DefinedType_NEIGHBOR , Name : "iBGPpeerset" },
290
+ & gobgpapi.ListDefinedSetRequest {DefinedType : gobgpapi .DefinedType_NEIGHBOR , Name : iBGPPeerSet },
275
291
func (ds * gobgpapi.DefinedSet ) {
276
292
currentDefinedSet = ds
277
293
})
@@ -281,7 +297,7 @@ func (nrc *NetworkRoutingController) addiBGPPeersDefinedSet() ([]string, error)
281
297
if currentDefinedSet == nil {
282
298
iBGPPeerNS := & gobgpapi.DefinedSet {
283
299
DefinedType : gobgpapi .DefinedType_NEIGHBOR ,
284
- Name : "iBGPpeerset" ,
300
+ Name : iBGPPeerSet ,
285
301
List : iBGPPeerCIDRs ,
286
302
}
287
303
err = nrc .bgpServer .AddDefinedSet (context .Background (), & gobgpapi.AddDefinedSetRequest {DefinedSet : iBGPPeerNS })
@@ -320,7 +336,7 @@ func (nrc *NetworkRoutingController) addiBGPPeersDefinedSet() ([]string, error)
320
336
}
321
337
iBGPPeerNS := & gobgpapi.DefinedSet {
322
338
DefinedType : gobgpapi .DefinedType_NEIGHBOR ,
323
- Name : "iBGPpeerset" ,
339
+ Name : iBGPPeerSet ,
324
340
List : toAdd ,
325
341
}
326
342
err = nrc .bgpServer .AddDefinedSet (context .Background (), & gobgpapi.AddDefinedSetRequest {DefinedSet : iBGPPeerNS })
@@ -329,7 +345,7 @@ func (nrc *NetworkRoutingController) addiBGPPeersDefinedSet() ([]string, error)
329
345
}
330
346
iBGPPeerNS = & gobgpapi.DefinedSet {
331
347
DefinedType : gobgpapi .DefinedType_NEIGHBOR ,
332
- Name : "iBGPpeerset" ,
348
+ Name : iBGPPeerSet ,
333
349
List : toDelete ,
334
350
}
335
351
err = nrc .bgpServer .DeleteDefinedSet (context .Background (),
@@ -346,7 +362,7 @@ func (nrc *NetworkRoutingController) addExternalBGPPeersDefinedSet() ([]string,
346
362
externalBgpPeers := make ([]string , 0 )
347
363
externalBGPPeerCIDRs := make ([]string , 0 )
348
364
err := nrc .bgpServer .ListDefinedSet (context .Background (),
349
- & gobgpapi.ListDefinedSetRequest {DefinedType : gobgpapi .DefinedType_NEIGHBOR , Name : "externalpeerset" },
365
+ & gobgpapi.ListDefinedSetRequest {DefinedType : gobgpapi .DefinedType_NEIGHBOR , Name : externalPeerSet },
350
366
func (ds * gobgpapi.DefinedSet ) {
351
367
currentDefinedSet = ds
352
368
})
@@ -370,7 +386,7 @@ func (nrc *NetworkRoutingController) addExternalBGPPeersDefinedSet() ([]string,
370
386
if currentDefinedSet == nil {
371
387
eBGPPeerNS := & gobgpapi.DefinedSet {
372
388
DefinedType : gobgpapi .DefinedType_NEIGHBOR ,
373
- Name : "externalpeerset" ,
389
+ Name : externalPeerSet ,
374
390
List : externalBGPPeerCIDRs ,
375
391
}
376
392
err = nrc .bgpServer .AddDefinedSet (context .Background (), & gobgpapi.AddDefinedSetRequest {DefinedSet : eBGPPeerNS })
@@ -384,7 +400,7 @@ func (nrc *NetworkRoutingController) addExternalBGPPeersDefinedSet() ([]string,
384
400
func (nrc * NetworkRoutingController ) addAllBGPPeersDefinedSet (iBGPPeerCIDRs , externalBGPPeerCIDRs []string ) error {
385
401
var currentDefinedSet * gobgpapi.DefinedSet
386
402
err := nrc .bgpServer .ListDefinedSet (context .Background (),
387
- & gobgpapi.ListDefinedSetRequest {DefinedType : gobgpapi .DefinedType_NEIGHBOR , Name : "allpeerset" },
403
+ & gobgpapi.ListDefinedSetRequest {DefinedType : gobgpapi .DefinedType_NEIGHBOR , Name : allPeerSet },
388
404
func (ds * gobgpapi.DefinedSet ) {
389
405
currentDefinedSet = ds
390
406
})
@@ -397,7 +413,7 @@ func (nrc *NetworkRoutingController) addAllBGPPeersDefinedSet(iBGPPeerCIDRs, ext
397
413
if currentDefinedSet == nil {
398
414
allPeerNS := & gobgpapi.DefinedSet {
399
415
DefinedType : gobgpapi .DefinedType_NEIGHBOR ,
400
- Name : "allpeerset" ,
416
+ Name : allPeerSet ,
401
417
List : allBgpPeers ,
402
418
}
403
419
return nrc .bgpServer .AddDefinedSet (context .Background (), & gobgpapi.AddDefinedSetRequest {DefinedSet : allPeerNS })
@@ -429,7 +445,7 @@ func (nrc *NetworkRoutingController) addAllBGPPeersDefinedSet(iBGPPeerCIDRs, ext
429
445
}
430
446
allPeerNS := & gobgpapi.DefinedSet {
431
447
DefinedType : gobgpapi .DefinedType_NEIGHBOR ,
432
- Name : "allpeerset" ,
448
+ Name : allPeerSet ,
433
449
List : toAdd ,
434
450
}
435
451
err = nrc .bgpServer .AddDefinedSet (context .Background (), & gobgpapi.AddDefinedSetRequest {DefinedSet : allPeerNS })
@@ -438,7 +454,7 @@ func (nrc *NetworkRoutingController) addAllBGPPeersDefinedSet(iBGPPeerCIDRs, ext
438
454
}
439
455
allPeerNS = & gobgpapi.DefinedSet {
440
456
DefinedType : gobgpapi .DefinedType_NEIGHBOR ,
441
- Name : "allpeerset" ,
457
+ Name : allPeerSet ,
442
458
List : toDelete ,
443
459
}
444
460
err = nrc .bgpServer .DeleteDefinedSet (context .Background (),
@@ -494,11 +510,11 @@ func (nrc *NetworkRoutingController) addExportPolicies() error {
494
510
Conditions : & gobgpapi.Conditions {
495
511
PrefixSet : & gobgpapi.MatchSet {
496
512
Type : gobgpapi .MatchSet_ANY ,
497
- Name : "podcidrdefinedset" ,
513
+ Name : podCIDRSet ,
498
514
},
499
515
NeighborSet : & gobgpapi.MatchSet {
500
516
Type : gobgpapi .MatchSet_ANY ,
501
- Name : "iBGPpeerset" ,
517
+ Name : iBGPPeerSet ,
502
518
},
503
519
},
504
520
Actions : & actions ,
@@ -526,11 +542,11 @@ func (nrc *NetworkRoutingController) addExportPolicies() error {
526
542
Conditions : & gobgpapi.Conditions {
527
543
PrefixSet : & gobgpapi.MatchSet {
528
544
Type : gobgpapi .MatchSet_ANY ,
529
- Name : "servicevipsdefinedset" ,
545
+ Name : serviceVIPsSet ,
530
546
},
531
547
NeighborSet : & gobgpapi.MatchSet {
532
548
Type : gobgpapi .MatchSet_ANY ,
533
- Name : "externalpeerset" ,
549
+ Name : externalPeerSet ,
534
550
},
535
551
},
536
552
Actions : & bgpActions ,
@@ -554,11 +570,11 @@ func (nrc *NetworkRoutingController) addExportPolicies() error {
554
570
Conditions : & gobgpapi.Conditions {
555
571
PrefixSet : & gobgpapi.MatchSet {
556
572
Type : gobgpapi .MatchSet_ANY ,
557
- Name : "podcidrdefinedset" ,
573
+ Name : podCIDRSet ,
558
574
},
559
575
NeighborSet : & gobgpapi.MatchSet {
560
576
Type : gobgpapi .MatchSet_ANY ,
561
- Name : "externalpeerset" ,
577
+ Name : externalPeerSet ,
562
578
},
563
579
},
564
580
Actions : & actions ,
@@ -567,13 +583,13 @@ func (nrc *NetworkRoutingController) addExportPolicies() error {
567
583
}
568
584
569
585
definition := gobgpapi.Policy {
570
- Name : "kube_router_export" ,
586
+ Name : kubeRouterExportPolicy ,
571
587
Statements : statements ,
572
588
}
573
589
574
590
policyAlreadyExists := false
575
591
checkExistingPolicy := func (existingPolicy * gobgpapi.Policy ) {
576
- if existingPolicy .Name == "kube_router_export" {
592
+ if existingPolicy .Name == kubeRouterExportPolicy {
577
593
policyAlreadyExists = true
578
594
}
579
595
}
@@ -592,7 +608,7 @@ func (nrc *NetworkRoutingController) addExportPolicies() error {
592
608
policyAssignmentExists := false
593
609
checkExistingPolicyAssignment := func (existingPolicyAssignment * gobgpapi.PolicyAssignment ) {
594
610
for _ , policy := range existingPolicyAssignment .Policies {
595
- if policy .Name == "kube_router_export" {
611
+ if policy .Name == kubeRouterExportPolicy {
596
612
policyAssignmentExists = true
597
613
}
598
614
}
@@ -634,11 +650,11 @@ func (nrc *NetworkRoutingController) addImportPolicies() error {
634
650
Conditions : & gobgpapi.Conditions {
635
651
PrefixSet : & gobgpapi.MatchSet {
636
652
Type : gobgpapi .MatchSet_ANY ,
637
- Name : "servicevipsdefinedset" ,
653
+ Name : serviceVIPsSet ,
638
654
},
639
655
NeighborSet : & gobgpapi.MatchSet {
640
656
Type : gobgpapi .MatchSet_ANY ,
641
- Name : "allpeerset" ,
657
+ Name : allPeerSet ,
642
658
},
643
659
},
644
660
Actions : & actions ,
@@ -648,11 +664,11 @@ func (nrc *NetworkRoutingController) addImportPolicies() error {
648
664
Conditions : & gobgpapi.Conditions {
649
665
PrefixSet : & gobgpapi.MatchSet {
650
666
Type : gobgpapi .MatchSet_ANY ,
651
- Name : "defaultroutedefinedset" ,
667
+ Name : defaultRouteSet ,
652
668
},
653
669
NeighborSet : & gobgpapi.MatchSet {
654
670
Type : gobgpapi .MatchSet_ANY ,
655
- Name : "allpeerset" ,
671
+ Name : allPeerSet ,
656
672
},
657
673
},
658
674
Actions : & actions ,
@@ -662,26 +678,26 @@ func (nrc *NetworkRoutingController) addImportPolicies() error {
662
678
statements = append (statements , & gobgpapi.Statement {
663
679
Conditions : & gobgpapi.Conditions {
664
680
PrefixSet : & gobgpapi.MatchSet {
665
- Name : "customimportrejectdefinedset" ,
681
+ Name : customImportRejectSet ,
666
682
Type : gobgpapi .MatchSet_ANY ,
667
683
},
668
684
NeighborSet : & gobgpapi.MatchSet {
669
685
Type : gobgpapi .MatchSet_ANY ,
670
- Name : "allpeerset" ,
686
+ Name : allPeerSet ,
671
687
},
672
688
},
673
689
Actions : & actions ,
674
690
})
675
691
}
676
692
677
693
definition := gobgpapi.Policy {
678
- Name : "kube_router_import" ,
694
+ Name : kubeRouterImportPolicy ,
679
695
Statements : statements ,
680
696
}
681
697
682
698
policyAlreadyExists := false
683
699
checkExistingPolicy := func (existingPolicy * gobgpapi.Policy ) {
684
- if existingPolicy .Name == "kube_router_import" {
700
+ if existingPolicy .Name == kubeRouterImportPolicy {
685
701
policyAlreadyExists = true
686
702
}
687
703
}
@@ -700,7 +716,7 @@ func (nrc *NetworkRoutingController) addImportPolicies() error {
700
716
policyAssignmentExists := false
701
717
checkExistingPolicyAssignment := func (existingPolicyAssignment * gobgpapi.PolicyAssignment ) {
702
718
for _ , policy := range existingPolicyAssignment .Policies {
703
- if policy .Name == "kube_router_import" {
719
+ if policy .Name == kubeRouterImportPolicy {
704
720
policyAssignmentExists = true
705
721
}
706
722
}
0 commit comments