diff --git a/cmd/apply.go b/cmd/apply.go index 389028c..0aaeae0 100644 --- a/cmd/apply.go +++ b/cmd/apply.go @@ -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 } @@ -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) @@ -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 @@ -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 != "" { @@ -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 diff --git a/config/config.go b/config/config.go index b9b742d..6d52a21 100644 --- a/config/config.go +++ b/config/config.go @@ -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) { diff --git a/iproute2/iproute2.go b/iproute2/iproute2.go index 6e7aa9a..b5bfc31 100644 --- a/iproute2/iproute2.go +++ b/iproute2/iproute2.go @@ -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 { @@ -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 {