diff --git a/api/agent.yaml b/api/agent.yaml index b28f183..47ec4ce 100644 --- a/api/agent.yaml +++ b/api/agent.yaml @@ -636,8 +636,17 @@ components: VMInterface: type: object - required: [subnet, ip, mac, primary] + required: [name, subnet, ip, mac, primary] properties: + name: + type: string + description: > + Name of the interface, chosen by the caller and unique within the VM. It is an API + identifier only, used to point at the interface (a floating IP for instance): no + Linux interface is named after it, neither in the guest nor on the host. The agent + does not check its format. Missing, empty or used twice in the VM → 400. Interfaces + of VMs created before it was required are named nic- at agent start. + example: nic-a1b2 subnet: type: string example: sn-00001 diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 548316e..4fed2d8 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -68,6 +68,11 @@ func run(confFile string) int { return 1 } + if err := migration.MigrateVMNICNames(db, log.With(slog.String("component", "migration"))); err != nil { + log.Error("vm nic name migration failed", "error", err) + return 1 + } + if err := migration.MigrateVMDisks(db, log.With(slog.String("component", "migration"))); err != nil { log.Error("vm disk migration failed", "error", err) return 1 diff --git a/docs/architecture/stockage.rst b/docs/architecture/stockage.rst index c587c68..999b09e 100644 --- a/docs/architecture/stockage.rst +++ b/docs/architecture/stockage.rst @@ -22,6 +22,7 @@ de sérialisation structurée. subnet//gateway → (optionnel) vm//state → creating | running | error | deleting | deleted + vm//nic//name → (choisi par l'appelant, unique dans la VM) vm//nic//subnet → (une interface par index, ordre du tableau interfaces) vm//nic//ip → vm//nic//mac → (fournie par l'appelant, conservée telle quelle) @@ -61,6 +62,9 @@ clés. Les VM ``deleted`` sont ignorées. Une interface dont la MAC est introuva démarrage en la nommant, et la table reste en place tant que toutes les interfaces ne sont pas migrées. +**Interfaces sans nom.** Avant la 0.3.0, une interface n'avait pas de nom. Au démarrage, l'agent +nomme ``nic-`` chaque interface qui n'en a pas, d'après son rang ; un nom existant est gardé. + **Anciens noms de disques.** Jusqu'en 0.1.0, les disques additionnels s'appelaient ``sdX``. Au démarrage, l'agent renomme chaque clé ``vm//disk/sdX`` en ``vm//disk/xvdX`` (même lettre, même cible SCSI). Si ``xvdX`` existe déjà avec un autre chemin, le démarrage échoue en diff --git a/docs/concepts/vpc-subnet-vm.rst b/docs/concepts/vpc-subnet-vm.rst index 14d8665..a49a784 100644 --- a/docs/concepts/vpc-subnet-vm.rst +++ b/docs/concepts/vpc-subnet-vm.rst @@ -109,7 +109,10 @@ VM Une VM est un processus QEMU/KVM raccordé à un ou plusieurs subnets par des taps. **Interfaces.** L'ordre du tableau ``interfaces`` détermine le slot PCI (``0x03 + index``), donc -le nom de l'interface dans le guest. Exactement une interface doit être ``primary`` : elle porte +le nom de l'interface dans le guest. Chaque interface porte un ``name``, obligatoire et unique dans +la VM, choisi par l'appelant (``nic-a1b2`` par exemple) : c'est l'identifiant stable par lequel +l'API désigne une interface, son rang pouvant changer. Aucune interface Linux n'en tire son nom, +ni dans le guest ni sur l'hôte. Exactement une interface doit être ``primary`` : elle porte la route par défaut et le serveur de metadata. Tous les subnets d'une VM doivent appartenir au **même VPC**. diff --git a/internal/api/agent/models.go b/internal/api/agent/models.go index bd86adc..7ed9d56 100644 --- a/internal/api/agent/models.go +++ b/internal/api/agent/models.go @@ -40,6 +40,7 @@ type Subnet struct { } type VMInterface struct { + Name string `json:"name"` Subnet string `json:"subnet"` IP string `json:"ip"` MAC string `json:"mac"` diff --git a/internal/api/agent/vm.go b/internal/api/agent/vm.go index 1819196..3db5833 100644 --- a/internal/api/agent/vm.go +++ b/internal/api/agent/vm.go @@ -96,6 +96,8 @@ func interfacesFromDB(prefix string, entries map[string]string) []VMInterface { byIndex[idx] = &VMInterface{} } switch parts[1] { + case "name": + byIndex[idx].Name = value case "subnet": byIndex[idx].Subnet = value case "ip": diff --git a/internal/api/agent/vm_test.go b/internal/api/agent/vm_test.go index 44981a2..082dd16 100644 --- a/internal/api/agent/vm_test.go +++ b/internal/api/agent/vm_test.go @@ -104,7 +104,7 @@ func TestStartVM_MultiDisk(t *testing.T) { body, _ := json.Marshal(VMCreateRequest{ Name: "vm-10", Interfaces: []VMInterface{ - {Subnet: "sn-1", IP: "10.0.0.10", MAC: "52:54:00:00:00:05", Primary: true}, + {Name: "nic-sn-1", Subnet: "sn-1", IP: "10.0.0.10", MAC: "52:54:00:00:00:05", Primary: true}, }, Storage: []VMStorage{ {Path: "/data/root.qcow2", Dev: "vda"}, @@ -139,7 +139,7 @@ func TestStartVM_StorageReturnedInResponse(t *testing.T) { body, _ := json.Marshal(VMCreateRequest{ Name: "vm-11", Interfaces: []VMInterface{ - {Subnet: "sn-1", IP: "10.0.0.11", MAC: "52:54:00:00:00:05", Primary: true}, + {Name: "nic-sn-1", Subnet: "sn-1", IP: "10.0.0.11", MAC: "52:54:00:00:00:05", Primary: true}, }, Storage: []VMStorage{ {Path: "/data/root.qcow2", Dev: "xvda"}, @@ -171,7 +171,7 @@ func TestStartVM_RejectsInvalidDiskDev(t *testing.T) { body, _ := json.Marshal(VMCreateRequest{ Name: "vm-12", - Interfaces: []VMInterface{{Subnet: "sn-1", IP: "10.0.0.12", MAC: "52:54:00:00:00:05", Primary: true}}, + Interfaces: []VMInterface{{Name: "nic-sn-1", Subnet: "sn-1", IP: "10.0.0.12", MAC: "52:54:00:00:00:05", Primary: true}}, Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}, {Path: "/data/data.qcow2", Dev: dev}}, }) @@ -200,7 +200,7 @@ func postVM(t *testing.T, name string, meta VMMetadata) (*httptest.ResponseRecor body, _ := json.Marshal(VMCreateRequest{ Name: name, - Interfaces: []VMInterface{{Subnet: "sn-1", IP: "10.0.0.20", MAC: "52:54:00:00:00:05", Primary: true}}, + Interfaces: []VMInterface{{Name: "nic-sn-1", Subnet: "sn-1", IP: "10.0.0.20", MAC: "52:54:00:00:00:05", Primary: true}}, Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}}, Memory: 1024, CPUs: 2, @@ -334,8 +334,8 @@ func TestStartVM_StoresAllInterfaces(t *testing.T) { body, _ := json.Marshal(VMCreateRequest{ Name: "vm-multi", Interfaces: []VMInterface{ - {Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true}, - {Subnet: "sn-2", IP: "10.2.0.5", MAC: "52:54:00:aa:bb:cc"}, + {Name: "nic-sn-1", Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true}, + {Name: "nic-sn-2", Subnet: "sn-2", IP: "10.2.0.5", MAC: "52:54:00:aa:bb:cc"}, }, Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}}, }) @@ -371,8 +371,8 @@ func TestStartVM_StoresDHCPServerOnlyWhenRequested(t *testing.T) { body, _ := json.Marshal(VMCreateRequest{ Name: "vm-pxe", Interfaces: []VMInterface{ - {Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true}, - {Subnet: "sn-2", IP: "10.2.0.5", MAC: "52:54:00:aa:bb:cc", DHCPServer: true}, + {Name: "nic-sn-1", Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true}, + {Name: "nic-sn-2", Subnet: "sn-2", IP: "10.2.0.5", MAC: "52:54:00:aa:bb:cc", DHCPServer: true}, }, Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}}, }) @@ -418,10 +418,10 @@ func TestVmFromDB_ReturnsDHCPServer(t *testing.T) { func TestStartVM_RejectsZeroOrTwoPrimaries(t *testing.T) { cases := map[string][]VMInterface{ - "aucune primaire": {{Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05"}}, + "aucune primaire": {{Name: "nic-sn-1", Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05"}}, "deux primaires": { - {Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true}, - {Subnet: "sn-2", IP: "10.2.0.5", MAC: "52:54:00:00:00:05", Primary: true}, + {Name: "nic-sn-1", Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true}, + {Name: "nic-sn-2", Subnet: "sn-2", IP: "10.2.0.5", MAC: "52:54:00:00:00:05", Primary: true}, }, } for label, ifaces := range cases { @@ -454,8 +454,8 @@ func TestStartVM_RejectsAnInterfaceWithoutMAC(t *testing.T) { body, _ := json.Marshal(VMCreateRequest{ Name: "vm-nomac", Interfaces: []VMInterface{ - {Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true}, - {Subnet: "sn-2", IP: "10.2.0.5"}, + {Name: "nic-sn-1", Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true}, + {Name: "nic-sn-2", Subnet: "sn-2", IP: "10.2.0.5"}, }, Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}}, }) @@ -488,7 +488,7 @@ func TestStartVM_RejectsReservedOrForeignAddresses(t *testing.T) { kv.AddInDB(db, "subnet/sn-1/gateway", "10.0.0.1") body, _ := json.Marshal(VMCreateRequest{ Name: "vm-ip", - Interfaces: []VMInterface{{Subnet: "sn-1", IP: ip, MAC: "52:54:00:00:00:05", Primary: true}}, + Interfaces: []VMInterface{{Name: "nic-sn-1", Subnet: "sn-1", IP: ip, MAC: "52:54:00:00:00:05", Primary: true}}, Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}}, }) w := httptest.NewRecorder() @@ -511,7 +511,7 @@ func TestStartVM_AcceptsHostAddressBesideReservedOnes(t *testing.T) { kv.AddInDB(db, "subnet/sn-1/relay_ip", "10.0.0.253") body, _ := json.Marshal(VMCreateRequest{ Name: "vm-ip", - Interfaces: []VMInterface{{Subnet: "sn-1", IP: "10.0.0.252", MAC: "52:54:00:00:00:05", Primary: true}}, + Interfaces: []VMInterface{{Name: "nic-sn-1", Subnet: "sn-1", IP: "10.0.0.252", MAC: "52:54:00:00:00:05", Primary: true}}, Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}}, }) w := httptest.NewRecorder() @@ -527,7 +527,7 @@ func TestStartVM_RejectsSubnetWithoutCIDR(t *testing.T) { kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1") body, _ := json.Marshal(VMCreateRequest{ Name: "vm-ip", - Interfaces: []VMInterface{{Subnet: "sn-1", IP: "10.0.0.5", MAC: "52:54:00:00:00:05", Primary: true}}, + Interfaces: []VMInterface{{Name: "nic-sn-1", Subnet: "sn-1", IP: "10.0.0.5", MAC: "52:54:00:00:00:05", Primary: true}}, Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}}, }) w := httptest.NewRecorder() @@ -536,3 +536,75 @@ func TestStartVM_RejectsSubnetWithoutCIDR(t *testing.T) { t.Errorf("attendu 422 « has no cidr », obtenu %d : %s", w.Code, w.Body.String()) } } + +func TestStartVM_RejectsAnInterfaceWithoutOrWithADuplicateName(t *testing.T) { + cases := []struct { + label string + ifaces []VMInterface + msg string + }{ + {"nom absent", []VMInterface{ + {Name: "nic-a", Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true}, + {Subnet: "sn-2", IP: "10.2.0.5", MAC: "52:54:00:aa:bb:cc"}, + }, "interface 1: name is required"}, + {"nom en double", []VMInterface{ + {Name: "nic-a", Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true}, + {Name: "nic-a", Subnet: "sn-2", IP: "10.2.0.5", MAC: "52:54:00:aa:bb:cc"}, + }, `interface 1: name \"nic-a\" is already used by interface 0`}, + } + for _, c := range cases { + s, db := newTestServer(t) + for sn, cidr := range map[string]string{"sn-1": "10.1.0.0/24", "sn-2": "10.2.0.0/24"} { + kv.AddInDB(db, "subnet/"+sn+"/state", "running") + kv.AddInDB(db, "subnet/"+sn+"/vpc", "vpc-1") + kv.AddInDB(db, "subnet/"+sn+"/cidr", cidr) + } + body, _ := json.Marshal(VMCreateRequest{ + Name: "vm-names", + Interfaces: c.ifaces, + 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(), c.msg) { + t.Errorf("%s : attendu 400 « %s », obtenu %d — %s", c.label, c.msg, w.Code, w.Body.String()) + } + if _, err := kv.GetFromDB(db, "vm/vm-names/state"); err == nil { + t.Errorf("%s : une VM refusée ne doit laisser aucune trace en base", c.label) + } + } +} + +func TestStartVM_StoresAndReturnsInterfaceNames(t *testing.T) { + s, db := newTestServer(t) + for sn, cidr := range map[string]string{"sn-1": "10.1.0.0/24", "sn-2": "10.2.0.0/24"} { + kv.AddInDB(db, "subnet/"+sn+"/state", "running") + kv.AddInDB(db, "subnet/"+sn+"/vpc", "vpc-1") + kv.AddInDB(db, "subnet/"+sn+"/cidr", cidr) + } + body, _ := json.Marshal(VMCreateRequest{ + Name: "vm-named", + Interfaces: []VMInterface{ + {Name: "nic-a1b2", Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true}, + {Name: "nic-c3d4", Subnet: "sn-2", IP: "10.2.0.5", MAC: "52:54:00:aa:bb:cc"}, + }, + 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.StatusAccepted { + t.Fatalf("attendu 202, obtenu %d : %s", w.Code, w.Body.String()) + } + for key, want := range map[string]string{"vm/vm-named/nic/0/name": "nic-a1b2", "vm/vm-named/nic/1/name": "nic-c3d4"} { + if got, err := kv.GetFromDB(db, key); err != nil || got != want { + t.Errorf("%s = %q (%v), attendu %q", key, got, err, want) + } + } + w = httptest.NewRecorder() + s.VmByNameHandler(w, httptest.NewRequest(http.MethodGet, "/vms/vm-named", nil)) + var got VM + json.NewDecoder(w.Body).Decode(&got) + if len(got.Interfaces) != 2 || got.Interfaces[0].Name != "nic-a1b2" || got.Interfaces[1].Name != "nic-c3d4" { + t.Errorf("interfaces renvoyées : %+v", got.Interfaces) + } +} diff --git a/internal/api/agent/vms.go b/internal/api/agent/vms.go index f679919..c704b55 100644 --- a/internal/api/agent/vms.go +++ b/internal/api/agent/vms.go @@ -72,13 +72,25 @@ func (s *Server) startVM(w http.ResponseWriter, r *http.Request) { nics := make([]dispatcher.VMNIC, len(req.Interfaces)) primaries := 0 + names := make(map[string]int, len(req.Interfaces)) for i, iface := range req.Interfaces { + if iface.Name == "" { + w.WriteHeader(http.StatusBadRequest) + json.NewEncoder(w).Encode(ErrorResponse{Error: fmt.Sprintf("interface %d: name is required", i)}) + return + } + if other, ok := names[iface.Name]; ok { + w.WriteHeader(http.StatusBadRequest) + json.NewEncoder(w).Encode(ErrorResponse{Error: fmt.Sprintf("interface %d: name %q is already used by interface %d", i, iface.Name, other)}) + return + } + names[iface.Name] = i if iface.MAC == "" { w.WriteHeader(http.StatusBadRequest) json.NewEncoder(w).Encode(ErrorResponse{Error: fmt.Sprintf("interface %d: mac is required", i)}) return } - nics[i] = dispatcher.VMNIC{Subnet: iface.Subnet, IP: iface.IP, MAC: iface.MAC, Primary: iface.Primary, DHCPServer: iface.DHCPServer} + nics[i] = dispatcher.VMNIC{Name: iface.Name, Subnet: iface.Subnet, IP: iface.IP, MAC: iface.MAC, Primary: iface.Primary, DHCPServer: iface.DHCPServer} if iface.Primary { primaries++ } diff --git a/internal/dispatcher/agent/vm_commands.go b/internal/dispatcher/agent/vm_commands.go index 314d112..a1d1e0f 100644 --- a/internal/dispatcher/agent/vm_commands.go +++ b/internal/dispatcher/agent/vm_commands.go @@ -22,6 +22,7 @@ type VMDisk struct { } type VMNIC struct { + Name string Subnet string IP string MAC string @@ -57,6 +58,9 @@ func (c StartVMCommand) Prepare(db *badger.DB, _ *configuration.Config) error { state.Set(db, c.Key(), state.Creating) for i, n := range c.NICs { prefix := fmt.Sprintf("vm/%s/nic/%d/", c.Name, i) + if err := kv.AddInDB(db, prefix+"name", n.Name); err != nil { + return fmt.Errorf("store nic %d name: %w", i, err) + } if err := kv.AddInDB(db, prefix+"subnet", n.Subnet); err != nil { return fmt.Errorf("store nic %d subnet: %w", i, err) } diff --git a/internal/migration/nicname.go b/internal/migration/nicname.go new file mode 100644 index 0000000..19b092b --- /dev/null +++ b/internal/migration/nicname.go @@ -0,0 +1,43 @@ +package migration + +import ( + "fmt" + "log/slog" + "sort" + "strings" + + "git.g3e.fr/syonad/two/pkg/db/kv" + + "github.com/dgraph-io/badger/v4" +) + +func MigrateVMNICNames(db *badger.DB, log *slog.Logger) error { + vms, err := kv.ListByPrefix(db, "vm/") + if err != nil { + return fmt.Errorf("list vm/: %w", err) + } + for _, prefix := range nicsWithoutName(vms) { + name := "nic-" + strings.Split(prefix, "/")[3] + if err := kv.AddInDB(db, prefix+"name", name); err != nil { + return fmt.Errorf("store %sname: %w", prefix, err) + } + log.Info("vm nic name migrated", "resource", strings.TrimSuffix(prefix, "/"), "name", name, "reason", "interface name now required") + } + return nil +} + +func nicsWithoutName(entries map[string]string) []string { + var prefixes []string + for key := range entries { + parts := strings.Split(key, "/") + if len(parts) != 5 || parts[2] != "nic" || parts[4] != "subnet" { + continue + } + prefix := strings.TrimSuffix(key, "subnet") + if _, ok := entries[prefix+"name"]; !ok { + prefixes = append(prefixes, prefix) + } + } + sort.Strings(prefixes) + return prefixes +} diff --git a/internal/migration/nicname_test.go b/internal/migration/nicname_test.go new file mode 100644 index 0000000..43cbd26 --- /dev/null +++ b/internal/migration/nicname_test.go @@ -0,0 +1,52 @@ +package migration + +import ( + "testing" + + "git.g3e.fr/syonad/two/pkg/db/kv" +) + +func TestMigrateVMNICNames_NamesEveryInterfaceByItsIndex(t *testing.T) { + db := newTestDB(t) + seedPreloadedVM(t, db, "vm-1", "running", map[int][2]string{0: {"sn-a", "10.0.0.10"}, 1: {"sn-b", "10.0.1.10"}}) + if err := MigrateVMNICNames(db, discardLogger()); err != nil { + t.Fatal(err) + } + assertKey(t, db, "vm/vm-1/nic/0/name", "nic-0") + assertKey(t, db, "vm/vm-1/nic/1/name", "nic-1") +} + +func TestMigrateVMNICNames_KeepsAnExistingName(t *testing.T) { + db := newTestDB(t) + seedPreloadedVM(t, db, "vm-1", "running", map[int][2]string{0: {"sn-a", "10.0.0.10"}, 1: {"sn-b", "10.0.1.10"}}) + kv.AddInDB(db, "vm/vm-1/nic/0/name", "nic-a1b2") + if err := MigrateVMNICNames(db, discardLogger()); err != nil { + t.Fatal(err) + } + assertKey(t, db, "vm/vm-1/nic/0/name", "nic-a1b2") + assertKey(t, db, "vm/vm-1/nic/1/name", "nic-1") +} + +func TestMigrateVMNICNames_IsIdempotent(t *testing.T) { + db := newTestDB(t) + seedPreloadedVM(t, db, "vm-1", "running", map[int][2]string{0: {"sn-a", "10.0.0.10"}}) + for i := 0; i < 2; i++ { + if err := MigrateVMNICNames(db, discardLogger()); err != nil { + t.Fatalf("passage %d : %v", i+1, err) + } + } + assertKey(t, db, "vm/vm-1/nic/0/name", "nic-0") +} + +func TestMigrateVMNICNames_IgnoresKeysOutsideInterfaces(t *testing.T) { + db := newTestDB(t) + kv.AddInDB(db, "vm/vm-1/state", "running") + kv.AddInDB(db, "vm/vm-1/disk/vda", "/data/vda.qcow2") + if err := MigrateVMNICNames(db, discardLogger()); err != nil { + t.Fatal(err) + } + entries, _ := kv.ListByPrefix(db, "vm/vm-1/") + if len(entries) != 2 { + t.Errorf("clés après migration : %v", entries) + } +} diff --git a/release_notes/0.3.0.md b/release_notes/0.3.0.md index ad333b1..5257055 100644 --- a/release_notes/0.3.0.md +++ b/release_notes/0.3.0.md @@ -57,6 +57,13 @@ (`br-public` par défaut, jamais `default_interface`), au lieu d'un nom en dur - Le watchdog signale un bridge public absent, éteint ou sans port VXLAN +**Interfaces de VM nommées (#88)** + +- Chaque interface d'une VM porte un `name`, **obligatoire** et unique dans la VM, choisi par + l'appelant (`nic-a1b2` par exemple) : 400 s'il est absent ou en double. Il est rendu par le GET. + C'est l'identifiant stable d'une interface dans l'API (une IP flottante la désigne par ce nom) ; + aucune interface Linux n'en tire son nom + **Passerelle NAT des VPC (#42)** - Nouvelle ressource `/nat-gateways` (`POST`, `GET`, `DELETE`) : `name`, `vpc`, `ip` (IP de @@ -139,6 +146,9 @@ la fuite vers l'underlay décrite plus haut reste possible. Pour la fermer sans recréer le VPC, la poser à la main sur chaque hyperviseur, la table étant le `l3vni` du VPC : `ip route replace unreachable default metric 4278198272 table ` +- **`name` est obligatoire sur chaque interface de `POST /vms` (#88)** : un appel qui l'omet reçoit + un 400. Au démarrage, l'agent nomme `nic-` (`nic-0`, `nic-1`…) les interfaces des VM + existantes - **Passerelle NAT (#42) : seuls les VPC créés par la 0.3.0 peuvent s'en servir.** Les VPC routés existants n'envoient pas leur route par défaut vers la VRF et leur route-map n'exporte pas celle de la passerelle ; l'agent ne les reprend pas au démarrage. Recréer le VPC avant de diff --git a/test/e2e/lib/vm.sh b/test/e2e/lib/vm.sh index ff58f04..badc4c7 100644 --- a/test/e2e/lib/vm.sh +++ b/test/e2e/lib/vm.sh @@ -48,7 +48,7 @@ vm_create () { api_state "vms/${name}" | grep -qx running && return 0 [ -n "${key}" ] && meta="{\"sshkey\":\"${key}\"}" vm_volume "${name}" || return 1 - api_post vms "{\"name\":\"${name}\",\"memory\":1024,\"cpus\":1,\"metadata\":${meta},\"interfaces\":[{\"subnet\":\"${subnet}\",\"ip\":\"${ip}\",\"mac\":\"$(mac_for_ip "${ip}")\",\"primary\":true}],\"storage\":[{\"path\":\"${VOLUMES}/${name}.qcow2\",\"dev\":\"vda\"}]}" \ + api_post vms "{\"name\":\"${name}\",\"memory\":1024,\"cpus\":1,\"metadata\":${meta},\"interfaces\":[{\"name\":\"nic-0\",\"subnet\":\"${subnet}\",\"ip\":\"${ip}\",\"mac\":\"$(mac_for_ip "${ip}")\",\"primary\":true}],\"storage\":[{\"path\":\"${VOLUMES}/${name}.qcow2\",\"dev\":\"vda\"}]}" \ && wait_state "vms/${name}" running } @@ -109,7 +109,7 @@ ping_rate () { vm_body () { local name="${1}" subnet="${2}" ip="${3}" disk="${4:-${VOLUMES}/${1}.qcow2}" meta="${5:-{\}}" dev="${6:-vda}" - printf '{"name":"%s","memory":1024,"cpus":1,"metadata":%s,"interfaces":[{"subnet":"%s","ip":"%s","mac":"%s","primary":true}],"storage":[{"path":"%s","dev":"%s"}]}' \ + printf '{"name":"%s","memory":1024,"cpus":1,"metadata":%s,"interfaces":[{"name":"nic-0","subnet":"%s","ip":"%s","mac":"%s","primary":true}],"storage":[{"path":"%s","dev":"%s"}]}' \ "${name}" "${meta}" "${subnet}" "${ip}" "$(mac_for_ip "${ip}")" "${disk}" "${dev}" } @@ -121,7 +121,7 @@ vm_json () { --nic) sn=${2%%=*}; rest=${2#*=}; ip=${rest%%=*}; mac=$(mac_for_ip "${ip}") [ "${rest}" != "${ip}" ] && mac=${rest#*=} - nics=$(jq -c --arg s "${sn}" --arg ip "${ip}" --arg m "${mac}" --argjson p "${primary}" '. + [{subnet: $s, ip: $ip, mac: $m, primary: $p}]' <<<"${nics}"); primary=false; shift 2 ;; + nics=$(jq -c --arg s "${sn}" --arg ip "${ip}" --arg m "${mac}" --argjson p "${primary}" '. + [{name: ("nic-" + (length | tostring)), subnet: $s, ip: $ip, mac: $m, primary: $p}]' <<<"${nics}"); primary=false; shift 2 ;; --dhcp-server) nics=$(jq -c '.[-1].dhcp_server = true' <<<"${nics}"); shift ;; --disk) disks=$(jq -c --arg d "${2%%=*}" --arg p "${2#*=}" '. + [{path: $p, dev: $d}]' <<<"${disks}"); shift 2 ;; --sshkey) meta=$(jq -c --arg v "${2}" '. + {sshkey: $v}' <<<"${meta}"); shift 2 ;; diff --git a/test/e2e/run_test.sh b/test/e2e/run_test.sh index 10f219d..3599bef 100755 --- a/test/e2e/run_test.sh +++ b/test/e2e/run_test.sh @@ -447,7 +447,7 @@ test_api_field_reads_one_field_or_nothing () { test_vm_body_builds_the_create_request () { setup "vm_body : requête de création, disque par défaut, métadonnées et dev" - [[ $(node 'vm_body t11-a sn-t11a 10.11.1.10') == '{"name":"t11-a","memory":1024,"cpus":1,"metadata":{},"interfaces":[{"subnet":"sn-t11a","ip":"10.11.1.10","mac":"52:54:00:0b:01:0a","primary":true}],"storage":[{"path":"/var/lib/two/volumes/t11-a.qcow2","dev":"vda"}]}' ]] \ + [[ $(node 'vm_body t11-a sn-t11a 10.11.1.10') == '{"name":"t11-a","memory":1024,"cpus":1,"metadata":{},"interfaces":[{"name":"nic-0","subnet":"sn-t11a","ip":"10.11.1.10","mac":"52:54:00:0b:01:0a","primary":true}],"storage":[{"path":"/var/lib/two/volumes/t11-a.qcow2","dev":"vda"}]}' ]] \ || fail "défaut : $(node 'vm_body t11-a sn-t11a 10.11.1.10')" [[ $(node 'vm_body t11-b sn-t11a 10.11.1.11 /x.qcow2 "{\"user_data\":\"YQ==\"}" xvdb | jq -c "[.metadata.user_data, .storage[0].path, .storage[0].dev]"') == '["YQ==","/x.qcow2","xvdb"]' ]] \ || fail "options : $(node 'vm_body t11-b sn-t11a 10.11.1.11 /x.qcow2 "{\"user_data\":\"YQ==\"}" xvdb')" @@ -458,10 +458,10 @@ test_vm_json_builds_nics_disks_metadata_and_uefi () { setup "vm_json : NIC (la première primaire), disques, métadonnées, uefi" local out out=$(node 'vm_json t25-m --disk vda=/v/a.qcow2 --nic sn-a=10.0.1.10 --nic sn-b=10.0.2.10 --disk xvda=/v/b.qcow2 --sshkey "ssh-ed25519 AAAA x" --password "\$6\$s\$h" --user-data "I2Nsb3Vk" --uefi') - [[ "${out}" == '{"name":"t25-m","memory":1024,"cpus":1,"metadata":{"sshkey":"ssh-ed25519 AAAA x","password":"$6$s$h","user_data":"I2Nsb3Vk"},"interfaces":[{"subnet":"sn-a","ip":"10.0.1.10","mac":"52:54:00:00:01:0a","primary":true},{"subnet":"sn-b","ip":"10.0.2.10","mac":"52:54:00:00:02:0a","primary":false}],"storage":[{"path":"/v/a.qcow2","dev":"vda"},{"path":"/v/b.qcow2","dev":"xvda"}],"uefi":true}' ]] \ + [[ "${out}" == '{"name":"t25-m","memory":1024,"cpus":1,"metadata":{"sshkey":"ssh-ed25519 AAAA x","password":"$6$s$h","user_data":"I2Nsb3Vk"},"interfaces":[{"name":"nic-0","subnet":"sn-a","ip":"10.0.1.10","mac":"52:54:00:00:01:0a","primary":true},{"name":"nic-1","subnet":"sn-b","ip":"10.0.2.10","mac":"52:54:00:00:02:0a","primary":false}],"storage":[{"path":"/v/a.qcow2","dev":"vda"},{"path":"/v/b.qcow2","dev":"xvda"}],"uefi":true}' ]] \ || fail "complet : ${out}" out=$(node 'vm_json t21-a --nic sn-a=10.0.1.10') - [[ "${out}" == '{"name":"t21-a","memory":1024,"cpus":1,"metadata":{},"interfaces":[{"subnet":"sn-a","ip":"10.0.1.10","mac":"52:54:00:00:01:0a","primary":true}],"storage":[],"uefi":false}' ]] || fail "minimal : ${out}" + [[ "${out}" == '{"name":"t21-a","memory":1024,"cpus":1,"metadata":{},"interfaces":[{"name":"nic-0","subnet":"sn-a","ip":"10.0.1.10","mac":"52:54:00:00:01:0a","primary":true}],"storage":[],"uefi":false}' ]] || fail "minimal : ${out}" out=$(node 'vm_json t21-m --nic sn-a=10.0.1.12=02:de:ad:be:ef:01 | jq -c ".interfaces[0] | [.ip, .mac]"') [[ "${out}" == '["10.0.1.12","02:de:ad:be:ef:01"]' ]] || fail "mac forcée : ${out}" out=$(node 'vm_json t76-p --nic sn-a=10.0.1.10 --nic sn-b=10.0.2.10 --dhcp-server | jq -c "[.interfaces[] | .dhcp_server]"') diff --git a/test/e2e/suite/11-validation.sh b/test/e2e/suite/11-validation.sh index 04df20d..15adc11 100644 --- a/test/e2e/suite/11-validation.sh +++ b/test/e2e/suite/11-validation.sh @@ -42,6 +42,8 @@ done check "VM : subnet inconnu, 422" expect_code 422 api_post_code vms "$(vm_body t11-x sn-t11z 10.11.1.12 "${NONE}")" check "VM : aucune interface primaire, 400" expect_code 400 api_post_code vms "$(vm_body t11-x sn-t11a 10.11.1.12 "${NONE}" | sed 's/"primary":true/"primary":false/')" check "VM : interface sans mac, 400" expect_code 400 api_post_code vms "$(vm_body t11-x sn-t11a 10.11.1.12 "${NONE}" | jq -c 'del(.interfaces[0].mac)')" +check "VM : interface sans nom, 400" expect_code 400 api_post_code vms "$(vm_body t11-x sn-t11a 10.11.1.12 "${NONE}" | jq -c 'del(.interfaces[0].name)')" +check "VM : deux interfaces de même nom, 400" expect_code 400 api_post_code vms "$(vm_json t11-x --disk "vda=${NONE}" --nic sn-t11a=10.11.1.12 --nic sn-t11a=10.11.1.13 | jq -c '.interfaces[1].name = "nic-0"')" for dev in sdb vdb xvdaa; do check "VM : disque additionnel ${dev}, 400" expect_code 400 api_post_code vms "$(vm_body t11-x sn-t11a 10.11.1.12 "${NONE}" | jq -c --arg d "${dev}" '.storage += [{"path":"/x.qcow2","dev":$d}]')" done