f-42: api: ressource /nat-gateways, état, métrique et watchdog #42
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
4aec9927ad
commit
2737b61bc4
16 changed files with 841 additions and 11 deletions
137
api/agent.yaml
137
api/agent.yaml
|
|
@ -91,7 +91,7 @@ paths:
|
|||
"404":
|
||||
$ref: "#/components/responses/NotFound"
|
||||
"409":
|
||||
description: VPC not deletable — only running or error states can be deleted, and all its subnets must be deleted or deleting; subnets still deleting are awaited by the asynchronous deletion
|
||||
description: VPC not deletable — only running or error states can be deleted, all its subnets must be deleted or deleting (subnets still deleting are awaited by the asynchronous deletion), and its NAT gateway, if any, must be deleted first
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
|
|
@ -299,6 +299,101 @@ paths:
|
|||
|
||||
# ── Components ──────────────────────────────────────────────────────────────
|
||||
|
||||
# ── NAT gateway ────────────────────────────────────────────────────────────
|
||||
|
||||
/nat-gateways:
|
||||
get:
|
||||
summary: List all NAT gateways of this agent
|
||||
operationId: listNATGateways
|
||||
responses:
|
||||
"200":
|
||||
description: List of NAT gateways
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
$ref: "#/components/schemas/NATGateway"
|
||||
"500":
|
||||
$ref: "#/components/responses/InternalError"
|
||||
|
||||
post:
|
||||
summary: Create the NAT gateway of a VPC on this agent
|
||||
operationId: createNATGateway
|
||||
description: >-
|
||||
Gives the VMs of a VPC without a public IP an Internet exit through this host. The
|
||||
gateway owns the given public IP on the public bridge (interfaces.internet) and announces
|
||||
a type-5 default route in the VRF of the VPC. Only VPCs created by this version or later
|
||||
route their default traffic to the VRF, and can use it. To move the gateway, delete it on
|
||||
the old host if it answers, then create it on the new one.
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/NATGatewayCreateRequest"
|
||||
responses:
|
||||
"202":
|
||||
description: NAT gateway creation accepted
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/NATGateway"
|
||||
"400":
|
||||
description: Missing required field, invalid request body, ip without prefix length or not IPv4, or gateway not IPv4
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Error"
|
||||
"409":
|
||||
description: NAT gateway already exists, VPC unknown on this agent or not running, VPC without l3vni, or VPC that already has a NAT gateway on this agent
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Error"
|
||||
"500":
|
||||
$ref: "#/components/responses/InternalError"
|
||||
|
||||
/nat-gateways/{name}:
|
||||
parameters:
|
||||
- $ref: "#/components/parameters/ResourceName"
|
||||
|
||||
get:
|
||||
summary: Get NAT gateway status and info
|
||||
operationId: getNATGateway
|
||||
responses:
|
||||
"200":
|
||||
description: NAT gateway found
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/NATGateway"
|
||||
"404":
|
||||
$ref: "#/components/responses/NotFound"
|
||||
"500":
|
||||
$ref: "#/components/responses/InternalError"
|
||||
|
||||
delete:
|
||||
summary: Delete a NAT gateway
|
||||
operationId: deleteNATGateway
|
||||
responses:
|
||||
"202":
|
||||
description: NAT gateway deletion accepted
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/NATGateway"
|
||||
"404":
|
||||
$ref: "#/components/responses/NotFound"
|
||||
"409":
|
||||
description: NAT gateway not deletable — only running or error states can be deleted
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Error"
|
||||
"500":
|
||||
$ref: "#/components/responses/InternalError"
|
||||
|
||||
components:
|
||||
|
||||
parameters:
|
||||
|
|
@ -626,6 +721,46 @@ components:
|
|||
type: boolean
|
||||
example: false
|
||||
|
||||
NATGatewayCreateRequest:
|
||||
type: object
|
||||
required: [name, vpc, ip, gateway]
|
||||
properties:
|
||||
name:
|
||||
type: string
|
||||
example: ng-42
|
||||
vpc:
|
||||
type: string
|
||||
description: VPC of the gateway; must be running on this agent and have an l3vni
|
||||
example: vp-42
|
||||
ip:
|
||||
type: string
|
||||
description: Public exit IP of the VPC with its prefix length, chosen by the caller and not checked by the agent
|
||||
example: "198.51.100.50/24"
|
||||
gateway:
|
||||
type: string
|
||||
description: Gateway of the border routers on the public segment, not checked by the agent
|
||||
example: "198.51.100.1"
|
||||
|
||||
NATGateway:
|
||||
type: object
|
||||
properties:
|
||||
name:
|
||||
type: string
|
||||
example: ng-42
|
||||
state:
|
||||
type: string
|
||||
enum: [creating, running, error, deleting, deleted]
|
||||
example: running
|
||||
vpc:
|
||||
type: string
|
||||
example: vp-42
|
||||
ip:
|
||||
type: string
|
||||
example: "198.51.100.50/24"
|
||||
gateway:
|
||||
type: string
|
||||
example: "198.51.100.1"
|
||||
|
||||
Error:
|
||||
type: object
|
||||
properties:
|
||||
|
|
|
|||
|
|
@ -82,3 +82,18 @@ type VM struct {
|
|||
type ErrorResponse struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
type NATGatewayCreateRequest struct {
|
||||
Name string `json:"name"`
|
||||
VPC string `json:"vpc"`
|
||||
IP string `json:"ip"`
|
||||
Gateway string `json:"gateway"`
|
||||
}
|
||||
|
||||
type NATGateway struct {
|
||||
Name string `json:"name"`
|
||||
State string `json:"state"`
|
||||
VPC string `json:"vpc,omitempty"`
|
||||
IP string `json:"ip,omitempty"`
|
||||
Gateway string `json:"gateway,omitempty"`
|
||||
}
|
||||
|
|
|
|||
65
internal/api/agent/natgw.go
Normal file
65
internal/api/agent/natgw.go
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
package agentapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
dispatcher "git.g3e.fr/syonad/two/internal/dispatcher/agent"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
)
|
||||
|
||||
func (s *Server) NATGatewayByNameHandler(w http.ResponseWriter, r *http.Request) {
|
||||
name := strings.TrimPrefix(r.URL.Path, "/nat-gateways/")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if name == "" {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "resource not found"})
|
||||
return
|
||||
}
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
s.getNATGateway(w, r, name)
|
||||
case http.MethodDelete:
|
||||
s.deleteNATGateway(w, r, name)
|
||||
default:
|
||||
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) getNATGateway(w http.ResponseWriter, _ *http.Request, name string) {
|
||||
prefix := "nat-gateway/" + name + "/"
|
||||
state, err := kv.GetFromDB(s.db, prefix+"state")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "nat gateway not found"})
|
||||
return
|
||||
}
|
||||
vpc, _ := kv.GetFromDB(s.db, prefix+"vpc")
|
||||
ip, _ := kv.GetFromDB(s.db, prefix+"ip")
|
||||
gateway, _ := kv.GetFromDB(s.db, prefix+"gateway")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(NATGateway{Name: name, State: state, VPC: vpc, IP: ip, Gateway: gateway})
|
||||
}
|
||||
|
||||
func (s *Server) deleteNATGateway(w http.ResponseWriter, _ *http.Request, name string) {
|
||||
cmd := dispatcher.DeleteNATGatewayCommand{Name: name}
|
||||
if err := s.dispatcher.Prepare(cmd); err != nil {
|
||||
if _, dbErr := kv.GetFromDB(s.db, "nat-gateway/"+name+"/state"); dbErr != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusConflict)
|
||||
}
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()})
|
||||
return
|
||||
}
|
||||
s.dispatcher.Dispatch(cmd)
|
||||
state, err := kv.GetFromDB(s.db, "nat-gateway/"+name+"/state")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "failed to read nat gateway state"})
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
json.NewEncoder(w).Encode(NATGateway{Name: name, State: state})
|
||||
}
|
||||
168
internal/api/agent/natgw_test.go
Normal file
168
internal/api/agent/natgw_test.go
Normal file
|
|
@ -0,0 +1,168 @@
|
|||
package agentapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
||||
func seedRoutedVPC(t *testing.T, db *badger.DB) {
|
||||
t.Helper()
|
||||
kv.AddInDB(db, "vpc/vp-s7/state", "running")
|
||||
kv.AddInDB(db, "vpc/vp-s7/cidr", "10.241.0.0/16")
|
||||
kv.AddInDB(db, "vpc/vp-s7/l3vni", "2700")
|
||||
}
|
||||
|
||||
func postNATGateway(t *testing.T, s *Server, req NATGatewayCreateRequest) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
body, _ := json.Marshal(req)
|
||||
w := httptest.NewRecorder()
|
||||
s.NATGatewaysHandler(w, httptest.NewRequest(http.MethodPost, "/nat-gateways", bytes.NewReader(body)))
|
||||
return w
|
||||
}
|
||||
|
||||
func validNATGatewayRequest() NATGatewayCreateRequest {
|
||||
return NATGatewayCreateRequest{Name: "ng-1", VPC: "vp-s7", IP: "198.51.100.50/24", Gateway: "198.51.100.1"}
|
||||
}
|
||||
|
||||
func TestPostNATGateway_Accepted(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
seedRoutedVPC(t, db)
|
||||
w := postNATGateway(t, s, validNATGatewayRequest())
|
||||
if w.Code != http.StatusAccepted {
|
||||
t.Fatalf("attendu 202, obtenu %d : %s", w.Code, w.Body.String())
|
||||
}
|
||||
var got NATGateway
|
||||
json.NewDecoder(w.Body).Decode(&got)
|
||||
want := NATGateway{Name: "ng-1", State: "creating", VPC: "vp-s7", IP: "198.51.100.50/24", Gateway: "198.51.100.1"}
|
||||
if got != want {
|
||||
t.Errorf("réponse %+v, attendu %+v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostNATGateway_BadRequest(t *testing.T) {
|
||||
cases := []struct {
|
||||
label string
|
||||
mutate func(*NATGatewayCreateRequest)
|
||||
msg string
|
||||
}{
|
||||
{"nom absent", func(r *NATGatewayCreateRequest) { r.Name = "" }, "name is required"},
|
||||
{"vpc absent", func(r *NATGatewayCreateRequest) { r.VPC = "" }, "vpc is required"},
|
||||
{"ip absente", func(r *NATGatewayCreateRequest) { r.IP = "" }, "ip is required"},
|
||||
{"gateway absente", func(r *NATGatewayCreateRequest) { r.Gateway = "" }, "gateway is required"},
|
||||
{"ip sans préfixe", func(r *NATGatewayCreateRequest) { r.IP = "198.51.100.50" }, "invalid ip"},
|
||||
{"ip IPv6", func(r *NATGatewayCreateRequest) { r.IP = "2001:db8::50/64" }, "invalid ip"},
|
||||
{"gateway invalide", func(r *NATGatewayCreateRequest) { r.Gateway = "bordure" }, "invalid gateway"},
|
||||
{"gateway IPv6", func(r *NATGatewayCreateRequest) { r.Gateway = "2001:db8::1" }, "invalid gateway"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
s, db := newTestServer(t)
|
||||
seedRoutedVPC(t, db)
|
||||
req := validNATGatewayRequest()
|
||||
c.mutate(&req)
|
||||
w := postNATGateway(t, s, req)
|
||||
if w.Code != http.StatusBadRequest || !strings.Contains(w.Body.String(), c.msg) {
|
||||
t.Errorf("%s : %d %s, 400 « %s » attendu", c.label, w.Code, w.Body.String(), c.msg)
|
||||
}
|
||||
if _, err := kv.GetFromDB(db, "nat-gateway/ng-1/state"); err == nil {
|
||||
t.Errorf("%s : passerelle écrite malgré le refus", c.label)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostNATGateway_InvalidBody(t *testing.T) {
|
||||
s, _ := newTestServer(t)
|
||||
w := httptest.NewRecorder()
|
||||
s.NATGatewaysHandler(w, httptest.NewRequest(http.MethodPost, "/nat-gateways", bytes.NewReader([]byte("not json"))))
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("attendu 400, obtenu %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostNATGateway_ConflictWithoutRunningRoutedVPC(t *testing.T) {
|
||||
s, _ := newTestServer(t)
|
||||
w := postNATGateway(t, s, validNATGatewayRequest())
|
||||
if w.Code != http.StatusConflict || !strings.Contains(w.Body.String(), `vpc \"vp-s7\" not found`) {
|
||||
t.Errorf("%d %s, 409 attendu", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestListNATGateways(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "nat-gateway/ng-1/state", "running")
|
||||
kv.AddInDB(db, "nat-gateway/ng-1/vpc", "vp-s7")
|
||||
kv.AddInDB(db, "nat-gateway/ng-1/ip", "198.51.100.50/24")
|
||||
kv.AddInDB(db, "nat-gateway/ng-1/gateway", "198.51.100.1")
|
||||
w := httptest.NewRecorder()
|
||||
s.NATGatewaysHandler(w, httptest.NewRequest(http.MethodGet, "/nat-gateways", nil))
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("attendu 200, obtenu %d", w.Code)
|
||||
}
|
||||
var got []NATGateway
|
||||
json.NewDecoder(w.Body).Decode(&got)
|
||||
want := NATGateway{Name: "ng-1", State: "running", VPC: "vp-s7", IP: "198.51.100.50/24", Gateway: "198.51.100.1"}
|
||||
if len(got) != 1 || got[0] != want {
|
||||
t.Errorf("liste %+v, attendu [%+v]", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListNATGateways_Empty(t *testing.T) {
|
||||
s, _ := newTestServer(t)
|
||||
w := httptest.NewRecorder()
|
||||
s.NATGatewaysHandler(w, httptest.NewRequest(http.MethodGet, "/nat-gateways", nil))
|
||||
if w.Code != http.StatusOK || strings.TrimSpace(w.Body.String()) != "[]" {
|
||||
t.Errorf("%d %q, 200 [] attendu", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetNATGateway(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "nat-gateway/ng-1/state", "running")
|
||||
kv.AddInDB(db, "nat-gateway/ng-1/vpc", "vp-s7")
|
||||
w := httptest.NewRecorder()
|
||||
s.NATGatewayByNameHandler(w, httptest.NewRequest(http.MethodGet, "/nat-gateways/ng-1", nil))
|
||||
var got NATGateway
|
||||
json.NewDecoder(w.Body).Decode(&got)
|
||||
if w.Code != http.StatusOK || got.Name != "ng-1" || got.State != "running" || got.VPC != "vp-s7" {
|
||||
t.Errorf("%d %+v", w.Code, got)
|
||||
}
|
||||
w = httptest.NewRecorder()
|
||||
s.NATGatewayByNameHandler(w, httptest.NewRequest(http.MethodGet, "/nat-gateways/ng-x", nil))
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Errorf("passerelle absente : %d, 404 attendu", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteNATGateway(t *testing.T) {
|
||||
cases := map[string]int{"running": http.StatusAccepted, "error": http.StatusAccepted, "creating": http.StatusConflict, "": http.StatusNotFound}
|
||||
for st, want := range cases {
|
||||
s, db := newTestServer(t)
|
||||
if st != "" {
|
||||
kv.AddInDB(db, "nat-gateway/ng-1/state", st)
|
||||
kv.AddInDB(db, "nat-gateway/ng-1/vpc", "vp-s7")
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
s.NATGatewayByNameHandler(w, httptest.NewRequest(http.MethodDelete, "/nat-gateways/ng-1", nil))
|
||||
if w.Code != want {
|
||||
t.Errorf("état %q : %d, %d attendu (%s)", st, w.Code, want, w.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteVpc_BlockedByNATGateway(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
seedRoutedVPC(t, db)
|
||||
kv.AddInDB(db, "nat-gateway/ng-1/state", "running")
|
||||
kv.AddInDB(db, "nat-gateway/ng-1/vpc", "vp-s7")
|
||||
w := httptest.NewRecorder()
|
||||
s.VpcByNameHandler(w, httptest.NewRequest(http.MethodDelete, "/vpcs/vp-s7", nil))
|
||||
if w.Code != http.StatusConflict || !strings.Contains(w.Body.String(), `nat gateway \"ng-1\" must be deleted`) {
|
||||
t.Errorf("%d %s, 409 attendu", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
100
internal/api/agent/natgws.go
Normal file
100
internal/api/agent/natgws.go
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
package agentapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
dispatcher "git.g3e.fr/syonad/two/internal/dispatcher/agent"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
)
|
||||
|
||||
func (s *Server) NATGatewaysHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
s.listNATGateways(w, r)
|
||||
case http.MethodPost:
|
||||
s.postNATGateway(w, r)
|
||||
default:
|
||||
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) listNATGateways(w http.ResponseWriter, _ *http.Request) {
|
||||
entries, err := kv.ListByPrefix(s.db, "nat-gateway/")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "failed to list nat gateways"})
|
||||
return
|
||||
}
|
||||
gateways := make(map[string]*NATGateway)
|
||||
for key, value := range entries {
|
||||
parts := strings.Split(key, "/")
|
||||
if len(parts) != 3 {
|
||||
continue
|
||||
}
|
||||
name := parts[1]
|
||||
if _, ok := gateways[name]; !ok {
|
||||
gateways[name] = &NATGateway{Name: name}
|
||||
}
|
||||
switch parts[2] {
|
||||
case "state":
|
||||
gateways[name].State = value
|
||||
case "vpc":
|
||||
gateways[name].VPC = value
|
||||
case "ip":
|
||||
gateways[name].IP = value
|
||||
case "gateway":
|
||||
gateways[name].Gateway = value
|
||||
}
|
||||
}
|
||||
result := make([]NATGateway, 0, len(gateways))
|
||||
for _, g := range gateways {
|
||||
result = append(result, *g)
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(result)
|
||||
}
|
||||
|
||||
func (s *Server) postNATGateway(w http.ResponseWriter, r *http.Request) {
|
||||
var req NATGatewayCreateRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "invalid request body"})
|
||||
return
|
||||
}
|
||||
for _, f := range []struct{ name, value string }{{"name", req.Name}, {"vpc", req.VPC}, {"ip", req.IP}, {"gateway", req.Gateway}} {
|
||||
if f.value == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: f.name + " is required"})
|
||||
return
|
||||
}
|
||||
}
|
||||
if ip, _, err := net.ParseCIDR(req.IP); err != nil || ip.To4() == nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "invalid ip: an IPv4 address with its prefix length is expected"})
|
||||
return
|
||||
}
|
||||
if gw := net.ParseIP(req.Gateway); gw == nil || gw.To4() == nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "invalid gateway: an IPv4 address is expected"})
|
||||
return
|
||||
}
|
||||
cmd := dispatcher.CreateNATGatewayCommand{Name: req.Name, VPC: req.VPC, IP: req.IP, Gateway: req.Gateway}
|
||||
if err := s.dispatcher.Prepare(cmd); err != nil {
|
||||
w.WriteHeader(http.StatusConflict)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()})
|
||||
return
|
||||
}
|
||||
s.dispatcher.Dispatch(cmd)
|
||||
state, err := kv.GetFromDB(s.db, "nat-gateway/"+req.Name+"/state")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "failed to read nat gateway state"})
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
json.NewEncoder(w).Encode(NATGateway{Name: req.Name, State: state, VPC: req.VPC, IP: req.IP, Gateway: req.Gateway})
|
||||
}
|
||||
|
|
@ -29,6 +29,8 @@ func New(d *dispatcher.Dispatcher, db *badger.DB, logger *slog.Logger, address s
|
|||
mux.HandleFunc("/subnets/", s.SubnetByNameHandler)
|
||||
mux.HandleFunc("/vms", s.VmsHandler)
|
||||
mux.HandleFunc("/vms/", s.VmByNameHandler)
|
||||
mux.HandleFunc("/nat-gateways", s.NATGatewaysHandler)
|
||||
mux.HandleFunc("/nat-gateways/", s.NATGatewayByNameHandler)
|
||||
s.srv = &http.Server{Addr: address, Handler: s.logMiddleware(mux)}
|
||||
return s
|
||||
}
|
||||
|
|
|
|||
125
internal/dispatcher/agent/natgw_commands.go
Normal file
125
internal/dispatcher/agent/natgw_commands.go
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
package dispatcher
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||
"git.g3e.fr/syonad/two/internal/natgw"
|
||||
"git.g3e.fr/syonad/two/internal/state"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
||||
const natGatewayPrefix = "nat-gateway/"
|
||||
|
||||
type CreateNATGatewayCommand struct {
|
||||
Name string
|
||||
VPC string
|
||||
IP string
|
||||
Gateway string
|
||||
}
|
||||
|
||||
func (c CreateNATGatewayCommand) Key() string { return natGatewayPrefix + c.Name }
|
||||
|
||||
func (c CreateNATGatewayCommand) Prepare(db *badger.DB, _ *configuration.Config) error {
|
||||
if _, err := kv.GetFromDB(db, c.Key()+"/state"); err == nil {
|
||||
return fmt.Errorf("nat gateway %q already exists", c.Name)
|
||||
}
|
||||
if _, _, err := parseNATGateway(c.IP, c.Gateway); err != nil {
|
||||
return err
|
||||
}
|
||||
vpcState, err := state.Get(db, "vpc/"+c.VPC)
|
||||
if err != nil {
|
||||
return fmt.Errorf("vpc %q not found", c.VPC)
|
||||
}
|
||||
if vpcState != state.Running {
|
||||
return fmt.Errorf("vpc %q is %s", c.VPC, vpcState)
|
||||
}
|
||||
if _, err := kv.GetFromDB(db, "vpc/"+c.VPC+"/l3vni"); err != nil {
|
||||
return fmt.Errorf("vpc %q has no l3vni", c.VPC)
|
||||
}
|
||||
other, err := natGatewayOf(db, c.VPC)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if other != "" {
|
||||
return fmt.Errorf("vpc %q already has nat gateway %q", c.VPC, other)
|
||||
}
|
||||
for key, value := range map[string]string{"vpc": c.VPC, "ip": c.IP, "gateway": c.Gateway} {
|
||||
if err := kv.AddInDB(db, c.Key()+"/"+key, value); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return state.Set(db, c.Key(), state.Creating)
|
||||
}
|
||||
|
||||
func (c CreateNATGatewayCommand) Execute(db *badger.DB, _ *configuration.Config) error {
|
||||
ip, gw, err := parseNATGateway(c.IP, c.Gateway)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := natgw.Create(db, c.VPC, ip, gw); err != nil {
|
||||
return err
|
||||
}
|
||||
return state.Set(db, c.Key(), state.Running)
|
||||
}
|
||||
|
||||
type DeleteNATGatewayCommand struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func (c DeleteNATGatewayCommand) Key() string { return natGatewayPrefix + c.Name }
|
||||
|
||||
func (c DeleteNATGatewayCommand) Prepare(db *badger.DB, _ *configuration.Config) error {
|
||||
current, err := state.Get(db, c.Key())
|
||||
if err != nil {
|
||||
return fmt.Errorf("nat gateway %q not found", c.Name)
|
||||
}
|
||||
if !state.CanDelete(current) {
|
||||
return fmt.Errorf("nat gateway %q cannot be deleted while %s", c.Name, current)
|
||||
}
|
||||
return state.Set(db, c.Key(), state.Deleting)
|
||||
}
|
||||
|
||||
func (c DeleteNATGatewayCommand) Execute(db *badger.DB, _ *configuration.Config) error {
|
||||
vpcName, err := kv.GetFromDB(db, c.Key()+"/vpc")
|
||||
if err != nil {
|
||||
return fmt.Errorf("nat gateway %q: reading vpc: %w", c.Name, err)
|
||||
}
|
||||
if err := natgw.Delete(db, vpcName); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := state.Set(db, c.Key(), state.Deleted); err != nil {
|
||||
return err
|
||||
}
|
||||
return kv.DeleteInDB(db, c.Key())
|
||||
}
|
||||
|
||||
func parseNATGateway(ip, gateway string) (*net.IPNet, net.IP, error) {
|
||||
addr, prefix, err := net.ParseCIDR(ip)
|
||||
if err != nil || addr.To4() == nil {
|
||||
return nil, nil, fmt.Errorf("invalid ip %q: an IPv4 address with its prefix length is expected", ip)
|
||||
}
|
||||
prefix.IP = addr.To4()
|
||||
gw := net.ParseIP(gateway).To4()
|
||||
if gw == nil {
|
||||
return nil, nil, fmt.Errorf("invalid gateway %q: an IPv4 address is expected", gateway)
|
||||
}
|
||||
return prefix, gw, nil
|
||||
}
|
||||
|
||||
func natGatewayOf(db *badger.DB, vpcName string) (string, error) {
|
||||
entries, err := kv.ListByPrefix(db, natGatewayPrefix)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to list nat gateways: %w", err)
|
||||
}
|
||||
for key, value := range entries {
|
||||
name, ok := strings.CutSuffix(strings.TrimPrefix(key, natGatewayPrefix), "/vpc")
|
||||
if ok && !strings.Contains(name, "/") && value == vpcName {
|
||||
return name, nil
|
||||
}
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
143
internal/dispatcher/agent/natgw_commands_test.go
Normal file
143
internal/dispatcher/agent/natgw_commands_test.go
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
package dispatcher
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
||||
func seedRoutedVPC(t *testing.T, db *badger.DB, name, st string) {
|
||||
t.Helper()
|
||||
kv.AddInDB(db, "vpc/"+name+"/state", st)
|
||||
kv.AddInDB(db, "vpc/"+name+"/cidr", "10.241.0.0/16")
|
||||
kv.AddInDB(db, "vpc/"+name+"/l3vni", "2700")
|
||||
}
|
||||
|
||||
func validNATGateway() CreateNATGatewayCommand {
|
||||
return CreateNATGatewayCommand{Name: "ng-1", VPC: "vp-s7", IP: "198.51.100.50/24", Gateway: "198.51.100.1"}
|
||||
}
|
||||
|
||||
func TestCreateNATGatewayCommand_Prepare_WritesTheGateway(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
seedRoutedVPC(t, db, "vp-s7", "running")
|
||||
if err := validNATGateway().Prepare(db, nil); err != nil {
|
||||
t.Fatalf("Prepare a échoué : %v", err)
|
||||
}
|
||||
for key, want := range map[string]string{
|
||||
"nat-gateway/ng-1/state": "creating",
|
||||
"nat-gateway/ng-1/vpc": "vp-s7",
|
||||
"nat-gateway/ng-1/ip": "198.51.100.50/24",
|
||||
"nat-gateway/ng-1/gateway": "198.51.100.1",
|
||||
} {
|
||||
got, err := kv.GetFromDB(db, key)
|
||||
if err != nil || got != want {
|
||||
t.Errorf("%s : %q (%v), %q attendu", key, got, err, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateNATGatewayCommand_Prepare_Refuses(t *testing.T) {
|
||||
cases := []struct {
|
||||
label string
|
||||
seed func(*badger.DB)
|
||||
mutate func(*CreateNATGatewayCommand)
|
||||
msg string
|
||||
}{
|
||||
{"passerelle existante", func(db *badger.DB) { kv.AddInDB(db, "nat-gateway/ng-1/state", "running") }, nil, `nat gateway "ng-1" already exists`},
|
||||
{"VPC absent", nil, func(c *CreateNATGatewayCommand) { c.VPC = "vp-absent" }, `vpc "vp-absent" not found`},
|
||||
{"VPC en création", func(db *badger.DB) { seedRoutedVPC(t, db, "vp-new", "creating") }, func(c *CreateNATGatewayCommand) { c.VPC = "vp-new" }, `vpc "vp-new" is creating`},
|
||||
{"VPC sans l3vni", func(db *badger.DB) {
|
||||
kv.AddInDB(db, "vpc/vp-flat/state", "running")
|
||||
kv.AddInDB(db, "vpc/vp-flat/cidr", "10.242.0.0/16")
|
||||
}, func(c *CreateNATGatewayCommand) { c.VPC = "vp-flat" }, `vpc "vp-flat" has no l3vni`},
|
||||
{"VPC qui a déjà une passerelle", func(db *badger.DB) {
|
||||
kv.AddInDB(db, "nat-gateway/ng-0/state", "error")
|
||||
kv.AddInDB(db, "nat-gateway/ng-0/vpc", "vp-s7")
|
||||
}, nil, `vpc "vp-s7" already has nat gateway "ng-0"`},
|
||||
{"ip sans longueur de préfixe", nil, func(c *CreateNATGatewayCommand) { c.IP = "198.51.100.50" }, `invalid ip "198.51.100.50"`},
|
||||
{"ip IPv6", nil, func(c *CreateNATGatewayCommand) { c.IP = "2001:db8::50/64" }, `invalid ip "2001:db8::50/64"`},
|
||||
{"gateway invalide", nil, func(c *CreateNATGatewayCommand) { c.Gateway = "bordure" }, `invalid gateway "bordure"`},
|
||||
}
|
||||
for _, c := range cases {
|
||||
_, db := newTestDispatcher(t)
|
||||
seedRoutedVPC(t, db, "vp-s7", "running")
|
||||
if c.seed != nil {
|
||||
c.seed(db)
|
||||
}
|
||||
cmd := validNATGateway()
|
||||
if c.mutate != nil {
|
||||
c.mutate(&cmd)
|
||||
}
|
||||
err := cmd.Prepare(db, nil)
|
||||
if err == nil || !strings.Contains(err.Error(), c.msg) {
|
||||
t.Errorf("%s : erreur %v, « %s » attendu", c.label, err, c.msg)
|
||||
continue
|
||||
}
|
||||
if c.label != "passerelle existante" {
|
||||
if _, err := kv.GetFromDB(db, "nat-gateway/ng-1/state"); err == nil {
|
||||
t.Errorf("%s : état écrit malgré le refus", c.label)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateNATGatewayCommand_Prepare_IgnoresTheGatewayOfAnotherVPC(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
seedRoutedVPC(t, db, "vp-s7", "running")
|
||||
kv.AddInDB(db, "nat-gateway/ng-0/state", "running")
|
||||
kv.AddInDB(db, "nat-gateway/ng-0/vpc", "vp-s70")
|
||||
if err := validNATGateway().Prepare(db, nil); err != nil {
|
||||
t.Errorf("Prepare a échoué : %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteNATGatewayCommand_Prepare(t *testing.T) {
|
||||
for st, ok := range map[string]bool{"running": true, "error": true, "creating": false, "deleting": false} {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "nat-gateway/ng-1/state", st)
|
||||
err := DeleteNATGatewayCommand{Name: "ng-1"}.Prepare(db, nil)
|
||||
if ok != (err == nil) {
|
||||
t.Errorf("état %s : erreur %v", st, err)
|
||||
}
|
||||
got, _ := kv.GetFromDB(db, "nat-gateway/ng-1/state")
|
||||
if ok && got != "deleting" {
|
||||
t.Errorf("état %s : %q après Prepare, deleting attendu", st, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteNATGatewayCommand_Prepare_Unknown(t *testing.T) {
|
||||
_, db := newTestDispatcher(t)
|
||||
err := DeleteNATGatewayCommand{Name: "ng-x"}.Prepare(db, nil)
|
||||
if err == nil || !strings.Contains(err.Error(), `nat gateway "ng-x" not found`) {
|
||||
t.Errorf("erreur %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteVPCCommand_Prepare_RefusesWhileANATGatewayExists(t *testing.T) {
|
||||
for _, st := range []string{"running", "error", "creating", "deleting"} {
|
||||
_, db := newTestDispatcher(t)
|
||||
kv.AddInDB(db, "vpc/vp-s7/state", "running")
|
||||
kv.AddInDB(db, "nat-gateway/ng-1/state", st)
|
||||
kv.AddInDB(db, "nat-gateway/ng-1/vpc", "vp-s7")
|
||||
err := DeleteVPCCommand{Name: "vp-s7"}.Prepare(db, nil)
|
||||
if err == nil || !strings.Contains(err.Error(), `nat gateway "ng-1" must be deleted before deleting vpc "vp-s7"`) {
|
||||
t.Errorf("passerelle %s : erreur %v", st, err)
|
||||
}
|
||||
if got, _ := kv.GetFromDB(db, "vpc/vp-s7/state"); got != "running" {
|
||||
t.Errorf("passerelle %s : VPC passé en %q malgré le refus", st, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseNATGatewayKeepsTheHostAddress(t *testing.T) {
|
||||
ip, gw, err := parseNATGateway("198.51.100.50/24", "198.51.100.1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ip.String() != "198.51.100.50/24" || gw.String() != "198.51.100.1" {
|
||||
t.Errorf("ip %s, gateway %s", ip, gw)
|
||||
}
|
||||
}
|
||||
|
|
@ -126,6 +126,13 @@ func (c DeleteVPCCommand) Prepare(db *badger.DB, _ *configuration.Config) error
|
|||
return fmt.Errorf("subnet %q must be deleted before deleting vpc %q", subnetName, c.Name)
|
||||
}
|
||||
}
|
||||
gateway, err := natGatewayOf(db, c.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if gateway != "" {
|
||||
return fmt.Errorf("nat gateway %q must be deleted before deleting vpc %q", gateway, c.Name)
|
||||
}
|
||||
return state.Set(db, c.Key(), state.Deleting)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ import (
|
|||
)
|
||||
|
||||
// prefixes énumère les familles de ressources portant une clé /state.
|
||||
var prefixes = []string{"vpc/", "subnet/", "vm/"}
|
||||
var prefixes = []string{"vpc/", "subnet/", "vm/", "nat-gateway/"}
|
||||
|
||||
// legacyStates traduit l'ancien vocabulaire (états VPC/subnet d'avant
|
||||
// l'unification, et états VM start/stop) vers l'enum courant.
|
||||
|
|
|
|||
|
|
@ -38,6 +38,9 @@ func TestMigrateStates_LegacyMapping(t *testing.T) {
|
|||
"vm stopping": {"vm/vm-4", "stopping", state.Error},
|
||||
"vpc creating": {"vpc/vpc-2", "creating", state.Error},
|
||||
"subnet deleting": {"subnet/sn-2", "deleting", state.Error},
|
||||
"nat creating": {"nat-gateway/ng-1", "creating", state.Error},
|
||||
"nat deleting": {"nat-gateway/ng-2", "deleting", state.Error},
|
||||
"nat running": {"nat-gateway/ng-3", "running", state.Running},
|
||||
}
|
||||
for _, c := range cases {
|
||||
kv.AddInDB(db, c.resource+"/state", c.from)
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ type AgentCollector struct {
|
|||
vpcsTotal *prometheus.Desc
|
||||
subnetsTotal *prometheus.Desc
|
||||
vmsTotal *prometheus.Desc
|
||||
natGateways *prometheus.Desc
|
||||
}
|
||||
|
||||
func NewAgentCollector(db *badger.DB) *AgentCollector {
|
||||
|
|
@ -38,6 +39,11 @@ func NewAgentCollector(db *badger.DB) *AgentCollector {
|
|||
"Number of VMs by state.",
|
||||
[]string{"state"}, nil,
|
||||
),
|
||||
natGateways: prometheus.NewDesc(
|
||||
"syonad_nat_gateways_total",
|
||||
"Number of NAT gateways by state.",
|
||||
[]string{"state"}, nil,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -45,12 +51,14 @@ func (c *AgentCollector) Describe(ch chan<- *prometheus.Desc) {
|
|||
ch <- c.vpcsTotal
|
||||
ch <- c.subnetsTotal
|
||||
ch <- c.vmsTotal
|
||||
ch <- c.natGateways
|
||||
}
|
||||
|
||||
func (c *AgentCollector) Collect(ch chan<- prometheus.Metric) {
|
||||
c.collectStates(ch, "vpc/", c.vpcsTotal)
|
||||
c.collectStates(ch, "subnet/", c.subnetsTotal)
|
||||
c.collectStates(ch, "vm/", c.vmsTotal)
|
||||
c.collectStates(ch, "nat-gateway/", c.natGateways)
|
||||
}
|
||||
|
||||
// collectStates counts resources under the given DB prefix by their state value
|
||||
|
|
|
|||
|
|
@ -44,6 +44,8 @@ func collect(t *testing.T, c *AgentCollector) map[string]map[string]float64 {
|
|||
name = "subnets"
|
||||
case strings.Contains(desc, "syonad_vms_total"):
|
||||
name = "vms"
|
||||
case strings.Contains(desc, "syonad_nat_gateways_total"):
|
||||
name = "nat_gateways"
|
||||
default:
|
||||
t.Fatalf("métrique inattendue : %s", desc)
|
||||
}
|
||||
|
|
@ -62,12 +64,13 @@ func collect(t *testing.T, c *AgentCollector) map[string]map[string]float64 {
|
|||
func TestCollector_CountsByState(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
for resource, s := range map[string]state.State{
|
||||
"vpc/vpc-1": state.Running,
|
||||
"vpc/vpc-2": state.Running,
|
||||
"vpc/vpc-3": state.Error,
|
||||
"subnet/sn-1": state.Creating,
|
||||
"vm/vm-1": state.Running,
|
||||
"vm/vm-2": state.Error,
|
||||
"vpc/vpc-1": state.Running,
|
||||
"vpc/vpc-2": state.Running,
|
||||
"vpc/vpc-3": state.Error,
|
||||
"subnet/sn-1": state.Creating,
|
||||
"vm/vm-1": state.Running,
|
||||
"vm/vm-2": state.Error,
|
||||
"nat-gateway/ng-1": state.Running,
|
||||
} {
|
||||
if err := state.Set(db, resource, s); err != nil {
|
||||
t.Fatalf("préparation du test : %v", err)
|
||||
|
|
@ -89,6 +92,9 @@ func TestCollector_CountsByState(t *testing.T) {
|
|||
t.Errorf("vms{state=%q} = %v, attendu %v", s, got["vms"][s], want)
|
||||
}
|
||||
}
|
||||
if got["nat_gateways"]["running"] != 1 || got["vpcs"]["running"] != 2 {
|
||||
t.Errorf("nat_gateways{state=running} = %v, vpcs{state=running} = %v, attendu 1 et 2", got["nat_gateways"]["running"], got["vpcs"]["running"])
|
||||
}
|
||||
}
|
||||
|
||||
// Les clés vm/<name>/disk/<dev> ne portent pas de suffixe /state : elles ne
|
||||
|
|
@ -111,7 +117,7 @@ func TestCollector_IgnoresVMDiskKeys(t *testing.T) {
|
|||
// côté Prometheus casse les alertes qui s'en servent.
|
||||
func TestCollector_EmitsEveryState(t *testing.T) {
|
||||
got := collect(t, NewAgentCollector(newTestDB(t)))
|
||||
for _, family := range []string{"vpcs", "subnets", "vms"} {
|
||||
for _, family := range []string{"vpcs", "subnets", "vms", "nat_gateways"} {
|
||||
if len(got[family]) != len(state.All()) {
|
||||
t.Errorf("%s : %d séries, attendu %d", family, len(got[family]), len(state.All()))
|
||||
}
|
||||
|
|
@ -145,7 +151,7 @@ func TestCollector_Describe(t *testing.T) {
|
|||
for range ch {
|
||||
n++
|
||||
}
|
||||
if n != 3 {
|
||||
t.Errorf("%d descripteurs, attendu 3", n)
|
||||
if n != 4 {
|
||||
t.Errorf("%d descripteurs, attendu 4", n)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -232,6 +232,20 @@ func expectedExports(db *badger.DB, vpcName string) (expected, tolerated map[str
|
|||
classify(st, ip.String()+"/32")
|
||||
}
|
||||
}
|
||||
|
||||
gateways, err := kv.ListByPrefix(db, prefixNATGW)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("watchdog: listing nat gateways: %w", err)
|
||||
}
|
||||
for key, value := range gateways {
|
||||
name, ok := strings.CutSuffix(strings.TrimPrefix(key, prefixNATGW), "/vpc")
|
||||
if !ok || strings.Contains(name, "/") || value != vpcName {
|
||||
continue
|
||||
}
|
||||
if st, err := state.Get(db, prefixNATGW+name); err == nil {
|
||||
classify(st, "0.0.0.0/0")
|
||||
}
|
||||
}
|
||||
return expected, tolerated, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -278,3 +278,41 @@ func TestCheckL3VPCsReportsAVRFWithoutUnreachableDefault(t *testing.T) {
|
|||
t.Errorf("notifications %q", notified(r))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckL3VPCsExpectsTheDefaultRouteOfARunningNATGateway(t *testing.T) {
|
||||
cases := []struct {
|
||||
label string
|
||||
gateway state.State
|
||||
vpc string
|
||||
exported bool
|
||||
want []string
|
||||
}{
|
||||
{"passerelle running, route exportée", state.Running, "vp-s7", true, nil},
|
||||
{"passerelle running, route absente", state.Running, "vp-s7", false,
|
||||
[]string{"vpc vp-s7: route 0.0.0.0/0 not exported in vrf-s7"}},
|
||||
{"passerelle en création, route exportée", state.Creating, "vp-s7", true, nil},
|
||||
{"passerelle en erreur, route absente", state.Error, "vp-s7", false, nil},
|
||||
{"passerelle d'un autre VPC, route exportée", state.Running, "vp-s70", true,
|
||||
[]string{"vpc vp-s7: route 0.0.0.0/0 exported in vrf-s7 without a running owner: its traffic is black-holed"}},
|
||||
}
|
||||
for _, c := range cases {
|
||||
db := newTestDB(t)
|
||||
seedResource(t, db, prefixVPC, "vp-s7", state.Running)
|
||||
seedKV(t, db, "vpc/vp-s7/cidr", "10.241.0.0/16")
|
||||
seedKV(t, db, "vpc/vp-s7/l3vni", "2700")
|
||||
seedResource(t, db, prefixNATGW, "ng-1", c.gateway)
|
||||
seedKV(t, db, "nat-gateway/ng-1/vpc", c.vpc)
|
||||
p := seedRoutedVPC(t)
|
||||
p.routes = map[uint32][]string{2700: nil}
|
||||
if c.exported {
|
||||
p.routes[2700] = []string{"0.0.0.0/0"}
|
||||
}
|
||||
r := &recorder{}
|
||||
if err := CheckL3VPCs(db, bgpCfg(), p, r); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := notified(r); !reflect.DeepEqual(got, c.want) {
|
||||
t.Errorf("%s :\n%q\nattendu :\n%q", c.label, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ const (
|
|||
prefixVPC = "vpc/"
|
||||
prefixSubnet = "subnet/"
|
||||
prefixVM = "vm/"
|
||||
prefixNATGW = "nat-gateway/"
|
||||
|
||||
kindVPC = "vpc"
|
||||
kindSubnet = "subnet"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue