f-34: route: add default gateway full gestion

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-08-25 20:18:09 +02:00
commit 46596f8142
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
13 changed files with 396 additions and 51 deletions

View file

@ -21,15 +21,16 @@ type CreateSubnetCommand struct {
InterfaceIP string
CIDR string
DefaultRoute bool
Gateway string
}
func (c CreateSubnetCommand) Key() string { return "subnet/" + c.Name }
func (c CreateSubnetCommand) Prepare(db *badger.DB, cfg *configuration.Config) error {
if c.Mode == "" {
c.Mode = "vxlan"
c.Mode = subnet.ModeVxlan
}
if c.Mode != "vxlan" && c.Mode != "bridge" {
if !subnet.ValidMode(c.Mode) {
return fmt.Errorf("unknown subnet mode %q", c.Mode)
}
if _, err := kv.GetFromDB(db, "subnet/"+c.Name+"/state"); err == nil {
@ -53,9 +54,14 @@ func (c CreateSubnetCommand) Prepare(db *badger.DB, cfg *configuration.Config) e
kv.AddInDB(db, "subnet/"+c.Name+"/interface_ip", c.InterfaceIP)
kv.AddInDB(db, "subnet/"+c.Name+"/cidr", c.CIDR)
kv.AddInDB(db, "subnet/"+c.Name+"/default_route", strconv.FormatBool(c.DefaultRoute))
if c.Mode == "vxlan" {
if c.Mode == subnet.ModeVxlan {
kv.AddInDB(db, "subnet/"+c.Name+"/vxlan_id", strconv.Itoa(c.VxlanID))
}
if c.Gateway != "" {
if err := kv.AddInDB(db, "subnet/"+c.Name+"/gateway", c.Gateway); err != nil {
return fmt.Errorf("store gateway: %w", err)
}
}
return nil
}

View file

@ -5,6 +5,7 @@ import (
configuration "git.g3e.fr/syonad/two/internal/config/agent"
"git.g3e.fr/syonad/two/pkg/db/kv"
"github.com/dgraph-io/badger/v4"
)
func testCfg() *configuration.Config {
@ -255,3 +256,74 @@ func TestDeleteSubnetCommand_Prepare_NotFound(t *testing.T) {
t.Error("Prepare devrait échouer si le subnet n'existe pas")
}
}
// --- gateway optionnelle et mode public_ip ---
func prepareSubnet(t *testing.T, cmd CreateSubnetCommand) (*badger.DB, error) {
t.Helper()
_, db := newTestDispatcher(t)
kv.AddInDB(db, "vpc/vpc-1/state", "running")
if cmd.VPC == "" {
cmd.VPC = "vpc-1"
}
return db, cmd.Prepare(db, testCfg())
}
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",
DefaultRoute: true, Gateway: "10.0.0.254",
})
if err != nil {
t.Fatalf("Prepare a échoué : %v", err)
}
gw, err := kv.GetFromDB(db, "subnet/sn-gw/gateway")
if err != nil {
t.Fatalf("clé gateway absente : %v", err)
}
if gw != "10.0.0.254" {
t.Errorf("gateway attendue 10.0.0.254, obtenu %q", gw)
}
}
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",
})
if err != nil {
t.Fatalf("Prepare a échoué : %v", err)
}
if _, err := kv.GetFromDB(db, "subnet/sn-nogw/gateway"); err == nil {
t.Error("aucune gateway fournie, aucune clé ne doit être écrite")
}
}
func TestCreateSubnetCommand_Prepare_AcceptsPublicIPMode(t *testing.T) {
db, err := prepareSubnet(t, CreateSubnetCommand{
Name: "sn-pub", Mode: "public_ip", IfaceType: "vms",
InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
DefaultRoute: true, Gateway: "203.0.113.1",
})
if err != nil {
t.Fatalf("le mode public_ip doit être accepté : %v", err)
}
mode, _ := kv.GetFromDB(db, "subnet/sn-pub/mode")
if mode != "public_ip" {
t.Errorf("mode attendu public_ip, obtenu %q", mode)
}
if _, err := kv.GetFromDB(db, "subnet/sn-pub/vxlan_id"); err == nil {
t.Error("vxlan_id ne doit être écrit que pour le mode vxlan")
}
}
func TestCreateSubnetCommand_Prepare_RejectsUnknownMode(t *testing.T) {
_, err := prepareSubnet(t, CreateSubnetCommand{
Name: "sn-bad", Mode: "public", IfaceType: "vms",
InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
})
if err == nil {
t.Error("un mode inconnu doit être refusé")
}
}