f-57: api: gateway et adresse d'émission calculées en vxlan, adresses réservées refusées pour les VM #57

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-13 14:23:52 +02:00
commit 5d314e45e1
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
14 changed files with 591 additions and 46 deletions

View file

@ -38,6 +38,20 @@ func (c CreateSubnetCommand) Prepare(db *badger.DB, cfg *configuration.Config) e
if _, err := kv.GetFromDB(db, "subnet/"+c.Name+"/state"); err == nil {
return fmt.Errorf("subnet %q already exists", c.Name)
}
relayIP := ""
if c.Mode == subnet.ModeVxlan {
if c.InterfaceIP != "" {
return fmt.Errorf("interface_ip is computed for vxlan subnets, do not set it")
}
addrs, err := subnet.AnycastAddresses(c.CIDR)
if err != nil {
return err
}
c.InterfaceIP = addrs.Gateway.String()
relayIP = addrs.Relay.String()
} else if c.InterfaceIP == "" {
return fmt.Errorf("interface_ip is required for %s subnets", c.Mode)
}
vpcState, err := state.Get(db, "vpc/"+c.VPC)
if err != nil {
return fmt.Errorf("vpc %q not found", c.VPC)
@ -58,6 +72,7 @@ func (c CreateSubnetCommand) Prepare(db *badger.DB, cfg *configuration.Config) e
kv.AddInDB(db, "subnet/"+c.Name+"/default_route", strconv.FormatBool(c.DefaultRoute))
if c.Mode == subnet.ModeVxlan {
kv.AddInDB(db, "subnet/"+c.Name+"/vxlan_id", strconv.Itoa(c.VxlanID))
kv.AddInDB(db, "subnet/"+c.Name+"/relay_ip", relayIP)
}
if c.Gateway != "" {
if err := kv.AddInDB(db, "subnet/"+c.Name+"/gateway", c.Gateway); err != nil {

View file

@ -22,7 +22,7 @@ func TestCreateSubnetCommand_Prepare_Success(t *testing.T) {
kv.AddInDB(db, "vpc/vpc-1/state", "running")
cmd := CreateSubnetCommand{
Name: "sn-1", VPC: "vpc-1", VxlanID: 100,
IfaceType: "vms", InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
IfaceType: "vms", CIDR: "10.0.0.0/24",
}
if err := cmd.Prepare(db, testCfg()); err != nil {
t.Fatalf("Prepare a échoué : %v", err)
@ -35,6 +35,10 @@ func TestCreateSubnetCommand_Prepare_Success(t *testing.T) {
if vpc != "vpc-1" {
t.Errorf("vpc attendu vpc-1, obtenu %q", vpc)
}
ip, _ := kv.GetFromDB(db, "subnet/sn-1/interface_ip")
if ip != "10.0.0.254" {
t.Errorf("interface_ip attendue 10.0.0.254, obtenu %q", ip)
}
}
func TestCreateSubnetCommand_Prepare_UsesIfaceTypeMapping(t *testing.T) {
@ -42,7 +46,7 @@ func TestCreateSubnetCommand_Prepare_UsesIfaceTypeMapping(t *testing.T) {
kv.AddInDB(db, "vpc/vpc-1/state", "running")
cmd := CreateSubnetCommand{
Name: "sn-1", VPC: "vpc-1", VxlanID: 100,
IfaceType: "vms", InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
IfaceType: "vms", CIDR: "10.0.0.0/24",
}
cmd.Prepare(db, testCfg())
iface, _ := kv.GetFromDB(db, "subnet/sn-1/local_iface")
@ -56,7 +60,7 @@ func TestCreateSubnetCommand_Prepare_UsesDefaultIfaceWhenTypeUnknown(t *testing.
kv.AddInDB(db, "vpc/vpc-1/state", "running")
cmd := CreateSubnetCommand{
Name: "sn-1", VPC: "vpc-1", VxlanID: 100,
IfaceType: "inconnu", InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
IfaceType: "inconnu", CIDR: "10.0.0.0/24",
}
cmd.Prepare(db, testCfg())
iface, _ := kv.GetFromDB(db, "subnet/sn-1/local_iface")
@ -71,7 +75,7 @@ func TestCreateSubnetCommand_Prepare_Duplicate(t *testing.T) {
kv.AddInDB(db, "subnet/sn-exist/state", "running")
cmd := CreateSubnetCommand{
Name: "sn-exist", VPC: "vpc-1", VxlanID: 100,
IfaceType: "vms", InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
IfaceType: "vms", CIDR: "10.0.0.0/24",
}
if err := cmd.Prepare(db, testCfg()); err == nil {
t.Error("Prepare devrait échouer sur un subnet déjà existant")
@ -82,7 +86,7 @@ func TestCreateSubnetCommand_Prepare_VPCNotFound(t *testing.T) {
_, db := newTestDispatcher(t)
cmd := CreateSubnetCommand{
Name: "sn-1", VPC: "vpc-inexistant", VxlanID: 100,
IfaceType: "vms", InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
IfaceType: "vms", CIDR: "10.0.0.0/24",
}
if err := cmd.Prepare(db, testCfg()); err == nil {
t.Error("Prepare devrait échouer si le VPC n'existe pas")
@ -94,7 +98,7 @@ func TestCreateSubnetCommand_Prepare_VPCDeleting(t *testing.T) {
kv.AddInDB(db, "vpc/vpc-dying/state", "deleting")
cmd := CreateSubnetCommand{
Name: "sn-1", VPC: "vpc-dying", VxlanID: 100,
IfaceType: "vms", InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
IfaceType: "vms", CIDR: "10.0.0.0/24",
}
if err := cmd.Prepare(db, testCfg()); err == nil {
t.Error("Prepare devrait échouer si le VPC est en cours de suppression")
@ -106,7 +110,7 @@ func TestCreateSubnetCommand_Prepare_VPCDeleted(t *testing.T) {
kv.AddInDB(db, "vpc/vpc-gone/state", "deleted")
cmd := CreateSubnetCommand{
Name: "sn-1", VPC: "vpc-gone", VxlanID: 100,
IfaceType: "vms", InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
IfaceType: "vms", CIDR: "10.0.0.0/24",
}
if err := cmd.Prepare(db, testCfg()); err == nil {
t.Error("Prepare devrait échouer si le VPC est supprimé")
@ -118,7 +122,7 @@ func TestCreateSubnetCommand_Prepare_DefaultsToVxlanMode(t *testing.T) {
kv.AddInDB(db, "vpc/vpc-1/state", "running")
cmd := CreateSubnetCommand{
Name: "sn-1", VPC: "vpc-1", VxlanID: 100,
IfaceType: "vms", InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
IfaceType: "vms", CIDR: "10.0.0.0/24",
}
cmd.Prepare(db, testCfg())
mode, _ := kv.GetFromDB(db, "subnet/sn-1/mode")
@ -180,7 +184,7 @@ func TestCreateSubnetCommand_Prepare_DefaultRouteStored(t *testing.T) {
kv.AddInDB(db, "vpc/vpc-1/state", "running")
cmd := CreateSubnetCommand{
Name: "sn-1", VPC: "vpc-1", VxlanID: 100,
IfaceType: "vms", InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
IfaceType: "vms", CIDR: "10.0.0.0/24",
DefaultRoute: true,
}
if err := cmd.Prepare(db, testCfg()); err != nil {
@ -200,7 +204,7 @@ func TestCreateSubnetCommand_Prepare_DefaultRouteFalseByDefault(t *testing.T) {
kv.AddInDB(db, "vpc/vpc-1/state", "running")
cmd := CreateSubnetCommand{
Name: "sn-1", VPC: "vpc-1", VxlanID: 100,
IfaceType: "vms", InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
IfaceType: "vms", CIDR: "10.0.0.0/24",
}
cmd.Prepare(db, testCfg())
val, _ := kv.GetFromDB(db, "subnet/sn-1/default_route")
@ -209,6 +213,76 @@ func TestCreateSubnetCommand_Prepare_DefaultRouteFalseByDefault(t *testing.T) {
}
}
func TestCreateSubnetCommand_Prepare_VxlanComputesAnycastAddresses(t *testing.T) {
db, err := prepareSubnet(t, CreateSubnetCommand{
Name: "sn-any", VxlanID: 100, IfaceType: "vms", CIDR: "10.1.0.0/16",
})
if err != nil {
t.Fatalf("Prepare a échoué : %v", err)
}
if ip, _ := kv.GetFromDB(db, "subnet/sn-any/interface_ip"); ip != "10.1.255.254" {
t.Errorf("interface_ip attendue 10.1.255.254, obtenu %q", ip)
}
if relay, _ := kv.GetFromDB(db, "subnet/sn-any/relay_ip"); relay != "10.1.255.253" {
t.Errorf("relay_ip attendu 10.1.255.253, obtenu %q", relay)
}
}
func TestCreateSubnetCommand_Prepare_VxlanRefusesInterfaceIP(t *testing.T) {
for _, mode := range []string{"", "vxlan"} {
db, err := prepareSubnet(t, CreateSubnetCommand{
Name: "sn-ip", Mode: mode, VxlanID: 100, IfaceType: "vms",
InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
})
if err == nil || !strings.Contains(err.Error(), "interface_ip is computed") {
t.Errorf("mode %q : une interface_ip fournie doit être refusée, obtenu %v", mode, err)
}
if _, err := kv.GetFromDB(db, "subnet/sn-ip/state"); err == nil {
t.Errorf("mode %q : rien ne doit être écrit après un refus", mode)
}
}
}
func TestCreateSubnetCommand_Prepare_VxlanRefusesSlash30(t *testing.T) {
db, err := prepareSubnet(t, CreateSubnetCommand{
Name: "sn-small", VxlanID: 100, IfaceType: "vms", CIDR: "10.0.0.0/30",
})
if err == nil {
t.Fatal("un /30 doit être refusé en mode vxlan")
}
if _, err := kv.GetFromDB(db, "subnet/sn-small/state"); err == nil {
t.Error("rien ne doit être écrit après un refus")
}
}
func TestCreateSubnetCommand_Prepare_BridgeRequiresInterfaceIP(t *testing.T) {
db, err := prepareSubnet(t, CreateSubnetCommand{
Name: "sn-br", Mode: "bridge", IfaceType: "vms", CIDR: "10.0.0.0/24",
})
if err == nil || !strings.Contains(err.Error(), "interface_ip is required") {
t.Errorf("une interface_ip absente doit être refusée en mode bridge, obtenu %v", err)
}
if _, err := kv.GetFromDB(db, "subnet/sn-br/state"); err == nil {
t.Error("rien ne doit être écrit après un refus")
}
}
func TestCreateSubnetCommand_Prepare_BridgeStoresGivenInterfaceIPWithoutRelay(t *testing.T) {
db, err := prepareSubnet(t, CreateSubnetCommand{
Name: "sn-br", Mode: "bridge", IfaceType: "vms",
InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
})
if err != nil {
t.Fatalf("Prepare a échoué : %v", err)
}
if ip, _ := kv.GetFromDB(db, "subnet/sn-br/interface_ip"); ip != "10.0.0.1" {
t.Errorf("interface_ip attendue 10.0.0.1, obtenu %q", ip)
}
if _, err := kv.GetFromDB(db, "subnet/sn-br/relay_ip"); err == nil {
t.Error("relay_ip ne doit être écrit qu'en mode vxlan")
}
}
// --- DeleteSubnetCommand.Prepare ---
func TestDeleteSubnetCommand_Prepare_Success(t *testing.T) {
@ -273,7 +347,7 @@ func prepareSubnet(t *testing.T, cmd CreateSubnetCommand) (*badger.DB, error) {
func TestCreateSubnetCommand_Prepare_StoresGateway(t *testing.T) {
db, err := prepareSubnet(t, CreateSubnetCommand{
Name: "sn-gw", VxlanID: 100, IfaceType: "vms",
InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
CIDR: "10.0.0.0/24",
DefaultRoute: true, Gateway: "10.0.0.254",
})
if err != nil {
@ -291,7 +365,7 @@ func TestCreateSubnetCommand_Prepare_StoresGateway(t *testing.T) {
func TestCreateSubnetCommand_Prepare_NoGatewayWritesNoKey(t *testing.T) {
db, err := prepareSubnet(t, CreateSubnetCommand{
Name: "sn-nogw", VxlanID: 100, IfaceType: "vms",
InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
CIDR: "10.0.0.0/24",
})
if err != nil {
t.Fatalf("Prepare a échoué : %v", err)

View file

@ -9,6 +9,7 @@ import (
configuration "git.g3e.fr/syonad/two/internal/config/agent"
"git.g3e.fr/syonad/two/internal/state"
"git.g3e.fr/syonad/two/internal/subnet"
"git.g3e.fr/syonad/two/internal/vm"
"git.g3e.fr/syonad/two/pkg/db/kv"
"github.com/dgraph-io/badger/v4"
@ -106,6 +107,16 @@ func (c StartVMCommand) validateNICs(db *badger.DB) error {
if subnetState != state.Creating && subnetState != state.Running {
return fmt.Errorf("subnet %q is %s", n.Subnet, subnetState)
}
cidr, err := kv.GetFromDB(db, "subnet/"+n.Subnet+"/cidr")
if err != nil {
return fmt.Errorf("subnet %q has no cidr", n.Subnet)
}
interfaceIP, _ := kv.GetFromDB(db, "subnet/"+n.Subnet+"/interface_ip")
relayIP, _ := kv.GetFromDB(db, "subnet/"+n.Subnet+"/relay_ip")
gateway, _ := kv.GetFromDB(db, "subnet/"+n.Subnet+"/gateway")
if err := subnet.CheckHostAddress(cidr, n.IP, interfaceIP, relayIP, gateway); err != nil {
return fmt.Errorf("vm %q on subnet %q: %w", c.Name, n.Subnet, err)
}
}
if primaries != 1 {
return fmt.Errorf("vm %q has %d primary interfaces, expected exactly one", c.Name, primaries)

View file

@ -13,6 +13,7 @@ func TestStartVMCommand_Prepare_SingleDisk(t *testing.T) {
_, db := newTestDispatcher(t)
kv.AddInDB(db, "subnet/sn-1/state", "running")
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1")
kv.AddInDB(db, "subnet/sn-1/cidr", "10.0.0.0/24")
cmd := StartVMCommand{
Name: "vm-1",
@ -36,6 +37,7 @@ func TestStartVMCommand_Prepare_MultiDisk(t *testing.T) {
_, db := newTestDispatcher(t)
kv.AddInDB(db, "subnet/sn-1/state", "running")
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1")
kv.AddInDB(db, "subnet/sn-1/cidr", "10.0.0.0/24")
cmd := StartVMCommand{
Name: "vm-2",
@ -67,6 +69,7 @@ func TestStartVMCommand_Prepare_SlotGap(t *testing.T) {
_, db := newTestDispatcher(t)
kv.AddInDB(db, "subnet/sn-1/state", "running")
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1")
kv.AddInDB(db, "subnet/sn-1/cidr", "10.0.0.0/24")
// sdb absent au boot — slot réservé pour hotplug
cmd := StartVMCommand{
@ -96,6 +99,7 @@ func TestStartVMCommand_Prepare_NoVolumePath(t *testing.T) {
_, db := newTestDispatcher(t)
kv.AddInDB(db, "subnet/sn-1/state", "running")
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1")
kv.AddInDB(db, "subnet/sn-1/cidr", "10.0.0.0/24")
cmd := StartVMCommand{
Name: "vm-4",
@ -177,6 +181,7 @@ func prepareWithDocuments(t *testing.T, docs map[string]string) *badger.DB {
_, db := newTestDispatcher(t)
kv.AddInDB(db, "subnet/sn-1/state", "running")
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1")
kv.AddInDB(db, "subnet/sn-1/cidr", "10.0.0.0/24")
cmd := StartVMCommand{
Name: "vm-doc",