two/internal/dispatcher/agent/vpc_commands.go
GnomeZworc 3c6852dc18
f-41: vpc: VRF, L3VNI et transit à la création des VPC routées #41
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-10-11 20:21:19 +02:00

146 lines
3.9 KiB
Go

package dispatcher
import (
"fmt"
"net"
"strconv"
"strings"
"time"
configuration "git.g3e.fr/syonad/two/internal/config/agent"
"git.g3e.fr/syonad/two/internal/state"
"git.g3e.fr/syonad/two/internal/vpc"
"git.g3e.fr/syonad/two/pkg/db/kv"
"github.com/dgraph-io/badger/v4"
)
type CreateVPCCommand struct {
Name string
CIDR string
L3VNI uint32
}
func (c CreateVPCCommand) Key() string { return "vpc/" + c.Name }
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, cfg *configuration.Config) error {
return vpc.CreateVPC(db, c.Name, cfg)
}
type DeleteVPCCommand struct {
Name string
}
func (c DeleteVPCCommand) Key() string { return "vpc/" + c.Name }
func (c DeleteVPCCommand) Prepare(db *badger.DB, _ *configuration.Config) error {
current, err := state.Get(db, c.Key())
if err != nil {
return fmt.Errorf("vpc %q not found", c.Name)
}
if !state.CanDelete(current) {
return fmt.Errorf("vpc %q cannot be deleted while %s", c.Name, current)
}
entries, err := kv.ListByPrefix(db, "subnet/")
if err != nil {
return fmt.Errorf("failed to list subnets: %w", err)
}
for key, value := range entries {
if !strings.HasSuffix(key, "/vpc") || value != c.Name {
continue
}
subnetName := strings.Split(key, "/")[1]
s, err := state.Get(db, "subnet/"+subnetName)
if err != nil || (s != state.Deleting && s != state.Deleted) {
return fmt.Errorf("subnet %q must be deleted before deleting vpc %q", subnetName, c.Name)
}
}
return state.Set(db, c.Key(), state.Deleting)
}
func (c DeleteVPCCommand) Execute(db *badger.DB, cfg *configuration.Config) error {
timeout := time.After(time.Duration(cfg.Dispatcher.TimeoutSeconds) * time.Second)
for {
entries, err := kv.ListByPrefix(db, "subnet/")
if err != nil {
return fmt.Errorf("failed to list subnets: %w", err)
}
pending := false
for key, value := range entries {
if strings.HasSuffix(key, "/vpc") && value == c.Name {
subnetName := strings.Split(key, "/")[1]
s, _ := state.Get(db, "subnet/"+subnetName)
if s == state.Deleting {
pending = true
break
}
}
}
if !pending {
break
}
select {
case <-timeout:
return fmt.Errorf("timed out waiting for subnets of vpc %q to be deleted", c.Name)
case <-time.After(time.Duration(cfg.Dispatcher.PollSeconds) * time.Second):
}
}
if err := vpc.DeleteVPC(db, c.Name, cfg); err != nil {
return err
}
current, err := state.Get(db, c.Key())
if err != nil {
return err
}
if current == state.Deleted {
kv.DeleteInDB(db, "vpc/"+c.Name)
}
return nil
}