f-89: fip: IP flottante refusée sur une interface public_ip #89
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
2b252bb40c
commit
047d231556
6 changed files with 25 additions and 4 deletions
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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"`},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue