f-41: api, vpc: champ l3vni des VPC #41
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
bf0e83862f
commit
3dd94f35dd
11 changed files with 295 additions and 10 deletions
|
|
@ -3,6 +3,7 @@ package dispatcher
|
|||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
|
@ -14,25 +15,61 @@ import (
|
|||
)
|
||||
|
||||
type CreateVPCCommand struct {
|
||||
Name string
|
||||
CIDR string
|
||||
Name string
|
||||
CIDR string
|
||||
L3VNI uint32
|
||||
}
|
||||
|
||||
func (c CreateVPCCommand) Key() string { return "vpc/" + c.Name }
|
||||
|
||||
func (c CreateVPCCommand) Prepare(db *badger.DB, _ *configuration.Config) error {
|
||||
func (c CreateVPCCommand) Prepare(db *badger.DB, cfg *configuration.Config) error {
|
||||
if _, err := kv.GetFromDB(db, "vpc/"+c.Name+"/state"); err == nil {
|
||||
return fmt.Errorf("vpc %q already exists", c.Name)
|
||||
}
|
||||
if _, _, err := net.ParseCIDR(c.CIDR); err != nil {
|
||||
return fmt.Errorf("invalid cidr %q: %w", c.CIDR, err)
|
||||
}
|
||||
if c.L3VNI != 0 {
|
||||
if err := c.checkL3VNI(db, cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := kv.AddInDB(db, "vpc/"+c.Name+"/cidr", c.CIDR); err != nil {
|
||||
return err
|
||||
}
|
||||
if c.L3VNI != 0 {
|
||||
if err := kv.AddInDB(db, "vpc/"+c.Name+"/l3vni", strconv.FormatUint(uint64(c.L3VNI), 10)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return state.Set(db, c.Key(), state.Creating)
|
||||
}
|
||||
|
||||
func (c CreateVPCCommand) checkL3VNI(db *badger.DB, cfg *configuration.Config) error {
|
||||
if err := vpc.ValidL3VNI(c.L3VNI); err != nil {
|
||||
return err
|
||||
}
|
||||
if cfg == nil || !cfg.BGP.Enabled() {
|
||||
return fmt.Errorf("l3vni requires the bgp section in the agent configuration")
|
||||
}
|
||||
entries, err := kv.ListByPrefix(db, "vpc/")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to list vpcs: %w", err)
|
||||
}
|
||||
want := strconv.FormatUint(uint64(c.L3VNI), 10)
|
||||
for key, value := range entries {
|
||||
parts := strings.Split(key, "/")
|
||||
if len(parts) != 3 || parts[2] != "l3vni" || value != want {
|
||||
continue
|
||||
}
|
||||
if s, err := state.Get(db, "vpc/"+parts[1]); err == nil && s == state.Deleted {
|
||||
continue
|
||||
}
|
||||
return fmt.Errorf("l3vni %d is already used by vpc %q", c.L3VNI, parts[1])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c CreateVPCCommand) Execute(db *badger.DB, _ *configuration.Config) error {
|
||||
return vpc.CreateVPC(db, c.Name)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
package dispatcher
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
)
|
||||
|
||||
|
|
@ -137,3 +139,81 @@ func TestDeleteVPCCommand_Prepare_AllowedWhenSubnetDeleting(t *testing.T) {
|
|||
t.Fatalf("Prepare devrait réussir si le subnet est deleting : %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func bgpConfig() *configuration.Config {
|
||||
cfg := &configuration.Config{}
|
||||
cfg.BGP.ASN = 64600
|
||||
cfg.BGP.RouteReflectors = []string{"10.255.255.1"}
|
||||
return cfg
|
||||
}
|
||||
|
||||
func TestCreateVPCCommand_Prepare_StoresTheL3VNI(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
cmd := CreateVPCCommand{Name: "vpc-1", CIDR: "10.0.0.0/16", L3VNI: 4001}
|
||||
if err := cmd.Prepare(db, bgpConfig()); err != nil {
|
||||
t.Fatalf("Prepare a échoué : %v", err)
|
||||
}
|
||||
if got, err := kv.GetFromDB(db, "vpc/vpc-1/l3vni"); err != nil || got != "4001" {
|
||||
t.Errorf("l3vni en DB = %q, %v ; attendu 4001", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateVPCCommand_Prepare_WithoutL3VNIStoresNothing(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
cmd := CreateVPCCommand{Name: "vpc-1", CIDR: "10.0.0.0/16"}
|
||||
if err := cmd.Prepare(db, nil); err != nil {
|
||||
t.Fatalf("Prepare a échoué : %v", err)
|
||||
}
|
||||
if got, err := kv.GetFromDB(db, "vpc/vpc-1/l3vni"); err == nil {
|
||||
t.Errorf("clé l3vni écrite : %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateVPCCommand_Prepare_RefusesAnL3VNIWithoutBGP(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
cmd := CreateVPCCommand{Name: "vpc-1", CIDR: "10.0.0.0/16", L3VNI: 4001}
|
||||
err := cmd.Prepare(db, &configuration.Config{})
|
||||
if err == nil || !strings.Contains(err.Error(), "requires the bgp section") {
|
||||
t.Fatalf("erreur %v", err)
|
||||
}
|
||||
if entries, _ := kv.ListByPrefix(db, "vpc/"); len(entries) != 0 {
|
||||
t.Errorf("écritures malgré le refus : %v", entries)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateVPCCommand_Prepare_RefusesAReservedL3VNI(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
cmd := CreateVPCCommand{Name: "vpc-1", CIDR: "10.0.0.0/16", L3VNI: 254}
|
||||
if err := cmd.Prepare(db, bgpConfig()); err == nil || !strings.Contains(err.Error(), "l3vni 254 is reserved") {
|
||||
t.Fatalf("erreur %v", err)
|
||||
}
|
||||
if entries, _ := kv.ListByPrefix(db, "vpc/"); len(entries) != 0 {
|
||||
t.Errorf("écritures malgré le refus : %v", entries)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateVPCCommand_Prepare_RefusesAnL3VNIAlreadyUsed(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vpc/vpc-a/state", "deleting")
|
||||
kv.AddInDB(db, "vpc/vpc-a/l3vni", "4001")
|
||||
cmd := CreateVPCCommand{Name: "vpc-b", CIDR: "10.0.0.0/16", L3VNI: 4001}
|
||||
err := cmd.Prepare(db, bgpConfig())
|
||||
if err == nil || !strings.Contains(err.Error(), `l3vni 4001 is already used by vpc "vpc-a"`) {
|
||||
t.Fatalf("erreur %v", err)
|
||||
}
|
||||
if _, err := kv.GetFromDB(db, "vpc/vpc-b/state"); err == nil {
|
||||
t.Error("vpc-b créé malgré le doublon")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateVPCCommand_Prepare_ReusesTheL3VNIOfADeletedVPC(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vpc/vpc-a/state", "deleted")
|
||||
kv.AddInDB(db, "vpc/vpc-a/l3vni", "4001")
|
||||
kv.AddInDB(db, "vpc/vpc-c/state", "running")
|
||||
kv.AddInDB(db, "vpc/vpc-c/l3vni", "4002")
|
||||
cmd := CreateVPCCommand{Name: "vpc-b", CIDR: "10.0.0.0/16", L3VNI: 4001}
|
||||
if err := cmd.Prepare(db, bgpConfig()); err != nil {
|
||||
t.Fatalf("Prepare a échoué : %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue