Skip to content

Commit

Permalink
Merge pull request #10 from buty4649/routes
Browse files Browse the repository at this point in the history
Add route support to device setup functions
  • Loading branch information
buty4649 authored Mar 20, 2024
2 parents 0e21a3c + 1cd723b commit 6d7a5ba
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
28 changes: 18 additions & 10 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,27 @@ var applyCmd = &cobra.Command{
},
}

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

slog.Info("add addresses", "name", name, "addresses", addresses)

for _, address := range addresses {
err := ip.AddAddress(name, address)
if err != nil {
return err
}
}

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 err != nil {
return err
}
}
return nil
}

Expand All @@ -98,21 +105,22 @@ func SetupLoopback(netns string) error {
})
}

func SetupEthernets(netns string, ethernets map[string]config.EthernetConfig) error {
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)
if err != nil {
return err
}

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

func SetupDummyDevices(netns string, devices map[string]config.EthernetConfig) error {
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)
Expand All @@ -121,13 +129,13 @@ func SetupDummyDevices(netns string, devices map[string]config.EthernetConfig) e
return err
}

return SetupDevice(name, values.Addresses)
return SetupDevice(name, values.Addresses, values.Routes)
})
}
return nil
}

func SetupVethDevices(netns string, devices map[string]config.VethDeviceConfig) error {
func SetupVethDevices(netns string, devices map[string]config.VethDevice) error {
for name, values := range devices {
peerName := values.Peer.Name
peerNetns := values.Peer.Netns
Expand All @@ -143,7 +151,7 @@ func SetupVethDevices(netns string, devices map[string]config.VethDeviceConfig)
return err
}
ip.IntoNetns(netns, func() error {
return SetupDevice(name, values.Addresses)
return SetupDevice(name, values.Addresses, values.Routes)
})

if peerNetns != "" {
Expand All @@ -152,10 +160,10 @@ func SetupVethDevices(netns string, devices map[string]config.VethDeviceConfig)
return err
}
ip.IntoNetns(netns, func() error {
return SetupDevice(peerName, values.Peer.Addresses)
return SetupDevice(peerName, values.Peer.Addresses, values.Peer.Routes)
})
} else {
SetupDevice(peerName, values.Peer.Addresses)
SetupDevice(peerName, values.Peer.Addresses, values.Peer.Routes)
}
}
return nil
Expand Down
28 changes: 18 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,36 @@ import (
)

type Config struct {
Netns map[string]NetnsConfig `yaml:"netns"`
Netns map[string]Netns `yaml:"netns"`
}

type NetnsConfig struct {
Ethernets map[string]EthernetConfig `yaml:"ethernets,omitempty"`
DummyDevices map[string]EthernetConfig `yaml:"dummy-devices,omitempty"`
VethDevices map[string]VethDeviceConfig `yaml:"veth-devices,omitempty"`
type Netns struct {
Ethernets map[string]Ethernet `yaml:"ethernets,omitempty"`
DummyDevices map[string]Ethernet `yaml:"dummy-devices,omitempty"`
VethDevices map[string]VethDevice `yaml:"veth-devices,omitempty"`
}

type EthernetConfig struct {
type Ethernet struct {
Addresses []string `yaml:"addresses"`
Routes []Route `yaml:"routes,omitempty"`
}

type VethDeviceConfig struct {
Addresses []string `yaml:"addresses"`
Peer PeerConfig `yaml:"peer"`
type VethDevice struct {
Addresses []string `yaml:"addresses"`
Routes []Route `yaml:"routes,omitempty"`
Peer Peer `yaml:"peer"`
}

type PeerConfig struct {
type Peer struct {
Name string `yaml:"name"`
Netns string `yaml:"netns,omitempty"`
Addresses []string `yaml:"addresses"`
Routes []Route `yaml:"routes,omitempty"`
}

type Route struct {
To string `yaml:"to"`
Via string `yaml:"via"`
}

func LoadConfig(path string) (*Config, error) {
Expand Down
12 changes: 6 additions & 6 deletions iproute2/iproute2.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func (i *Iproute2) AddDummyDevice(name string) error {
return i.AddLink(name, "dummy")
}

func (i *Iproute2) AddVethDevice(name1 string, name2 string) error {
return i.AddLink(name1, "veth", "peer", "name", name2)
func (i *Iproute2) AddVethDevice(name string, peerName string) error {
return i.AddLink(name, "veth", "peer", "name", peerName)
}

func (i *Iproute2) SetLinkUp(name string) error {
Expand All @@ -87,12 +87,12 @@ func (i *Iproute2) DelAddress(name string, address string) error {
return i.execute("address", "del", address, "dev", name)
}

func (i *Iproute2) AddRoute(dst string, via string, dev string) error {
return i.execute("route", "add", dst, "via", via, "dev", dev)
func (i *Iproute2) AddRoute(name string, to string, via string) error {
return i.execute("route", "add", to, "via", via, "dev", name)
}

func (i *Iproute2) DelRoute(dst string, via string, dev string) error {
return i.execute("route", "del", dst, "via", via, "dev", dev)
func (i *Iproute2) DelRoute(name string, to string, via string) error {
return i.execute("route", "del", to, "via", via, "dev", name)
}

func (i *Iproute2) AddNetns(name string) error {
Expand Down

0 comments on commit 6d7a5ba

Please sign in to comment.