f-88: api: nom obligatoire des interfaces de VM, migration nic-<i> #88
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
74e8c6220e
commit
ee7ecbf69f
15 changed files with 244 additions and 25 deletions
|
|
@ -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"`
|
||||
|
|
|
|||
|
|
@ -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":
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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++
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
43
internal/migration/nicname.go
Normal file
43
internal/migration/nicname.go
Normal file
|
|
@ -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
|
||||
}
|
||||
52
internal/migration/nicname_test.go
Normal file
52
internal/migration/nicname_test.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue