Merge branch 'feature-62'
All checks were successful
Documentation / publish (push) Successful in 1m30s
All checks were successful
Documentation / publish (push) Successful in 1m30s
This commit is contained in:
commit
9a6c9485b7
33 changed files with 491 additions and 204 deletions
|
|
@ -57,7 +57,7 @@ curl -X POST http://127.0.0.1:8080/vms \
|
|||
-d '{"name": "i-web", "memory": 2048, "cpus": 2,
|
||||
"metadata": {"sshkey": "ssh-ed25519 AAAA…",
|
||||
"user_data": "'"$(base64 -w0 < user-data.yml)"'"},
|
||||
"interfaces": [{"subnet": "sn-000001", "ip": "10.1.1.2", "primary": true}],
|
||||
"interfaces": [{"subnet": "sn-000001", "ip": "10.1.1.2", "mac": "52:54:00:01:01:02", "primary": true}],
|
||||
"storage": [{"path": "/data/disks/vms/i-web.qcow2", "dev": "vda"}]}'
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -534,7 +534,7 @@ components:
|
|||
|
||||
VMInterface:
|
||||
type: object
|
||||
required: [subnet, ip, primary]
|
||||
required: [subnet, ip, mac, primary]
|
||||
properties:
|
||||
subnet:
|
||||
type: string
|
||||
|
|
@ -543,6 +543,15 @@ components:
|
|||
type: string
|
||||
format: ipv4
|
||||
example: "10.0.0.5"
|
||||
mac:
|
||||
type: string
|
||||
pattern: '^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$'
|
||||
description: >
|
||||
MAC address of the interface, chosen by the caller. It is given to
|
||||
QEMU and to the DHCP reservation as is, and returned unchanged by
|
||||
GET. The agent does not check its format nor its uniqueness: that
|
||||
is the caller's job. Missing or empty → 400.
|
||||
example: "52:54:00:00:00:05"
|
||||
primary:
|
||||
type: boolean
|
||||
example: true
|
||||
|
|
|
|||
|
|
@ -63,6 +63,11 @@ func run(confFile string) int {
|
|||
return 1
|
||||
}
|
||||
|
||||
if err := migration.MigrateVMMACs(db, log.With(slog.String("component", "migration"))); err != nil {
|
||||
log.Error("vm mac migration failed", "error", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
if cfg.BGP.Enabled() {
|
||||
if err := applyBGPBase(cfg, frr.NewClient()); err != nil {
|
||||
log.Error("failed to apply the bgp base configuration", "error", err)
|
||||
|
|
@ -106,11 +111,18 @@ func run(confFile string) int {
|
|||
go adminSrv.Start()
|
||||
}
|
||||
|
||||
watchdogStopped := make(chan struct{})
|
||||
if cfg.Watchdog.Enabled {
|
||||
wlog := log.With(slog.String("component", "watchdog"))
|
||||
go watchdog.New(db, cfg, notify.NewStderr(wlog), wlog,
|
||||
w := watchdog.New(db, cfg, notify.NewStderr(wlog), wlog,
|
||||
time.Duration(cfg.Watchdog.IntervalSeconds)*time.Second,
|
||||
).Run(ctx)
|
||||
)
|
||||
go func() {
|
||||
defer close(watchdogStopped)
|
||||
w.Run(ctx)
|
||||
}()
|
||||
} else {
|
||||
close(watchdogStopped)
|
||||
}
|
||||
|
||||
<-ctx.Done()
|
||||
|
|
@ -120,7 +132,7 @@ func run(confFile string) int {
|
|||
if adminSrv != nil {
|
||||
servers["admin"] = adminSrv
|
||||
}
|
||||
closeDB = shutdown(log, q, servers, shutdownTimeout)
|
||||
closeDB = shutdown(log, q, servers, watchdogStopped, shutdownTimeout)
|
||||
return 0
|
||||
}
|
||||
|
||||
|
|
@ -144,7 +156,7 @@ type httpShutdowner interface {
|
|||
Shutdown(context.Context) error
|
||||
}
|
||||
|
||||
func shutdown(log *slog.Logger, q *worker.Queue, servers map[string]httpShutdowner, timeout time.Duration) bool {
|
||||
func shutdown(log *slog.Logger, q *worker.Queue, servers map[string]httpShutdowner, watchdogStopped <-chan struct{}, timeout time.Duration) bool {
|
||||
log.Info("shutting down", "timeout", timeout)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
|
|
@ -165,9 +177,16 @@ func shutdown(log *slog.Logger, q *worker.Queue, servers map[string]httpShutdown
|
|||
select {
|
||||
case <-drained:
|
||||
log.Info("workers drained")
|
||||
return true
|
||||
case <-ctx.Done():
|
||||
log.Error("workers still running after timeout, leaving database untouched")
|
||||
return false
|
||||
}
|
||||
|
||||
select {
|
||||
case <-watchdogStopped:
|
||||
return true
|
||||
case <-ctx.Done():
|
||||
log.Error("watchdog still running after timeout, leaving database untouched")
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ func TestShutdown_DrainReussi(t *testing.T) {
|
|||
api, prom := &fakeServer{}, &fakeServer{}
|
||||
servers := map[string]httpShutdowner{"api": api, "prometheus": prom}
|
||||
|
||||
if !shutdown(discardLogger(), q, servers, 5*time.Second) {
|
||||
if !shutdown(discardLogger(), q, servers, stopped(), 5*time.Second) {
|
||||
t.Fatal("un drainage réussi doit autoriser la fermeture de la base")
|
||||
}
|
||||
if !api.called.Load() || !prom.called.Load() {
|
||||
|
|
@ -65,7 +65,7 @@ func TestShutdown_TimeoutLaisseLaBaseIntacte(t *testing.T) {
|
|||
var buf strings.Builder
|
||||
log := slog.New(slog.NewTextHandler(&buf, nil))
|
||||
|
||||
if shutdown(log, q, map[string]httpShutdowner{}, 50*time.Millisecond) {
|
||||
if shutdown(log, q, map[string]httpShutdowner{}, stopped(), 50*time.Millisecond) {
|
||||
t.Fatal("un drainage incomplet ne doit pas autoriser la fermeture de la base")
|
||||
}
|
||||
if !strings.Contains(buf.String(), "leaving database untouched") {
|
||||
|
|
@ -84,7 +84,7 @@ func TestShutdown_ErreurServeurNEmpechePasLeDrainage(t *testing.T) {
|
|||
"prometheus": &fakeServer{},
|
||||
}
|
||||
|
||||
if !shutdown(log, q, servers, 5*time.Second) {
|
||||
if !shutdown(log, q, servers, stopped(), 5*time.Second) {
|
||||
t.Fatal("une erreur d'arrêt HTTP ne doit pas empêcher le drainage")
|
||||
}
|
||||
if !strings.Contains(buf.String(), "http server shutdown") {
|
||||
|
|
@ -96,7 +96,7 @@ func TestShutdown_SansServeur(t *testing.T) {
|
|||
q := worker.New(10)
|
||||
q.Start(1)
|
||||
|
||||
if !shutdown(discardLogger(), q, map[string]httpShutdowner{}, 5*time.Second) {
|
||||
if !shutdown(discardLogger(), q, map[string]httpShutdowner{}, stopped(), 5*time.Second) {
|
||||
t.Fatal("l'absence de serveur ne doit pas empêcher un arrêt propre")
|
||||
}
|
||||
}
|
||||
|
|
@ -108,6 +108,47 @@ func (f *fakeBase) ApplyBase(frr.Base) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func stopped() <-chan struct{} {
|
||||
c := make(chan struct{})
|
||||
close(c)
|
||||
return c
|
||||
}
|
||||
|
||||
func TestShutdown_AttendLaFinDuWatchdogAvantLaBase(t *testing.T) {
|
||||
q := worker.New(10)
|
||||
q.Start(1)
|
||||
|
||||
watchdogStopped := make(chan struct{})
|
||||
var finished atomic.Bool
|
||||
go func() {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
finished.Store(true)
|
||||
close(watchdogStopped)
|
||||
}()
|
||||
|
||||
if !shutdown(discardLogger(), q, map[string]httpShutdowner{}, watchdogStopped, 5*time.Second) {
|
||||
t.Fatal("un watchdog arrêté dans le délai doit autoriser la fermeture de la base")
|
||||
}
|
||||
if !finished.Load() {
|
||||
t.Error("la fermeture de la base a été autorisée avant la fin du passage du watchdog")
|
||||
}
|
||||
}
|
||||
|
||||
func TestShutdown_WatchdogEncoreEnCoursLaisseLaBaseIntacte(t *testing.T) {
|
||||
q := worker.New(10)
|
||||
q.Start(1)
|
||||
|
||||
var buf strings.Builder
|
||||
log := slog.New(slog.NewTextHandler(&buf, nil))
|
||||
|
||||
if shutdown(log, q, map[string]httpShutdowner{}, make(chan struct{}), 50*time.Millisecond) {
|
||||
t.Fatal("un watchdog encore en cours ne doit pas autoriser la fermeture de la base")
|
||||
}
|
||||
if !strings.Contains(buf.String(), "watchdog still running after timeout") {
|
||||
t.Errorf("le dépassement devrait être logué, obtenu %q", buf.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyBGPBase_RefusesAnInvalidSectionBeforeTouchingFRR(t *testing.T) {
|
||||
cfg := &configuration.Config{}
|
||||
cfg.BGP.RouteReflectors = []string{"10.255.255.1"}
|
||||
|
|
|
|||
|
|
@ -20,11 +20,11 @@ de sérialisation structurée.
|
|||
subnet/<name>/local_iface → <bridge-name>
|
||||
subnet/<name>/default_route → "true" | "false"
|
||||
subnet/<name>/gateway → <ip> (optionnel)
|
||||
subnet/<name>/dhcp/<ip> → <mac>
|
||||
|
||||
vm/<name>/state → creating | running | error | deleting | deleted
|
||||
vm/<name>/nic/<i>/subnet → <subnet-name> (une interface par index, ordre du tableau interfaces)
|
||||
vm/<name>/nic/<i>/ip → <ip>
|
||||
vm/<name>/nic/<i>/mac → <mac> (fournie par l'appelant, conservée telle quelle)
|
||||
vm/<name>/nic/<i>/primary → "true" (exactement une interface ; absent sur les autres)
|
||||
vm/<name>/nic/<i>/tap_id → <int> (alloué au premier démarrage, puis conservé)
|
||||
vm/<name>/metadata_port → <port>
|
||||
|
|
@ -48,6 +48,13 @@ lus depuis le subnet, leur source canonique. De même, la L3VNI d'un VPC n'est s
|
|||
et ``tap_id``. L'agent les migre au démarrage vers ``nic/0/…``, marquée ``primary``, avant
|
||||
d'ouvrir son API ; une VM qui a déjà des clés ``nic/`` est laissée telle quelle.
|
||||
|
||||
**Ancienne table des MAC.** Jusqu'en 0.1.0, la MAC d'une interface était dérivée du rang de son IP
|
||||
et préchargée pour tout le ``cidr`` sous ``subnet/<name>/dhcp/<ip>``. Au démarrage, l'agent
|
||||
recopie la MAC de chaque interface existante dans ``vm/<name>/nic/<i>/mac``, puis supprime ces
|
||||
clés. Les VM ``deleted`` sont ignorées. Une interface dont la MAC est introuvable fait échouer le
|
||||
démarrage en la nommant, et la table reste en place tant que toutes les interfaces ne sont pas
|
||||
migrées.
|
||||
|
||||
**Les états passent par ``state``.** Toujours ``state.Set`` / ``state.Get`` : ``Set`` refuse une
|
||||
valeur hors énumération, ``Get`` refuse de retourner une valeur non reconnue. ``error`` n'est
|
||||
écrit que par ``Dispatcher.Dispatch``, via ``cmd.Key()``.
|
||||
|
|
|
|||
|
|
@ -84,6 +84,10 @@ le nom de l'interface dans le guest. Exactement une interface doit être ``prima
|
|||
la route par défaut et le serveur de metadata. Tous les subnets d'une VM doivent appartenir au
|
||||
**même VPC**.
|
||||
|
||||
**MAC.** Chaque interface porte la MAC choisie par l'appelant, comme son IP. L'agent la donne
|
||||
telle quelle à QEMU et à la réservation DHCP, sans en vérifier le format ni l'unicité : c'est à
|
||||
l'appelant de garantir qu'elle est unique sur le segment L2.
|
||||
|
||||
**Routes.** Dans un VPC routé par ``l3vni``, chaque interface sur un subnet ``vxlan`` est annoncée
|
||||
en ``/32`` dans la VRF du VPC une fois QEMU démarré, juste avant le passage en ``running``. À la
|
||||
suppression, ces ``/32`` sont retirés **avant** l'arrêt de QEMU. Comme un subnet ne peut être
|
||||
|
|
@ -98,7 +102,7 @@ Ce qui est stocké, et où
|
|||
------------------------
|
||||
|
||||
Une ressource ne porte en base que ce qui lui est propre. Une VM stocke le **lien** vers son
|
||||
subnet (``vm/<name>/subnet``), pas le VPC ni le bridge ni la gateway : ces valeurs sont lues
|
||||
subnet (``vm/<name>/nic/<i>/subnet``), pas le VPC ni le bridge ni la gateway : ces valeurs sont lues
|
||||
depuis le subnet, leur source canonique. Le schéma complet des clés est dans
|
||||
:doc:`/architecture/stockage`.
|
||||
|
||||
|
|
|
|||
|
|
@ -93,7 +93,7 @@ Là encore, attendez ``running`` :
|
|||
"uefi": true,
|
||||
"metadata": {"sshkey": "ssh-ed25519 AAAA…",
|
||||
"user_data": "'"$(base64 < user-data.yml | tr -d '\n')"'"},
|
||||
"interfaces": [{"subnet": "sn-000001", "ip": "10.1.1.2", "primary": true}],
|
||||
"interfaces": [{"subnet": "sn-000001", "ip": "10.1.1.2", "mac": "52:54:00:01:01:02", "primary": true}],
|
||||
"storage": [{"path": "/var/lib/two/volumes/i-web.qcow2", "dev": "vda"}]}'
|
||||
|
||||
Quatre points qui coûtent du temps quand on les découvre en production :
|
||||
|
|
@ -106,6 +106,10 @@ Quatre points qui coûtent du temps quand on les découvre en production :
|
|||
Le champ attend la valeur de la clé ``passwd`` de cloud-config (``$6$…``). Sans ``password``
|
||||
ni ``sshkey``, aucun compte n'est créé.
|
||||
|
||||
Chaque interface a sa ``mac``
|
||||
Choisie par l'appelant, obligatoire (400 sinon) et jamais vérifiée : deux interfaces sur le
|
||||
même segment L2 doivent avoir des MAC distinctes.
|
||||
|
||||
Exactement une interface est ``primary``
|
||||
Elle porte la route par défaut et le serveur de metadata. L'ordre du tableau détermine le
|
||||
slot PCI (``0x03 + index``), donc le nom de l'interface dans le guest. Tous les subnets d'une
|
||||
|
|
|
|||
|
|
@ -294,7 +294,7 @@ Générée par ``test/e2e/run.sh --matrice`` à partir des en-têtes ; à régé
|
|||
* - ``11-validation``
|
||||
- validation de l'API — refus synchrones (400, 409, 422) des VPC, subnets et VM, sans trace en base
|
||||
- hv1
|
||||
- #41, #57, #33
|
||||
- #41, #57, #33, #62
|
||||
* - ``12-etats``
|
||||
- machine d'états — 409 pendant creating, suppression depuis error, dépendances VM → subnet → VPC, recréation sous le même nom
|
||||
- hv1
|
||||
|
|
@ -302,7 +302,7 @@ Générée par ``test/e2e/run.sh --matrice`` à partir des en-têtes ; à régé
|
|||
* - ``21-dhcp-two``
|
||||
- DHCP par le serveur intégré (backend two) — bail, server-id, renouvellement, option 121, un serveur par subnet, démarrages simultanés
|
||||
- hv1
|
||||
- #46, #57
|
||||
- #46, #57, #62
|
||||
* - ``22-dhcp-dnsmasq``
|
||||
- DHCP par dnsmasq (backend dnsmasq) — bail, server-id, renouvellement, option 121, une instance par subnet écoutant sur la gateway anycast
|
||||
- hv2
|
||||
|
|
@ -434,7 +434,9 @@ Chaque bloc est précédé des bibliothèques de ``test/e2e/lib/`` :
|
|||
|
||||
* ``core.sh`` : ``check``, ``known``, ``info``, appels à l'API, attente des états ;
|
||||
* ``vm.sh`` : image Debian compatible two, clé SSH, ``vm_launch`` (plusieurs NIC et disques, UEFI,
|
||||
``user_data``, mot de passe), SSH, bail, métadonnées ;
|
||||
``user_data``, mot de passe), SSH, bail, métadonnées. La MAC de chaque NIC est fournie à l'API :
|
||||
``52:54:00`` suivi des trois derniers octets de l'IP (``mac_for_ip``), unique dans la suite
|
||||
puisque chaque test a sa plage ; ``--nic sn=ip=mac`` en impose une autre ;
|
||||
* ``net.sh`` : VPC, subnets, MAC anycast, trames brutes, règles ebtables, VRRP ;
|
||||
* ``evpn.sh`` : VRF, BGP, L3VNI, watchdog ;
|
||||
* ``fixture.sh`` : nettoyage et ``assert_clean``, appelés par le runner.
|
||||
|
|
@ -463,8 +465,9 @@ Les règles :
|
|||
la cible est vivante et que le chemin du test fonctionne ;
|
||||
* **``vm_fails`` pour le négatif**, jamais ``!`` devant un ``vm_ssh`` : un SSH en panne doit
|
||||
échouer, pas passer pour une isolation ;
|
||||
* **une donnée qui distingue réellement les cas** : two dérive la MAC du rang de l'IP, donc deux
|
||||
subnets ont les mêmes MAC ; comparer des couples MAC/IP, pas des MAC ;
|
||||
* **une donnée qui distingue réellement les cas** : rien n'empêche deux subnets de porter les
|
||||
mêmes MAC (c'était le cas quand two les dérivait du rang de l'IP) ; comparer des couples MAC/IP,
|
||||
pas des MAC ;
|
||||
* **les mesures en ``INFO``**, les attentes en ``check`` : un temps de reconvergence se mesure,
|
||||
il ne se décrète pas ;
|
||||
* **l'en-tête ``# hôtes:`` liste exactement les hôtes où le test crée des ressources** : ce sont
|
||||
|
|
@ -500,7 +503,7 @@ hv2 sur dnsmasq — toute la série en 8 minutes :
|
|||
=== s6-rr-loss : 8 réussi(s), 0 échoué(s)
|
||||
|
||||
Dans ce run, le contrôle « uniquement les MAC de son subnet » de ``s1`` ne prouvait rien : two
|
||||
dérive la MAC du rang de l'IP, et les VM ``.10``/``.11`` des deux subnets avaient les mêmes MAC.
|
||||
dérivait alors la MAC du rang de l'IP, et les VM ``.10``/``.11`` des deux subnets avaient les mêmes MAC.
|
||||
Vérifié à la main sur le lab, chaque serveur DHCP ne connaissait que les couples MAC/IP de son
|
||||
subnet ; le scénario compare désormais ces couples.
|
||||
|
||||
|
|
|
|||
|
|
@ -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"`
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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++
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
@ -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=") {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
84
internal/migration/mac.go
Normal 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
|
||||
}
|
||||
116
internal/migration/mac_test.go
Normal file
116
internal/migration/mac_test.go
Normal 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")
|
||||
}
|
||||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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{
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -78,6 +78,15 @@ L3VNI, avec une configuration BGP pilotée par l'agent ; documentation du projet
|
|||
l'adresse de réseau ou de broadcast du subnet, ni une adresse hors de son `cidr` (422)
|
||||
- Le watchdog contrôle le macvlan, sa MAC, les deux adresses et l'entrée FDB
|
||||
|
||||
**MAC des VM fournie à la création (#62)**
|
||||
|
||||
- Champ `mac` obligatoire par interface dans `POST /vms`, rendu par `GET /vms/{name}`. Il est
|
||||
passé tel quel à QEMU et à la réservation DHCP ; son format et son unicité relèvent de l'appelant
|
||||
- Fin de la table `subnet/<name>/dhcp/<ip>`, qui préchargeait une MAC par adresse du `cidr` : la
|
||||
MAC dérivée du rang de l'IP donnait la même MAC à deux interfaces d'une VM de même rang dans
|
||||
leurs subnets (VM injoignable), et n'était pas tenable en IPv6
|
||||
- Migration au démarrage : les VM existantes gardent leur MAC, recopiée sur chaque interface
|
||||
|
||||
**Exploitation**
|
||||
|
||||
- Le watchdog contrôle aussi, avec une section `bgp` : les sessions vers les route reflectors ;
|
||||
|
|
@ -155,12 +164,14 @@ bash ./deploy.sh -t 0.2.0
|
|||
```
|
||||
|
||||
Sans `dhcp.backend` ni `bgp` dans `/etc/two/agent.yml`, le comportement est celui de la 0.1.0, à
|
||||
deux exceptions près, qui touchent les appelants de l'API :
|
||||
trois exceptions près, qui touchent les appelants de l'API :
|
||||
|
||||
- **`interface_ip` ne se fournit plus pour un subnet `vxlan`** : il est refusé (400), l'agent le
|
||||
calcule. Le retirer des appels de création, et ne plus placer de VM sur les deux dernières
|
||||
adresses utilisables du subnet. Il reste obligatoire en `bridge` et `public_ip`
|
||||
- **un subnet encore utilisé par une VM ne se supprime plus** : supprimer les VM d'abord
|
||||
- **chaque interface d'une VM doit porter sa `mac`** (400 sinon) : l'appelant la choisit et en
|
||||
garantit l'unicité. Les VM existantes gardent la leur, recopiée au premier démarrage de l'agent
|
||||
|
||||
Les subnets `vxlan` existants gardent leur adressage et le modèle précédent (gateway sur le
|
||||
bridge) jusqu'à leur recréation.
|
||||
|
|
|
|||
|
|
@ -81,7 +81,8 @@ watchdog_alerts () {
|
|||
|
||||
watchdog_quiet () {
|
||||
local out
|
||||
out=$(watchdog_alerts "${1:-300}" | { if [ -n "${2:-}" ]; then grep -vF -- "${2}"; else cat; fi; }) || return 0
|
||||
out=$(watchdog_alerts "${1:-300}" | { if [ -n "${2:-}" ]; then grep -vF -- "${2}"; else cat; fi; })
|
||||
[ -z "${out}" ] && return 0
|
||||
printf '%s\n' "${out}"
|
||||
return 1
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,13 +24,19 @@ vm_key () {
|
|||
cat "${VM_KEY}.pub"
|
||||
}
|
||||
|
||||
mac_for_ip () {
|
||||
local IFS=.
|
||||
set -- ${1}
|
||||
printf '52:54:00:%02x:%02x:%02x' "${2}" "${3}" "${4}"
|
||||
}
|
||||
|
||||
vm_create () {
|
||||
local name="${1}" subnet="${2}" ip="${3}" key="${4:-}" meta="{}"
|
||||
api_state "vms/${name}" | grep -qx running && return 0
|
||||
[ -n "${key}" ] && meta="{\"sshkey\":\"${key}\"}"
|
||||
mkdir -p "${VOLUMES}"
|
||||
cp "${TWO_IMAGE}" "${VOLUMES}/${name}.qcow2" || return 1
|
||||
api_post vms "{\"name\":\"${name}\",\"memory\":1024,\"cpus\":1,\"metadata\":${meta},\"interfaces\":[{\"subnet\":\"${subnet}\",\"ip\":\"${ip}\",\"primary\":true}],\"storage\":[{\"path\":\"${VOLUMES}/${name}.qcow2\",\"dev\":\"vda\"}]}" \
|
||||
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\"}]}" \
|
||||
&& wait_state "vms/${name}" running
|
||||
}
|
||||
|
||||
|
|
@ -83,16 +89,19 @@ 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","primary":true}],"storage":[{"path":"%s","dev":"%s"}]}' \
|
||||
"${name}" "${meta}" "${subnet}" "${ip}" "${disk}" "${dev}"
|
||||
printf '{"name":"%s","memory":1024,"cpus":1,"metadata":%s,"interfaces":[{"subnet":"%s","ip":"%s","mac":"%s","primary":true}],"storage":[{"path":"%s","dev":"%s"}]}' \
|
||||
"${name}" "${meta}" "${subnet}" "${ip}" "$(mac_for_ip "${ip}")" "${disk}" "${dev}"
|
||||
}
|
||||
|
||||
vm_json () {
|
||||
local name="${1}" nics='[]' disks='[]' meta='{}' uefi=false primary=true
|
||||
local name="${1}" nics='[]' disks='[]' meta='{}' uefi=false primary=true sn rest ip mac
|
||||
shift
|
||||
while [ $# -gt 0 ]; do
|
||||
case "${1}" in
|
||||
--nic) nics=$(jq -c --arg s "${2%%=*}" --arg ip "${2#*=}" --argjson p "${primary}" '. + [{subnet: $s, ip: $ip, primary: $p}]' <<<"${nics}"); primary=false; shift 2 ;;
|
||||
--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 ;;
|
||||
--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 ;;
|
||||
--password) meta=$(jq -c --arg v "${2}" '. + {password: $v}' <<<"${meta}"); shift 2 ;;
|
||||
|
|
|
|||
|
|
@ -155,6 +155,14 @@ test_vrf_members_lists_interface_names () {
|
|||
teardown
|
||||
}
|
||||
|
||||
test_mac_for_ip_uses_the_last_three_octets () {
|
||||
setup "mac_for_ip : 52:54:00 suivi des trois derniers octets de l'IP, en hexadécimal"
|
||||
[[ $(node 'mac_for_ip 10.25.1.30') == 52:54:00:19:01:1e ]] || fail "$(node 'mac_for_ip 10.25.1.30')"
|
||||
[[ $(node 'mac_for_ip 10.25.2.30') == 52:54:00:19:02:1e ]] || fail "même rang, subnet voisin : $(node 'mac_for_ip 10.25.2.30')"
|
||||
[[ $(node 'mac_for_ip 10.255.255.255') == 52:54:00:ff:ff:ff ]] || fail "$(node 'mac_for_ip 10.255.255.255')"
|
||||
teardown
|
||||
}
|
||||
|
||||
test_vm_create_is_idempotent () {
|
||||
setup "vm_create : VM déjà en running, rien n'est copié ni envoyé"
|
||||
[[ $(node 'api_state () { echo running; }; cp () { echo COPIE; }; api_post () { echo POST; }; vm_create v sn 10.0.0.10 && echo OK') == OK ]] \
|
||||
|
|
@ -224,6 +232,16 @@ test_api_code_returns_the_http_status () {
|
|||
teardown
|
||||
}
|
||||
|
||||
test_watchdog_quiet_passes_without_alert_and_fails_on_one () {
|
||||
setup "watchdog_quiet : réussit sans alerte, avec ou sans exclusion ; échoue sur une alerte non exclue"
|
||||
local alert='{"level":"ERROR","msg":"watchdog: inconsistency detected","problem":"stale dhcp reservation on the server"}'
|
||||
[[ $(node "journalctl () { :; }; watchdog_quiet 300 && echo OUI") == OUI ]] || fail "journal vide, sans exclusion : échec"
|
||||
[[ $(node "journalctl () { :; }; watchdog_quiet 300 'stale dhcp' && echo OUI") == OUI ]] || fail "journal vide, avec exclusion : échec"
|
||||
[[ $(node "journalctl () { echo '${alert}'; }; watchdog_quiet 300 >/dev/null || echo NON") == NON ]] || fail "alerte acceptée sans exclusion"
|
||||
[[ $(node "journalctl () { echo '${alert}'; }; watchdog_quiet 300 'stale dhcp' && echo OUI") == OUI ]] || fail "alerte exclue refusée"
|
||||
teardown
|
||||
}
|
||||
|
||||
test_watchdog_reported_matches_only_watchdog_alerts () {
|
||||
setup "watchdog_reported : alerte du watchdog contenant le texte, rien d'autre"
|
||||
local alert='{"level":"ERROR","msg":"watchdog: inconsistency detected","kind":"vpc","name":"vp-s8","problem":"route 10.242.1.99/32 exported in vrf-s8 without a running owner: its traffic is black-holed"}'
|
||||
|
|
@ -341,7 +359,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","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":[{"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==\"}" sdb | jq -c "[.metadata.user_data, .storage[0].path, .storage[0].dev]"') == '["YQ==","/x.qcow2","sdb"]' ]] \
|
||||
|| fail "options : $(node 'vm_body t11-b sn-t11a 10.11.1.11 /x.qcow2 "{\"user_data\":\"YQ==\"}" sdb')"
|
||||
|
|
@ -352,10 +370,12 @@ 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 sda=/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","primary":true},{"subnet":"sn-b","ip":"10.0.2.10","primary":false}],"storage":[{"path":"/v/a.qcow2","dev":"vda"},{"path":"/v/b.qcow2","dev":"sda"}],"uefi":true}' ]] \
|
||||
[[ "${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":"sda"}],"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","primary":true}],"storage":[],"uefi":false}' ]] || fail "minimal : ${out}"
|
||||
[[ "${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=$(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}"
|
||||
[[ $(node 'vm_json t || echo NON' 2>&1) == '{"name":"t","memory":1024,"cpus":1,"metadata":{},"interfaces":[],"storage":[],"uefi":false}' ]] || fail "vide"
|
||||
[[ $(node 'vm_json t --inconnue x || echo NON') == *NON ]] || fail "option inconnue acceptée"
|
||||
teardown
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# fonctionnalité: validation de l'API — refus synchrones (400, 409, 422) des VPC, subnets et VM, sans trace en base
|
||||
# hôtes: hv1
|
||||
# tickets: #41, #57, #33
|
||||
# tickets: #41, #57, #33, #62
|
||||
|
||||
on hv1 <<'NODE'
|
||||
check "VPC vp-t11a, l3vni 1100 (témoin)" vpc_create vp-t11a 10.11.0.0/16 1100
|
||||
|
|
@ -39,6 +39,7 @@ for case in "gateway calculée:sn-t11a:10.11.1.254" "adresse d'émission:sn-t11a
|
|||
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 : user_data en base64 invalide, 400" expect_code 400 api_post_code vms "$(vm_body t11-x sn-t11a 10.11.1.12 "${NONE}" '{"user_data":"pas du base64 !"}')"
|
||||
check "VMs refusées : aucune trace de t11-x" expect_code 404 api_code GET vms/t11-x
|
||||
cp "${TWO_IMAGE}" "${VOLUMES}/t11-ud.qcow2"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# fonctionnalité: DHCP par le serveur intégré (backend two) — bail, server-id, renouvellement, option 121, un serveur par subnet, démarrages simultanés
|
||||
# hôtes: hv1
|
||||
# tickets: #46, #57
|
||||
# tickets: #46, #57, #62
|
||||
|
||||
on hv1 <<'NODE'
|
||||
KEY=$(vm_key)
|
||||
|
|
@ -33,8 +33,13 @@ done
|
|||
check "10.21.1.10 : bail renouvelé (networkctl renew)" lease_renewed vp-t21 10.21.1.10
|
||||
check "10.21.1.10 : même adresse après le renouvellement" has_addr vp-t21 10.21.1.10
|
||||
check "10.21.1.10 : renouvellement servi par la gateway" test "$(vm_lease vp-t21 10.21.1.10 SERVER_ADDRESS)" = 10.21.1.254
|
||||
check "VM t21-m : MAC 02:de:ad:be:ef:01 fournie, sans lien avec son adresse 10.21.1.12" vm_launch t21-m --nic sn-t21a=10.21.1.12=02:de:ad:be:ef:01 --sshkey "${KEY}"
|
||||
check "t21-m joignable" vm_wait vp-t21 10.21.1.12
|
||||
check "t21-m : l'invité voit la MAC fournie sur ens3" test "$(vm_ssh vp-t21 10.21.1.12 'cat /sys/class/net/ens3/address')" = 02:de:ad:be:ef:01
|
||||
check "t21-m : bail de 10.21.1.12 servi par la gateway" test "$(vm_lease vp-t21 10.21.1.12 SERVER_ADDRESS)" = 10.21.1.254
|
||||
check "t21-m : l'API rend la MAC fournie" test "$(curl -fsS "${API}/vms/t21-m" | jq -r '.interfaces[0].mac')" = 02:de:ad:be:ef:01
|
||||
vm_host () { echo "$(vm_ssh vp-t21 "${1}" 'cat /sys/class/net/ens3/address')=${1}"; }
|
||||
A_HOSTS=$(printf '%s\n' "$(vm_host 10.21.1.10)" "$(vm_host 10.21.1.11)" | sort | tr '\n' ' ' | sed 's/ $//')
|
||||
A_HOSTS=$(printf '%s\n' "$(vm_host 10.21.1.10)" "$(vm_host 10.21.1.11)" "$(vm_host 10.21.1.12)" | sort | tr '\n' ' ' | sed 's/ $//')
|
||||
B_HOSTS=$(printf '%s\n' "$(vm_host 10.21.2.10)" "$(vm_host 10.21.2.11)" | sort | tr '\n' ' ' | sed 's/ $//')
|
||||
info "sn-t21a : ${A_HOSTS} ; sn-t21b : ${B_HOSTS}"
|
||||
check "probe sn-t21a : uniquement les couples MAC/IP de sn-t21a" test "$(dhcp_hosts vp-t21_br-t21a)" = "${A_HOSTS}"
|
||||
|
|
|
|||
|
|
@ -22,5 +22,5 @@ check "t25-m : route /32 des métadonnées présente" vm_grep vp-t25 10.25.1.10
|
|||
nic_macs () { ps -eo args | grep "[q]emu.*${1}" | grep -oE 'mac=[0-9A-Fa-f:]+' | sort | uniq | wc -l; }
|
||||
check "t25-m : deux MAC distinctes sur la ligne QEMU (témoin)" test "$(nic_macs t25-m)" -eq 2
|
||||
check "VM t25-r : deux NIC de même rang (10.25.1.30, 10.25.2.30)" vm_launch t25-r --nic sn-t25a=10.25.1.30 --nic sn-t25b=10.25.2.30 --sshkey "${KEY}"
|
||||
known "#62" "t25-r : deux NIC de même rang reçoivent des MAC distinctes" test "$(nic_macs t25-r)" -eq 2
|
||||
check "t25-r : deux NIC de même rang reçoivent des MAC distinctes" test "$(nic_macs t25-r)" -eq 2
|
||||
NODE
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue