diff --git a/api/agent.yaml b/api/agent.yaml index d4a737c..3c1c5f0 100644 --- a/api/agent.yaml +++ b/api/agent.yaml @@ -566,8 +566,10 @@ components: description: > Subnet mode. "vxlan" (default): creates a VXLAN tunnel and a host bridge. "bridge": attaches directly to an existing bridge resolved from iface_type in the agent config. - "public_ip": accepted and routed like vxlan for DHCP purposes, but its host network - setup is not implemented yet — creating such a subnet currently fails in Execute. + "public_ip": attached like bridge, but always to the public bridge (interfaces.internet, + br-public when absent); iface_type is ignored. The VMs carry public IPs; the VPC route + keeps interface_ip as next-hop. Several VPCs may use the same range with the same + interface_ip: each has its own subnet, whose name the caller keeps unique on the agent. "vlan" is reserved for future use. enum: [vxlan, bridge, public_ip] default: vxlan diff --git a/internal/dispatcher/agent/mtu_test.go b/internal/dispatcher/agent/mtu_test.go index 2313284..042b2be 100644 --- a/internal/dispatcher/agent/mtu_test.go +++ b/internal/dispatcher/agent/mtu_test.go @@ -86,3 +86,21 @@ func TestCreateVPCCommand_Prepare_L3VNIChecksTheUnderlay(t *testing.T) { t.Errorf("un VPC sans l3vni ne contrôle pas l'underlay : %v", err) } } + +func TestCreateSubnetCommand_Prepare_PublicIPChecksThePublicBridge(t *testing.T) { + withLinkMTU(t, map[string]int{"br-vms": 9000, "br-public": 1499}) + _, db := newTestDispatcher(t) + kv.AddInDB(db, "vpc/vpc-1/state", "running") + cmd := CreateSubnetCommand{Name: "sn-pub", VPC: "vpc-1", Mode: "public_ip", IfaceType: "vms", InterfaceIP: "203.0.113.254", CIDR: "203.0.113.0/24"} + err := cmd.Prepare(db, testCfg()) + if err == nil || !strings.Contains(err.Error(), "br-public") { + t.Fatalf("bridge public à 1499 : %v", err) + } + if _, err := kv.GetFromDB(db, "subnet/sn-pub/state"); err == nil { + t.Error("aucune clé ne doit être écrite après un refus") + } + withLinkMTU(t, map[string]int{"br-public": 1500}) + if err := cmd.Prepare(db, testCfg()); err != nil { + t.Errorf("bridge public à 1500 : %v", err) + } +} diff --git a/internal/dispatcher/agent/subnet_commands.go b/internal/dispatcher/agent/subnet_commands.go index 91f08bc..d6ea147 100644 --- a/internal/dispatcher/agent/subnet_commands.go +++ b/internal/dispatcher/agent/subnet_commands.go @@ -64,12 +64,15 @@ func (c CreateSubnetCommand) Prepare(db *badger.DB, cfg *configuration.Config) e if !ok { localIface = cfg.DefaultInterface } + if c.Mode == subnet.ModePublicIP { + localIface = cfg.PublicBridge() + } switch c.Mode { case subnet.ModeVxlan: if err := checkUnderlay(cfg, localIface); err != nil { return err } - case subnet.ModeBridge: + case subnet.ModeBridge, subnet.ModePublicIP: if err := checkBridge(cfg, localIface); err != nil { return err } diff --git a/internal/dispatcher/agent/subnet_commands_test.go b/internal/dispatcher/agent/subnet_commands_test.go index a2d6cd5..0373d29 100644 --- a/internal/dispatcher/agent/subnet_commands_test.go +++ b/internal/dispatcher/agent/subnet_commands_test.go @@ -391,6 +391,23 @@ func TestCreateSubnetCommand_Prepare_AcceptsPublicIPMode(t *testing.T) { 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") } + if got, _ := kv.GetFromDB(db, "subnet/sn-pub/local_iface"); got != "br-public" { + t.Errorf("local_iface %q, br-public attendu malgré iface_type vms", got) + } +} + +func TestCreateSubnetCommand_Prepare_PublicIPFollowsInterfacesInternet(t *testing.T) { + cfg := testCfg() + cfg.Interfaces["internet"] = "br-pub2" + _, db := newTestDispatcher(t) + kv.AddInDB(db, "vpc/vpc-1/state", "running") + cmd := CreateSubnetCommand{Name: "sn-pub", VPC: "vpc-1", Mode: "public_ip", IfaceType: "vms", InterfaceIP: "203.0.113.254", CIDR: "203.0.113.0/24"} + if err := cmd.Prepare(db, cfg); err != nil { + t.Fatal(err) + } + if got, _ := kv.GetFromDB(db, "subnet/sn-pub/local_iface"); got != "br-pub2" { + t.Errorf("local_iface %q, br-pub2 attendu", got) + } } func TestCreateSubnetCommand_Prepare_RejectsUnknownMode(t *testing.T) { diff --git a/internal/subnet/create.go b/internal/subnet/create.go index f0ae80a..9d19709 100644 --- a/internal/subnet/create.go +++ b/internal/subnet/create.go @@ -61,9 +61,7 @@ func createSubnet(db *badger.DB, subnetName string, d subnetData, backend dhcpba if err := setupVxlanHost(d, vethE); err != nil { return err } - case ModePublicIP: - return fmt.Errorf("subnet mode %q: host network setup is not implemented yet", d.mode) - case ModeBridge: + case ModeBridge, ModePublicIP: if err := netif.BridgeSetMaster(vethE, d.localIface); err != nil { return fmt.Errorf("add veth-e to bridge: %w", err) } @@ -122,7 +120,7 @@ func createSubnet(db *badger.DB, subnetName string, d subnetData, backend dhcpba }); err != nil { return fmt.Errorf("configure netns: %w", err) } - case d.mode == ModeBridge: + case Bridged(d.mode): if err := netns.Call(d.vpc, func() error { if err := netif.AddrAdd(d.bridge, d.interfaceIP); err != nil { return fmt.Errorf("add addr: %w", err) diff --git a/internal/subnet/delete.go b/internal/subnet/delete.go index e38c224..4ddd357 100644 --- a/internal/subnet/delete.go +++ b/internal/subnet/delete.go @@ -48,7 +48,7 @@ func DeleteSubnet(db *badger.DB, subnetName string, cfg *configuration.Config) e if err := deleteSubnetVxlan(d); err != nil { return err } - case "bridge": + case ModeBridge, ModePublicIP: if err := deleteSubnetBridge(d); err != nil { return err } diff --git a/internal/subnet/mode.go b/internal/subnet/mode.go index cfc61dd..c963251 100644 --- a/internal/subnet/mode.go +++ b/internal/subnet/mode.go @@ -13,3 +13,7 @@ func ValidMode(mode string) bool { } return false } + +func Bridged(mode string) bool { + return mode == ModeBridge || mode == ModePublicIP +} diff --git a/internal/subnet/mode_test.go b/internal/subnet/mode_test.go new file mode 100644 index 0000000..0c7b606 --- /dev/null +++ b/internal/subnet/mode_test.go @@ -0,0 +1,11 @@ +package subnet + +import "testing" + +func TestBridged(t *testing.T) { + for mode, want := range map[string]bool{"bridge": true, "public_ip": true, "vxlan": false, "": false} { + if got := Bridged(mode); got != want { + t.Errorf("%q : %v, %v attendu", mode, got, want) + } + } +}