Skip to content

Commit

Permalink
Merge pull request #148 from telekom/fix/detect-community-drop
Browse files Browse the repository at this point in the history
Detect if community based drop mechanism is used and act accordingly
  • Loading branch information
Cellebyte authored Nov 21, 2024
2 parents f267955 + a2864e1 commit 3d97cd5
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 14 deletions.
14 changes: 8 additions & 6 deletions pkg/frr/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func (m *Manager) Configure(in Configuration, nm *nl.Manager, nwopCfg *config.Co
targetConfig = fixRouteTargetReload(targetConfig)
targetConfig = applyCfgReplacements(targetConfig, nwopCfg.Replacements)

in.HasCommunityDrop = m.hasCommunityDrop

if !bytes.Equal(currentConfig, targetConfig) {
err = os.WriteFile(m.ConfigPath, targetConfig, frrPermissions)
if err != nil {
Expand Down Expand Up @@ -99,27 +101,27 @@ func (m *Manager) renderSubtemplates(in Configuration, nlManager *nl.Manager) (*
return nil, fmt.Errorf("error getting node's name")
}

vrfs, err := render(vrfTpl, in.VRFs)
vrfs, err := render(vrfTpl, in)
if err != nil {
return nil, err
}
neighbors, err := render(neighborTpl, in.VRFs)
neighbors, err := render(neighborTpl, in)
if err != nil {
return nil, err
}
neighborsV4, err := render(neighborV4Tpl, in.VRFs)
neighborsV4, err := render(neighborV4Tpl, in)
if err != nil {
return nil, err
}
neighborsV6, err := render(neighborV6Tpl, in.VRFs)
neighborsV6, err := render(neighborV6Tpl, in)
if err != nil {
return nil, err
}
prefixlists, err := render(prefixListTpl, in.VRFs)
prefixlists, err := render(prefixListTpl, in)
if err != nil {
return nil, err
}
routemaps, err := render(routeMapTpl, in.VRFs)
routemaps, err := render(routeMapTpl, in)
if err != nil {
return nil, err
}
Expand Down
12 changes: 10 additions & 2 deletions pkg/frr/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Manager struct {
ipv4MgmtRouteMapIn *string
ipv6MgmtRouteMapIn *string
mgmtVrf string
hasCommunityDrop bool

ConfigPath string
TemplatePath string
Expand Down Expand Up @@ -61,8 +62,9 @@ type VRFConfiguration struct {
}

type Configuration struct {
ASN int
VRFs []VRFConfiguration
ASN int
VRFs []VRFConfiguration
HasCommunityDrop bool
}

func NewFRRManager() *Manager {
Expand Down Expand Up @@ -105,6 +107,12 @@ func (m *Manager) Init(mgmtVrf string) error {
}
m.ipv6MgmtRouteMapIn = routeMap

communityDrop, err := hasCommunityDrop(m.ConfigPath)
if err != nil {
return fmt.Errorf("error checking for community drop in FRR config: %w", err)
}
m.hasCommunityDrop = communityDrop

return nil
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/frr/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"regexp"
"strings"
"text/template"
)

Expand Down Expand Up @@ -103,6 +104,15 @@ func getRouteMapName(file, addressFamily, mgmtVrfName string) (*string, error) {
return &matches[1], nil
}

func hasCommunityDrop(file string) (bool, error) {
fileContent, err := os.ReadFile(file)
if err != nil {
return false, fmt.Errorf("error reading frr config file %s: %w", file, err)
}
content := string(fileContent)
return strings.Contains(content, "cm-received-fabric"), nil
}

func generateTemplateConfig(tplFile, original string) error {
fileContent, err := os.ReadFile(original)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/frr/tpl/bgp-neighbor-v4.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{range $vrf := .}}
{{range $vrf := .VRFs}}
{{if and $vrf.ShouldTemplateVRF (not $vrf.IsTaaS)}}
neighbor dv.{{$vrf.Name}} activate
neighbor dv.{{$vrf.Name}} allowas-in origin
Expand Down
2 changes: 1 addition & 1 deletion pkg/frr/tpl/bgp-neighbor-v6.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{range $vrf := .}}
{{range $vrf := .VRFs}}
{{if and $vrf.ShouldTemplateVRF (not $vrf.IsTaaS)}}
neighbor dv.{{$vrf.Name}} activate
neighbor dv.{{$vrf.Name}} allowas-in origin
Expand Down
2 changes: 1 addition & 1 deletion pkg/frr/tpl/bgp-neighbor.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{range $vrf := .}}
{{range $vrf := .VRFs}}
{{if and $vrf.ShouldTemplateVRF (not $vrf.IsTaaS)}}
neighbor dv.{{$vrf.Name}} interface remote-as internal
{{end}}
Expand Down
2 changes: 1 addition & 1 deletion pkg/frr/tpl/prefix-list.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{range $vrf := .}}
{{range $vrf := .VRFs}}
{{if not $vrf.IsTaaS}}
{{range $i, $pl := $vrf.Import}}
{{range $item := $pl.Items}}
Expand Down
10 changes: 9 additions & 1 deletion pkg/frr/tpl/route-map.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{range $vrf := .}}
{{range $vrf := .VRFs}}
{{if not $vrf.IsTaaS}}
{{range $i, $pl := $vrf.Import}}
route-map rm_{{$vrf.Name}}_import {{if $vrf.ShouldTemplateVRF}}permit{{else}}deny{{end}} {{$pl.Seq}}
Expand All @@ -10,11 +10,19 @@ exit
{{- end}}

route-map rm_{{$vrf.Name}}_export deny 1
{{if $.HasCommunityDrop}}
match community cm-received-fabric
{{else}}
match tag 20000
{{- end}}
exit

route-map rm6_{{$vrf.Name}}_export deny 1
{{if $.HasCommunityDrop}}
match community cm-received-fabric
{{else}}
match tag 20000
{{- end}}
exit

{{range $i, $pl := $vrf.Export}}
Expand Down
2 changes: 1 addition & 1 deletion pkg/frr/tpl/vrf.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{range $vrf := .}}
{{range $vrf := .VRFs}}
{{if and $vrf.ShouldTemplateVRF (not $vrf.IsTaaS)}}
vrf vr.{{$vrf.Name}}
vni {{$vrf.VNI}}
Expand Down

0 comments on commit 3d97cd5

Please sign in to comment.