Skip to content

Commit 01f2ff2

Browse files
committed
fact(NRC): convert BGP set names to const
Convert all BGP set names to constants and then refer to them via the constant across the code base so that we reduce the effect of typos.
1 parent 85cecb6 commit 01f2ff2

File tree

2 files changed

+144
-128
lines changed

2 files changed

+144
-128
lines changed

pkg/controllers/routing/bgp_policies.go

+54-38
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ import (
1616
"github.com./cloudnativelabs/kube-router/pkg/utils"
1717
)
1818

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+
1935
// AddPolicies adds BGP import and export policies
2036
func (nrc *NetworkRoutingController) AddPolicies() error {
2137
// we are rr server do not add export policies
@@ -75,7 +91,7 @@ func (nrc *NetworkRoutingController) AddPolicies() error {
7591
func (nrc *NetworkRoutingController) addPodCidrDefinedSet() error {
7692
var currentDefinedSet *gobgpapi.DefinedSet
7793
err := nrc.bgpServer.ListDefinedSet(context.Background(),
78-
&gobgpapi.ListDefinedSetRequest{DefinedType: gobgpapi.DefinedType_PREFIX, Name: "podcidrdefinedset"},
94+
&gobgpapi.ListDefinedSetRequest{DefinedType: gobgpapi.DefinedType_PREFIX, Name: podCIDRSet},
7995
func(ds *gobgpapi.DefinedSet) {
8096
currentDefinedSet = ds
8197
})
@@ -89,7 +105,7 @@ func (nrc *NetworkRoutingController) addPodCidrDefinedSet() error {
89105
}
90106
podCidrDefinedSet := &gobgpapi.DefinedSet{
91107
DefinedType: gobgpapi.DefinedType_PREFIX,
92-
Name: "podcidrdefinedset",
108+
Name: podCIDRSet,
93109
Prefixes: []*gobgpapi.Prefix{
94110
{
95111
IpPrefix: nrc.podCidr,
@@ -108,7 +124,7 @@ func (nrc *NetworkRoutingController) addPodCidrDefinedSet() error {
108124
func (nrc *NetworkRoutingController) addServiceVIPsDefinedSet() error {
109125
var currentDefinedSet *gobgpapi.DefinedSet
110126
err := nrc.bgpServer.ListDefinedSet(context.Background(),
111-
&gobgpapi.ListDefinedSetRequest{DefinedType: gobgpapi.DefinedType_PREFIX, Name: "servicevipsdefinedset"},
127+
&gobgpapi.ListDefinedSetRequest{DefinedType: gobgpapi.DefinedType_PREFIX, Name: serviceVIPsSet},
112128
func(ds *gobgpapi.DefinedSet) {
113129
currentDefinedSet = ds
114130
})
@@ -124,7 +140,7 @@ func (nrc *NetworkRoutingController) addServiceVIPsDefinedSet() error {
124140
if currentDefinedSet == nil {
125141
clusterIPPrefixSet := &gobgpapi.DefinedSet{
126142
DefinedType: gobgpapi.DefinedType_PREFIX,
127-
Name: "servicevipsdefinedset",
143+
Name: serviceVIPsSet,
128144
Prefixes: advIPPrefixList,
129145
}
130146
return nrc.bgpServer.AddDefinedSet(context.Background(),
@@ -167,7 +183,7 @@ func (nrc *NetworkRoutingController) addServiceVIPsDefinedSet() error {
167183
}
168184
clusterIPPrefixSet := &gobgpapi.DefinedSet{
169185
DefinedType: gobgpapi.DefinedType_PREFIX,
170-
Name: "servicevipsdefinedset",
186+
Name: serviceVIPsSet,
171187
Prefixes: toAdd,
172188
}
173189
err = nrc.bgpServer.AddDefinedSet(context.Background(),
@@ -177,7 +193,7 @@ func (nrc *NetworkRoutingController) addServiceVIPsDefinedSet() error {
177193
}
178194
clusterIPPrefixSet = &gobgpapi.DefinedSet{
179195
DefinedType: gobgpapi.DefinedType_PREFIX,
180-
Name: "servicevipsdefinedset",
196+
Name: serviceVIPsSet,
181197
Prefixes: toDelete,
182198
}
183199
err = nrc.bgpServer.DeleteDefinedSet(context.Background(),
@@ -193,7 +209,7 @@ func (nrc *NetworkRoutingController) addServiceVIPsDefinedSet() error {
193209
func (nrc *NetworkRoutingController) addDefaultRouteDefinedSet() error {
194210
var currentDefinedSet *gobgpapi.DefinedSet
195211
err := nrc.bgpServer.ListDefinedSet(context.Background(),
196-
&gobgpapi.ListDefinedSetRequest{DefinedType: gobgpapi.DefinedType_PREFIX, Name: "defaultroutedefinedset"},
212+
&gobgpapi.ListDefinedSetRequest{DefinedType: gobgpapi.DefinedType_PREFIX, Name: defaultRouteSet},
197213
func(ds *gobgpapi.DefinedSet) {
198214
currentDefinedSet = ds
199215
})
@@ -204,7 +220,7 @@ func (nrc *NetworkRoutingController) addDefaultRouteDefinedSet() error {
204220
cidrLen := 0
205221
defaultRouteDefinedSet := &gobgpapi.DefinedSet{
206222
DefinedType: gobgpapi.DefinedType_PREFIX,
207-
Name: "defaultroutedefinedset",
223+
Name: defaultRouteSet,
208224
Prefixes: []*gobgpapi.Prefix{
209225
{
210226
IpPrefix: "0.0.0.0/0",
@@ -223,7 +239,7 @@ func (nrc *NetworkRoutingController) addDefaultRouteDefinedSet() error {
223239
func (nrc *NetworkRoutingController) addCustomImportRejectDefinedSet() error {
224240
var currentDefinedSet *gobgpapi.DefinedSet
225241
err := nrc.bgpServer.ListDefinedSet(context.Background(),
226-
&gobgpapi.ListDefinedSetRequest{DefinedType: gobgpapi.DefinedType_PREFIX, Name: "customimportrejectdefinedset"},
242+
&gobgpapi.ListDefinedSetRequest{DefinedType: gobgpapi.DefinedType_PREFIX, Name: customImportRejectSet},
227243
func(ds *gobgpapi.DefinedSet) {
228244
currentDefinedSet = ds
229245
})
@@ -242,7 +258,7 @@ func (nrc *NetworkRoutingController) addCustomImportRejectDefinedSet() error {
242258
}
243259
customImportRejectDefinedSet := &gobgpapi.DefinedSet{
244260
DefinedType: gobgpapi.DefinedType_PREFIX,
245-
Name: "customimportrejectdefinedset",
261+
Name: customImportRejectSet,
246262
Prefixes: prefixes,
247263
}
248264
return nrc.bgpServer.AddDefinedSet(context.Background(),
@@ -271,7 +287,7 @@ func (nrc *NetworkRoutingController) addiBGPPeersDefinedSet() ([]string, error)
271287

272288
var currentDefinedSet *gobgpapi.DefinedSet
273289
err := nrc.bgpServer.ListDefinedSet(context.Background(),
274-
&gobgpapi.ListDefinedSetRequest{DefinedType: gobgpapi.DefinedType_NEIGHBOR, Name: "iBGPpeerset"},
290+
&gobgpapi.ListDefinedSetRequest{DefinedType: gobgpapi.DefinedType_NEIGHBOR, Name: iBGPPeerSet},
275291
func(ds *gobgpapi.DefinedSet) {
276292
currentDefinedSet = ds
277293
})
@@ -281,7 +297,7 @@ func (nrc *NetworkRoutingController) addiBGPPeersDefinedSet() ([]string, error)
281297
if currentDefinedSet == nil {
282298
iBGPPeerNS := &gobgpapi.DefinedSet{
283299
DefinedType: gobgpapi.DefinedType_NEIGHBOR,
284-
Name: "iBGPpeerset",
300+
Name: iBGPPeerSet,
285301
List: iBGPPeerCIDRs,
286302
}
287303
err = nrc.bgpServer.AddDefinedSet(context.Background(), &gobgpapi.AddDefinedSetRequest{DefinedSet: iBGPPeerNS})
@@ -320,7 +336,7 @@ func (nrc *NetworkRoutingController) addiBGPPeersDefinedSet() ([]string, error)
320336
}
321337
iBGPPeerNS := &gobgpapi.DefinedSet{
322338
DefinedType: gobgpapi.DefinedType_NEIGHBOR,
323-
Name: "iBGPpeerset",
339+
Name: iBGPPeerSet,
324340
List: toAdd,
325341
}
326342
err = nrc.bgpServer.AddDefinedSet(context.Background(), &gobgpapi.AddDefinedSetRequest{DefinedSet: iBGPPeerNS})
@@ -329,7 +345,7 @@ func (nrc *NetworkRoutingController) addiBGPPeersDefinedSet() ([]string, error)
329345
}
330346
iBGPPeerNS = &gobgpapi.DefinedSet{
331347
DefinedType: gobgpapi.DefinedType_NEIGHBOR,
332-
Name: "iBGPpeerset",
348+
Name: iBGPPeerSet,
333349
List: toDelete,
334350
}
335351
err = nrc.bgpServer.DeleteDefinedSet(context.Background(),
@@ -346,7 +362,7 @@ func (nrc *NetworkRoutingController) addExternalBGPPeersDefinedSet() ([]string,
346362
externalBgpPeers := make([]string, 0)
347363
externalBGPPeerCIDRs := make([]string, 0)
348364
err := nrc.bgpServer.ListDefinedSet(context.Background(),
349-
&gobgpapi.ListDefinedSetRequest{DefinedType: gobgpapi.DefinedType_NEIGHBOR, Name: "externalpeerset"},
365+
&gobgpapi.ListDefinedSetRequest{DefinedType: gobgpapi.DefinedType_NEIGHBOR, Name: externalPeerSet},
350366
func(ds *gobgpapi.DefinedSet) {
351367
currentDefinedSet = ds
352368
})
@@ -370,7 +386,7 @@ func (nrc *NetworkRoutingController) addExternalBGPPeersDefinedSet() ([]string,
370386
if currentDefinedSet == nil {
371387
eBGPPeerNS := &gobgpapi.DefinedSet{
372388
DefinedType: gobgpapi.DefinedType_NEIGHBOR,
373-
Name: "externalpeerset",
389+
Name: externalPeerSet,
374390
List: externalBGPPeerCIDRs,
375391
}
376392
err = nrc.bgpServer.AddDefinedSet(context.Background(), &gobgpapi.AddDefinedSetRequest{DefinedSet: eBGPPeerNS})
@@ -384,7 +400,7 @@ func (nrc *NetworkRoutingController) addExternalBGPPeersDefinedSet() ([]string,
384400
func (nrc *NetworkRoutingController) addAllBGPPeersDefinedSet(iBGPPeerCIDRs, externalBGPPeerCIDRs []string) error {
385401
var currentDefinedSet *gobgpapi.DefinedSet
386402
err := nrc.bgpServer.ListDefinedSet(context.Background(),
387-
&gobgpapi.ListDefinedSetRequest{DefinedType: gobgpapi.DefinedType_NEIGHBOR, Name: "allpeerset"},
403+
&gobgpapi.ListDefinedSetRequest{DefinedType: gobgpapi.DefinedType_NEIGHBOR, Name: allPeerSet},
388404
func(ds *gobgpapi.DefinedSet) {
389405
currentDefinedSet = ds
390406
})
@@ -397,7 +413,7 @@ func (nrc *NetworkRoutingController) addAllBGPPeersDefinedSet(iBGPPeerCIDRs, ext
397413
if currentDefinedSet == nil {
398414
allPeerNS := &gobgpapi.DefinedSet{
399415
DefinedType: gobgpapi.DefinedType_NEIGHBOR,
400-
Name: "allpeerset",
416+
Name: allPeerSet,
401417
List: allBgpPeers,
402418
}
403419
return nrc.bgpServer.AddDefinedSet(context.Background(), &gobgpapi.AddDefinedSetRequest{DefinedSet: allPeerNS})
@@ -429,7 +445,7 @@ func (nrc *NetworkRoutingController) addAllBGPPeersDefinedSet(iBGPPeerCIDRs, ext
429445
}
430446
allPeerNS := &gobgpapi.DefinedSet{
431447
DefinedType: gobgpapi.DefinedType_NEIGHBOR,
432-
Name: "allpeerset",
448+
Name: allPeerSet,
433449
List: toAdd,
434450
}
435451
err = nrc.bgpServer.AddDefinedSet(context.Background(), &gobgpapi.AddDefinedSetRequest{DefinedSet: allPeerNS})
@@ -438,7 +454,7 @@ func (nrc *NetworkRoutingController) addAllBGPPeersDefinedSet(iBGPPeerCIDRs, ext
438454
}
439455
allPeerNS = &gobgpapi.DefinedSet{
440456
DefinedType: gobgpapi.DefinedType_NEIGHBOR,
441-
Name: "allpeerset",
457+
Name: allPeerSet,
442458
List: toDelete,
443459
}
444460
err = nrc.bgpServer.DeleteDefinedSet(context.Background(),
@@ -494,11 +510,11 @@ func (nrc *NetworkRoutingController) addExportPolicies() error {
494510
Conditions: &gobgpapi.Conditions{
495511
PrefixSet: &gobgpapi.MatchSet{
496512
Type: gobgpapi.MatchSet_ANY,
497-
Name: "podcidrdefinedset",
513+
Name: podCIDRSet,
498514
},
499515
NeighborSet: &gobgpapi.MatchSet{
500516
Type: gobgpapi.MatchSet_ANY,
501-
Name: "iBGPpeerset",
517+
Name: iBGPPeerSet,
502518
},
503519
},
504520
Actions: &actions,
@@ -526,11 +542,11 @@ func (nrc *NetworkRoutingController) addExportPolicies() error {
526542
Conditions: &gobgpapi.Conditions{
527543
PrefixSet: &gobgpapi.MatchSet{
528544
Type: gobgpapi.MatchSet_ANY,
529-
Name: "servicevipsdefinedset",
545+
Name: serviceVIPsSet,
530546
},
531547
NeighborSet: &gobgpapi.MatchSet{
532548
Type: gobgpapi.MatchSet_ANY,
533-
Name: "externalpeerset",
549+
Name: externalPeerSet,
534550
},
535551
},
536552
Actions: &bgpActions,
@@ -554,11 +570,11 @@ func (nrc *NetworkRoutingController) addExportPolicies() error {
554570
Conditions: &gobgpapi.Conditions{
555571
PrefixSet: &gobgpapi.MatchSet{
556572
Type: gobgpapi.MatchSet_ANY,
557-
Name: "podcidrdefinedset",
573+
Name: podCIDRSet,
558574
},
559575
NeighborSet: &gobgpapi.MatchSet{
560576
Type: gobgpapi.MatchSet_ANY,
561-
Name: "externalpeerset",
577+
Name: externalPeerSet,
562578
},
563579
},
564580
Actions: &actions,
@@ -567,13 +583,13 @@ func (nrc *NetworkRoutingController) addExportPolicies() error {
567583
}
568584

569585
definition := gobgpapi.Policy{
570-
Name: "kube_router_export",
586+
Name: kubeRouterExportPolicy,
571587
Statements: statements,
572588
}
573589

574590
policyAlreadyExists := false
575591
checkExistingPolicy := func(existingPolicy *gobgpapi.Policy) {
576-
if existingPolicy.Name == "kube_router_export" {
592+
if existingPolicy.Name == kubeRouterExportPolicy {
577593
policyAlreadyExists = true
578594
}
579595
}
@@ -592,7 +608,7 @@ func (nrc *NetworkRoutingController) addExportPolicies() error {
592608
policyAssignmentExists := false
593609
checkExistingPolicyAssignment := func(existingPolicyAssignment *gobgpapi.PolicyAssignment) {
594610
for _, policy := range existingPolicyAssignment.Policies {
595-
if policy.Name == "kube_router_export" {
611+
if policy.Name == kubeRouterExportPolicy {
596612
policyAssignmentExists = true
597613
}
598614
}
@@ -634,11 +650,11 @@ func (nrc *NetworkRoutingController) addImportPolicies() error {
634650
Conditions: &gobgpapi.Conditions{
635651
PrefixSet: &gobgpapi.MatchSet{
636652
Type: gobgpapi.MatchSet_ANY,
637-
Name: "servicevipsdefinedset",
653+
Name: serviceVIPsSet,
638654
},
639655
NeighborSet: &gobgpapi.MatchSet{
640656
Type: gobgpapi.MatchSet_ANY,
641-
Name: "allpeerset",
657+
Name: allPeerSet,
642658
},
643659
},
644660
Actions: &actions,
@@ -648,11 +664,11 @@ func (nrc *NetworkRoutingController) addImportPolicies() error {
648664
Conditions: &gobgpapi.Conditions{
649665
PrefixSet: &gobgpapi.MatchSet{
650666
Type: gobgpapi.MatchSet_ANY,
651-
Name: "defaultroutedefinedset",
667+
Name: defaultRouteSet,
652668
},
653669
NeighborSet: &gobgpapi.MatchSet{
654670
Type: gobgpapi.MatchSet_ANY,
655-
Name: "allpeerset",
671+
Name: allPeerSet,
656672
},
657673
},
658674
Actions: &actions,
@@ -662,26 +678,26 @@ func (nrc *NetworkRoutingController) addImportPolicies() error {
662678
statements = append(statements, &gobgpapi.Statement{
663679
Conditions: &gobgpapi.Conditions{
664680
PrefixSet: &gobgpapi.MatchSet{
665-
Name: "customimportrejectdefinedset",
681+
Name: customImportRejectSet,
666682
Type: gobgpapi.MatchSet_ANY,
667683
},
668684
NeighborSet: &gobgpapi.MatchSet{
669685
Type: gobgpapi.MatchSet_ANY,
670-
Name: "allpeerset",
686+
Name: allPeerSet,
671687
},
672688
},
673689
Actions: &actions,
674690
})
675691
}
676692

677693
definition := gobgpapi.Policy{
678-
Name: "kube_router_import",
694+
Name: kubeRouterImportPolicy,
679695
Statements: statements,
680696
}
681697

682698
policyAlreadyExists := false
683699
checkExistingPolicy := func(existingPolicy *gobgpapi.Policy) {
684-
if existingPolicy.Name == "kube_router_import" {
700+
if existingPolicy.Name == kubeRouterImportPolicy {
685701
policyAlreadyExists = true
686702
}
687703
}
@@ -700,7 +716,7 @@ func (nrc *NetworkRoutingController) addImportPolicies() error {
700716
policyAssignmentExists := false
701717
checkExistingPolicyAssignment := func(existingPolicyAssignment *gobgpapi.PolicyAssignment) {
702718
for _, policy := range existingPolicyAssignment.Policies {
703-
if policy.Name == "kube_router_import" {
719+
if policy.Name == kubeRouterImportPolicy {
704720
policyAssignmentExists = true
705721
}
706722
}

0 commit comments

Comments
 (0)