f-41: vm: /32 de chaque interface annoncé dans la VRF #41
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
fe27d45262
commit
03029eabe8
5 changed files with 125 additions and 0 deletions
|
|
@ -15,6 +15,7 @@ import (
|
|||
"git.g3e.fr/syonad/two/internal/netns"
|
||||
"git.g3e.fr/syonad/two/internal/qemu"
|
||||
"git.g3e.fr/syonad/two/internal/state"
|
||||
"git.g3e.fr/syonad/two/internal/vpc"
|
||||
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
|
@ -111,6 +112,10 @@ func StartVM(db *badger.DB, name string, cfg *configuration.Config) error {
|
|||
return fmt.Errorf("start qemu: %w", err)
|
||||
}
|
||||
|
||||
if err := applyHostRoutes(db, d, vpc.ExportRoute); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return state.Set(db, "vm/"+name, state.Running)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import (
|
|||
"git.g3e.fr/syonad/two/internal/netns"
|
||||
"git.g3e.fr/syonad/two/internal/qmp"
|
||||
"git.g3e.fr/syonad/two/internal/state"
|
||||
"git.g3e.fr/syonad/two/internal/vpc"
|
||||
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
|
@ -38,6 +39,10 @@ func StopVM(db *badger.DB, name string, cfg *configuration.Config) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if err := applyHostRoutes(db, d, vpc.WithdrawRoute); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
socketPath := filepath.Join(cfg.QEMU.QMPDir, name+".sock")
|
||||
|
||||
if _, err := os.Stat(socketPath); err == nil {
|
||||
|
|
|
|||
43
internal/vm/routes.go
Normal file
43
internal/vm/routes.go
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package vm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
||||
type hostRoute struct {
|
||||
vpc string
|
||||
dst *net.IPNet
|
||||
}
|
||||
|
||||
type routeFunc func(db *badger.DB, vpcName string, dst *net.IPNet) error
|
||||
|
||||
func hostRoutes(d vmData) ([]hostRoute, error) {
|
||||
var routes []hostRoute
|
||||
for _, n := range d.nics {
|
||||
if n.mode != "vxlan" {
|
||||
continue
|
||||
}
|
||||
ip := net.ParseIP(n.ip).To4()
|
||||
if ip == nil {
|
||||
return nil, fmt.Errorf("interface %d: %q is not an IPv4 address", n.index, n.ip)
|
||||
}
|
||||
routes = append(routes, hostRoute{vpc: n.vpcName, dst: &net.IPNet{IP: ip, Mask: net.CIDRMask(32, 32)}})
|
||||
}
|
||||
return routes, nil
|
||||
}
|
||||
|
||||
func applyHostRoutes(db *badger.DB, d vmData, apply routeFunc) error {
|
||||
routes, err := hostRoutes(d)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, r := range routes {
|
||||
if err := apply(db, r.vpc, r.dst); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
66
internal/vm/routes_test.go
Normal file
66
internal/vm/routes_test.go
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
package vm
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
||||
func routedVM() vmData {
|
||||
return vmData{nics: []nicData{
|
||||
{index: 0, vpcName: "vp-s7", mode: "vxlan", ip: "10.241.1.10"},
|
||||
{index: 1, vpcName: "vp-s7", mode: "bridge", ip: "192.168.50.10"},
|
||||
{index: 2, vpcName: "vp-s7", mode: "vxlan", ip: "10.241.2.10"},
|
||||
}}
|
||||
}
|
||||
|
||||
type recorder struct {
|
||||
calls []string
|
||||
fail string
|
||||
}
|
||||
|
||||
func (r *recorder) apply(_ *badger.DB, vpcName string, dst *net.IPNet) error {
|
||||
r.calls = append(r.calls, vpcName+" "+dst.String())
|
||||
if dst.String() == r.fail {
|
||||
return errors.New("refused")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestApplyHostRoutesCoversEveryVxlanInterfaceAsA32(t *testing.T) {
|
||||
r := &recorder{}
|
||||
if err := applyHostRoutes(nil, routedVM(), r.apply); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []string{"vp-s7 10.241.1.10/32", "vp-s7 10.241.2.10/32"}
|
||||
if !reflect.DeepEqual(r.calls, want) {
|
||||
t.Errorf("appels %q, attendu %q", r.calls, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyHostRoutesStopsAtTheFirstError(t *testing.T) {
|
||||
r := &recorder{fail: "10.241.1.10/32"}
|
||||
if err := applyHostRoutes(nil, routedVM(), r.apply); err == nil || err.Error() != "refused" {
|
||||
t.Errorf("erreur %v", err)
|
||||
}
|
||||
if len(r.calls) != 1 {
|
||||
t.Errorf("appels après l'échec : %q", r.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyHostRoutesRefusesANonIPv4AddressBeforeAnyCall(t *testing.T) {
|
||||
d := routedVM()
|
||||
d.nics[2].ip = "fd00::10"
|
||||
r := &recorder{}
|
||||
err := applyHostRoutes(nil, d, r.apply)
|
||||
if err == nil || !strings.Contains(err.Error(), `interface 2: "fd00::10" is not an IPv4 address`) {
|
||||
t.Errorf("erreur %v", err)
|
||||
}
|
||||
if len(r.calls) != 0 {
|
||||
t.Errorf("appels : %q", r.calls)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue