f-41: api, vpc: champ l3vni des VPC #41

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-11 20:05:20 +02:00
commit 3dd94f35dd
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
11 changed files with 295 additions and 10 deletions

View file

@ -45,13 +45,13 @@ paths:
schema:
$ref: "#/components/schemas/VPC"
"400":
description: Missing required field or invalid request body
description: Missing required field, invalid request body, or l3vni out of range
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"409":
description: VPC already exists
description: VPC already exists, l3vni already used by another VPC, or l3vni requested on an agent without a bgp section
content:
application/json:
schema:
@ -317,6 +317,17 @@ components:
type: string
description: CIDR block for the entire VPC address space
example: "10.0.0.0/16"
l3vni:
type: integer
format: int32
minimum: 1
maximum: 16777215
description: >-
Optional. L3VNI of the VPC, used for inter-subnet routing between hypervisors
(symmetric IRB). Also the id of the routing table of the VPC VRF, so 253-255 are
rejected. Requires the bgp section in the agent configuration, and must not be used
by another VPC of the agent. Absent: the VPC has no L3VNI.
example: 4001
VPC:
type: object
@ -331,6 +342,10 @@ components:
cidr:
type: string
example: "10.0.0.0/16"
l3vni:
type: integer
description: L3VNI of the VPC, absent when the VPC has none
example: 4001
SubnetCreateRequest:
type: object

View file

@ -8,6 +8,7 @@ de sérialisation structurée.
vpc/<name>/state → creating | running | error | deleting | deleted
vpc/<name>/cidr → <cidr>
vpc/<name>/l3vni → <id> (optionnel : VPC routé par L3VNI)
subnet/<name>/state → creating | running | error | deleting | deleted
subnet/<name>/vpc → <vpc-name>

View file

@ -16,11 +16,15 @@ import (
// The worker queue is buffered but has no running goroutines: Dispatch enqueues
// without blocking and Execute never runs, so DB state reflects only Prepare writes.
func newTestServer(t *testing.T) (*Server, *badger.DB) {
t.Helper()
return newTestServerWith(t, &configuration.Config{DefaultInterface: "br-test"})
}
func newTestServerWith(t *testing.T, cfg *configuration.Config) (*Server, *badger.DB) {
t.Helper()
db := kv.InitDB(kv.Config{Path: t.TempDir()}, false)
t.Cleanup(func() { db.Close() })
q := worker.New(100)
cfg := &configuration.Config{DefaultInterface: "br-test"}
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
d := dispatcher.New(q, db, cfg, logger)
return New(d, db, logger, "127.0.0.1:0"), db

View file

@ -1,14 +1,16 @@
package agentapi
type VPCCreateRequest struct {
Name string `json:"name"`
CIDR string `json:"cidr"`
Name string `json:"name"`
CIDR string `json:"cidr"`
L3VNI *uint32 `json:"l3vni,omitempty"`
}
type VPC struct {
Name string `json:"name"`
State string `json:"state"`
CIDR string `json:"cidr"`
L3VNI uint32 `json:"l3vni,omitempty"`
}
type SubnetCreateRequest struct {

View file

@ -36,8 +36,9 @@ func (s *Server) getVpc(w http.ResponseWriter, _ *http.Request, name string) {
return
}
cidr, _ := kv.GetFromDB(s.db, "vpc/"+name+"/cidr")
l3vni, _ := kv.GetFromDB(s.db, "vpc/"+name+"/l3vni")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(VPC{Name: name, State: state, CIDR: cidr})
json.NewEncoder(w).Encode(VPC{Name: name, State: state, CIDR: cidr, L3VNI: parseL3VNI(l3vni)})
}
func (s *Server) deleteVpc(w http.ResponseWriter, _ *http.Request, name string) {

View file

@ -5,9 +5,12 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
configuration "git.g3e.fr/syonad/two/internal/config/agent"
"git.g3e.fr/syonad/two/pkg/db/kv"
"github.com/dgraph-io/badger/v4"
)
// --- VpcsHandler ---
@ -236,3 +239,84 @@ func TestVpcByName_InvalidMethod(t *testing.T) {
t.Errorf("attendu 405, obtenu %d", w.Code)
}
}
func newBGPTestServer(t *testing.T) (*Server, *badger.DB) {
cfg := &configuration.Config{DefaultInterface: "br-test"}
cfg.BGP.ASN = 64600
cfg.BGP.RouteReflectors = []string{"10.255.255.1"}
return newTestServerWith(t, cfg)
}
func postVpcJSON(s *Server, body string) *httptest.ResponseRecorder {
w := httptest.NewRecorder()
s.VpcsHandler(w, httptest.NewRequest(http.MethodPost, "/vpcs", strings.NewReader(body)))
return w
}
func TestPostVpc_WithL3VNI(t *testing.T) {
s, db := newBGPTestServer(t)
w := postVpcJSON(s, `{"name":"vp-1","cidr":"10.0.0.0/16","l3vni":4001}`)
if w.Code != http.StatusAccepted {
t.Fatalf("attendu 202, obtenu %d: %s", w.Code, w.Body.String())
}
if !strings.Contains(w.Body.String(), `"l3vni":4001`) {
t.Errorf("réponse sans l3vni : %s", w.Body.String())
}
if got, _ := kv.GetFromDB(db, "vpc/vp-1/l3vni"); got != "4001" {
t.Errorf("l3vni en DB = %q", got)
}
}
func TestPostVpc_WithoutL3VNIOmitsTheField(t *testing.T) {
s, _ := newBGPTestServer(t)
w := postVpcJSON(s, `{"name":"vp-1","cidr":"10.0.0.0/16"}`)
if w.Code != http.StatusAccepted || strings.Contains(w.Body.String(), "l3vni") {
t.Errorf("%d : %s", w.Code, w.Body.String())
}
}
func TestPostVpc_RejectsAnInvalidL3VNIAsABadRequest(t *testing.T) {
s, db := newBGPTestServer(t)
for _, body := range []string{
`{"name":"vp-1","cidr":"10.0.0.0/16","l3vni":0}`,
`{"name":"vp-1","cidr":"10.0.0.0/16","l3vni":255}`,
`{"name":"vp-1","cidr":"10.0.0.0/16","l3vni":16777216}`,
`{"name":"vp-1","cidr":"10.0.0.0/16","l3vni":-1}`,
} {
if w := postVpcJSON(s, body); w.Code != http.StatusBadRequest {
t.Errorf("%s : attendu 400, obtenu %d: %s", body, w.Code, w.Body.String())
}
}
if entries, _ := kv.ListByPrefix(db, "vpc/"); len(entries) != 0 {
t.Errorf("écritures malgré le refus : %v", entries)
}
}
func TestPostVpc_L3VNIWithoutBGPIsAConflict(t *testing.T) {
s, _ := newTestServer(t)
w := postVpcJSON(s, `{"name":"vp-1","cidr":"10.0.0.0/16","l3vni":4001}`)
if w.Code != http.StatusConflict || !strings.Contains(w.Body.String(), "requires the bgp section") {
t.Errorf("%d : %s", w.Code, w.Body.String())
}
}
func TestGetAndListVpc_ReturnTheL3VNI(t *testing.T) {
s, db := newTestServer(t)
kv.AddInDB(db, "vpc/vp-1/state", "running")
kv.AddInDB(db, "vpc/vp-1/cidr", "10.0.0.0/16")
kv.AddInDB(db, "vpc/vp-1/l3vni", "4001")
w := httptest.NewRecorder()
s.VpcByNameHandler(w, httptest.NewRequest(http.MethodGet, "/vpcs/vp-1", nil))
var got VPC
json.NewDecoder(w.Body).Decode(&got)
if got.L3VNI != 4001 {
t.Errorf("GET : l3vni = %d", got.L3VNI)
}
w = httptest.NewRecorder()
s.VpcsHandler(w, httptest.NewRequest(http.MethodGet, "/vpcs", nil))
var list []VPC
json.NewDecoder(w.Body).Decode(&list)
if len(list) != 1 || list[0].L3VNI != 4001 {
t.Errorf("liste : %+v", list)
}
}

View file

@ -4,9 +4,11 @@ import (
"encoding/json"
"net"
"net/http"
"strconv"
"strings"
dispatcher "git.g3e.fr/syonad/two/internal/dispatcher/agent"
"git.g3e.fr/syonad/two/internal/vpc"
"git.g3e.fr/syonad/two/pkg/db/kv"
)
@ -44,6 +46,8 @@ func (s *Server) listVpcs(w http.ResponseWriter, _ *http.Request) {
vpcs[name].State = value
case "cidr":
vpcs[name].CIDR = value
case "l3vni":
vpcs[name].L3VNI = parseL3VNI(value)
}
}
result := make([]VPC, 0, len(vpcs))
@ -77,6 +81,14 @@ func (s *Server) postVpc(w http.ResponseWriter, r *http.Request) {
return
}
cmd := dispatcher.CreateVPCCommand{Name: req.Name, CIDR: req.CIDR}
if req.L3VNI != nil {
if err := vpc.ValidL3VNI(*req.L3VNI); err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()})
return
}
cmd.L3VNI = *req.L3VNI
}
if err := s.dispatcher.Prepare(cmd); err != nil {
w.WriteHeader(http.StatusConflict)
json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()})
@ -90,5 +102,10 @@ func (s *Server) postVpc(w http.ResponseWriter, r *http.Request) {
return
}
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(VPC{Name: req.Name, State: state, CIDR: req.CIDR})
json.NewEncoder(w).Encode(VPC{Name: req.Name, State: state, CIDR: req.CIDR, L3VNI: cmd.L3VNI})
}
func parseL3VNI(value string) uint32 {
id, _ := strconv.ParseUint(value, 10, 32)
return uint32(id)
}

View file

@ -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)
}

View file

@ -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)
}
}

15
internal/vpc/l3vni.go Normal file
View file

@ -0,0 +1,15 @@
package vpc
import "fmt"
const maxL3VNI = 1<<24 - 1
func ValidL3VNI(id uint32) error {
if id == 0 || id > maxL3VNI {
return fmt.Errorf("l3vni %d does not fit in 24 bits (1-%d)", id, maxL3VNI)
}
if id >= 253 && id <= 255 {
return fmt.Errorf("l3vni %d is reserved: it is also the routing table id, and tables 253-255 belong to the kernel", id)
}
return nil
}

View file

@ -0,0 +1,29 @@
package vpc
import (
"strings"
"testing"
)
func TestValidL3VNI_AcceptsEvery24BitValueOutsideTheKernelTables(t *testing.T) {
for _, id := range []uint32{1, 252, 256, 4001, 16777215} {
if err := ValidL3VNI(id); err != nil {
t.Errorf("ValidL3VNI(%d) = %v", id, err)
}
}
}
func TestValidL3VNI_RejectsOutOfRangeAndReservedTables(t *testing.T) {
cases := map[uint32]string{
0: "does not fit in 24 bits",
16777216: "does not fit in 24 bits",
253: "is reserved",
254: "is reserved",
255: "is reserved",
}
for id, want := range cases {
if err := ValidL3VNI(id); err == nil || !strings.Contains(err.Error(), want) {
t.Errorf("ValidL3VNI(%d) = %v, attendu %q", id, err, want)
}
}
}