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
46aad1e7c5
commit
7d15ccf56f
6 changed files with 25 additions and 4 deletions
|
|
@ -441,7 +441,7 @@ paths:
|
||||||
schema:
|
schema:
|
||||||
$ref: "#/components/schemas/Error"
|
$ref: "#/components/schemas/Error"
|
||||||
"409":
|
"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:
|
content:
|
||||||
application/json:
|
application/json:
|
||||||
schema:
|
schema:
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ func seedRunningVM(t *testing.T, db *badger.DB) {
|
||||||
for key, value := range map[string]string{
|
for key, value := range map[string]string{
|
||||||
"vpc/vp-t47/cidr": "10.47.0.0/16",
|
"vpc/vp-t47/cidr": "10.47.0.0/16",
|
||||||
"subnet/sn-a/vpc": "vp-t47",
|
"subnet/sn-a/vpc": "vp-t47",
|
||||||
|
"subnet/sn-a/mode": "vxlan",
|
||||||
"vm/vm-1/state": "running",
|
"vm/vm-1/state": "running",
|
||||||
"vm/vm-1/nic/0/name": "nic-a",
|
"vm/vm-1/nic/0/name": "nic-a",
|
||||||
"vm/vm-1/nic/0/subnet": "sn-a",
|
"vm/vm-1/nic/0/subnet": "sn-a",
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||||
"git.g3e.fr/syonad/two/internal/fip"
|
"git.g3e.fr/syonad/two/internal/fip"
|
||||||
"git.g3e.fr/syonad/two/internal/state"
|
"git.g3e.fr/syonad/two/internal/state"
|
||||||
|
"git.g3e.fr/syonad/two/internal/subnet"
|
||||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||||
"github.com/dgraph-io/badger/v4"
|
"github.com/dgraph-io/badger/v4"
|
||||||
)
|
)
|
||||||
|
|
@ -39,9 +40,13 @@ func (c CreateFloatingIPCommand) Prepare(db *badger.DB, _ *configuration.Config)
|
||||||
if vmState != state.Running {
|
if vmState != state.Running {
|
||||||
return fmt.Errorf("vm %q is %s", c.VM, vmState)
|
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
|
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)
|
others, err := floatingIPs(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,12 @@ func seedVMWithInterfaces(t *testing.T, db *badger.DB, name, st string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
for key, value := range map[string]string{
|
for key, value := range map[string]string{
|
||||||
"vpc/vp-t47/cidr": "10.47.0.0/16",
|
"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",
|
"subnet/sn-a/vpc": "vp-t47",
|
||||||
"vm/" + name + "/state": st,
|
"vm/" + name + "/state": st,
|
||||||
"vm/" + name + "/nic/0/name": "nic-a",
|
"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/interface", "nic-ab")
|
||||||
kv.AddInDB(db, "floating-ip/fip-0/ip", "240.0.0.47/24")
|
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"`},
|
}, 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"`},
|
{"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"`},
|
{"gateway invalide", nil, func(c *CreateFloatingIPCommand) { c.Gateway = "bordure" }, `invalid gateway "bordure"`},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,8 @@ type Target struct {
|
||||||
VPC string
|
VPC string
|
||||||
CIDR *net.IPNet
|
CIDR *net.IPNet
|
||||||
Private net.IP
|
Private net.IP
|
||||||
|
Subnet string
|
||||||
|
Mode string
|
||||||
}
|
}
|
||||||
|
|
||||||
type floatingIP struct {
|
type floatingIP struct {
|
||||||
|
|
@ -85,7 +87,11 @@ func Resolve(db *badger.DB, vmName, ifaceName string) (Target, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Target{}, fmt.Errorf("vm %q: interface %q: invalid cidr %q of vpc %q", vmName, ifaceName, raw, vpc)
|
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)
|
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() })
|
t.Cleanup(func() { db.Close() })
|
||||||
for key, value := range map[string]string{
|
for key, value := range map[string]string{
|
||||||
"vpc/vp-t47/cidr": "10.47.0.0/16",
|
"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-a/vpc": "vp-t47",
|
||||||
"subnet/sn-b/vpc": "vp-t47",
|
"subnet/sn-b/vpc": "vp-t47",
|
||||||
"vm/vm-1/nic/0/name": "nic-a1b2",
|
"vm/vm-1/nic/0/name": "nic-a1b2",
|
||||||
|
|
@ -206,7 +208,7 @@ func TestResolveFindsTheInterfaceByName(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
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)
|
t.Errorf("cible %+v", got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue