Skip to content

Commit

Permalink
Refactor network configuration code for better structure
Browse files Browse the repository at this point in the history
  • Loading branch information
buty4649 committed Mar 20, 2024
1 parent 6d7a5ba commit 5f04db4
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 277 deletions.
102 changes: 68 additions & 34 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var applyCmd = &cobra.Command{
Long: "Apply netns networks configuration to running system",
RunE: func(cmd *cobra.Command, args []string) error {
for netns, values := range cfg.Netns {
if ip.NetnsExists(netns) {
if ip.ExistsNetns(netns) {
slog.Warn("netns is already exists", "name", netns)
} else {
slog.Info("create netns", "name", netns)
Expand Down Expand Up @@ -69,13 +69,25 @@ var applyCmd = &cobra.Command{
},
}

func SetupDevice(name string, addresses []string, routes []config.Route) error {
err := SetLinkUp(name)
type IpCommand interface {
SetLinkUp(name string) error
AddAddress(name, address string) error
AddRoute(name, to, via string) error
InNetns() bool
Netns() string
}

func SetupDevice(ip IpCommand, name string, addresses []string, routes []config.Route) error {
err := SetLinkUp(ip, name)
if err != nil {
return err
}

slog.Info("add addresses", "name", name, "addresses", addresses)
if ip.InNetns() {
slog.Info("add addresses", "name", name, "addresses", addresses, "netns", ip.Netns())
} else {
slog.Info("add addresses", "name", name, "addresses", addresses)
}
for _, address := range addresses {
err := ip.AddAddress(name, address)
if err != nil {
Expand All @@ -84,53 +96,67 @@ func SetupDevice(name string, addresses []string, routes []config.Route) error {
}

for _, route := range routes {
slog.Info("add route", "name", name, "to", route.To, "via", route.Via)
err := ip.AddRoute(route.To, route.Via, name)
if ip.InNetns() {
slog.Info("add route", "name", name, "to", route.To, "via", route.Via, "netns", ip.Netns())
} else {
slog.Info("add route", "name", name, "to", route.To, "via", route.Via)
}
err := ip.AddRoute(name, route.To, route.Via)
if err != nil {
return err
}
}
return nil
}

func SetLinkUp(name string) error {
slog.Info("link up", "name", name, "netns", ip.Netns())
func SetLinkUp(ip IpCommand, name string) error {
if ip.InNetns() {
slog.Info("link up", "name", name, "netns", ip.Netns())
} else {
slog.Info("link up", "name", name)
}

return ip.SetLinkUp(name)
}

func SetupLoopback(netns string) error {
return ip.IntoNetns(netns, func() error {
return SetLinkUp("lo")
})
return SetLinkUp(ip.IntoNetns(netns), "lo")
}

func SetNetns(name string, netns string) error {
slog.Info("set netns", "name", name, "netns", netns)
return ip.SetNetns(name, netns)
}

func SetupEthernets(netns string, ethernets map[string]config.Ethernet) error {
for name, values := range ethernets {
slog.Info("set netns", "name", name, "netns", netns)
err := ip.SetNetns(name, netns)
err := SetNetns(name, netns)
if err != nil {
return err
}

ip.IntoNetns(netns, func() error {
return SetupDevice(name, values.Addresses, values.Routes)
})
err = SetupDevice(ip.IntoNetns(netns), name, values.Addresses, values.Routes)
if err != nil {
return err
}
}
return nil
}

func SetupDummyDevices(netns string, devices map[string]config.Ethernet) error {
for name, values := range devices {
ip.IntoNetns(netns, func() error {
slog.Info("add dummy device", "name", name, "netns", netns)
err := ip.AddDummyDevice(name)
if err != nil {
return err
}
n := ip.IntoNetns(netns)

return SetupDevice(name, values.Addresses, values.Routes)
})
slog.Info("add dummy device", "name", name, "netns", netns)
err := n.AddDummyDevice(name)
if err != nil {
return err
}

err = SetupDevice(n, name, values.Addresses, values.Routes)
if err != nil {
return err
}
}
return nil
}
Expand All @@ -140,30 +166,38 @@ func SetupVethDevices(netns string, devices map[string]config.VethDevice) error
peerName := values.Peer.Name
peerNetns := values.Peer.Netns

slog.Info("add veth device", "name", name, "netns", netns, "peer name", peerName, "peer netns", peerNetns)
slog.Info("add veth device", "name", name, "peer name", peerName)
err := ip.AddVethDevice(name, peerName)
if err != nil {
return err
}

err = ip.SetNetns(name, netns)
err = SetNetns(name, netns)
if err != nil {
return err
}

n := ip.IntoNetns(netns)
err = SetupDevice(n, name, values.Addresses, values.Routes)
if err != nil {
return err
}
ip.IntoNetns(netns, func() error {
return SetupDevice(name, values.Addresses, values.Routes)
})

if peerNetns != "" {
err = ip.SetNetns(peerName, peerNetns)
err = SetNetns(peerName, peerNetns)
if err != nil {
return err
}
n := ip.IntoNetns(peerNetns)
err = SetupDevice(n, peerName, values.Peer.Addresses, values.Peer.Routes)
if err != nil {
return err
}
ip.IntoNetns(netns, func() error {
return SetupDevice(peerName, values.Peer.Addresses, values.Peer.Routes)
})
} else {
SetupDevice(peerName, values.Peer.Addresses, values.Peer.Routes)
err = SetupDevice(ip, peerName, values.Peer.Addresses, values.Peer.Routes)
if err != nil {
return err
}
}
}
return nil
Expand Down
8 changes: 4 additions & 4 deletions cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ import (

// destroyCmd represents the destroy command
var destroyCmd = &cobra.Command{
Use: "destroy",
Short: "Destroy netns networks configuration from running system",
Long: "Destroy netns networks configuration from running system",
Use: "destroy",
Short: "Destroy netns networks configuration from running system",
Long: "Destroy netns networks configuration from running system",
RunE: func(cmd *cobra.Command, args []string) error {
for n := range cfg.Netns {
if ip.NetnsExists(n) {
if ip.ExistsNetns(n) {
slog.Info("delete netns", "name", n)
err := ip.DelNetns(n)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Flags struct {
var flags Flags

var cfg *config.Config
var ip *iproute2.Iproute2
var ip *iproute2.IpCmd

var rootCmd = &cobra.Command{
Use: "netnsplan",
Expand Down
66 changes: 0 additions & 66 deletions iproute2/addresses.go

This file was deleted.

Loading

0 comments on commit 5f04db4

Please sign in to comment.