Skip to content

Commit

Permalink
Merge pull request #135 from telekom-mms/feature/move-splitrt-cleanup…
Browse files Browse the repository at this point in the history
…-to-vpnsetup

Move cleanup commands of Split Routing to VPNSetup
  • Loading branch information
hwipl authored Jan 22, 2025
2 parents 0949054 + f7d2438 commit c108664
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 67 deletions.
25 changes: 10 additions & 15 deletions internal/cmdtmpl/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,21 +217,6 @@ add element inet oc-daemon-routing excludes4 { {{.}} }
},
defaultTemplate: SplitRoutingDefaultTemplate,
}
case "SplitRoutingCleanup":
// Cleanup
cl = &CommandList{
Name: name,
Commands: []*Command{
{Line: "{{.Executables.IP}} -4 rule delete pref {{.SplitRouting.RulePriority1}}"},
{Line: "{{.Executables.IP}} -4 rule delete pref {{.SplitRouting.RulePriority2}}"},
{Line: "{{.Executables.IP}} -6 rule delete pref {{.SplitRouting.RulePriority1}}"},
{Line: "{{.Executables.IP}} -6 rule delete pref {{.SplitRouting.RulePriority2}}"},
{Line: "{{.Executables.IP}} -4 route flush table {{.SplitRouting.RoutingTable}}"},
{Line: "{{.Executables.IP}} -6 route flush table {{.SplitRouting.RoutingTable}}"},
{Line: "{{.Executables.Nft}} -f - delete table inet oc-daemon-routing"},
},
defaultTemplate: SplitRoutingDefaultTemplate,
}
default:
return nil

Expand Down Expand Up @@ -579,8 +564,18 @@ func getCommandListVPNSetup(name string) *CommandList {
cl = &CommandList{
Name: name,
Commands: []*Command{
// DNS cleanup
{Line: "{{.Executables.Resolvectl}} revert {{.OpenConnect.VPNDevice}}"},
// Device cleanup
{Line: "{{.Executables.IP}} link delete {{.OpenConnect.VPNDevice}}"},
// Routing cleanup
{Line: "{{.Executables.IP}} -4 rule delete pref {{.SplitRouting.RulePriority1}}"},
{Line: "{{.Executables.IP}} -4 rule delete pref {{.SplitRouting.RulePriority2}}"},
{Line: "{{.Executables.IP}} -6 rule delete pref {{.SplitRouting.RulePriority1}}"},
{Line: "{{.Executables.IP}} -6 rule delete pref {{.SplitRouting.RulePriority2}}"},
{Line: "{{.Executables.IP}} -4 route flush table {{.SplitRouting.RoutingTable}}"},
{Line: "{{.Executables.IP}} -6 route flush table {{.SplitRouting.RoutingTable}}"},
{Line: "{{.Executables.Nft}} -f - delete table inet oc-daemon-routing"},
},
defaultTemplate: "",
}
Expand Down
2 changes: 0 additions & 2 deletions internal/cmdtmpl/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func TestGetCommandList(t *testing.T) {
"SplitRoutingSetupRouting",
"SplitRoutingTeardownRouting",
"SplitRoutingSetExcludes",
"SplitRoutingCleanup",

// Traffic Policing
"TrafPolSetFilterRules",
Expand Down Expand Up @@ -98,7 +97,6 @@ func TestGetCmds(t *testing.T) {
"SplitRoutingSetupRouting",
"SplitRoutingTeardownRouting",
// "SplitRoutingSetExcludes", // skip, requires excludes
"SplitRoutingCleanup",

// Traffic Policing
"TrafPolSetFilterRules",
Expand Down
17 changes: 0 additions & 17 deletions internal/splitrt/splitrt.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,20 +345,3 @@ func NewSplitRouting(config *daemoncfg.Config) *SplitRouting {
closed: make(chan struct{}),
}
}

// Cleanup cleans up old configuration after a failed shutdown.
func Cleanup(ctx context.Context, config *daemoncfg.Config) {
cmds, err := cmdtmpl.GetCmds("SplitRoutingCleanup", config)
if err != nil {
log.WithError(err).Error("SplitRouting could not get cleanup commands")
}
for _, c := range cmds {
if _, _, err := c.Run(ctx); err == nil {
log.WithFields(log.Fields{
"command": c.Cmd,
"args": c.Args,
"stdin": c.Stdin,
}).Debug("SplitRouting cleaned up configuration")
}
}
}
31 changes: 0 additions & 31 deletions internal/splitrt/splitrt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"net/netip"
"reflect"
"strings"
"testing"

"github.com/telekom-mms/oc-daemon/internal/addrmon"
Expand Down Expand Up @@ -350,33 +349,3 @@ func TestNewSplitRouting(t *testing.T) {
t.Errorf("got nil, want != nil")
}
}

// TestCleanup tests Cleanup.
func TestCleanup(t *testing.T) {
got := []string{}

oldRunCmd := execs.RunCmd
execs.RunCmd = func(_ context.Context, cmd string, s string, arg ...string) ([]byte, []byte, error) {
if s == "" {
got = append(got, cmd+" "+strings.Join(arg, " "))
return nil, nil, nil
}
got = append(got, cmd+" "+strings.Join(arg, " ")+" "+s)
return nil, nil, nil
}
defer func() { execs.RunCmd = oldRunCmd }()

Cleanup(context.Background(), daemoncfg.NewConfig())
want := []string{
"ip -4 rule delete pref 2111",
"ip -4 rule delete pref 2112",
"ip -6 rule delete pref 2111",
"ip -6 rule delete pref 2112",
"ip -4 route flush table 42111",
"ip -6 route flush table 42111",
"nft -f - delete table inet oc-daemon-routing",
}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
}
3 changes: 1 addition & 2 deletions internal/vpnsetup/vpnsetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,7 @@ func Cleanup(ctx context.Context, config *daemoncfg.Config) {
"command": c.Cmd,
"args": c.Args,
"stdin": c.Stdin,
}).Warn("VPNSetup cleaned up configuration")
}).Debug("VPNSetup cleaned up configuration")
}
}
splitrt.Cleanup(ctx, config)
}

0 comments on commit c108664

Please sign in to comment.