From fdabd943f2e402d57f74fe06d41a33bfad912040 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Thu, 29 Oct 2026 21:48:12 +0100 Subject: [PATCH] f-88: api: ressource /floating-ips, refus et affichage sur la VM #88 Signed-off-by: GnomeZworc --- api/agent.yaml | 162 ++++++++++++++- internal/api/agent/fip.go | 66 ++++++ internal/api/agent/fip_test.go | 195 ++++++++++++++++++ internal/api/agent/fips.go | 102 +++++++++ internal/api/agent/models.go | 25 +++ internal/api/agent/server.go | 2 + internal/api/agent/vm.go | 22 ++ internal/api/agent/vms.go | 12 ++ internal/dispatcher/agent/fip_commands.go | 166 +++++++++++++++ .../dispatcher/agent/fip_commands_test.go | 157 ++++++++++++++ internal/dispatcher/agent/vm_commands.go | 7 + internal/migration/state.go | 2 +- internal/migration/state_test.go | 2 + internal/prometheus/agent/collector.go | 8 + internal/prometheus/agent/collector_test.go | 26 ++- 15 files changed, 942 insertions(+), 12 deletions(-) create mode 100644 internal/api/agent/fip.go create mode 100644 internal/api/agent/fip_test.go create mode 100644 internal/api/agent/fips.go create mode 100644 internal/dispatcher/agent/fip_commands.go create mode 100644 internal/dispatcher/agent/fip_commands_test.go diff --git a/api/agent.yaml b/api/agent.yaml index 47ec4ce..d4a737c 100644 --- a/api/agent.yaml +++ b/api/agent.yaml @@ -189,7 +189,7 @@ paths: "404": $ref: "#/components/responses/NotFound" "409": - description: VM not stoppable — only running or error states can be stopped + description: VM not stoppable — only running or error states can be stopped, and its floating IPs must be deleted first content: application/json: schema: @@ -394,6 +394,101 @@ paths: "500": $ref: "#/components/responses/InternalError" + # ── Floating IP ──────────────────────────────────────────────────────────── + + /floating-ips: + get: + summary: List all floating IPs of this agent + operationId: listFloatingIPs + responses: + "200": + description: List of floating IPs + content: + application/json: + schema: + type: array + items: + $ref: "#/components/schemas/FloatingIP" + "500": + $ref: "#/components/responses/InternalError" + + post: + summary: Attach a floating IP to an interface of a VM of this agent + operationId: createFloatingIP + description: >- + Makes an interface of a VM reachable through a public IP it does not carry: stateless 1:1 + translation in the netns of the VPC, on the host of the VM, the public IP answering ARP on + the public bridge (interfaces.internet). The VM leaves through its floating IP, even in a + VPC with a NAT gateway. To move it with the VM, delete it on the old host, then create it + on the new one. Attach and detach without touching the VM. + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/FloatingIPCreateRequest" + responses: + "202": + description: Floating IP creation accepted + content: + application/json: + schema: + $ref: "#/components/schemas/FloatingIP" + "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: 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 + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + "500": + $ref: "#/components/responses/InternalError" + + /floating-ips/{name}: + parameters: + - $ref: "#/components/parameters/ResourceName" + + get: + summary: Get floating IP status and info + operationId: getFloatingIP + responses: + "200": + description: Floating IP found + content: + application/json: + schema: + $ref: "#/components/schemas/FloatingIP" + "404": + $ref: "#/components/responses/NotFound" + "500": + $ref: "#/components/responses/InternalError" + + delete: + summary: Detach and delete a floating IP + operationId: deleteFloatingIP + responses: + "202": + description: Floating IP deletion accepted + content: + application/json: + schema: + $ref: "#/components/schemas/FloatingIP" + "404": + $ref: "#/components/responses/NotFound" + "409": + description: Floating IP 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: @@ -677,6 +772,24 @@ components: of the subnet: restricting it is the caller's job. Always returned by GET. example: false + floating_ip: + readOnly: true + description: > + Floating IP attached to this interface, computed on read from /floating-ips; absent + when the interface has none. Refused in a creation request (400): a floating IP is + attached with /floating-ips. + type: object + properties: + name: + type: string + example: fip-7 + ip: + type: string + example: "203.0.113.20/24" + state: + type: string + enum: [creating, running, error, deleting, deleted] + example: running VMStorage: type: object @@ -770,6 +883,53 @@ components: type: string example: "198.51.100.1" + FloatingIPCreateRequest: + type: object + required: [name, vm, interface, ip, gateway] + properties: + name: + type: string + example: fip-7 + vm: + type: string + description: VM of the interface; must be running on this agent + example: vm-7 + interface: + type: string + description: Name of the interface of the VM (VMInterface.name), not a Linux interface + example: nic-a1b2 + ip: + type: string + description: Public IP with its prefix length, chosen by the caller and not checked by the agent + example: "203.0.113.20/24" + gateway: + type: string + description: Gateway of the border routers on the public segment, not checked by the agent + example: "203.0.113.1" + + FloatingIP: + type: object + properties: + name: + type: string + example: fip-7 + state: + type: string + enum: [creating, running, error, deleting, deleted] + example: running + vm: + type: string + example: vm-7 + interface: + type: string + example: nic-a1b2 + ip: + type: string + example: "203.0.113.20/24" + gateway: + type: string + example: "203.0.113.1" + Error: type: object properties: diff --git a/internal/api/agent/fip.go b/internal/api/agent/fip.go new file mode 100644 index 0000000..cbb0cb8 --- /dev/null +++ b/internal/api/agent/fip.go @@ -0,0 +1,66 @@ +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) FloatingIPByNameHandler(w http.ResponseWriter, r *http.Request) { + name := strings.TrimPrefix(r.URL.Path, "/floating-ips/") + 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.getFloatingIP(w, r, name) + case http.MethodDelete: + s.deleteFloatingIP(w, r, name) + default: + http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed) + } +} + +func (s *Server) getFloatingIP(w http.ResponseWriter, _ *http.Request, name string) { + prefix := "floating-ip/" + name + "/" + state, err := kv.GetFromDB(s.db, prefix+"state") + if err != nil { + w.WriteHeader(http.StatusNotFound) + json.NewEncoder(w).Encode(ErrorResponse{Error: "floating ip not found"}) + return + } + vm, _ := kv.GetFromDB(s.db, prefix+"vm") + iface, _ := kv.GetFromDB(s.db, prefix+"interface") + ip, _ := kv.GetFromDB(s.db, prefix+"ip") + gateway, _ := kv.GetFromDB(s.db, prefix+"gateway") + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(FloatingIP{Name: name, State: state, VM: vm, Interface: iface, IP: ip, Gateway: gateway}) +} + +func (s *Server) deleteFloatingIP(w http.ResponseWriter, _ *http.Request, name string) { + cmd := dispatcher.DeleteFloatingIPCommand{Name: name} + if err := s.dispatcher.Prepare(cmd); err != nil { + if _, dbErr := kv.GetFromDB(s.db, "floating-ip/"+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, "floating-ip/"+name+"/state") + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + json.NewEncoder(w).Encode(ErrorResponse{Error: "failed to read floating ip state"}) + return + } + w.WriteHeader(http.StatusAccepted) + json.NewEncoder(w).Encode(FloatingIP{Name: name, State: state}) +} diff --git a/internal/api/agent/fip_test.go b/internal/api/agent/fip_test.go new file mode 100644 index 0000000..baeb624 --- /dev/null +++ b/internal/api/agent/fip_test.go @@ -0,0 +1,195 @@ +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 seedRunningVM(t *testing.T, db *badger.DB) { + t.Helper() + for key, value := range map[string]string{ + "vpc/vp-t47/cidr": "10.47.0.0/16", + "subnet/sn-a/vpc": "vp-t47", + "vm/vm-1/state": "running", + "vm/vm-1/nic/0/name": "nic-a", + "vm/vm-1/nic/0/subnet": "sn-a", + "vm/vm-1/nic/0/ip": "10.47.1.10", + "vm/vm-1/nic/1/name": "nic-b", + "vm/vm-1/nic/1/subnet": "sn-a", + "vm/vm-1/nic/1/ip": "10.47.1.11", + } { + kv.AddInDB(db, key, value) + } +} + +func seedFloatingIP(t *testing.T, db *badger.DB, name, vm, iface, st string) { + t.Helper() + kv.AddInDB(db, "floating-ip/"+name+"/state", st) + kv.AddInDB(db, "floating-ip/"+name+"/vm", vm) + kv.AddInDB(db, "floating-ip/"+name+"/interface", iface) + kv.AddInDB(db, "floating-ip/"+name+"/ip", "240.0.0.47/24") + kv.AddInDB(db, "floating-ip/"+name+"/gateway", "240.0.0.1") +} + +func validFloatingIPRequest() FloatingIPCreateRequest { + return FloatingIPCreateRequest{Name: "fip-1", VM: "vm-1", Interface: "nic-a", IP: "240.0.0.47/24", Gateway: "240.0.0.1"} +} + +func postFloatingIP(t *testing.T, s *Server, req FloatingIPCreateRequest) *httptest.ResponseRecorder { + t.Helper() + body, _ := json.Marshal(req) + w := httptest.NewRecorder() + s.FloatingIPsHandler(w, httptest.NewRequest(http.MethodPost, "/floating-ips", bytes.NewReader(body))) + return w +} + +func TestPostFloatingIP_Accepted(t *testing.T) { + s, db := newTestServer(t) + seedRunningVM(t, db) + w := postFloatingIP(t, s, validFloatingIPRequest()) + if w.Code != http.StatusAccepted { + t.Fatalf("attendu 202, obtenu %d : %s", w.Code, w.Body.String()) + } + var got FloatingIP + json.NewDecoder(w.Body).Decode(&got) + want := FloatingIP{Name: "fip-1", State: "creating", VM: "vm-1", Interface: "nic-a", IP: "240.0.0.47/24", Gateway: "240.0.0.1"} + if got != want { + t.Errorf("réponse %+v, attendu %+v", got, want) + } +} + +func TestPostFloatingIP_BadRequest(t *testing.T) { + cases := []struct { + label string + mutate func(*FloatingIPCreateRequest) + msg string + }{ + {"nom absent", func(r *FloatingIPCreateRequest) { r.Name = "" }, "name is required"}, + {"vm absente", func(r *FloatingIPCreateRequest) { r.VM = "" }, "vm is required"}, + {"interface absente", func(r *FloatingIPCreateRequest) { r.Interface = "" }, "interface is required"}, + {"ip absente", func(r *FloatingIPCreateRequest) { r.IP = "" }, "ip is required"}, + {"gateway absente", func(r *FloatingIPCreateRequest) { r.Gateway = "" }, "gateway is required"}, + {"ip sans préfixe", func(r *FloatingIPCreateRequest) { r.IP = "240.0.0.47" }, "invalid ip"}, + {"gateway IPv6", func(r *FloatingIPCreateRequest) { r.Gateway = "2001:db8::1" }, "invalid gateway"}, + } + for _, c := range cases { + s, db := newTestServer(t) + seedRunningVM(t, db) + req := validFloatingIPRequest() + c.mutate(&req) + w := postFloatingIP(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, "floating-ip/fip-1/state"); err == nil { + t.Errorf("%s : IP flottante écrite malgré le refus", c.label) + } + } +} + +func TestPostFloatingIP_ConflictOnUnknownInterface(t *testing.T) { + s, db := newTestServer(t) + seedRunningVM(t, db) + req := validFloatingIPRequest() + req.Interface = "nic-z" + w := postFloatingIP(t, s, req) + if w.Code != http.StatusConflict || !strings.Contains(w.Body.String(), `has no interface \"nic-z\"`) { + t.Errorf("%d %s, 409 attendu", w.Code, w.Body.String()) + } +} + +func TestFloatingIPGetListAndDelete(t *testing.T) { + s, db := newTestServer(t) + seedFloatingIP(t, db, "fip-1", "vm-1", "nic-a", "running") + want := FloatingIP{Name: "fip-1", State: "running", VM: "vm-1", Interface: "nic-a", IP: "240.0.0.47/24", Gateway: "240.0.0.1"} + + w := httptest.NewRecorder() + s.FloatingIPsHandler(w, httptest.NewRequest(http.MethodGet, "/floating-ips", nil)) + var list []FloatingIP + json.NewDecoder(w.Body).Decode(&list) + if w.Code != http.StatusOK || len(list) != 1 || list[0] != want { + t.Errorf("liste %d %+v", w.Code, list) + } + + w = httptest.NewRecorder() + s.FloatingIPByNameHandler(w, httptest.NewRequest(http.MethodGet, "/floating-ips/fip-1", nil)) + var got FloatingIP + json.NewDecoder(w.Body).Decode(&got) + if w.Code != http.StatusOK || got != want { + t.Errorf("get %d %+v", w.Code, got) + } + + w = httptest.NewRecorder() + s.FloatingIPByNameHandler(w, httptest.NewRequest(http.MethodDelete, "/floating-ips/fip-x", nil)) + if w.Code != http.StatusNotFound { + t.Errorf("suppression d'une IP flottante absente : %d, 404 attendu", w.Code) + } + w = httptest.NewRecorder() + s.FloatingIPByNameHandler(w, httptest.NewRequest(http.MethodDelete, "/floating-ips/fip-1", nil)) + if w.Code != http.StatusAccepted { + t.Errorf("suppression : %d, 202 attendu", w.Code) + } +} + +func TestGetVM_ShowsTheFloatingIPOfItsInterface(t *testing.T) { + s, db := newTestServer(t) + seedRunningVM(t, db) + seedFloatingIP(t, db, "fip-1", "vm-1", "nic-b", "running") + seedFloatingIP(t, db, "fip-9", "vm-10", "nic-a", "running") + w := httptest.NewRecorder() + s.VmByNameHandler(w, httptest.NewRequest(http.MethodGet, "/vms/vm-1", nil)) + raw := w.Body.String() + var vm VM + json.Unmarshal([]byte(raw), &vm) + if len(vm.Interfaces) != 2 { + t.Fatalf("interfaces %+v", vm.Interfaces) + } + if vm.Interfaces[0].FloatingIP != nil { + t.Errorf("nic-a porte une IP flottante qui n'est pas la sienne : %+v", vm.Interfaces[0].FloatingIP) + } + want := VMFloatingIP{Name: "fip-1", IP: "240.0.0.47/24", State: "running"} + if vm.Interfaces[1].FloatingIP == nil || *vm.Interfaces[1].FloatingIP != want { + t.Errorf("nic-b : %+v, attendu %+v", vm.Interfaces[1].FloatingIP, want) + } + if strings.Count(raw, "floating_ip") != 1 { + t.Errorf("floating_ip doit être absent du JSON d'une interface sans IP flottante : %s", raw) + } +} + +func TestDeleteVM_BlockedByAFloatingIP(t *testing.T) { + s, db := newTestServer(t) + seedRunningVM(t, db) + seedFloatingIP(t, db, "fip-1", "vm-1", "nic-a", "running") + w := httptest.NewRecorder() + s.VmByNameHandler(w, httptest.NewRequest(http.MethodDelete, "/vms/vm-1", nil)) + if w.Code != http.StatusConflict || !strings.Contains(w.Body.String(), `floating ip \"fip-1\" must be deleted`) { + t.Errorf("%d %s, 409 attendu", w.Code, w.Body.String()) + } +} + +func TestStartVM_RefusesAFloatingIPInTheRequest(t *testing.T) { + s, db := newTestServer(t) + kv.AddInDB(db, "subnet/sn-1/state", "running") + kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1") + kv.AddInDB(db, "subnet/sn-1/cidr", "10.0.0.0/24") + body, _ := json.Marshal(VMCreateRequest{ + Name: "vm-f", + Interfaces: []VMInterface{{Name: "nic-a", Subnet: "sn-1", IP: "10.0.0.10", MAC: "52:54:00:00:00:05", Primary: true, FloatingIP: &VMFloatingIP{Name: "fip-1"}}}, + Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}}, + }) + w := httptest.NewRecorder() + s.VmsHandler(w, httptest.NewRequest(http.MethodPost, "/vms", bytes.NewReader(body))) + if w.Code != http.StatusBadRequest || !strings.Contains(w.Body.String(), "floating_ip is read-only") { + t.Errorf("%d %s, 400 attendu", w.Code, w.Body.String()) + } + if _, err := kv.GetFromDB(db, "vm/vm-f/state"); err == nil { + t.Error("une VM refusée ne doit laisser aucune trace en base") + } +} diff --git a/internal/api/agent/fips.go b/internal/api/agent/fips.go new file mode 100644 index 0000000..1a179c1 --- /dev/null +++ b/internal/api/agent/fips.go @@ -0,0 +1,102 @@ +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) FloatingIPsHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch r.Method { + case http.MethodGet: + s.listFloatingIPs(w, r) + case http.MethodPost: + s.postFloatingIP(w, r) + default: + http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed) + } +} + +func (s *Server) listFloatingIPs(w http.ResponseWriter, _ *http.Request) { + entries, err := kv.ListByPrefix(s.db, "floating-ip/") + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + json.NewEncoder(w).Encode(ErrorResponse{Error: "failed to list floating ips"}) + return + } + ips := make(map[string]*FloatingIP) + for key, value := range entries { + parts := strings.Split(key, "/") + if len(parts) != 3 { + continue + } + name := parts[1] + if _, ok := ips[name]; !ok { + ips[name] = &FloatingIP{Name: name} + } + switch parts[2] { + case "state": + ips[name].State = value + case "vm": + ips[name].VM = value + case "interface": + ips[name].Interface = value + case "ip": + ips[name].IP = value + case "gateway": + ips[name].Gateway = value + } + } + result := make([]FloatingIP, 0, len(ips)) + for _, f := range ips { + result = append(result, *f) + } + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(result) +} + +func (s *Server) postFloatingIP(w http.ResponseWriter, r *http.Request) { + var req FloatingIPCreateRequest + 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}, {"vm", req.VM}, {"interface", req.Interface}, {"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.CreateFloatingIPCommand{Name: req.Name, VM: req.VM, Interface: req.Interface, 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, "floating-ip/"+req.Name+"/state") + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + json.NewEncoder(w).Encode(ErrorResponse{Error: "failed to read floating ip state"}) + return + } + w.WriteHeader(http.StatusAccepted) + json.NewEncoder(w).Encode(FloatingIP{Name: req.Name, State: state, VM: req.VM, Interface: req.Interface, IP: req.IP, Gateway: req.Gateway}) +} diff --git a/internal/api/agent/models.go b/internal/api/agent/models.go index 7ed9d56..e5b74b0 100644 --- a/internal/api/agent/models.go +++ b/internal/api/agent/models.go @@ -46,6 +46,14 @@ type VMInterface struct { MAC string `json:"mac"` Primary bool `json:"primary"` DHCPServer bool `json:"dhcp_server"` + + FloatingIP *VMFloatingIP `json:"floating_ip,omitempty"` +} + +type VMFloatingIP struct { + Name string `json:"name"` + IP string `json:"ip"` + State string `json:"state"` } type VMStorage struct { @@ -98,3 +106,20 @@ type NATGateway struct { IP string `json:"ip,omitempty"` Gateway string `json:"gateway,omitempty"` } + +type FloatingIPCreateRequest struct { + Name string `json:"name"` + VM string `json:"vm"` + Interface string `json:"interface"` + IP string `json:"ip"` + Gateway string `json:"gateway"` +} + +type FloatingIP struct { + Name string `json:"name"` + State string `json:"state"` + VM string `json:"vm,omitempty"` + Interface string `json:"interface,omitempty"` + IP string `json:"ip,omitempty"` + Gateway string `json:"gateway,omitempty"` +} diff --git a/internal/api/agent/server.go b/internal/api/agent/server.go index f5b2273..fe7ddf1 100644 --- a/internal/api/agent/server.go +++ b/internal/api/agent/server.go @@ -31,6 +31,8 @@ func New(d *dispatcher.Dispatcher, db *badger.DB, logger *slog.Logger, address s mux.HandleFunc("/vms/", s.VmByNameHandler) mux.HandleFunc("/nat-gateways", s.NATGatewaysHandler) mux.HandleFunc("/nat-gateways/", s.NATGatewayByNameHandler) + mux.HandleFunc("/floating-ips", s.FloatingIPsHandler) + mux.HandleFunc("/floating-ips/", s.FloatingIPByNameHandler) s.srv = &http.Server{Addr: address, Handler: s.logMiddleware(mux)} return s } diff --git a/internal/api/agent/vm.go b/internal/api/agent/vm.go index 3db5833..374a4f9 100644 --- a/internal/api/agent/vm.go +++ b/internal/api/agent/vm.go @@ -44,6 +44,13 @@ func (s *Server) getVM(w http.ResponseWriter, _ *http.Request, name string) { json.NewEncoder(w).Encode(ErrorResponse{Error: "failed to read vm"}) return } + floating, err := kv.ListByPrefix(s.db, "floating-ip/") + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + json.NewEncoder(w).Encode(ErrorResponse{Error: "failed to list floating ips"}) + return + } + attachFloatingIPs(&vm, floating) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(vm) } @@ -145,3 +152,18 @@ func vmFromDB(name string, entries map[string]string) (VM, error) { return vm, nil } + +func attachFloatingIPs(vm *VM, entries map[string]string) { + for key, value := range entries { + name, ok := strings.CutSuffix(strings.TrimPrefix(key, "floating-ip/"), "/vm") + if !ok || strings.Contains(name, "/") || value != vm.Name { + continue + } + prefix := "floating-ip/" + name + "/" + for i := range vm.Interfaces { + if vm.Interfaces[i].Name == entries[prefix+"interface"] { + vm.Interfaces[i].FloatingIP = &VMFloatingIP{Name: name, IP: entries[prefix+"ip"], State: entries[prefix+"state"]} + } + } + } +} diff --git a/internal/api/agent/vms.go b/internal/api/agent/vms.go index c704b55..720d3bf 100644 --- a/internal/api/agent/vms.go +++ b/internal/api/agent/vms.go @@ -44,12 +44,19 @@ func (s *Server) listVMs(w http.ResponseWriter, _ *http.Request) { } } + floating, err := kv.ListByPrefix(s.db, "floating-ip/") + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + json.NewEncoder(w).Encode(ErrorResponse{Error: "failed to list floating ips"}) + return + } result := make([]VM, 0, len(names)) for name := range names { vm, err := vmFromDB(name, entries) if err != nil { continue } + attachFloatingIPs(&vm, floating) result = append(result, vm) } @@ -85,6 +92,11 @@ func (s *Server) startVM(w http.ResponseWriter, r *http.Request) { return } names[iface.Name] = i + if iface.FloatingIP != nil { + w.WriteHeader(http.StatusBadRequest) + json.NewEncoder(w).Encode(ErrorResponse{Error: fmt.Sprintf("interface %d: floating_ip is read-only, attach it with /floating-ips", i)}) + return + } if iface.MAC == "" { w.WriteHeader(http.StatusBadRequest) json.NewEncoder(w).Encode(ErrorResponse{Error: fmt.Sprintf("interface %d: mac is required", i)}) diff --git a/internal/dispatcher/agent/fip_commands.go b/internal/dispatcher/agent/fip_commands.go new file mode 100644 index 0000000..cd48943 --- /dev/null +++ b/internal/dispatcher/agent/fip_commands.go @@ -0,0 +1,166 @@ +package dispatcher + +import ( + "fmt" + "net" + "strings" + + configuration "git.g3e.fr/syonad/two/internal/config/agent" + "git.g3e.fr/syonad/two/internal/fip" + "git.g3e.fr/syonad/two/internal/state" + "git.g3e.fr/syonad/two/pkg/db/kv" + "github.com/dgraph-io/badger/v4" +) + +const floatingIPPrefix = "floating-ip/" + +type CreateFloatingIPCommand struct { + Name string + VM string + Interface string + IP string + Gateway string +} + +func (c CreateFloatingIPCommand) Key() string { return floatingIPPrefix + c.Name } + +func (c CreateFloatingIPCommand) Prepare(db *badger.DB, _ *configuration.Config) error { + if _, err := kv.GetFromDB(db, c.Key()+"/state"); err == nil { + return fmt.Errorf("floating ip %q already exists", c.Name) + } + public, _, err := parseNATGateway(c.IP, c.Gateway) + if err != nil { + return err + } + vmState, err := state.Get(db, "vm/"+c.VM) + if err != nil { + return fmt.Errorf("vm %q not found", c.VM) + } + if vmState != state.Running { + return fmt.Errorf("vm %q is %s", c.VM, vmState) + } + if _, err := fip.Resolve(db, c.VM, c.Interface); err != nil { + return err + } + others, err := floatingIPs(db) + if err != nil { + return err + } + for name, o := range others { + if o.vm == c.VM && o.iface == c.Interface { + return fmt.Errorf("interface %q of vm %q already has floating ip %q", c.Interface, c.VM, name) + } + if o.public != nil && o.public.Equal(public.IP) { + return fmt.Errorf("public ip %s is already used by floating ip %q", public.IP, name) + } + } + for key, value := range map[string]string{"vm": c.VM, "interface": c.Interface, "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 CreateFloatingIPCommand) Execute(db *badger.DB, _ *configuration.Config) error { + public, gw, err := parseNATGateway(c.IP, c.Gateway) + if err != nil { + return err + } + target, err := fip.Resolve(db, c.VM, c.Interface) + if err != nil { + return err + } + if err := fip.Create(target, public, gw); err != nil { + return err + } + return state.Set(db, c.Key(), state.Running) +} + +type DeleteFloatingIPCommand struct { + Name string +} + +func (c DeleteFloatingIPCommand) Key() string { return floatingIPPrefix + c.Name } + +func (c DeleteFloatingIPCommand) Prepare(db *badger.DB, _ *configuration.Config) error { + current, err := state.Get(db, c.Key()) + if err != nil { + return fmt.Errorf("floating ip %q not found", c.Name) + } + if !state.CanDelete(current) { + return fmt.Errorf("floating ip %q cannot be deleted while %s", c.Name, current) + } + return state.Set(db, c.Key(), state.Deleting) +} + +func (c DeleteFloatingIPCommand) Execute(db *badger.DB, _ *configuration.Config) error { + entries, err := kv.ListByPrefix(db, c.Key()+"/") + if err != nil { + return fmt.Errorf("floating ip %q: %w", c.Name, err) + } + prefix := c.Key() + "/" + public, _, err := parseNATGateway(entries[prefix+"ip"], entries[prefix+"gateway"]) + if err != nil { + return fmt.Errorf("floating ip %q: %w", c.Name, err) + } + target, err := fip.Resolve(db, entries[prefix+"vm"], entries[prefix+"interface"]) + if err != nil { + return err + } + if err := fip.Delete(target, public); err != nil { + return err + } + if err := state.Set(db, c.Key(), state.Deleted); err != nil { + return err + } + return kv.DeleteInDB(db, c.Key()) +} + +type floatingIPRef struct { + vm string + iface string + public net.IP +} + +func floatingIPs(db *badger.DB) (map[string]floatingIPRef, error) { + entries, err := kv.ListByPrefix(db, floatingIPPrefix) + if err != nil { + return nil, fmt.Errorf("failed to list floating ips: %w", err) + } + refs := map[string]floatingIPRef{} + for key, value := range entries { + parts := strings.Split(strings.TrimPrefix(key, floatingIPPrefix), "/") + if len(parts) != 2 { + continue + } + r := refs[parts[0]] + switch parts[1] { + case "vm": + r.vm = value + case "interface": + r.iface = value + case "ip": + if ip, _, err := net.ParseCIDR(value); err == nil { + r.public = ip + } + default: + continue + } + refs[parts[0]] = r + } + return refs, nil +} + +func floatingIPOf(db *badger.DB, vmName string) (string, error) { + refs, err := floatingIPs(db) + if err != nil { + return "", err + } + for name, r := range refs { + if r.vm == vmName { + return name, nil + } + } + return "", nil +} diff --git a/internal/dispatcher/agent/fip_commands_test.go b/internal/dispatcher/agent/fip_commands_test.go new file mode 100644 index 0000000..de681eb --- /dev/null +++ b/internal/dispatcher/agent/fip_commands_test.go @@ -0,0 +1,157 @@ +package dispatcher + +import ( + "strings" + "testing" + + "git.g3e.fr/syonad/two/pkg/db/kv" + "github.com/dgraph-io/badger/v4" +) + +func seedVMWithInterfaces(t *testing.T, db *badger.DB, name, st string) { + t.Helper() + for key, value := range map[string]string{ + "vpc/vp-t47/cidr": "10.47.0.0/16", + "subnet/sn-a/vpc": "vp-t47", + "vm/" + name + "/state": st, + "vm/" + name + "/nic/0/name": "nic-a", + "vm/" + name + "/nic/0/subnet": "sn-a", + "vm/" + name + "/nic/0/ip": "10.47.1.10", + "vm/" + name + "/nic/1/name": "nic-ab", + "vm/" + name + "/nic/1/subnet": "sn-a", + "vm/" + name + "/nic/1/ip": "10.47.1.11", + } { + kv.AddInDB(db, key, value) + } +} + +func validFloatingIP() CreateFloatingIPCommand { + return CreateFloatingIPCommand{Name: "fip-1", VM: "vm-1", Interface: "nic-a", IP: "240.0.0.47/24", Gateway: "240.0.0.1"} +} + +func TestCreateFloatingIPCommand_Prepare_WritesTheFloatingIP(t *testing.T) { + _, db := newTestDispatcher(t) + seedVMWithInterfaces(t, db, "vm-1", "running") + if err := validFloatingIP().Prepare(db, nil); err != nil { + t.Fatalf("Prepare a échoué : %v", err) + } + for key, want := range map[string]string{ + "floating-ip/fip-1/state": "creating", + "floating-ip/fip-1/vm": "vm-1", + "floating-ip/fip-1/interface": "nic-a", + "floating-ip/fip-1/ip": "240.0.0.47/24", + "floating-ip/fip-1/gateway": "240.0.0.1", + } { + if got, err := kv.GetFromDB(db, key); err != nil || got != want { + t.Errorf("%s : %q (%v), %q attendu", key, got, err, want) + } + } +} + +func TestCreateFloatingIPCommand_Prepare_Refuses(t *testing.T) { + cases := []struct { + label string + seed func(*badger.DB) + mutate func(*CreateFloatingIPCommand) + msg string + }{ + {"IP flottante existante", func(db *badger.DB) { kv.AddInDB(db, "floating-ip/fip-1/state", "running") }, nil, `floating ip "fip-1" already exists`}, + {"VM absente", nil, func(c *CreateFloatingIPCommand) { c.VM = "vm-x" }, `vm "vm-x" not found`}, + {"VM en création", func(db *badger.DB) { seedVMWithInterfaces(t, db, "vm-2", "creating") }, func(c *CreateFloatingIPCommand) { c.VM = "vm-2" }, `vm "vm-2" is creating`}, + {"interface inconnue", nil, func(c *CreateFloatingIPCommand) { c.Interface = "nic-z" }, `vm "vm-1" has no interface "nic-z"`}, + {"interface qui a déjà une IP flottante", func(db *badger.DB) { + kv.AddInDB(db, "floating-ip/fip-0/state", "error") + kv.AddInDB(db, "floating-ip/fip-0/vm", "vm-1") + kv.AddInDB(db, "floating-ip/fip-0/interface", "nic-a") + kv.AddInDB(db, "floating-ip/fip-0/ip", "240.0.0.40/24") + }, nil, `interface "nic-a" of vm "vm-1" already has floating ip "fip-0"`}, + {"IP publique déjà portée", func(db *badger.DB) { + kv.AddInDB(db, "floating-ip/fip-0/state", "running") + kv.AddInDB(db, "floating-ip/fip-0/vm", "vm-1") + kv.AddInDB(db, "floating-ip/fip-0/interface", "nic-ab") + 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"`}, + {"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"`}, + } + for _, c := range cases { + _, db := newTestDispatcher(t) + seedVMWithInterfaces(t, db, "vm-1", "running") + if c.seed != nil { + c.seed(db) + } + cmd := validFloatingIP() + 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 != "IP flottante existante" { + if _, err := kv.GetFromDB(db, "floating-ip/fip-1/state"); err == nil { + t.Errorf("%s : état écrit malgré le refus", c.label) + } + } + } +} + +func TestCreateFloatingIPCommand_Prepare_IgnoresNearbyNames(t *testing.T) { + _, db := newTestDispatcher(t) + seedVMWithInterfaces(t, db, "vm-1", "running") + seedVMWithInterfaces(t, db, "vm-10", "running") + kv.AddInDB(db, "floating-ip/fip-0/state", "running") + kv.AddInDB(db, "floating-ip/fip-0/vm", "vm-10") + kv.AddInDB(db, "floating-ip/fip-0/interface", "nic-a") + kv.AddInDB(db, "floating-ip/fip-0/ip", "240.0.0.40/24") + kv.AddInDB(db, "floating-ip/fip-2/state", "running") + kv.AddInDB(db, "floating-ip/fip-2/vm", "vm-1") + kv.AddInDB(db, "floating-ip/fip-2/interface", "nic-ab") + kv.AddInDB(db, "floating-ip/fip-2/ip", "240.0.0.41/24") + if err := validFloatingIP().Prepare(db, nil); err != nil { + t.Errorf("Prepare a échoué : %v", err) + } +} + +func TestDeleteFloatingIPCommand_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, "floating-ip/fip-1/state", st) + err := DeleteFloatingIPCommand{Name: "fip-1"}.Prepare(db, nil) + if ok != (err == nil) { + t.Errorf("état %s : erreur %v", st, err) + } + } + _, db := newTestDispatcher(t) + if err := (DeleteFloatingIPCommand{Name: "fip-x"}).Prepare(db, nil); err == nil || !strings.Contains(err.Error(), `floating ip "fip-x" not found`) { + t.Errorf("IP flottante absente : %v", err) + } +} + +func TestStopVMCommand_Prepare_RefusesWhileAFloatingIPIsAttached(t *testing.T) { + for _, st := range []string{"running", "error", "creating", "deleting"} { + _, db := newTestDispatcher(t) + seedVMWithInterfaces(t, db, "vm-1", "running") + kv.AddInDB(db, "floating-ip/fip-1/state", st) + kv.AddInDB(db, "floating-ip/fip-1/vm", "vm-1") + kv.AddInDB(db, "floating-ip/fip-1/interface", "nic-a") + err := StopVMCommand{Name: "vm-1"}.Prepare(db, nil) + if err == nil || !strings.Contains(err.Error(), `floating ip "fip-1" must be deleted before deleting vm "vm-1"`) { + t.Errorf("IP flottante %s : erreur %v", st, err) + } + if got, _ := kv.GetFromDB(db, "vm/vm-1/state"); got != "running" { + t.Errorf("IP flottante %s : VM passée en %q malgré le refus", st, got) + } + } +} + +func TestStopVMCommand_Prepare_IgnoresTheFloatingIPOfANearbyVM(t *testing.T) { + _, db := newTestDispatcher(t) + seedVMWithInterfaces(t, db, "vm-1", "running") + kv.AddInDB(db, "floating-ip/fip-1/state", "running") + kv.AddInDB(db, "floating-ip/fip-1/vm", "vm-10") + if err := (StopVMCommand{Name: "vm-1"}).Prepare(db, nil); err != nil { + t.Errorf("Prepare a échoué : %v", err) + } +} diff --git a/internal/dispatcher/agent/vm_commands.go b/internal/dispatcher/agent/vm_commands.go index a1d1e0f..849ef06 100644 --- a/internal/dispatcher/agent/vm_commands.go +++ b/internal/dispatcher/agent/vm_commands.go @@ -198,6 +198,13 @@ func (c StopVMCommand) Prepare(db *badger.DB, _ *configuration.Config) error { if !state.CanDelete(current) { return fmt.Errorf("vm %q cannot be stopped while %s", c.Name, current) } + floating, err := floatingIPOf(db, c.Name) + if err != nil { + return err + } + if floating != "" { + return fmt.Errorf("floating ip %q must be deleted before deleting vm %q", floating, c.Name) + } return state.Set(db, c.Key(), state.Deleting) } diff --git a/internal/migration/state.go b/internal/migration/state.go index 27fe805..f8f3472 100644 --- a/internal/migration/state.go +++ b/internal/migration/state.go @@ -14,7 +14,7 @@ import ( ) // prefixes énumère les familles de ressources portant une clé /state. -var prefixes = []string{"vpc/", "subnet/", "vm/", "nat-gateway/"} +var prefixes = []string{"vpc/", "subnet/", "vm/", "nat-gateway/", "floating-ip/"} // legacyStates traduit l'ancien vocabulaire (états VPC/subnet d'avant // l'unification, et états VM start/stop) vers l'enum courant. diff --git a/internal/migration/state_test.go b/internal/migration/state_test.go index 6896489..80fba9e 100644 --- a/internal/migration/state_test.go +++ b/internal/migration/state_test.go @@ -41,6 +41,8 @@ func TestMigrateStates_LegacyMapping(t *testing.T) { "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}, + "fip creating": {"floating-ip/fip-1", "creating", state.Error}, + "fip running": {"floating-ip/fip-2", "running", state.Running}, } for _, c := range cases { kv.AddInDB(db, c.resource+"/state", c.from) diff --git a/internal/prometheus/agent/collector.go b/internal/prometheus/agent/collector.go index 6e1be01..61c3df8 100644 --- a/internal/prometheus/agent/collector.go +++ b/internal/prometheus/agent/collector.go @@ -19,6 +19,7 @@ type AgentCollector struct { subnetsTotal *prometheus.Desc vmsTotal *prometheus.Desc natGateways *prometheus.Desc + floatingIPs *prometheus.Desc } func NewAgentCollector(db *badger.DB) *AgentCollector { @@ -44,6 +45,11 @@ func NewAgentCollector(db *badger.DB) *AgentCollector { "Number of NAT gateways by state.", []string{"state"}, nil, ), + floatingIPs: prometheus.NewDesc( + "syonad_floating_ips_total", + "Number of floating IPs by state.", + []string{"state"}, nil, + ), } } @@ -52,6 +58,7 @@ func (c *AgentCollector) Describe(ch chan<- *prometheus.Desc) { ch <- c.subnetsTotal ch <- c.vmsTotal ch <- c.natGateways + ch <- c.floatingIPs } func (c *AgentCollector) Collect(ch chan<- prometheus.Metric) { @@ -59,6 +66,7 @@ func (c *AgentCollector) Collect(ch chan<- prometheus.Metric) { c.collectStates(ch, "subnet/", c.subnetsTotal) c.collectStates(ch, "vm/", c.vmsTotal) c.collectStates(ch, "nat-gateway/", c.natGateways) + c.collectStates(ch, "floating-ip/", c.floatingIPs) } // collectStates counts resources under the given DB prefix by their state value diff --git a/internal/prometheus/agent/collector_test.go b/internal/prometheus/agent/collector_test.go index e425f62..e2e3b83 100644 --- a/internal/prometheus/agent/collector_test.go +++ b/internal/prometheus/agent/collector_test.go @@ -46,6 +46,8 @@ func collect(t *testing.T, c *AgentCollector) map[string]map[string]float64 { name = "vms" case strings.Contains(desc, "syonad_nat_gateways_total"): name = "nat_gateways" + case strings.Contains(desc, "syonad_floating_ips_total"): + name = "floating_ips" default: t.Fatalf("métrique inattendue : %s", desc) } @@ -64,13 +66,14 @@ 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, - "nat-gateway/ng-1": state.Running, + "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, + "floating-ip/fip-1": state.Error, } { if err := state.Set(db, resource, s); err != nil { t.Fatalf("préparation du test : %v", err) @@ -92,6 +95,9 @@ func TestCollector_CountsByState(t *testing.T) { t.Errorf("vms{state=%q} = %v, attendu %v", s, got["vms"][s], want) } } + if got["floating_ips"]["error"] != 1 || got["floating_ips"]["running"] != 0 { + t.Errorf("floating_ips{state=error} = %v, {state=running} = %v, attendu 1 et 0", got["floating_ips"]["error"], got["floating_ips"]["running"]) + } 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"]) } @@ -117,7 +123,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", "nat_gateways"} { + for _, family := range []string{"vpcs", "subnets", "vms", "nat_gateways", "floating_ips"} { if len(got[family]) != len(state.All()) { t.Errorf("%s : %d séries, attendu %d", family, len(got[family]), len(state.All())) } @@ -151,7 +157,7 @@ func TestCollector_Describe(t *testing.T) { for range ch { n++ } - if n != 4 { - t.Errorf("%d descripteurs, attendu 4", n) + if n != 5 { + t.Errorf("%d descripteurs, attendu 5", n) } }