From 7d15ccf56f4fc80592a009c4f16349ad610b595c Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Fri, 30 Oct 2026 01:00:24 +0100 Subject: [PATCH] =?UTF-8?q?f-89:=20fip:=20IP=20flottante=20refus=C3=A9e=20?= =?UTF-8?q?sur=20une=20interface=20public=5Fip=20#89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: GnomeZworc --- api/agent.yaml | 2 +- internal/api/agent/fip_test.go | 1 + internal/dispatcher/agent/fip_commands.go | 7 ++++++- internal/dispatcher/agent/fip_commands_test.go | 7 +++++++ internal/fip/fip.go | 8 +++++++- internal/fip/fip_test.go | 4 +++- 6 files changed, 25 insertions(+), 4 deletions(-) diff --git a/api/agent.yaml b/api/agent.yaml index 3c1c5f0..bb3bb10 100644 --- a/api/agent.yaml +++ b/api/agent.yaml @@ -441,7 +441,7 @@ paths: schema: $ref: "#/components/schemas/Error" "409": - description: Floating IP already exists, VM unknown on this agent or not running, no interface of that name on the VM, interface that already has a floating IP, or public IP already used by another floating IP of this agent + description: Floating IP already exists, VM unknown on this agent or not running, no interface of that name on the VM, interface on a public_ip subnet (it carries its public IP itself), interface that already has a floating IP, or public IP already used by another floating IP of this agent content: application/json: schema: diff --git a/internal/api/agent/fip_test.go b/internal/api/agent/fip_test.go index baeb624..ddf9e70 100644 --- a/internal/api/agent/fip_test.go +++ b/internal/api/agent/fip_test.go @@ -17,6 +17,7 @@ func seedRunningVM(t *testing.T, db *badger.DB) { for key, value := range map[string]string{ "vpc/vp-t47/cidr": "10.47.0.0/16", "subnet/sn-a/vpc": "vp-t47", + "subnet/sn-a/mode": "vxlan", "vm/vm-1/state": "running", "vm/vm-1/nic/0/name": "nic-a", "vm/vm-1/nic/0/subnet": "sn-a", diff --git a/internal/dispatcher/agent/fip_commands.go b/internal/dispatcher/agent/fip_commands.go index cd48943..fb59c1c 100644 --- a/internal/dispatcher/agent/fip_commands.go +++ b/internal/dispatcher/agent/fip_commands.go @@ -8,6 +8,7 @@ import ( configuration "git.g3e.fr/syonad/two/internal/config/agent" "git.g3e.fr/syonad/two/internal/fip" "git.g3e.fr/syonad/two/internal/state" + "git.g3e.fr/syonad/two/internal/subnet" "git.g3e.fr/syonad/two/pkg/db/kv" "github.com/dgraph-io/badger/v4" ) @@ -39,9 +40,13 @@ func (c CreateFloatingIPCommand) Prepare(db *badger.DB, _ *configuration.Config) if vmState != state.Running { return fmt.Errorf("vm %q is %s", c.VM, vmState) } - if _, err := fip.Resolve(db, c.VM, c.Interface); err != nil { + target, err := fip.Resolve(db, c.VM, c.Interface) + if err != nil { return err } + if target.Mode == subnet.ModePublicIP { + return fmt.Errorf("interface %q of vm %q is on public_ip subnet %q: it carries its public ip itself", c.Interface, c.VM, target.Subnet) + } others, err := floatingIPs(db) if err != nil { return err diff --git a/internal/dispatcher/agent/fip_commands_test.go b/internal/dispatcher/agent/fip_commands_test.go index de681eb..c5815a3 100644 --- a/internal/dispatcher/agent/fip_commands_test.go +++ b/internal/dispatcher/agent/fip_commands_test.go @@ -12,6 +12,12 @@ func seedVMWithInterfaces(t *testing.T, db *badger.DB, name, st string) { t.Helper() for key, value := range map[string]string{ "vpc/vp-t47/cidr": "10.47.0.0/16", + "subnet/sn-a/mode": "vxlan", + "subnet/sn-p/vpc": "vp-t47", + "subnet/sn-p/mode": "public_ip", + "vm/" + name + "/nic/2/name": "nic-pub", + "vm/" + name + "/nic/2/subnet": "sn-p", + "vm/" + name + "/nic/2/ip": "203.0.113.20", "subnet/sn-a/vpc": "vp-t47", "vm/" + name + "/state": st, "vm/" + name + "/nic/0/name": "nic-a", @@ -71,6 +77,7 @@ func TestCreateFloatingIPCommand_Prepare_Refuses(t *testing.T) { kv.AddInDB(db, "floating-ip/fip-0/interface", "nic-ab") kv.AddInDB(db, "floating-ip/fip-0/ip", "240.0.0.47/24") }, nil, `public ip 240.0.0.47 is already used by floating ip "fip-0"`}, + {"interface sur un subnet public_ip", nil, func(c *CreateFloatingIPCommand) { c.Interface = "nic-pub" }, `interface "nic-pub" of vm "vm-1" is on public_ip subnet "sn-p"`}, {"ip sans préfixe", nil, func(c *CreateFloatingIPCommand) { c.IP = "240.0.0.47" }, `invalid ip "240.0.0.47"`}, {"gateway invalide", nil, func(c *CreateFloatingIPCommand) { c.Gateway = "bordure" }, `invalid gateway "bordure"`}, } diff --git a/internal/fip/fip.go b/internal/fip/fip.go index 69883df..ea6150b 100644 --- a/internal/fip/fip.go +++ b/internal/fip/fip.go @@ -24,6 +24,8 @@ type Target struct { VPC string CIDR *net.IPNet Private net.IP + Subnet string + Mode string } type floatingIP struct { @@ -85,7 +87,11 @@ func Resolve(db *badger.DB, vmName, ifaceName string) (Target, error) { if err != nil { return Target{}, fmt.Errorf("vm %q: interface %q: invalid cidr %q of vpc %q", vmName, ifaceName, raw, vpc) } - return Target{VPC: vpc, CIDR: cidr, Private: ip}, nil + mode, err := kv.GetFromDB(db, "subnet/"+subnet+"/mode") + if err != nil { + return Target{}, fmt.Errorf("vm %q: interface %q: mode of subnet %q: %w", vmName, ifaceName, subnet, err) + } + return Target{VPC: vpc, CIDR: cidr, Private: ip, Subnet: subnet, Mode: mode}, nil } return Target{}, fmt.Errorf("vm %q has no interface %q", vmName, ifaceName) } diff --git a/internal/fip/fip_test.go b/internal/fip/fip_test.go index e889d13..4c4900e 100644 --- a/internal/fip/fip_test.go +++ b/internal/fip/fip_test.go @@ -184,6 +184,8 @@ func seedVM(t *testing.T) *badger.DB { t.Cleanup(func() { db.Close() }) for key, value := range map[string]string{ "vpc/vp-t47/cidr": "10.47.0.0/16", + "subnet/sn-a/mode": "vxlan", + "subnet/sn-b/mode": "public_ip", "subnet/sn-a/vpc": "vp-t47", "subnet/sn-b/vpc": "vp-t47", "vm/vm-1/nic/0/name": "nic-a1b2", @@ -206,7 +208,7 @@ func TestResolveFindsTheInterfaceByName(t *testing.T) { if err != nil { t.Fatal(err) } - if got.VPC != "vp-t47" || got.CIDR.String() != "10.47.0.0/16" || got.Private.String() != "10.47.2.10" { + if got.VPC != "vp-t47" || got.CIDR.String() != "10.47.0.0/16" || got.Private.String() != "10.47.2.10" || got.Subnet != "sn-b" || got.Mode != "public_ip" { t.Errorf("cible %+v", got) } }