Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permanently save the FRR configuration on each FRR command run #402

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion pkg/frr/frr.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,10 @@ func setUpVrf(vrf *infradb.Vrf) (string, bool) {
if err != nil {
return "", false
}
err = Frr.Save(ctx)
if err != nil {
log.Printf("FRR(setUpVrf): Failed to run save command: %v\n", err)
}
log.Printf("FRR: Executed frr config t %s %s exit-vrf exit\n", vrfName, vniID)
var LbiP string

Expand All @@ -383,14 +387,21 @@ func setUpVrf(vrf *infradb.Vrf) (string, bool) {
if err != nil {
return "", false
}

err = Frr.Save(ctx)
if err != nil {
log.Printf("FRR(setUpVrf): Failed to run save command: %v\n", err)
}
log.Printf("FRR: Executed config t bgpVrfName router bgp %+v vrf %s bgp_route_id %s no bgp ebgp-requires-policy exit-vrf exit\n", localas, vrf.Name, LbiP)
// Update the vrf with attributes from FRR
cmd := fmt.Sprintf("show bgp l2vpn evpn vni %d json", *vrf.Spec.Vni)
cp, err := Frr.FrrBgpCmd(ctx, cmd)
if err != nil {
log.Printf("error-%v", err)
}
err = Frr.Save(ctx)
if err != nil {
log.Printf("FRR(setUpVrf): Failed to run save command: %v\n", err)
}
hname, _ := os.Hostname()
L2vpnCmd := strings.Split(cp, "json")
L2vpnCmd = strings.Split(L2vpnCmd[1], hname)
Expand All @@ -412,6 +423,10 @@ func setUpVrf(vrf *infradb.Vrf) (string, bool) {
if err != nil {
log.Printf("error-%v", err)
}
err = Frr.Save(ctx)
if err != nil {
log.Printf("FRR(setUpVrf): Failed to run save command: %v\n", err)
}
BgpCmd := strings.Split(cp, "json")
BgpCmd = strings.Split(BgpCmd[1], hname)
cp = BgpCmd[0]
Expand Down Expand Up @@ -466,6 +481,10 @@ func setUpSvi(svi *infradb.Svi) bool {
log.Printf("FRR: Error in conf svi %s %s command %s\n", svi.Name, path.Base(svi.Spec.Vrf), data)
return false
}
err = Frr.Save(ctx)
if err != nil {
log.Printf("FRR(setUpSvi): Failed to run save command: %v\n", err)
}
return true
}
return true
Expand All @@ -488,6 +507,10 @@ func tearDownSvi(svi *infradb.Svi) bool {
log.Printf("FRR: Error in conf Delete vrf/VNI command %s\n", data)
return false
}
err = Frr.Save(ctx)
if err != nil {
log.Printf("FRR(tearDownSvi): Failed to run save command: %v\n", err)
}
log.Printf("FRR: Executed vtysh -c conf t -c router bgp %+v vrf %s -c no neighbor %s peer-group -c exit\n", localas, path.Base(svi.Spec.Vrf), linkSvi)
return true
}
Expand All @@ -509,6 +532,10 @@ func tearDownVrf(vrf *infradb.Vrf) bool {
log.Printf("CP FRR %s\n", data)
return true
}
err = Frr.Save(ctx)
if err != nil {
log.Printf("FRR(tearDownVrf): Failed to run save command: %v\n", err)
}
// Clean up FRR last
if !reflect.ValueOf(vrf.Spec.Vni).IsZero() {
log.Printf("FRR Deleted event")
Expand All @@ -518,10 +545,18 @@ func tearDownVrf(vrf *infradb.Vrf) bool {
if err != nil {
return false
}
err = Frr.Save(ctx)
if err != nil {
log.Printf("FRR(tearDownVrf): Failed to run save command: %v\n", err)
}
_, err = Frr.FrrZebraCmd(ctx, fmt.Sprintf("configure terminal\n %s\n exit\n", delCmd2))
if err != nil {
return false
}
err = Frr.Save(ctx)
if err != nil {
log.Printf("FRR(tearDownVrf): Failed to run save command: %v\n", err)
}
log.Printf("FRR: Executed vtysh -c conf t -c %s -c %s -c exit\n", delCmd1, delCmd2)
}
return true
Expand Down
12 changes: 12 additions & 0 deletions pkg/utils/frr.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bufio"
"context"
"fmt"
"os/exec"
"strings"
"time"

Expand Down Expand Up @@ -52,6 +53,7 @@ type Frr interface {
TelnetDialAndCommunicate(ctx context.Context, command string, port int) (string, error)
FrrZebraCmd(ctx context.Context, command string) (string, error)
FrrBgpCmd(ctx context.Context, command string) (string, error)
Save(context.Context) error
Password(conn *telnet.Conn, delim string) error
EnterPrivileged(conn *telnet.Conn) error
ExitPrivileged(conn *telnet.Conn) error
Expand Down Expand Up @@ -125,6 +127,16 @@ func (n *FrrWrapper) FrrBgpCmd(ctx context.Context, command string) (string, err
return n.TelnetDialAndCommunicate(ctx, command, bgpd)
}

// Save command save the current config to /etc/frr/frr.conf
func (n *FrrWrapper) Save(ctx context.Context) error {
cmd := exec.CommandContext(ctx, "vtysh", "-c", "write")
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to save FRR config: %v, output: %s", err, output)
}
return nil
}

// MultiLineCmd breaks command by lines, sends each and waits for output and returns combined output
func (n *FrrWrapper) MultiLineCmd(conn *telnet.Conn, command string) (string, error) {
// multi-line command
Expand Down
Loading