f-62: vm: MAC fournie à la création par interface, fin de la table préchargée, migration #62

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-19 21:25:18 +02:00
commit 077141443e
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
20 changed files with 338 additions and 171 deletions

View file

@ -42,6 +42,7 @@ type Subnet struct {
type VMInterface struct {
Subnet string `json:"subnet"`
IP string `json:"ip"`
MAC string `json:"mac"`
Primary bool `json:"primary"`
}

View file

@ -100,6 +100,8 @@ func interfacesFromDB(prefix string, entries map[string]string) []VMInterface {
byIndex[idx].Subnet = value
case "ip":
byIndex[idx].IP = value
case "mac":
byIndex[idx].MAC = value
case "primary":
byIndex[idx].Primary = value == "true"
}

View file

@ -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", Primary: true},
{Subnet: "sn-1", IP: "10.0.0.10", MAC: "52:54:00:00:00:05", Primary: true},
},
Storage: []VMStorage{
{Path: "/data/root.qcow2", Dev: "sda"},
@ -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", Primary: true},
{Subnet: "sn-1", IP: "10.0.0.11", MAC: "52:54:00:00:00:05", Primary: true},
},
Storage: []VMStorage{
{Path: "/data/root.qcow2", Dev: "sda"},
@ -173,7 +173,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", Primary: true}},
Interfaces: []VMInterface{{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,
@ -273,6 +273,7 @@ func TestVmFromDB_MultipleInterfacesSortedByIndex(t *testing.T) {
"vm/vm-multi/state": "running",
"vm/vm-multi/nic/1/subnet": "sn-2",
"vm/vm-multi/nic/1/ip": "10.2.0.5",
"vm/vm-multi/nic/1/mac": "52:54:00:aa:bb:cc",
"vm/vm-multi/nic/0/subnet": "sn-1",
"vm/vm-multi/nic/0/ip": "10.1.0.5",
"vm/vm-multi/nic/0/primary": "true",
@ -290,6 +291,9 @@ func TestVmFromDB_MultipleInterfacesSortedByIndex(t *testing.T) {
if vm.Interfaces[1].Subnet != "sn-2" || vm.Interfaces[1].Primary {
t.Errorf("la seconde doit être l'index 1, non primaire : %+v", vm.Interfaces[1])
}
if vm.Interfaces[1].MAC != "52:54:00:aa:bb:cc" {
t.Errorf("la mac de la seconde doit être rendue : %+v", vm.Interfaces[1])
}
}
func TestStartVM_StoresAllInterfaces(t *testing.T) {
@ -303,8 +307,8 @@ func TestStartVM_StoresAllInterfaces(t *testing.T) {
body, _ := json.Marshal(VMCreateRequest{
Name: "vm-multi",
Interfaces: []VMInterface{
{Subnet: "sn-1", IP: "10.1.0.5", Primary: true},
{Subnet: "sn-2", IP: "10.2.0.5"},
{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"},
},
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}},
})
@ -318,6 +322,9 @@ func TestStartVM_StoresAllInterfaces(t *testing.T) {
if got, _ := kv.GetFromDB(db, "vm/vm-multi/nic/1/subnet"); got != "sn-2" {
t.Errorf("seconde interface non stockée : %q", got)
}
if got, _ := kv.GetFromDB(db, "vm/vm-multi/nic/1/mac"); got != "52:54:00:aa:bb:cc" {
t.Errorf("mac de la seconde interface non stockée telle quelle : %q", got)
}
if got, _ := kv.GetFromDB(db, "vm/vm-multi/nic/0/primary"); got != "true" {
t.Errorf("primaire non marquée : %q", got)
}
@ -328,10 +335,10 @@ func TestStartVM_StoresAllInterfaces(t *testing.T) {
func TestStartVM_RejectsZeroOrTwoPrimaries(t *testing.T) {
cases := map[string][]VMInterface{
"aucune primaire": {{Subnet: "sn-1", IP: "10.1.0.5"}},
"aucune primaire": {{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", Primary: true},
{Subnet: "sn-2", IP: "10.2.0.5", Primary: true},
{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},
},
}
for label, ifaces := range cases {
@ -354,6 +361,31 @@ func TestStartVM_RejectsZeroOrTwoPrimaries(t *testing.T) {
}
}
func TestStartVM_RejectsAnInterfaceWithoutMAC(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-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"},
},
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(), "interface 1: mac is required") {
t.Errorf("attendu 400 nommant l'interface 1, obtenu %d — %s", w.Code, w.Body.String())
}
if _, err := kv.GetFromDB(db, "vm/vm-nomac/state"); err == nil {
t.Error("une VM refusée ne doit laisser aucune trace en base")
}
}
func TestStartVM_RejectsReservedOrForeignAddresses(t *testing.T) {
cases := map[string]string{
"passerelle anycast": "10.0.0.254",
@ -373,7 +405,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, Primary: true}},
Interfaces: []VMInterface{{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()
@ -396,7 +428,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", Primary: true}},
Interfaces: []VMInterface{{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()
@ -412,7 +444,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", Primary: true}},
Interfaces: []VMInterface{{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()

View file

@ -70,7 +70,12 @@ func (s *Server) startVM(w http.ResponseWriter, r *http.Request) {
nics := make([]dispatcher.VMNIC, len(req.Interfaces))
primaries := 0
for i, iface := range req.Interfaces {
nics[i] = dispatcher.VMNIC{Subnet: iface.Subnet, IP: iface.IP, Primary: iface.Primary}
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}
if iface.Primary {
primaries++
}

View file

@ -1,19 +0,0 @@
package dhcp
import (
"git.g3e.fr/syonad/two/pkg/db/kv"
"github.com/dgraph-io/badger/v4"
)
func StoreDHCPEntries(db *badger.DB, subnetName string, entries map[string]string) error {
for ip, mac := range entries {
if err := kv.AddInDB(db, "subnet/"+subnetName+"/dhcp/"+ip, mac); err != nil {
return err
}
}
return nil
}
func GetMACForIP(db *badger.DB, subnetName, ip string) (string, error) {
return kv.GetFromDB(db, "subnet/"+subnetName+"/dhcp/"+ip)
}

View file

@ -17,35 +17,6 @@ func parseNet(t *testing.T, cidr string) *net.IPNet {
return network
}
// --- cloneIP ---
func TestCloneIP_IsIndependent(t *testing.T) {
ip := net.ParseIP("10.0.0.1").To4()
clone := cloneIP(ip)
clone[3] = 99
if ip[3] == 99 {
t.Error("cloneIP devrait retourner une copie indépendante")
}
}
// --- incrementIP ---
func TestIncrementIP_Simple(t *testing.T) {
ip := net.ParseIP("10.0.0.1").To4()
incrementIP(ip)
if ip.String() != "10.0.0.2" {
t.Errorf("attendu 10.0.0.2, obtenu %s", ip)
}
}
func TestIncrementIP_Carry(t *testing.T) {
ip := net.ParseIP("10.0.0.255").To4()
incrementIP(ip)
if ip.String() != "10.0.1.0" {
t.Errorf("attendu 10.0.1.0, obtenu %s", ip)
}
}
// --- GenerateConfig ---
func newConf(t *testing.T, cidr string) Config {
@ -65,7 +36,7 @@ func newConf(t *testing.T, cidr string) Config {
func TestGenerateConfig_CreatesFile(t *testing.T) {
conf := newConf(t, "192.168.1.0/29") // 6 hôtes
path, _, err := GenerateConfig(conf)
path, err := GenerateConfig(conf)
if err != nil {
t.Fatalf("GenerateConfig a échoué : %v", err)
}
@ -77,7 +48,7 @@ func TestGenerateConfig_CreatesFile(t *testing.T) {
func TestGenerateConfig_FilenameMatchesName(t *testing.T) {
conf := newConf(t, "192.168.1.0/29")
path, _, err := GenerateConfig(conf)
path, err := GenerateConfig(conf)
if err != nil {
t.Fatalf("GenerateConfig a échoué : %v", err)
}
@ -90,7 +61,7 @@ func TestGenerateConfig_FilenameMatchesName(t *testing.T) {
func TestGenerateConfig_ContainsDefaultGateway(t *testing.T) {
conf := newConf(t, "192.168.1.0/29")
path, _, _ := GenerateConfig(conf)
path, _ := GenerateConfig(conf)
content, _ := os.ReadFile(path)
if !strings.Contains(string(content), "dhcp-option=3,192.168.1.1") {
@ -101,7 +72,7 @@ func TestGenerateConfig_ContainsDefaultGateway(t *testing.T) {
func TestGenerateConfig_NoDefaultGateway(t *testing.T) {
conf := newConf(t, "192.168.1.0/29")
conf.DefaultGateway = nil
path, _, _ := GenerateConfig(conf)
path, _ := GenerateConfig(conf)
content, _ := os.ReadFile(path)
if strings.Contains(string(content), "dhcp-option=3,") {
@ -112,7 +83,7 @@ func TestGenerateConfig_NoDefaultGateway(t *testing.T) {
func TestGenerateConfig_NoDefaultGatewaySuppressesRouterOption(t *testing.T) {
conf := newConf(t, "192.168.1.0/29")
conf.DefaultGateway = nil
path, _, _ := GenerateConfig(conf)
path, _ := GenerateConfig(conf)
content, _ := os.ReadFile(path)
if !strings.Contains(string(content), "\ndhcp-option=3\n") {
@ -127,7 +98,7 @@ func TestGenerateConfig_NilDefaultGatewayEmitsNoRoute(t *testing.T) {
conf := newConf(t, "192.168.1.0/29")
conf.DefaultGateway = nil
path, _, _ := GenerateConfig(conf)
path, _ := GenerateConfig(conf)
content, _ := os.ReadFile(path)
if !strings.Contains(string(content), "dhcp-option=121,") {
@ -163,7 +134,7 @@ func TestGenerateConfig_ContainsDhcpRange(t *testing.T) {
Name: "vpc1",
ConfDir: t.TempDir(),
}
path, _, _ := GenerateConfig(conf)
path, _ := GenerateConfig(conf)
content, _ := os.ReadFile(path)
if !strings.Contains(string(content), "dhcp-range=10.10.0.0,static,255.255.255.0,12h") {
@ -171,18 +142,6 @@ func TestGenerateConfig_ContainsDhcpRange(t *testing.T) {
}
}
func TestGenerateConfig_OneEntryPerIP(t *testing.T) {
// /29 = 8 adresses. Les entrées ne vont plus dans le fichier dnsmasq mais
// dans la map retournée, que StoreDHCPEntries écrit en base pour GetMACForIP.
_, entries, err := GenerateConfig(newConf(t, "10.0.0.0/29"))
if err != nil {
t.Fatalf("GenerateConfig : %v", err)
}
if len(entries) != 8 {
t.Errorf("attendu 8 entrées ip->mac, obtenu %d", len(entries))
}
}
func TestGenerateConfig_NoPreGeneratedHosts(t *testing.T) {
// Une entrée dhcp-host pré-générée fait rejeter celle du dhcp-hostsdir
// (« duplicate dhcp-host IP address »), sans erreur : la VM reçoit alors
@ -212,18 +171,6 @@ func TestGenerateConfig_PointsToDirs(t *testing.T) {
}
}
func TestGenerateConfig_MACPrefix(t *testing.T) {
_, entries, err := GenerateConfig(newConf(t, "10.0.0.0/30"))
if err != nil {
t.Fatalf("GenerateConfig : %v", err)
}
for ip, mac := range entries {
if !strings.HasPrefix(mac, "00:22:33:") {
t.Errorf("mac de %s sans le préfixe 00:22:33: : %s", ip, mac)
}
}
}
func TestGenerateConfig_CreatesConfDir(t *testing.T) {
dir := filepath.Join(t.TempDir(), "sous", "dossier")
_, network, _ := net.ParseCIDR("10.0.0.0/30")
@ -233,7 +180,7 @@ func TestGenerateConfig_CreatesConfDir(t *testing.T) {
Name: "net",
ConfDir: dir,
}
if _, _, err := GenerateConfig(conf); err != nil {
if _, err := GenerateConfig(conf); err != nil {
t.Fatalf("GenerateConfig devrait créer les répertoires manquants : %v", err)
}
if _, err := os.Stat(dir); os.IsNotExist(err) {
@ -245,7 +192,7 @@ func TestGenerateConfig_CreatesConfDir(t *testing.T) {
func confLines(t *testing.T, c Config) string {
t.Helper()
path, _, err := GenerateConfig(c)
path, err := GenerateConfig(c)
if err != nil {
t.Fatalf("GenerateConfig : %v", err)
}
@ -312,7 +259,7 @@ func TestGenerateConfig_MissingInterfaceIPIsAnError(t *testing.T) {
conf := newConf(t, "192.168.1.0/29")
conf.InterfaceIP = nil
if _, _, err := GenerateConfig(conf); err == nil {
if _, err := GenerateConfig(conf); err == nil {
t.Error("sans interface_ip aucune route metadata n'est possible : il faut échouer, pas écrire une conf muette")
}
}
@ -321,7 +268,7 @@ func TestGenerateConfig_ListensOnTheAnycastGateway(t *testing.T) {
conf := newConf(t, "192.168.1.0/29")
conf.Listen = "gw-s9r1"
conf.Except = "br-s9r1"
path, _, _ := GenerateConfig(conf)
path, _ := GenerateConfig(conf)
content, _ := os.ReadFile(path)
for _, line := range []string{"\ninterface=gw-s9r1\n", "\nexcept-interface=br-s9r1\n"} {
@ -333,7 +280,7 @@ func TestGenerateConfig_ListensOnTheAnycastGateway(t *testing.T) {
func TestGenerateConfig_NoInterfaceLinesByDefault(t *testing.T) {
conf := newConf(t, "192.168.1.0/29")
path, _, _ := GenerateConfig(conf)
path, _ := GenerateConfig(conf)
content, _ := os.ReadFile(path)
if strings.Contains(string(content), "interface=") {

View file

@ -2,7 +2,6 @@ package dhcp
import (
"fmt"
"net"
"os"
"path/filepath"
"strings"
@ -10,9 +9,9 @@ import (
"git.g3e.fr/syonad/two/internal/metadata"
)
func GenerateConfig(c Config) (string, map[string]string, error) {
func GenerateConfig(c Config) (string, error) {
if c.InterfaceIP == nil {
return "", nil, fmt.Errorf("interface ip is required: guests would have no route to the metadata server")
return "", fmt.Errorf("interface ip is required: guests would have no route to the metadata server")
}
mask := fmt.Sprintf("%d.%d.%d.%d", c.Network.Mask[0], c.Network.Mask[1], c.Network.Mask[2], c.Network.Mask[3])
@ -35,26 +34,14 @@ func GenerateConfig(c Config) (string, map[string]string, error) {
fmt.Fprintf(&sb, "dhcp-hostsdir=%s\n", HostsDir(c.ConfDir, c.Name))
fmt.Fprintf(&sb, "dhcp-optsdir=%s\n", OptsDir(c.ConfDir, c.Name))
entries := Entries(c.Network)
for _, dir := range []string{c.ConfDir, HostsDir(c.ConfDir, c.Name), OptsDir(c.ConfDir, c.Name)} {
if err := os.MkdirAll(dir, 0755); err != nil {
return "", nil, fmt.Errorf("create %s: %w", dir, err)
return "", fmt.Errorf("create %s: %w", dir, err)
}
}
outPath := filepath.Join(c.ConfDir, c.Name+".conf")
return outPath, entries, os.WriteFile(outPath, []byte(sb.String()), 0644)
}
func Entries(network *net.IPNet) map[string]string {
entries := make(map[string]string)
i := 0
for ip := cloneIP(network.IP); network.Contains(ip); incrementIP(ip) {
entries[ip.String()] = fmt.Sprintf("00:22:33:%02X:%02X:%02X", (i>>16)&0xFF, (i>>8)&0xFF, i&0xFF)
i++
}
return entries
return outPath, os.WriteFile(outPath, []byte(sb.String()), 0644)
}
func classlessRoutes(c Config) []string {
@ -69,18 +56,3 @@ func classlessRoutes(c Config) []string {
}
return routes
}
func incrementIP(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] != 0 {
break
}
}
}
func cloneIP(ip net.IP) net.IP {
clone := make(net.IP, len(ip))
copy(clone, ip)
return clone
}

View file

@ -44,7 +44,7 @@ func (b Dnsmasq) config(s Subnet) dhcp.Config {
}
func (b Dnsmasq) ConfigureSubnet(s Subnet) error {
if _, _, err := dhcp.GenerateConfig(b.config(s)); err != nil {
if _, err := dhcp.GenerateConfig(b.config(s)); err != nil {
return fmt.Errorf("generate dhcp config: %w", err)
}

View file

@ -23,6 +23,7 @@ type VMDisk struct {
type VMNIC struct {
Subnet string
IP string
MAC string
Primary bool
}
@ -60,6 +61,9 @@ func (c StartVMCommand) Prepare(db *badger.DB, _ *configuration.Config) error {
if err := kv.AddInDB(db, prefix+"ip", n.IP); err != nil {
return fmt.Errorf("store nic %d ip: %w", i, err)
}
if err := kv.AddInDB(db, prefix+"mac", n.MAC); err != nil {
return fmt.Errorf("store nic %d mac: %w", i, err)
}
if n.Primary {
if err := kv.AddInDB(db, prefix+"primary", "true"); err != nil {
return fmt.Errorf("store nic %d primary: %w", i, err)

View file

@ -17,7 +17,7 @@ func TestStartVMCommand_Prepare_SingleDisk(t *testing.T) {
cmd := StartVMCommand{
Name: "vm-1",
NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.5", Primary: true}},
NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.5", MAC: "52:54:00:00:00:05", Primary: true}},
Disks: []VMDisk{{Path: "/data/root.qcow2", Dev: "sda"}},
}
if err := cmd.Prepare(db, nil); err != nil {
@ -41,7 +41,7 @@ func TestStartVMCommand_Prepare_MultiDisk(t *testing.T) {
cmd := StartVMCommand{
Name: "vm-2",
NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.6", Primary: true}},
NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.6", MAC: "52:54:00:00:00:05", Primary: true}},
Disks: []VMDisk{
{Path: "/data/root.qcow2", Dev: "sda"},
{Path: "/data/data.qcow2", Dev: "sdb"},
@ -74,7 +74,7 @@ func TestStartVMCommand_Prepare_SlotGap(t *testing.T) {
// sdb absent au boot — slot réservé pour hotplug
cmd := StartVMCommand{
Name: "vm-3",
NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.7", Primary: true}},
NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.7", MAC: "52:54:00:00:00:05", Primary: true}},
Disks: []VMDisk{
{Path: "/data/root.qcow2", Dev: "sda"},
{Path: "/data/extra.qcow2", Dev: "sdc"},
@ -103,7 +103,7 @@ func TestStartVMCommand_Prepare_NoVolumePath(t *testing.T) {
cmd := StartVMCommand{
Name: "vm-4",
NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.8", Primary: true}},
NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.8", MAC: "52:54:00:00:00:05", Primary: true}},
Disks: []VMDisk{{Path: "/data/root.qcow2", Dev: "sda"}},
}
if err := cmd.Prepare(db, nil); err != nil {
@ -166,7 +166,7 @@ func TestStartVMCommand_Prepare_Duplicate(t *testing.T) {
cmd := StartVMCommand{
Name: "vm-exist",
NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.9", Primary: true}},
NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.9", MAC: "52:54:00:00:00:05", Primary: true}},
Disks: []VMDisk{{Path: "/data/root.qcow2", Dev: "sda"}},
}
if err := cmd.Prepare(db, nil); err == nil {
@ -185,7 +185,7 @@ func prepareWithDocuments(t *testing.T, docs map[string]string) *badger.DB {
cmd := StartVMCommand{
Name: "vm-doc",
NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.5", Primary: true}},
NICs: []VMNIC{{Subnet: "sn-1", IP: "10.0.0.5", MAC: "52:54:00:00:00:05", Primary: true}},
Disks: []VMDisk{{Path: "/data/root.qcow2", Dev: "vda"}},
Documents: docs,
}

84
internal/migration/mac.go Normal file
View file

@ -0,0 +1,84 @@
package migration
import (
"fmt"
"log/slog"
"sort"
"strings"
"git.g3e.fr/syonad/two/internal/state"
"git.g3e.fr/syonad/two/pkg/db/kv"
"github.com/dgraph-io/badger/v4"
)
func MigrateVMMACs(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 nicsWithoutMAC(vms) {
name := strings.Split(prefix, "/")[1]
if st, err := state.Get(db, "vm/"+name); err == nil && st == state.Deleted {
continue
}
subnet, ip := vms[prefix+"subnet"], vms[prefix+"ip"]
if ip == "" {
return fmt.Errorf("%s has no ip: its mac cannot be recovered", strings.TrimSuffix(prefix, "/"))
}
mac, err := kv.GetFromDB(db, "subnet/"+subnet+"/dhcp/"+ip)
if err != nil {
return fmt.Errorf("%s: no mac for %s in subnet/%s/dhcp: %w", strings.TrimSuffix(prefix, "/"), ip, subnet, err)
}
if err := kv.AddInDB(db, prefix+"mac", mac); err != nil {
return fmt.Errorf("store %smac: %w", prefix, err)
}
log.Info("vm nic mac migrated", "resource", strings.TrimSuffix(prefix, "/"), "mac", mac, "reason", "mac now provided at creation")
}
subnets, err := kv.ListByPrefix(db, "subnet/")
if err != nil {
return fmt.Errorf("list subnet/: %w", err)
}
for _, name := range subnetsWithMACTable(subnets) {
if err := kv.DeleteInDB(db, "subnet/"+name+"/dhcp"); err != nil {
return fmt.Errorf("delete subnet/%s/dhcp: %w", name, err)
}
log.Info("preloaded mac table removed", "resource", "subnet/"+name, "reason", "mac now provided at creation")
}
return nil
}
func nicsWithoutMAC(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+"mac"]; !ok {
prefixes = append(prefixes, prefix)
}
}
sort.Strings(prefixes)
return prefixes
}
func subnetsWithMACTable(entries map[string]string) []string {
seen := make(map[string]bool)
for key := range entries {
parts := strings.SplitN(key, "/", 4)
if len(parts) == 4 && parts[2] == "dhcp" {
seen[parts[1]] = true
}
}
names := make([]string, 0, len(seen))
for name := range seen {
names = append(names, name)
}
sort.Strings(names)
return names
}

View file

@ -0,0 +1,116 @@
package migration
import (
"strings"
"testing"
"git.g3e.fr/syonad/two/pkg/db/kv"
"github.com/dgraph-io/badger/v4"
)
func seedPreloadedVM(t *testing.T, db *badger.DB, name, state string, nics map[int][2]string) {
t.Helper()
kv.AddInDB(db, "vm/"+name+"/state", state)
for idx, nic := range nics {
prefix := "vm/" + name + "/nic/" + string(rune('0'+idx)) + "/"
kv.AddInDB(db, prefix+"subnet", nic[0])
kv.AddInDB(db, prefix+"ip", nic[1])
}
}
func seedMACTable(t *testing.T, db *badger.DB, subnet string, macs map[string]string) {
t.Helper()
kv.AddInDB(db, "subnet/"+subnet+"/state", "running")
kv.AddInDB(db, "subnet/"+subnet+"/cidr", "10.0.0.0/24")
for ip, mac := range macs {
kv.AddInDB(db, "subnet/"+subnet+"/dhcp/"+ip, mac)
}
}
func assertKey(t *testing.T, db *badger.DB, key, want string) {
t.Helper()
got, err := kv.GetFromDB(db, key)
if err != nil || got != want {
t.Errorf("%s = %q (%v), attendu %q", key, got, err, want)
}
}
func assertNoMACTable(t *testing.T, db *badger.DB) {
t.Helper()
entries, err := kv.ListByPrefix(db, "subnet/")
if err != nil {
t.Fatalf("ListByPrefix : %v", err)
}
for key := range entries {
if strings.Contains(key, "/dhcp/") {
t.Errorf("clé de l'ancienne table restante : %s", key)
}
}
}
func TestMigrateVMMACs_CopiesTheMACOfEveryInterfaceThenDropsTheTable(t *testing.T) {
db := newNICDB(t)
seedMACTable(t, db, "sn-a", map[string]string{"10.1.0.10": "00:22:33:00:00:0A", "10.1.0.11": "00:22:33:00:00:0B"})
seedMACTable(t, db, "sn-b", map[string]string{"10.2.0.10": "00:22:33:00:00:0A"})
seedPreloadedVM(t, db, "vm-1", "running", map[int][2]string{0: {"sn-a", "10.1.0.10"}, 1: {"sn-b", "10.2.0.10"}})
if err := MigrateVMMACs(db, quietLog()); err != nil {
t.Fatalf("MigrateVMMACs : %v", err)
}
assertKey(t, db, "vm/vm-1/nic/0/mac", "00:22:33:00:00:0A")
assertKey(t, db, "vm/vm-1/nic/1/mac", "00:22:33:00:00:0A")
assertNoMACTable(t, db)
assertKey(t, db, "subnet/sn-a/cidr", "10.0.0.0/24")
}
func TestMigrateVMMACs_KeepsAnExistingMAC(t *testing.T) {
db := newNICDB(t)
seedMACTable(t, db, "sn-a", map[string]string{"10.1.0.10": "00:22:33:00:00:0A"})
seedPreloadedVM(t, db, "vm-1", "running", map[int][2]string{0: {"sn-a", "10.1.0.10"}})
kv.AddInDB(db, "vm/vm-1/nic/0/mac", "52:54:00:ab:cd:ef")
if err := MigrateVMMACs(db, quietLog()); err != nil {
t.Fatalf("MigrateVMMACs : %v", err)
}
assertKey(t, db, "vm/vm-1/nic/0/mac", "52:54:00:ab:cd:ef")
assertNoMACTable(t, db)
}
func TestMigrateVMMACs_IsIdempotent(t *testing.T) {
db := newNICDB(t)
seedMACTable(t, db, "sn-a", map[string]string{"10.1.0.10": "00:22:33:00:00:0A"})
seedPreloadedVM(t, db, "vm-1", "running", map[int][2]string{0: {"sn-a", "10.1.0.10"}})
for i := range 2 {
if err := MigrateVMMACs(db, quietLog()); err != nil {
t.Fatalf("passage %d : %v", i+1, err)
}
}
assertKey(t, db, "vm/vm-1/nic/0/mac", "00:22:33:00:00:0A")
}
func TestMigrateVMMACs_IgnoresDeletedVMs(t *testing.T) {
db := newNICDB(t)
seedPreloadedVM(t, db, "vm-gone", "deleted", map[int][2]string{0: {"sn-gone", "10.9.0.10"}})
if err := MigrateVMMACs(db, quietLog()); err != nil {
t.Fatalf("une VM supprimée dont le subnet a disparu ne doit pas bloquer le démarrage : %v", err)
}
if _, err := kv.GetFromDB(db, "vm/vm-gone/nic/0/mac"); err == nil {
t.Error("aucune mac ne doit être inventée pour une VM supprimée")
}
}
func TestMigrateVMMACs_AMissingEntryFailsAndKeepsTheTable(t *testing.T) {
db := newNICDB(t)
seedMACTable(t, db, "sn-a", map[string]string{"10.1.0.10": "00:22:33:00:00:0A"})
seedPreloadedVM(t, db, "vm-1", "running", map[int][2]string{0: {"sn-a", "10.1.0.99"}})
err := MigrateVMMACs(db, quietLog())
if err == nil || !strings.Contains(err.Error(), "vm/vm-1/nic/0") {
t.Fatalf("erreur = %v, attendu une erreur nommant vm/vm-1/nic/0", err)
}
assertKey(t, db, "subnet/sn-a/dhcp/10.1.0.10", "00:22:33:00:00:0A")
}

View file

@ -5,7 +5,6 @@ import (
"net"
configuration "git.g3e.fr/syonad/two/internal/config/agent"
"git.g3e.fr/syonad/two/internal/dhcp"
"git.g3e.fr/syonad/two/internal/dhcpbackend"
"git.g3e.fr/syonad/two/internal/ebtables"
"git.g3e.fr/syonad/two/internal/l3vni"
@ -139,7 +138,7 @@ func createSubnet(db *badger.DB, subnetName string, d subnetData, backend dhcpba
}
}
return startDHCP(db, subnetName, d, backend)
return startDHCP(subnetName, d, backend)
}
func setupVxlanHost(d subnetData, vethE string) error {
@ -165,16 +164,12 @@ func setupVxlanHost(d subnetData, vethE string) error {
return nil
}
func startDHCP(db *badger.DB, subnetName string, d subnetData, backend dhcpbackend.Backend) error {
func startDHCP(subnetName string, d subnetData, backend dhcpbackend.Backend) error {
defaultGateway, vpcRoute, err := dhcpRouting(d, netif.GetDefaultGateway)
if err != nil {
return err
}
if err := dhcp.StoreDHCPEntries(db, subnetName, dhcp.Entries(d.cidr)); err != nil {
return fmt.Errorf("store dhcp entries: %w", err)
}
listen := ""
if d.anycast() {
listen = d.gatewayLink()

View file

@ -10,7 +10,6 @@ import (
"git.g3e.fr/syonad/two/internal/netif"
"git.g3e.fr/syonad/two/internal/netns"
"git.g3e.fr/syonad/two/internal/state"
"git.g3e.fr/syonad/two/pkg/db/kv"
"github.com/dgraph-io/badger/v4"
)
@ -40,7 +39,7 @@ func DeleteSubnet(db *badger.DB, subnetName string, cfg *configuration.Config) e
}
}
if err := stopDHCP(db, subnetName, d, backend); err != nil {
if err := stopDHCP(subnetName, d, backend); err != nil {
return err
}
@ -60,19 +59,12 @@ func DeleteSubnet(db *badger.DB, subnetName string, cfg *configuration.Config) e
return state.Set(db, "subnet/"+subnetName, state.Deleted)
}
func stopDHCP(db *badger.DB, subnetName string, d subnetData, backend dhcpbackend.Backend) error {
if err := backend.TeardownSubnet(dhcpbackend.Subnet{
func stopDHCP(subnetName string, d subnetData, backend dhcpbackend.Backend) error {
return backend.TeardownSubnet(dhcpbackend.Subnet{
Name: subnetName,
VPC: d.vpc,
Bridge: d.bridge,
}); err != nil {
return err
}
if err := kv.DeleteInDB(db, "subnet/"+subnetName+"/dhcp"); err != nil {
return fmt.Errorf("delete dhcp entries: %w", err)
}
return nil
})
}
func deleteSubnetVxlan(d subnetData) error {

View file

@ -8,7 +8,6 @@ import (
"strconv"
"strings"
"git.g3e.fr/syonad/two/internal/dhcp"
"git.g3e.fr/syonad/two/pkg/db/kv"
"github.com/dgraph-io/badger/v4"
)
@ -215,11 +214,10 @@ func loadNIC(db *badger.DB, name string, idx int, entries map[string]string) (ni
return n, fmt.Errorf("nic %d of vm %s has no ip", idx, name)
}
mac, err := dhcp.GetMACForIP(db, n.subnetName, n.ip)
if err != nil {
return n, fmt.Errorf("get mac for ip %s: %w", n.ip, err)
n.mac = entries[prefix+"mac"]
if n.mac == "" {
return n, fmt.Errorf("nic %d of vm %s has no mac", idx, name)
}
n.mac = mac
if tapIDStr, ok := entries[prefix+"tap_id"]; ok {
tapID, err := strconv.Atoi(tapIDStr)

View file

@ -3,6 +3,7 @@ package vm
import (
"fmt"
"strconv"
"strings"
"testing"
"git.g3e.fr/syonad/two/pkg/db/kv"
@ -20,8 +21,8 @@ func newVMInDB(t *testing.T) *badger.DB {
kv.AddInDB(db, "subnet/sn-000001/interface_ip", "10.1.1.1")
kv.AddInDB(db, "subnet/sn-000001/mode", "vxlan")
kv.AddInDB(db, "vpc/vp-admin/cidr", "192.168.0.0/16")
kv.AddInDB(db, "subnet/sn-000001/dhcp/10.1.1.2", "00:22:33:00:01:02")
kv.AddInDB(db, "vm/vm-1/nic/0/ip", "10.1.1.2")
kv.AddInDB(db, "vm/vm-1/nic/0/mac", "00:22:33:00:01:02")
kv.AddInDB(db, "vm/vm-1/metadata_port", "8081")
kv.AddInDB(db, "vm/vm-1/disk/vda", "/data/root.qcow2")
kv.AddInDB(db, "vm/vm-1/memory", "2048")
@ -101,6 +102,7 @@ func addNIC(t *testing.T, db *badger.DB, idx int, subnet, ip, mac string, primar
prefix := fmt.Sprintf("vm/vm-1/nic/%d/", idx)
kv.AddInDB(db, prefix+"subnet", subnet)
kv.AddInDB(db, prefix+"ip", ip)
kv.AddInDB(db, prefix+"mac", mac)
if primary {
kv.AddInDB(db, prefix+"primary", "true")
}
@ -108,7 +110,6 @@ func addNIC(t *testing.T, db *badger.DB, idx int, subnet, ip, mac string, primar
kv.AddInDB(db, "subnet/"+subnet+"/interface_ip", "10.1.1.1")
kv.AddInDB(db, "subnet/"+subnet+"/mode", "vxlan")
kv.AddInDB(db, "vpc/vp-admin/cidr", "192.168.0.0/16")
kv.AddInDB(db, "subnet/"+subnet+"/dhcp/"+ip, mac)
}
func TestLoadVM_SingleNIC(t *testing.T) {
@ -143,6 +144,29 @@ func TestLoadVM_MultipleNICsSortedByIndex(t *testing.T) {
}
}
func TestLoadVM_MACComesFromTheInterface(t *testing.T) {
db := newVMInDB(t)
addNIC(t, db, 1, "sn-000002", "10.2.0.5", "52:54:00:ab:cd:ef", false)
d, err := loadVM(db, "vm-1")
if err != nil {
t.Fatalf("loadVM : %v", err)
}
if d.nics[1].mac != "52:54:00:ab:cd:ef" {
t.Errorf("mac = %q, attendu celle fournie à la création, sans lien avec l'IP", d.nics[1].mac)
}
}
func TestLoadVM_InterfaceWithoutMACIsAnError(t *testing.T) {
db := newVMInDB(t)
kv.AddInDB(db, "vm/vm-1/nic/1/subnet", "sn-000001")
kv.AddInDB(db, "vm/vm-1/nic/1/ip", "10.1.1.3")
if _, err := loadVM(db, "vm-1"); err == nil || !strings.Contains(err.Error(), "has no mac") {
t.Errorf("loadVM = %v, attendu une erreur sur la mac absente", err)
}
}
func TestLoadVM_TapAllocatedPerNIC(t *testing.T) {
db := newVMInDB(t)
addNIC(t, db, 1, "sn-000002", "10.2.0.5", "00:22:33:00:02:05", false)

View file

@ -8,7 +8,6 @@ import (
"strings"
dhcpapi "git.g3e.fr/syonad/two/internal/api/dhcp"
"git.g3e.fr/syonad/two/internal/dhcp"
"git.g3e.fr/syonad/two/internal/dhcpbackend"
"git.g3e.fr/syonad/two/internal/state"
"git.g3e.fr/syonad/two/internal/watchdog/notify"
@ -111,9 +110,9 @@ func expectedVMHosts(db *badger.DB, vmName, subnetName string) ([]dhcpapi.Host,
if ip == "" {
return nil, fmt.Errorf("nic %d of vm %s has no ip", idx, vmName)
}
mac, err := dhcp.GetMACForIP(db, subnetName, ip)
if err != nil {
return nil, fmt.Errorf("get mac for ip %s: %w", ip, err)
mac := entries[nic+"mac"]
if mac == "" {
return nil, fmt.Errorf("nic %d of vm %s has no mac", idx, vmName)
}
hosts = append(hosts, dhcpapi.Host{

View file

@ -69,13 +69,13 @@ func seedSubnetWithVM(t *testing.T, db *badger.DB, vmName, ip, mac string, prima
t.Helper()
seedKV(t, db, "subnet/"+testSubnetName+"/vpc", testVPC)
seedKV(t, db, "subnet/"+testSubnetName+"/dhcp/"+ip, mac)
if err := state.Set(db, "vm/"+vmName, state.Running); err != nil {
t.Fatalf("state.Set: %v", err)
}
seedKV(t, db, "vm/"+vmName+"/nic/0/subnet", testSubnetName)
seedKV(t, db, "vm/"+vmName+"/nic/0/ip", ip)
seedKV(t, db, "vm/"+vmName+"/nic/0/mac", mac)
seedKV(t, db, "vm/"+vmName+"/nic/0/primary", fmt.Sprintf("%t", primary))
}
@ -147,13 +147,14 @@ func TestExpectedHosts_SortsByMAC(t *testing.T) {
}
}
func TestExpectedHosts_ReportsAnIPWithoutAMAC(t *testing.T) {
func TestExpectedHosts_ReportsAnInterfaceWithoutAMAC(t *testing.T) {
db := newTestDB(t)
seedSubnetWithVM(t, db, "vm-web", "10.0.5.10", "00:22:33:00:00:0A", true)
seedKV(t, db, "vm/vm-web/nic/0/ip", "10.0.5.99")
seedResource(t, db, "vm/", "vm-web", state.Running)
seedKV(t, db, "vm/vm-web/nic/0/subnet", testSubnetName)
seedKV(t, db, "vm/vm-web/nic/0/ip", "10.0.5.10")
if _, _, err := expectedHosts(db, testSubnetName); err == nil {
t.Fatal("an ip absent from the address plan must be reported, not skipped")
t.Fatal("an interface without a mac must be reported, not skipped")
}
}