Merge branch 'feature-88'
This commit is contained in:
commit
8f1cb96a82
44 changed files with 2158 additions and 48 deletions
173
api/agent.yaml
173
api/agent.yaml
|
|
@ -189,7 +189,7 @@ paths:
|
||||||
"404":
|
"404":
|
||||||
$ref: "#/components/responses/NotFound"
|
$ref: "#/components/responses/NotFound"
|
||||||
"409":
|
"409":
|
||||||
description: VM not stoppable — only running or error states can be stopped
|
description: VM not stoppable — only running or error states can be stopped, and its floating IPs must be deleted first
|
||||||
content:
|
content:
|
||||||
application/json:
|
application/json:
|
||||||
schema:
|
schema:
|
||||||
|
|
@ -394,6 +394,101 @@ paths:
|
||||||
"500":
|
"500":
|
||||||
$ref: "#/components/responses/InternalError"
|
$ref: "#/components/responses/InternalError"
|
||||||
|
|
||||||
|
# ── Floating IP ────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/floating-ips:
|
||||||
|
get:
|
||||||
|
summary: List all floating IPs of this agent
|
||||||
|
operationId: listFloatingIPs
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: List of floating IPs
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: array
|
||||||
|
items:
|
||||||
|
$ref: "#/components/schemas/FloatingIP"
|
||||||
|
"500":
|
||||||
|
$ref: "#/components/responses/InternalError"
|
||||||
|
|
||||||
|
post:
|
||||||
|
summary: Attach a floating IP to an interface of a VM of this agent
|
||||||
|
operationId: createFloatingIP
|
||||||
|
description: >-
|
||||||
|
Makes an interface of a VM reachable through a public IP it does not carry: stateless 1:1
|
||||||
|
translation in the netns of the VPC, on the host of the VM, the public IP answering ARP on
|
||||||
|
the public bridge (interfaces.internet). The VM leaves through its floating IP, even in a
|
||||||
|
VPC with a NAT gateway. To move it with the VM, delete it on the old host, then create it
|
||||||
|
on the new one. Attach and detach without touching the VM.
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/FloatingIPCreateRequest"
|
||||||
|
responses:
|
||||||
|
"202":
|
||||||
|
description: Floating IP creation accepted
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/FloatingIP"
|
||||||
|
"400":
|
||||||
|
description: Missing required field, invalid request body, ip without prefix length or not IPv4, or gateway not IPv4
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Error"
|
||||||
|
"409":
|
||||||
|
description: Floating IP already exists, VM unknown on this agent or not running, no interface of that name on the VM, interface that already has a floating IP, or public IP already used by another floating IP of this agent
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Error"
|
||||||
|
"500":
|
||||||
|
$ref: "#/components/responses/InternalError"
|
||||||
|
|
||||||
|
/floating-ips/{name}:
|
||||||
|
parameters:
|
||||||
|
- $ref: "#/components/parameters/ResourceName"
|
||||||
|
|
||||||
|
get:
|
||||||
|
summary: Get floating IP status and info
|
||||||
|
operationId: getFloatingIP
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: Floating IP found
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/FloatingIP"
|
||||||
|
"404":
|
||||||
|
$ref: "#/components/responses/NotFound"
|
||||||
|
"500":
|
||||||
|
$ref: "#/components/responses/InternalError"
|
||||||
|
|
||||||
|
delete:
|
||||||
|
summary: Detach and delete a floating IP
|
||||||
|
operationId: deleteFloatingIP
|
||||||
|
responses:
|
||||||
|
"202":
|
||||||
|
description: Floating IP deletion accepted
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/FloatingIP"
|
||||||
|
"404":
|
||||||
|
$ref: "#/components/responses/NotFound"
|
||||||
|
"409":
|
||||||
|
description: Floating IP not deletable — only running or error states can be deleted
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Error"
|
||||||
|
"500":
|
||||||
|
$ref: "#/components/responses/InternalError"
|
||||||
|
|
||||||
components:
|
components:
|
||||||
|
|
||||||
parameters:
|
parameters:
|
||||||
|
|
@ -636,8 +731,17 @@ components:
|
||||||
|
|
||||||
VMInterface:
|
VMInterface:
|
||||||
type: object
|
type: object
|
||||||
required: [subnet, ip, mac, primary]
|
required: [name, subnet, ip, mac, primary]
|
||||||
properties:
|
properties:
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
description: >
|
||||||
|
Name of the interface, chosen by the caller and unique within the VM. It is an API
|
||||||
|
identifier only, used to point at the interface (a floating IP for instance): no
|
||||||
|
Linux interface is named after it, neither in the guest nor on the host. The agent
|
||||||
|
does not check its format. Missing, empty or used twice in the VM → 400. Interfaces
|
||||||
|
of VMs created before it was required are named nic-<index> at agent start.
|
||||||
|
example: nic-a1b2
|
||||||
subnet:
|
subnet:
|
||||||
type: string
|
type: string
|
||||||
example: sn-00001
|
example: sn-00001
|
||||||
|
|
@ -668,6 +772,24 @@ components:
|
||||||
of the subnet: restricting it is the caller's job. Always returned
|
of the subnet: restricting it is the caller's job. Always returned
|
||||||
by GET.
|
by GET.
|
||||||
example: false
|
example: false
|
||||||
|
floating_ip:
|
||||||
|
readOnly: true
|
||||||
|
description: >
|
||||||
|
Floating IP attached to this interface, computed on read from /floating-ips; absent
|
||||||
|
when the interface has none. Refused in a creation request (400): a floating IP is
|
||||||
|
attached with /floating-ips.
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
example: fip-7
|
||||||
|
ip:
|
||||||
|
type: string
|
||||||
|
example: "203.0.113.20/24"
|
||||||
|
state:
|
||||||
|
type: string
|
||||||
|
enum: [creating, running, error, deleting, deleted]
|
||||||
|
example: running
|
||||||
|
|
||||||
VMStorage:
|
VMStorage:
|
||||||
type: object
|
type: object
|
||||||
|
|
@ -761,6 +883,53 @@ components:
|
||||||
type: string
|
type: string
|
||||||
example: "198.51.100.1"
|
example: "198.51.100.1"
|
||||||
|
|
||||||
|
FloatingIPCreateRequest:
|
||||||
|
type: object
|
||||||
|
required: [name, vm, interface, ip, gateway]
|
||||||
|
properties:
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
example: fip-7
|
||||||
|
vm:
|
||||||
|
type: string
|
||||||
|
description: VM of the interface; must be running on this agent
|
||||||
|
example: vm-7
|
||||||
|
interface:
|
||||||
|
type: string
|
||||||
|
description: Name of the interface of the VM (VMInterface.name), not a Linux interface
|
||||||
|
example: nic-a1b2
|
||||||
|
ip:
|
||||||
|
type: string
|
||||||
|
description: Public IP with its prefix length, chosen by the caller and not checked by the agent
|
||||||
|
example: "203.0.113.20/24"
|
||||||
|
gateway:
|
||||||
|
type: string
|
||||||
|
description: Gateway of the border routers on the public segment, not checked by the agent
|
||||||
|
example: "203.0.113.1"
|
||||||
|
|
||||||
|
FloatingIP:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
example: fip-7
|
||||||
|
state:
|
||||||
|
type: string
|
||||||
|
enum: [creating, running, error, deleting, deleted]
|
||||||
|
example: running
|
||||||
|
vm:
|
||||||
|
type: string
|
||||||
|
example: vm-7
|
||||||
|
interface:
|
||||||
|
type: string
|
||||||
|
example: nic-a1b2
|
||||||
|
ip:
|
||||||
|
type: string
|
||||||
|
example: "203.0.113.20/24"
|
||||||
|
gateway:
|
||||||
|
type: string
|
||||||
|
example: "203.0.113.1"
|
||||||
|
|
||||||
Error:
|
Error:
|
||||||
type: object
|
type: object
|
||||||
properties:
|
properties:
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,11 @@ func run(confFile string) int {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := migration.MigrateVMNICNames(db, log.With(slog.String("component", "migration"))); err != nil {
|
||||||
|
log.Error("vm nic name migration failed", "error", err)
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
if err := migration.MigrateVMDisks(db, log.With(slog.String("component", "migration"))); err != nil {
|
if err := migration.MigrateVMDisks(db, log.With(slog.String("component", "migration"))); err != nil {
|
||||||
log.Error("vm disk migration failed", "error", err)
|
log.Error("vm disk migration failed", "error", err)
|
||||||
return 1
|
return 1
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ de sérialisation structurée.
|
||||||
subnet/<name>/gateway → <ip> (optionnel)
|
subnet/<name>/gateway → <ip> (optionnel)
|
||||||
|
|
||||||
vm/<name>/state → creating | running | error | deleting | deleted
|
vm/<name>/state → creating | running | error | deleting | deleted
|
||||||
|
vm/<name>/nic/<i>/name → <nom> (choisi par l'appelant, unique dans la VM)
|
||||||
vm/<name>/nic/<i>/subnet → <subnet-name> (une interface par index, ordre du tableau interfaces)
|
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>/ip → <ip>
|
||||||
vm/<name>/nic/<i>/mac → <mac> (fournie par l'appelant, conservée telle quelle)
|
vm/<name>/nic/<i>/mac → <mac> (fournie par l'appelant, conservée telle quelle)
|
||||||
|
|
@ -42,6 +43,12 @@ de sérialisation structurée.
|
||||||
nat-gateway/<name>/ip → <ip>/<préfixe> (IP de sortie, telle que fournie)
|
nat-gateway/<name>/ip → <ip>/<préfixe> (IP de sortie, telle que fournie)
|
||||||
nat-gateway/<name>/gateway → <ip> (gateway des bordures)
|
nat-gateway/<name>/gateway → <ip> (gateway des bordures)
|
||||||
|
|
||||||
|
floating-ip/<name>/state → creating | running | error | deleting | deleted
|
||||||
|
floating-ip/<name>/vm → <vm-name>
|
||||||
|
floating-ip/<name>/interface → <nom> (nom de l'interface de la VM ; l'IP privée y est lue)
|
||||||
|
floating-ip/<name>/ip → <ip>/<préfixe> (IP publique, telle que fournie)
|
||||||
|
floating-ip/<name>/gateway → <ip> (gateway des bordures)
|
||||||
|
|
||||||
Règles
|
Règles
|
||||||
------
|
------
|
||||||
|
|
||||||
|
|
@ -61,6 +68,9 @@ clés. Les VM ``deleted`` sont ignorées. Une interface dont la MAC est introuva
|
||||||
démarrage en la nommant, et la table reste en place tant que toutes les interfaces ne sont pas
|
démarrage en la nommant, et la table reste en place tant que toutes les interfaces ne sont pas
|
||||||
migrées.
|
migrées.
|
||||||
|
|
||||||
|
**Interfaces sans nom.** Avant la 0.3.0, une interface n'avait pas de nom. Au démarrage, l'agent
|
||||||
|
nomme ``nic-<i>`` chaque interface qui n'en a pas, d'après son rang ; un nom existant est gardé.
|
||||||
|
|
||||||
**Anciens noms de disques.** Jusqu'en 0.1.0, les disques additionnels s'appelaient ``sdX``. Au
|
**Anciens noms de disques.** Jusqu'en 0.1.0, les disques additionnels s'appelaient ``sdX``. Au
|
||||||
démarrage, l'agent renomme chaque clé ``vm/<name>/disk/sdX`` en ``vm/<name>/disk/xvdX`` (même
|
démarrage, l'agent renomme chaque clé ``vm/<name>/disk/sdX`` en ``vm/<name>/disk/xvdX`` (même
|
||||||
lettre, même cible SCSI). Si ``xvdX`` existe déjà avec un autre chemin, le démarrage échoue en
|
lettre, même cible SCSI). Si ``xvdX`` existe déjà avec un autre chemin, le démarrage échoue en
|
||||||
|
|
|
||||||
|
|
@ -14,3 +14,4 @@ fonctionnent entre eux. Le contrat HTTP correspondant est dans
|
||||||
metadata-cloud-init
|
metadata-cloud-init
|
||||||
dns-ntp
|
dns-ntp
|
||||||
passerelle-nat
|
passerelle-nat
|
||||||
|
ip-flottante
|
||||||
|
|
|
||||||
82
docs/concepts/ip-flottante.rst
Normal file
82
docs/concepts/ip-flottante.rst
Normal file
|
|
@ -0,0 +1,82 @@
|
||||||
|
IP flottante
|
||||||
|
============
|
||||||
|
|
||||||
|
Une IP flottante rend une interface de VM **joignable par une IP publique qu'elle ne porte pas**.
|
||||||
|
C'est une ressource de l'API de l'agent (``/floating-ips``), créée sur l'hyperviseur de la VM :
|
||||||
|
la VM garde son adresse privée, et le netns de son VPC traduit l'IP publique vers cette adresse,
|
||||||
|
dans les deux sens, sans suivi de connexion propre à la traduction.
|
||||||
|
|
||||||
|
.. code-block:: text
|
||||||
|
|
||||||
|
POST /floating-ips
|
||||||
|
{ "name": "fip-7", "vm": "vm-7", "interface": "nic-a1b2",
|
||||||
|
"ip": "203.0.113.20/24", "gateway": "203.0.113.1" }
|
||||||
|
|
||||||
|
``interface`` est le **nom** d'une interface de la VM (``name`` de l'interface, voir
|
||||||
|
:doc:`vpc-subnet-vm`), pas une interface Linux. L'IP publique, son préfixe et la gateway des
|
||||||
|
bordures sont fournis par l'appelant ; l'agent ne les vérifie pas. Une interface a au plus une IP
|
||||||
|
flottante, et une IP publique n'est portée que par une IP flottante de l'hôte. Le VPC n'a pas
|
||||||
|
besoin de ``l3vni``.
|
||||||
|
|
||||||
|
On attache et détache une IP flottante sans toucher à la VM. Le ``GET`` d'une VM l'affiche sur
|
||||||
|
l'interface concernée (``floating_ip`` : nom, IP, état), en lecture seule ; la suppression d'une
|
||||||
|
VM est refusée (409) tant qu'une IP flottante y est attachée.
|
||||||
|
|
||||||
|
Ce que l'agent pose
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
Dans le netns du VPC de l'interface, sur l'hôte de la VM :
|
||||||
|
|
||||||
|
* un ``macvlan`` ``fip-<IP publique en hexadécimal>`` (``203.0.113.20`` → ``fip-cb007114``) sur
|
||||||
|
le bridge ``br-public`` du netns. Sa MAC est ``02:05`` suivi des quatre octets de l'IP publique
|
||||||
|
(``02:05:cb:00:71:14``) : unique dans l'infrastructure, la même sur tout hôte ;
|
||||||
|
* une entrée FDB locale de cette MAC, posée par remplacement (le bridge a pu l'apprendre sur le
|
||||||
|
segment tant que l'IP flottante vivait sur un autre hôte), et ``arp_ignore=1``,
|
||||||
|
``arp_announce=2``, ``arp_notify=1``, comme pour la :doc:`passerelle NAT <passerelle-nat>` ;
|
||||||
|
* deux chaînes dans la table nftables ``ip two`` :
|
||||||
|
|
||||||
|
* ``pre-fip-…``, en ``prerouting`` à la priorité ``raw`` : ce qui arrive par ``fip-…`` vers
|
||||||
|
l'IP publique est réécrit vers l'adresse privée, **avant** conntrack ;
|
||||||
|
* ``post-fip-…``, en ``postrouting`` : ce que la VM envoie par ``fip-…`` repart avec l'IP
|
||||||
|
publique pour source ;
|
||||||
|
|
||||||
|
conntrack ne voit que l'échange entre l'adresse privée et l'extérieur. Il n'y a pas de
|
||||||
|
``notrack`` : la redirection des métadonnées et le relais DNS et NTP, qui reposent sur
|
||||||
|
conntrack, restent servis à la VM. L'entrée est ouverte : c'est le but d'une IP flottante ;
|
||||||
|
* l'IP publique en ``/32`` sur ``fip-…`` : aucune route du segment public n'est ajoutée à la table
|
||||||
|
``main`` du netns ;
|
||||||
|
* une table de routage propre, numérotée par l'adresse privée lue comme un entier sur 32 bits
|
||||||
|
(``10.47.1.10`` → ``170852618``), qui porte la route du segment public par ``fip-…`` puis la
|
||||||
|
route par défaut vers la gateway des bordures ;
|
||||||
|
* deux règles de routage pour l'adresse privée :
|
||||||
|
|
||||||
|
* ``100: from <privée> to <cidr du VPC> lookup main`` : le trafic interne au VPC suit les routes
|
||||||
|
habituelles ;
|
||||||
|
* ``101: from <privée> lookup <table propre>`` : tout le reste sort par l'IP flottante, y compris
|
||||||
|
dans un VPC qui a une passerelle NAT, et sur l'hôte de celle-ci.
|
||||||
|
|
||||||
|
La table ``local`` passant avant ces règles, le trafic vers les adresses du netns (gateway,
|
||||||
|
métadonnées, DNS et NTP) n'est pas concerné.
|
||||||
|
|
||||||
|
Les règles nftables sont posées avant l'adresse et les routes : rien ne sort par ``fip-…`` sans
|
||||||
|
traduction. La suppression retire d'abord les règles de routage, puis les règles nftables,
|
||||||
|
l'entrée FDB et le ``macvlan``, dont les routes partent avec lui. Chaque étape tolère ce qui est
|
||||||
|
déjà absent : une suppression interrompue se relance depuis ``error``.
|
||||||
|
|
||||||
|
Le segment public n'apparaît pas dans ``main`` du fait de l'IP flottante : sinon, une VM sans IP
|
||||||
|
flottante du même netns pourrait sortir par ``fip-…`` sans traduction, et la réponse d'une VM à
|
||||||
|
IP flottante vers une adresse du segment (la bordure elle-même) sortirait par la passerelle NAT
|
||||||
|
avec son adresse privée.
|
||||||
|
|
||||||
|
Déplacer une IP flottante
|
||||||
|
-------------------------
|
||||||
|
|
||||||
|
Quand la VM change d'hôte, l'appelant supprime l'IP flottante sur l'ancien hôte, puis la crée sur
|
||||||
|
le nouveau. La MAC étant la même, le déplacement se réduit à un ARP gratuit et à la mobilité MAC
|
||||||
|
d'EVPN. Les connexions en cours sont perdues.
|
||||||
|
|
||||||
|
Limites
|
||||||
|
-------
|
||||||
|
|
||||||
|
* IPv4 seulement.
|
||||||
|
* Une VM qui joint l'IP flottante d'une autre VM passe par les bordures.
|
||||||
|
|
@ -92,6 +92,9 @@ Si l'hôte de la passerelle tombe, ses routes restent annoncées jusqu'à l'expi
|
||||||
BGP vers les route reflectors (180 s), et le VPC n'a plus de sortie jusqu'à la création d'une
|
BGP vers les route reflectors (180 s), et le VPC n'a plus de sortie jusqu'à la création d'une
|
||||||
nouvelle passerelle.
|
nouvelle passerelle.
|
||||||
|
|
||||||
|
Une VM qui a une :doc:`IP flottante <ip-flottante>` ne sort pas par la passerelle : elle sort par
|
||||||
|
son IP flottante, y compris sur l'hôte de la passerelle.
|
||||||
|
|
||||||
Limites
|
Limites
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -117,7 +117,12 @@ VM
|
||||||
Une VM est un processus QEMU/KVM raccordé à un ou plusieurs subnets par des taps.
|
Une VM est un processus QEMU/KVM raccordé à un ou plusieurs subnets par des taps.
|
||||||
|
|
||||||
**Interfaces.** L'ordre du tableau ``interfaces`` détermine le slot PCI (``0x03 + index``), donc
|
**Interfaces.** L'ordre du tableau ``interfaces`` détermine le slot PCI (``0x03 + index``), donc
|
||||||
le nom de l'interface dans le guest. Exactement une interface doit être ``primary`` : elle porte
|
le nom de l'interface dans le guest. Chaque interface porte un ``name``, obligatoire et unique dans
|
||||||
|
la VM, choisi par l'appelant (``nic-a1b2`` par exemple) : c'est l'identifiant stable par lequel
|
||||||
|
l'API désigne une interface, son rang pouvant changer. Aucune interface Linux n'en tire son nom,
|
||||||
|
ni dans le guest ni sur l'hôte. Une :doc:`IP flottante <ip-flottante>` désigne l'interface par ce
|
||||||
|
nom ; le ``GET`` de la VM l'affiche sur l'interface (``floating_ip``, en lecture seule, refusé à la
|
||||||
|
création), et la suppression de la VM est refusée (409) tant qu'elle est attachée. Exactement une interface doit être ``primary`` : elle porte
|
||||||
la route par défaut et le serveur de metadata. Tous les subnets d'une VM doivent appartenir au
|
la route par défaut et le serveur de metadata. Tous les subnets d'une VM doivent appartenir au
|
||||||
**même VPC**.
|
**même VPC**.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -400,6 +400,10 @@ Générée par ``test/e2e/run.sh --matrice`` à partir des en-têtes ; à régé
|
||||||
- passerelle NAT d'un VPC — macvlan ng-<id> sur le br-public du netns, MAC 02:04:00 + l3vni, SNAT vers l'IP de sortie (source vue à la sortie du netns), route par défaut en type 5 importée par un autre hôte, unreachable default jamais annoncée, rien n'entre sans connexion établie, refus 409, suppression propre, bascule vers un autre hôte par mobilité MAC
|
- passerelle NAT d'un VPC — macvlan ng-<id> sur le br-public du netns, MAC 02:04:00 + l3vni, SNAT vers l'IP de sortie (source vue à la sortie du netns), route par défaut en type 5 importée par un autre hôte, unreachable default jamais annoncée, rien n'entre sans connexion établie, refus 409, suppression propre, bascule vers un autre hôte par mobilité MAC
|
||||||
- hv1 hv2 hv3
|
- hv1 hv2 hv3
|
||||||
- #42
|
- #42
|
||||||
|
* - ``47-ip-flottante``
|
||||||
|
- IP flottante d'une interface de VM — macvlan fip-<hex> sur le br-public du netns, MAC 02:05 + IP publique, NAT 1:1 sans état, routage par source (sortie par l'IP flottante, pas par la passerelle NAT du VPC), entrée neuve depuis le segment public, métadonnées servies, affichage sur la VM, refus 409, détachement, déplacement vers un autre hôte par mobilité MAC
|
||||||
|
- hv1 hv2 hv3
|
||||||
|
- #88
|
||||||
* - ``51-l3vni``
|
* - ``51-l3vni``
|
||||||
- routage inter-subnet entre hôtes par L3VNI (IRB symétrique) — VRF, RMAC, exports /24 et /32, RT-5 réfléchies, TTL, isolation par RT, unreachable default dans la VRF, démontage d'un VPC routé
|
- routage inter-subnet entre hôtes par L3VNI (IRB symétrique) — VRF, RMAC, exports /24 et /32, RT-5 réfléchies, TTL, isolation par RT, unreachable default dans la VRF, démontage d'un VPC routé
|
||||||
- hv1 hv2
|
- hv1 hv2
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,8 @@ Exposées sur le port ``prometheus.port`` (9090 par défaut), alimentées par l'
|
||||||
- nombre de VM, par état
|
- nombre de VM, par état
|
||||||
* - ``syonad_nat_gateways_total``
|
* - ``syonad_nat_gateways_total``
|
||||||
- nombre de passerelles NAT, par état
|
- nombre de passerelles NAT, par état
|
||||||
|
* - ``syonad_floating_ips_total``
|
||||||
|
- nombre d'IP flottantes, par état
|
||||||
|
|
||||||
Les états sont ceux du :doc:`cycle de vie </concepts/cycle-de-vie>`. Une valeur non nulle et
|
Les états sont ceux du :doc:`cycle de vie </concepts/cycle-de-vie>`. Une valeur non nulle et
|
||||||
durable sur ``error`` est l'alerte la plus utile à poser ; une valeur durable sur ``creating``
|
durable sur ``error`` est l'alerte la plus utile à poser ; une valeur durable sur ``creating``
|
||||||
|
|
|
||||||
66
internal/api/agent/fip.go
Normal file
66
internal/api/agent/fip.go
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
package agentapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
dispatcher "git.g3e.fr/syonad/two/internal/dispatcher/agent"
|
||||||
|
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Server) FloatingIPByNameHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
name := strings.TrimPrefix(r.URL.Path, "/floating-ips/")
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
if name == "" {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "resource not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch r.Method {
|
||||||
|
case http.MethodGet:
|
||||||
|
s.getFloatingIP(w, r, name)
|
||||||
|
case http.MethodDelete:
|
||||||
|
s.deleteFloatingIP(w, r, name)
|
||||||
|
default:
|
||||||
|
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) getFloatingIP(w http.ResponseWriter, _ *http.Request, name string) {
|
||||||
|
prefix := "floating-ip/" + name + "/"
|
||||||
|
state, err := kv.GetFromDB(s.db, prefix+"state")
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "floating ip not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
vm, _ := kv.GetFromDB(s.db, prefix+"vm")
|
||||||
|
iface, _ := kv.GetFromDB(s.db, prefix+"interface")
|
||||||
|
ip, _ := kv.GetFromDB(s.db, prefix+"ip")
|
||||||
|
gateway, _ := kv.GetFromDB(s.db, prefix+"gateway")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
json.NewEncoder(w).Encode(FloatingIP{Name: name, State: state, VM: vm, Interface: iface, IP: ip, Gateway: gateway})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) deleteFloatingIP(w http.ResponseWriter, _ *http.Request, name string) {
|
||||||
|
cmd := dispatcher.DeleteFloatingIPCommand{Name: name}
|
||||||
|
if err := s.dispatcher.Prepare(cmd); err != nil {
|
||||||
|
if _, dbErr := kv.GetFromDB(s.db, "floating-ip/"+name+"/state"); dbErr != nil {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
} else {
|
||||||
|
w.WriteHeader(http.StatusConflict)
|
||||||
|
}
|
||||||
|
json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.dispatcher.Dispatch(cmd)
|
||||||
|
state, err := kv.GetFromDB(s.db, "floating-ip/"+name+"/state")
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "failed to read floating ip state"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusAccepted)
|
||||||
|
json.NewEncoder(w).Encode(FloatingIP{Name: name, State: state})
|
||||||
|
}
|
||||||
195
internal/api/agent/fip_test.go
Normal file
195
internal/api/agent/fip_test.go
Normal file
|
|
@ -0,0 +1,195 @@
|
||||||
|
package agentapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||||
|
"github.com/dgraph-io/badger/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
func seedRunningVM(t *testing.T, db *badger.DB) {
|
||||||
|
t.Helper()
|
||||||
|
for key, value := range map[string]string{
|
||||||
|
"vpc/vp-t47/cidr": "10.47.0.0/16",
|
||||||
|
"subnet/sn-a/vpc": "vp-t47",
|
||||||
|
"vm/vm-1/state": "running",
|
||||||
|
"vm/vm-1/nic/0/name": "nic-a",
|
||||||
|
"vm/vm-1/nic/0/subnet": "sn-a",
|
||||||
|
"vm/vm-1/nic/0/ip": "10.47.1.10",
|
||||||
|
"vm/vm-1/nic/1/name": "nic-b",
|
||||||
|
"vm/vm-1/nic/1/subnet": "sn-a",
|
||||||
|
"vm/vm-1/nic/1/ip": "10.47.1.11",
|
||||||
|
} {
|
||||||
|
kv.AddInDB(db, key, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func seedFloatingIP(t *testing.T, db *badger.DB, name, vm, iface, st string) {
|
||||||
|
t.Helper()
|
||||||
|
kv.AddInDB(db, "floating-ip/"+name+"/state", st)
|
||||||
|
kv.AddInDB(db, "floating-ip/"+name+"/vm", vm)
|
||||||
|
kv.AddInDB(db, "floating-ip/"+name+"/interface", iface)
|
||||||
|
kv.AddInDB(db, "floating-ip/"+name+"/ip", "240.0.0.47/24")
|
||||||
|
kv.AddInDB(db, "floating-ip/"+name+"/gateway", "240.0.0.1")
|
||||||
|
}
|
||||||
|
|
||||||
|
func validFloatingIPRequest() FloatingIPCreateRequest {
|
||||||
|
return FloatingIPCreateRequest{Name: "fip-1", VM: "vm-1", Interface: "nic-a", IP: "240.0.0.47/24", Gateway: "240.0.0.1"}
|
||||||
|
}
|
||||||
|
|
||||||
|
func postFloatingIP(t *testing.T, s *Server, req FloatingIPCreateRequest) *httptest.ResponseRecorder {
|
||||||
|
t.Helper()
|
||||||
|
body, _ := json.Marshal(req)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
s.FloatingIPsHandler(w, httptest.NewRequest(http.MethodPost, "/floating-ips", bytes.NewReader(body)))
|
||||||
|
return w
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPostFloatingIP_Accepted(t *testing.T) {
|
||||||
|
s, db := newTestServer(t)
|
||||||
|
seedRunningVM(t, db)
|
||||||
|
w := postFloatingIP(t, s, validFloatingIPRequest())
|
||||||
|
if w.Code != http.StatusAccepted {
|
||||||
|
t.Fatalf("attendu 202, obtenu %d : %s", w.Code, w.Body.String())
|
||||||
|
}
|
||||||
|
var got FloatingIP
|
||||||
|
json.NewDecoder(w.Body).Decode(&got)
|
||||||
|
want := FloatingIP{Name: "fip-1", State: "creating", VM: "vm-1", Interface: "nic-a", IP: "240.0.0.47/24", Gateway: "240.0.0.1"}
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("réponse %+v, attendu %+v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPostFloatingIP_BadRequest(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
label string
|
||||||
|
mutate func(*FloatingIPCreateRequest)
|
||||||
|
msg string
|
||||||
|
}{
|
||||||
|
{"nom absent", func(r *FloatingIPCreateRequest) { r.Name = "" }, "name is required"},
|
||||||
|
{"vm absente", func(r *FloatingIPCreateRequest) { r.VM = "" }, "vm is required"},
|
||||||
|
{"interface absente", func(r *FloatingIPCreateRequest) { r.Interface = "" }, "interface is required"},
|
||||||
|
{"ip absente", func(r *FloatingIPCreateRequest) { r.IP = "" }, "ip is required"},
|
||||||
|
{"gateway absente", func(r *FloatingIPCreateRequest) { r.Gateway = "" }, "gateway is required"},
|
||||||
|
{"ip sans préfixe", func(r *FloatingIPCreateRequest) { r.IP = "240.0.0.47" }, "invalid ip"},
|
||||||
|
{"gateway IPv6", func(r *FloatingIPCreateRequest) { r.Gateway = "2001:db8::1" }, "invalid gateway"},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
s, db := newTestServer(t)
|
||||||
|
seedRunningVM(t, db)
|
||||||
|
req := validFloatingIPRequest()
|
||||||
|
c.mutate(&req)
|
||||||
|
w := postFloatingIP(t, s, req)
|
||||||
|
if w.Code != http.StatusBadRequest || !strings.Contains(w.Body.String(), c.msg) {
|
||||||
|
t.Errorf("%s : %d %s, 400 « %s » attendu", c.label, w.Code, w.Body.String(), c.msg)
|
||||||
|
}
|
||||||
|
if _, err := kv.GetFromDB(db, "floating-ip/fip-1/state"); err == nil {
|
||||||
|
t.Errorf("%s : IP flottante écrite malgré le refus", c.label)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPostFloatingIP_ConflictOnUnknownInterface(t *testing.T) {
|
||||||
|
s, db := newTestServer(t)
|
||||||
|
seedRunningVM(t, db)
|
||||||
|
req := validFloatingIPRequest()
|
||||||
|
req.Interface = "nic-z"
|
||||||
|
w := postFloatingIP(t, s, req)
|
||||||
|
if w.Code != http.StatusConflict || !strings.Contains(w.Body.String(), `has no interface \"nic-z\"`) {
|
||||||
|
t.Errorf("%d %s, 409 attendu", w.Code, w.Body.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFloatingIPGetListAndDelete(t *testing.T) {
|
||||||
|
s, db := newTestServer(t)
|
||||||
|
seedFloatingIP(t, db, "fip-1", "vm-1", "nic-a", "running")
|
||||||
|
want := FloatingIP{Name: "fip-1", State: "running", VM: "vm-1", Interface: "nic-a", IP: "240.0.0.47/24", Gateway: "240.0.0.1"}
|
||||||
|
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
s.FloatingIPsHandler(w, httptest.NewRequest(http.MethodGet, "/floating-ips", nil))
|
||||||
|
var list []FloatingIP
|
||||||
|
json.NewDecoder(w.Body).Decode(&list)
|
||||||
|
if w.Code != http.StatusOK || len(list) != 1 || list[0] != want {
|
||||||
|
t.Errorf("liste %d %+v", w.Code, list)
|
||||||
|
}
|
||||||
|
|
||||||
|
w = httptest.NewRecorder()
|
||||||
|
s.FloatingIPByNameHandler(w, httptest.NewRequest(http.MethodGet, "/floating-ips/fip-1", nil))
|
||||||
|
var got FloatingIP
|
||||||
|
json.NewDecoder(w.Body).Decode(&got)
|
||||||
|
if w.Code != http.StatusOK || got != want {
|
||||||
|
t.Errorf("get %d %+v", w.Code, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
w = httptest.NewRecorder()
|
||||||
|
s.FloatingIPByNameHandler(w, httptest.NewRequest(http.MethodDelete, "/floating-ips/fip-x", nil))
|
||||||
|
if w.Code != http.StatusNotFound {
|
||||||
|
t.Errorf("suppression d'une IP flottante absente : %d, 404 attendu", w.Code)
|
||||||
|
}
|
||||||
|
w = httptest.NewRecorder()
|
||||||
|
s.FloatingIPByNameHandler(w, httptest.NewRequest(http.MethodDelete, "/floating-ips/fip-1", nil))
|
||||||
|
if w.Code != http.StatusAccepted {
|
||||||
|
t.Errorf("suppression : %d, 202 attendu", w.Code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetVM_ShowsTheFloatingIPOfItsInterface(t *testing.T) {
|
||||||
|
s, db := newTestServer(t)
|
||||||
|
seedRunningVM(t, db)
|
||||||
|
seedFloatingIP(t, db, "fip-1", "vm-1", "nic-b", "running")
|
||||||
|
seedFloatingIP(t, db, "fip-9", "vm-10", "nic-a", "running")
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
s.VmByNameHandler(w, httptest.NewRequest(http.MethodGet, "/vms/vm-1", nil))
|
||||||
|
raw := w.Body.String()
|
||||||
|
var vm VM
|
||||||
|
json.Unmarshal([]byte(raw), &vm)
|
||||||
|
if len(vm.Interfaces) != 2 {
|
||||||
|
t.Fatalf("interfaces %+v", vm.Interfaces)
|
||||||
|
}
|
||||||
|
if vm.Interfaces[0].FloatingIP != nil {
|
||||||
|
t.Errorf("nic-a porte une IP flottante qui n'est pas la sienne : %+v", vm.Interfaces[0].FloatingIP)
|
||||||
|
}
|
||||||
|
want := VMFloatingIP{Name: "fip-1", IP: "240.0.0.47/24", State: "running"}
|
||||||
|
if vm.Interfaces[1].FloatingIP == nil || *vm.Interfaces[1].FloatingIP != want {
|
||||||
|
t.Errorf("nic-b : %+v, attendu %+v", vm.Interfaces[1].FloatingIP, want)
|
||||||
|
}
|
||||||
|
if strings.Count(raw, "floating_ip") != 1 {
|
||||||
|
t.Errorf("floating_ip doit être absent du JSON d'une interface sans IP flottante : %s", raw)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDeleteVM_BlockedByAFloatingIP(t *testing.T) {
|
||||||
|
s, db := newTestServer(t)
|
||||||
|
seedRunningVM(t, db)
|
||||||
|
seedFloatingIP(t, db, "fip-1", "vm-1", "nic-a", "running")
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
s.VmByNameHandler(w, httptest.NewRequest(http.MethodDelete, "/vms/vm-1", nil))
|
||||||
|
if w.Code != http.StatusConflict || !strings.Contains(w.Body.String(), `floating ip \"fip-1\" must be deleted`) {
|
||||||
|
t.Errorf("%d %s, 409 attendu", w.Code, w.Body.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStartVM_RefusesAFloatingIPInTheRequest(t *testing.T) {
|
||||||
|
s, db := newTestServer(t)
|
||||||
|
kv.AddInDB(db, "subnet/sn-1/state", "running")
|
||||||
|
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1")
|
||||||
|
kv.AddInDB(db, "subnet/sn-1/cidr", "10.0.0.0/24")
|
||||||
|
body, _ := json.Marshal(VMCreateRequest{
|
||||||
|
Name: "vm-f",
|
||||||
|
Interfaces: []VMInterface{{Name: "nic-a", Subnet: "sn-1", IP: "10.0.0.10", MAC: "52:54:00:00:00:05", Primary: true, FloatingIP: &VMFloatingIP{Name: "fip-1"}}},
|
||||||
|
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}},
|
||||||
|
})
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
s.VmsHandler(w, httptest.NewRequest(http.MethodPost, "/vms", bytes.NewReader(body)))
|
||||||
|
if w.Code != http.StatusBadRequest || !strings.Contains(w.Body.String(), "floating_ip is read-only") {
|
||||||
|
t.Errorf("%d %s, 400 attendu", w.Code, w.Body.String())
|
||||||
|
}
|
||||||
|
if _, err := kv.GetFromDB(db, "vm/vm-f/state"); err == nil {
|
||||||
|
t.Error("une VM refusée ne doit laisser aucune trace en base")
|
||||||
|
}
|
||||||
|
}
|
||||||
102
internal/api/agent/fips.go
Normal file
102
internal/api/agent/fips.go
Normal file
|
|
@ -0,0 +1,102 @@
|
||||||
|
package agentapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
dispatcher "git.g3e.fr/syonad/two/internal/dispatcher/agent"
|
||||||
|
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Server) FloatingIPsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
switch r.Method {
|
||||||
|
case http.MethodGet:
|
||||||
|
s.listFloatingIPs(w, r)
|
||||||
|
case http.MethodPost:
|
||||||
|
s.postFloatingIP(w, r)
|
||||||
|
default:
|
||||||
|
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) listFloatingIPs(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
entries, err := kv.ListByPrefix(s.db, "floating-ip/")
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "failed to list floating ips"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ips := make(map[string]*FloatingIP)
|
||||||
|
for key, value := range entries {
|
||||||
|
parts := strings.Split(key, "/")
|
||||||
|
if len(parts) != 3 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
name := parts[1]
|
||||||
|
if _, ok := ips[name]; !ok {
|
||||||
|
ips[name] = &FloatingIP{Name: name}
|
||||||
|
}
|
||||||
|
switch parts[2] {
|
||||||
|
case "state":
|
||||||
|
ips[name].State = value
|
||||||
|
case "vm":
|
||||||
|
ips[name].VM = value
|
||||||
|
case "interface":
|
||||||
|
ips[name].Interface = value
|
||||||
|
case "ip":
|
||||||
|
ips[name].IP = value
|
||||||
|
case "gateway":
|
||||||
|
ips[name].Gateway = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result := make([]FloatingIP, 0, len(ips))
|
||||||
|
for _, f := range ips {
|
||||||
|
result = append(result, *f)
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
json.NewEncoder(w).Encode(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) postFloatingIP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var req FloatingIPCreateRequest
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "invalid request body"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, f := range []struct{ name, value string }{{"name", req.Name}, {"vm", req.VM}, {"interface", req.Interface}, {"ip", req.IP}, {"gateway", req.Gateway}} {
|
||||||
|
if f.value == "" {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
json.NewEncoder(w).Encode(ErrorResponse{Error: f.name + " is required"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ip, _, err := net.ParseCIDR(req.IP); err != nil || ip.To4() == nil {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "invalid ip: an IPv4 address with its prefix length is expected"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if gw := net.ParseIP(req.Gateway); gw == nil || gw.To4() == nil {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "invalid gateway: an IPv4 address is expected"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cmd := dispatcher.CreateFloatingIPCommand{Name: req.Name, VM: req.VM, Interface: req.Interface, IP: req.IP, Gateway: req.Gateway}
|
||||||
|
if err := s.dispatcher.Prepare(cmd); err != nil {
|
||||||
|
w.WriteHeader(http.StatusConflict)
|
||||||
|
json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.dispatcher.Dispatch(cmd)
|
||||||
|
state, err := kv.GetFromDB(s.db, "floating-ip/"+req.Name+"/state")
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "failed to read floating ip state"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusAccepted)
|
||||||
|
json.NewEncoder(w).Encode(FloatingIP{Name: req.Name, State: state, VM: req.VM, Interface: req.Interface, IP: req.IP, Gateway: req.Gateway})
|
||||||
|
}
|
||||||
|
|
@ -40,11 +40,20 @@ type Subnet struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type VMInterface struct {
|
type VMInterface struct {
|
||||||
|
Name string `json:"name"`
|
||||||
Subnet string `json:"subnet"`
|
Subnet string `json:"subnet"`
|
||||||
IP string `json:"ip"`
|
IP string `json:"ip"`
|
||||||
MAC string `json:"mac"`
|
MAC string `json:"mac"`
|
||||||
Primary bool `json:"primary"`
|
Primary bool `json:"primary"`
|
||||||
DHCPServer bool `json:"dhcp_server"`
|
DHCPServer bool `json:"dhcp_server"`
|
||||||
|
|
||||||
|
FloatingIP *VMFloatingIP `json:"floating_ip,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type VMFloatingIP struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
IP string `json:"ip"`
|
||||||
|
State string `json:"state"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type VMStorage struct {
|
type VMStorage struct {
|
||||||
|
|
@ -97,3 +106,20 @@ type NATGateway struct {
|
||||||
IP string `json:"ip,omitempty"`
|
IP string `json:"ip,omitempty"`
|
||||||
Gateway string `json:"gateway,omitempty"`
|
Gateway string `json:"gateway,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type FloatingIPCreateRequest struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
VM string `json:"vm"`
|
||||||
|
Interface string `json:"interface"`
|
||||||
|
IP string `json:"ip"`
|
||||||
|
Gateway string `json:"gateway"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type FloatingIP struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
State string `json:"state"`
|
||||||
|
VM string `json:"vm,omitempty"`
|
||||||
|
Interface string `json:"interface,omitempty"`
|
||||||
|
IP string `json:"ip,omitempty"`
|
||||||
|
Gateway string `json:"gateway,omitempty"`
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,8 @@ func New(d *dispatcher.Dispatcher, db *badger.DB, logger *slog.Logger, address s
|
||||||
mux.HandleFunc("/vms/", s.VmByNameHandler)
|
mux.HandleFunc("/vms/", s.VmByNameHandler)
|
||||||
mux.HandleFunc("/nat-gateways", s.NATGatewaysHandler)
|
mux.HandleFunc("/nat-gateways", s.NATGatewaysHandler)
|
||||||
mux.HandleFunc("/nat-gateways/", s.NATGatewayByNameHandler)
|
mux.HandleFunc("/nat-gateways/", s.NATGatewayByNameHandler)
|
||||||
|
mux.HandleFunc("/floating-ips", s.FloatingIPsHandler)
|
||||||
|
mux.HandleFunc("/floating-ips/", s.FloatingIPByNameHandler)
|
||||||
s.srv = &http.Server{Addr: address, Handler: s.logMiddleware(mux)}
|
s.srv = &http.Server{Addr: address, Handler: s.logMiddleware(mux)}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,13 @@ func (s *Server) getVM(w http.ResponseWriter, _ *http.Request, name string) {
|
||||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "failed to read vm"})
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "failed to read vm"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
floating, err := kv.ListByPrefix(s.db, "floating-ip/")
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "failed to list floating ips"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
attachFloatingIPs(&vm, floating)
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
json.NewEncoder(w).Encode(vm)
|
json.NewEncoder(w).Encode(vm)
|
||||||
}
|
}
|
||||||
|
|
@ -96,6 +103,8 @@ func interfacesFromDB(prefix string, entries map[string]string) []VMInterface {
|
||||||
byIndex[idx] = &VMInterface{}
|
byIndex[idx] = &VMInterface{}
|
||||||
}
|
}
|
||||||
switch parts[1] {
|
switch parts[1] {
|
||||||
|
case "name":
|
||||||
|
byIndex[idx].Name = value
|
||||||
case "subnet":
|
case "subnet":
|
||||||
byIndex[idx].Subnet = value
|
byIndex[idx].Subnet = value
|
||||||
case "ip":
|
case "ip":
|
||||||
|
|
@ -143,3 +152,18 @@ func vmFromDB(name string, entries map[string]string) (VM, error) {
|
||||||
|
|
||||||
return vm, nil
|
return vm, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func attachFloatingIPs(vm *VM, entries map[string]string) {
|
||||||
|
for key, value := range entries {
|
||||||
|
name, ok := strings.CutSuffix(strings.TrimPrefix(key, "floating-ip/"), "/vm")
|
||||||
|
if !ok || strings.Contains(name, "/") || value != vm.Name {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
prefix := "floating-ip/" + name + "/"
|
||||||
|
for i := range vm.Interfaces {
|
||||||
|
if vm.Interfaces[i].Name == entries[prefix+"interface"] {
|
||||||
|
vm.Interfaces[i].FloatingIP = &VMFloatingIP{Name: name, IP: entries[prefix+"ip"], State: entries[prefix+"state"]}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -104,7 +104,7 @@ func TestStartVM_MultiDisk(t *testing.T) {
|
||||||
body, _ := json.Marshal(VMCreateRequest{
|
body, _ := json.Marshal(VMCreateRequest{
|
||||||
Name: "vm-10",
|
Name: "vm-10",
|
||||||
Interfaces: []VMInterface{
|
Interfaces: []VMInterface{
|
||||||
{Subnet: "sn-1", IP: "10.0.0.10", MAC: "52:54:00:00:00:05", Primary: true},
|
{Name: "nic-sn-1", Subnet: "sn-1", IP: "10.0.0.10", MAC: "52:54:00:00:00:05", Primary: true},
|
||||||
},
|
},
|
||||||
Storage: []VMStorage{
|
Storage: []VMStorage{
|
||||||
{Path: "/data/root.qcow2", Dev: "vda"},
|
{Path: "/data/root.qcow2", Dev: "vda"},
|
||||||
|
|
@ -139,7 +139,7 @@ func TestStartVM_StorageReturnedInResponse(t *testing.T) {
|
||||||
body, _ := json.Marshal(VMCreateRequest{
|
body, _ := json.Marshal(VMCreateRequest{
|
||||||
Name: "vm-11",
|
Name: "vm-11",
|
||||||
Interfaces: []VMInterface{
|
Interfaces: []VMInterface{
|
||||||
{Subnet: "sn-1", IP: "10.0.0.11", MAC: "52:54:00:00:00:05", Primary: true},
|
{Name: "nic-sn-1", Subnet: "sn-1", IP: "10.0.0.11", MAC: "52:54:00:00:00:05", Primary: true},
|
||||||
},
|
},
|
||||||
Storage: []VMStorage{
|
Storage: []VMStorage{
|
||||||
{Path: "/data/root.qcow2", Dev: "xvda"},
|
{Path: "/data/root.qcow2", Dev: "xvda"},
|
||||||
|
|
@ -171,7 +171,7 @@ func TestStartVM_RejectsInvalidDiskDev(t *testing.T) {
|
||||||
|
|
||||||
body, _ := json.Marshal(VMCreateRequest{
|
body, _ := json.Marshal(VMCreateRequest{
|
||||||
Name: "vm-12",
|
Name: "vm-12",
|
||||||
Interfaces: []VMInterface{{Subnet: "sn-1", IP: "10.0.0.12", MAC: "52:54:00:00:00:05", Primary: true}},
|
Interfaces: []VMInterface{{Name: "nic-sn-1", Subnet: "sn-1", IP: "10.0.0.12", MAC: "52:54:00:00:00:05", Primary: true}},
|
||||||
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}, {Path: "/data/data.qcow2", Dev: dev}},
|
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}, {Path: "/data/data.qcow2", Dev: dev}},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
@ -200,7 +200,7 @@ func postVM(t *testing.T, name string, meta VMMetadata) (*httptest.ResponseRecor
|
||||||
|
|
||||||
body, _ := json.Marshal(VMCreateRequest{
|
body, _ := json.Marshal(VMCreateRequest{
|
||||||
Name: name,
|
Name: name,
|
||||||
Interfaces: []VMInterface{{Subnet: "sn-1", IP: "10.0.0.20", MAC: "52:54:00:00:00:05", Primary: true}},
|
Interfaces: []VMInterface{{Name: "nic-sn-1", Subnet: "sn-1", IP: "10.0.0.20", MAC: "52:54:00:00:00:05", Primary: true}},
|
||||||
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}},
|
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}},
|
||||||
Memory: 1024,
|
Memory: 1024,
|
||||||
CPUs: 2,
|
CPUs: 2,
|
||||||
|
|
@ -334,8 +334,8 @@ func TestStartVM_StoresAllInterfaces(t *testing.T) {
|
||||||
body, _ := json.Marshal(VMCreateRequest{
|
body, _ := json.Marshal(VMCreateRequest{
|
||||||
Name: "vm-multi",
|
Name: "vm-multi",
|
||||||
Interfaces: []VMInterface{
|
Interfaces: []VMInterface{
|
||||||
{Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true},
|
{Name: "nic-sn-1", Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true},
|
||||||
{Subnet: "sn-2", IP: "10.2.0.5", MAC: "52:54:00:aa:bb:cc"},
|
{Name: "nic-sn-2", Subnet: "sn-2", IP: "10.2.0.5", MAC: "52:54:00:aa:bb:cc"},
|
||||||
},
|
},
|
||||||
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}},
|
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}},
|
||||||
})
|
})
|
||||||
|
|
@ -371,8 +371,8 @@ func TestStartVM_StoresDHCPServerOnlyWhenRequested(t *testing.T) {
|
||||||
body, _ := json.Marshal(VMCreateRequest{
|
body, _ := json.Marshal(VMCreateRequest{
|
||||||
Name: "vm-pxe",
|
Name: "vm-pxe",
|
||||||
Interfaces: []VMInterface{
|
Interfaces: []VMInterface{
|
||||||
{Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true},
|
{Name: "nic-sn-1", Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true},
|
||||||
{Subnet: "sn-2", IP: "10.2.0.5", MAC: "52:54:00:aa:bb:cc", DHCPServer: true},
|
{Name: "nic-sn-2", Subnet: "sn-2", IP: "10.2.0.5", MAC: "52:54:00:aa:bb:cc", DHCPServer: true},
|
||||||
},
|
},
|
||||||
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}},
|
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}},
|
||||||
})
|
})
|
||||||
|
|
@ -418,10 +418,10 @@ func TestVmFromDB_ReturnsDHCPServer(t *testing.T) {
|
||||||
|
|
||||||
func TestStartVM_RejectsZeroOrTwoPrimaries(t *testing.T) {
|
func TestStartVM_RejectsZeroOrTwoPrimaries(t *testing.T) {
|
||||||
cases := map[string][]VMInterface{
|
cases := map[string][]VMInterface{
|
||||||
"aucune primaire": {{Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05"}},
|
"aucune primaire": {{Name: "nic-sn-1", Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05"}},
|
||||||
"deux primaires": {
|
"deux primaires": {
|
||||||
{Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true},
|
{Name: "nic-sn-1", Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true},
|
||||||
{Subnet: "sn-2", IP: "10.2.0.5", MAC: "52:54:00:00:00:05", Primary: true},
|
{Name: "nic-sn-2", Subnet: "sn-2", IP: "10.2.0.5", MAC: "52:54:00:00:00:05", Primary: true},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for label, ifaces := range cases {
|
for label, ifaces := range cases {
|
||||||
|
|
@ -454,8 +454,8 @@ func TestStartVM_RejectsAnInterfaceWithoutMAC(t *testing.T) {
|
||||||
body, _ := json.Marshal(VMCreateRequest{
|
body, _ := json.Marshal(VMCreateRequest{
|
||||||
Name: "vm-nomac",
|
Name: "vm-nomac",
|
||||||
Interfaces: []VMInterface{
|
Interfaces: []VMInterface{
|
||||||
{Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true},
|
{Name: "nic-sn-1", Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true},
|
||||||
{Subnet: "sn-2", IP: "10.2.0.5"},
|
{Name: "nic-sn-2", Subnet: "sn-2", IP: "10.2.0.5"},
|
||||||
},
|
},
|
||||||
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}},
|
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}},
|
||||||
})
|
})
|
||||||
|
|
@ -488,7 +488,7 @@ func TestStartVM_RejectsReservedOrForeignAddresses(t *testing.T) {
|
||||||
kv.AddInDB(db, "subnet/sn-1/gateway", "10.0.0.1")
|
kv.AddInDB(db, "subnet/sn-1/gateway", "10.0.0.1")
|
||||||
body, _ := json.Marshal(VMCreateRequest{
|
body, _ := json.Marshal(VMCreateRequest{
|
||||||
Name: "vm-ip",
|
Name: "vm-ip",
|
||||||
Interfaces: []VMInterface{{Subnet: "sn-1", IP: ip, MAC: "52:54:00:00:00:05", Primary: true}},
|
Interfaces: []VMInterface{{Name: "nic-sn-1", Subnet: "sn-1", IP: ip, MAC: "52:54:00:00:00:05", Primary: true}},
|
||||||
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}},
|
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}},
|
||||||
})
|
})
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
@ -511,7 +511,7 @@ func TestStartVM_AcceptsHostAddressBesideReservedOnes(t *testing.T) {
|
||||||
kv.AddInDB(db, "subnet/sn-1/relay_ip", "10.0.0.253")
|
kv.AddInDB(db, "subnet/sn-1/relay_ip", "10.0.0.253")
|
||||||
body, _ := json.Marshal(VMCreateRequest{
|
body, _ := json.Marshal(VMCreateRequest{
|
||||||
Name: "vm-ip",
|
Name: "vm-ip",
|
||||||
Interfaces: []VMInterface{{Subnet: "sn-1", IP: "10.0.0.252", MAC: "52:54:00:00:00:05", Primary: true}},
|
Interfaces: []VMInterface{{Name: "nic-sn-1", Subnet: "sn-1", IP: "10.0.0.252", MAC: "52:54:00:00:00:05", Primary: true}},
|
||||||
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}},
|
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}},
|
||||||
})
|
})
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
@ -527,7 +527,7 @@ func TestStartVM_RejectsSubnetWithoutCIDR(t *testing.T) {
|
||||||
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1")
|
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1")
|
||||||
body, _ := json.Marshal(VMCreateRequest{
|
body, _ := json.Marshal(VMCreateRequest{
|
||||||
Name: "vm-ip",
|
Name: "vm-ip",
|
||||||
Interfaces: []VMInterface{{Subnet: "sn-1", IP: "10.0.0.5", MAC: "52:54:00:00:00:05", Primary: true}},
|
Interfaces: []VMInterface{{Name: "nic-sn-1", Subnet: "sn-1", IP: "10.0.0.5", MAC: "52:54:00:00:00:05", Primary: true}},
|
||||||
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}},
|
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}},
|
||||||
})
|
})
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
@ -536,3 +536,75 @@ func TestStartVM_RejectsSubnetWithoutCIDR(t *testing.T) {
|
||||||
t.Errorf("attendu 422 « has no cidr », obtenu %d : %s", w.Code, w.Body.String())
|
t.Errorf("attendu 422 « has no cidr », obtenu %d : %s", w.Code, w.Body.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStartVM_RejectsAnInterfaceWithoutOrWithADuplicateName(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
label string
|
||||||
|
ifaces []VMInterface
|
||||||
|
msg string
|
||||||
|
}{
|
||||||
|
{"nom absent", []VMInterface{
|
||||||
|
{Name: "nic-a", Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true},
|
||||||
|
{Subnet: "sn-2", IP: "10.2.0.5", MAC: "52:54:00:aa:bb:cc"},
|
||||||
|
}, "interface 1: name is required"},
|
||||||
|
{"nom en double", []VMInterface{
|
||||||
|
{Name: "nic-a", Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true},
|
||||||
|
{Name: "nic-a", Subnet: "sn-2", IP: "10.2.0.5", MAC: "52:54:00:aa:bb:cc"},
|
||||||
|
}, `interface 1: name \"nic-a\" is already used by interface 0`},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
s, db := newTestServer(t)
|
||||||
|
for sn, cidr := range map[string]string{"sn-1": "10.1.0.0/24", "sn-2": "10.2.0.0/24"} {
|
||||||
|
kv.AddInDB(db, "subnet/"+sn+"/state", "running")
|
||||||
|
kv.AddInDB(db, "subnet/"+sn+"/vpc", "vpc-1")
|
||||||
|
kv.AddInDB(db, "subnet/"+sn+"/cidr", cidr)
|
||||||
|
}
|
||||||
|
body, _ := json.Marshal(VMCreateRequest{
|
||||||
|
Name: "vm-names",
|
||||||
|
Interfaces: c.ifaces,
|
||||||
|
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}},
|
||||||
|
})
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
s.VmsHandler(w, httptest.NewRequest(http.MethodPost, "/vms", bytes.NewReader(body)))
|
||||||
|
if w.Code != http.StatusBadRequest || !strings.Contains(w.Body.String(), c.msg) {
|
||||||
|
t.Errorf("%s : attendu 400 « %s », obtenu %d — %s", c.label, c.msg, w.Code, w.Body.String())
|
||||||
|
}
|
||||||
|
if _, err := kv.GetFromDB(db, "vm/vm-names/state"); err == nil {
|
||||||
|
t.Errorf("%s : une VM refusée ne doit laisser aucune trace en base", c.label)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStartVM_StoresAndReturnsInterfaceNames(t *testing.T) {
|
||||||
|
s, db := newTestServer(t)
|
||||||
|
for sn, cidr := range map[string]string{"sn-1": "10.1.0.0/24", "sn-2": "10.2.0.0/24"} {
|
||||||
|
kv.AddInDB(db, "subnet/"+sn+"/state", "running")
|
||||||
|
kv.AddInDB(db, "subnet/"+sn+"/vpc", "vpc-1")
|
||||||
|
kv.AddInDB(db, "subnet/"+sn+"/cidr", cidr)
|
||||||
|
}
|
||||||
|
body, _ := json.Marshal(VMCreateRequest{
|
||||||
|
Name: "vm-named",
|
||||||
|
Interfaces: []VMInterface{
|
||||||
|
{Name: "nic-a1b2", Subnet: "sn-1", IP: "10.1.0.5", MAC: "52:54:00:00:00:05", Primary: true},
|
||||||
|
{Name: "nic-c3d4", Subnet: "sn-2", IP: "10.2.0.5", MAC: "52:54:00:aa:bb:cc"},
|
||||||
|
},
|
||||||
|
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}},
|
||||||
|
})
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
s.VmsHandler(w, httptest.NewRequest(http.MethodPost, "/vms", bytes.NewReader(body)))
|
||||||
|
if w.Code != http.StatusAccepted {
|
||||||
|
t.Fatalf("attendu 202, obtenu %d : %s", w.Code, w.Body.String())
|
||||||
|
}
|
||||||
|
for key, want := range map[string]string{"vm/vm-named/nic/0/name": "nic-a1b2", "vm/vm-named/nic/1/name": "nic-c3d4"} {
|
||||||
|
if got, err := kv.GetFromDB(db, key); err != nil || got != want {
|
||||||
|
t.Errorf("%s = %q (%v), attendu %q", key, got, err, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w = httptest.NewRecorder()
|
||||||
|
s.VmByNameHandler(w, httptest.NewRequest(http.MethodGet, "/vms/vm-named", nil))
|
||||||
|
var got VM
|
||||||
|
json.NewDecoder(w.Body).Decode(&got)
|
||||||
|
if len(got.Interfaces) != 2 || got.Interfaces[0].Name != "nic-a1b2" || got.Interfaces[1].Name != "nic-c3d4" {
|
||||||
|
t.Errorf("interfaces renvoyées : %+v", got.Interfaces)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -44,12 +44,19 @@ func (s *Server) listVMs(w http.ResponseWriter, _ *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
floating, err := kv.ListByPrefix(s.db, "floating-ip/")
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "failed to list floating ips"})
|
||||||
|
return
|
||||||
|
}
|
||||||
result := make([]VM, 0, len(names))
|
result := make([]VM, 0, len(names))
|
||||||
for name := range names {
|
for name := range names {
|
||||||
vm, err := vmFromDB(name, entries)
|
vm, err := vmFromDB(name, entries)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
attachFloatingIPs(&vm, floating)
|
||||||
result = append(result, vm)
|
result = append(result, vm)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -72,13 +79,30 @@ func (s *Server) startVM(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
nics := make([]dispatcher.VMNIC, len(req.Interfaces))
|
nics := make([]dispatcher.VMNIC, len(req.Interfaces))
|
||||||
primaries := 0
|
primaries := 0
|
||||||
|
names := make(map[string]int, len(req.Interfaces))
|
||||||
for i, iface := range req.Interfaces {
|
for i, iface := range req.Interfaces {
|
||||||
|
if iface.Name == "" {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
json.NewEncoder(w).Encode(ErrorResponse{Error: fmt.Sprintf("interface %d: name is required", i)})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if other, ok := names[iface.Name]; ok {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
json.NewEncoder(w).Encode(ErrorResponse{Error: fmt.Sprintf("interface %d: name %q is already used by interface %d", i, iface.Name, other)})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
names[iface.Name] = i
|
||||||
|
if iface.FloatingIP != nil {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
json.NewEncoder(w).Encode(ErrorResponse{Error: fmt.Sprintf("interface %d: floating_ip is read-only, attach it with /floating-ips", i)})
|
||||||
|
return
|
||||||
|
}
|
||||||
if iface.MAC == "" {
|
if iface.MAC == "" {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
json.NewEncoder(w).Encode(ErrorResponse{Error: fmt.Sprintf("interface %d: mac is required", i)})
|
json.NewEncoder(w).Encode(ErrorResponse{Error: fmt.Sprintf("interface %d: mac is required", i)})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
nics[i] = dispatcher.VMNIC{Subnet: iface.Subnet, IP: iface.IP, MAC: iface.MAC, Primary: iface.Primary, DHCPServer: iface.DHCPServer}
|
nics[i] = dispatcher.VMNIC{Name: iface.Name, Subnet: iface.Subnet, IP: iface.IP, MAC: iface.MAC, Primary: iface.Primary, DHCPServer: iface.DHCPServer}
|
||||||
if iface.Primary {
|
if iface.Primary {
|
||||||
primaries++
|
primaries++
|
||||||
}
|
}
|
||||||
|
|
|
||||||
166
internal/dispatcher/agent/fip_commands.go
Normal file
166
internal/dispatcher/agent/fip_commands.go
Normal file
|
|
@ -0,0 +1,166 @@
|
||||||
|
package dispatcher
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||||
|
"git.g3e.fr/syonad/two/internal/fip"
|
||||||
|
"git.g3e.fr/syonad/two/internal/state"
|
||||||
|
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||||
|
"github.com/dgraph-io/badger/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
const floatingIPPrefix = "floating-ip/"
|
||||||
|
|
||||||
|
type CreateFloatingIPCommand struct {
|
||||||
|
Name string
|
||||||
|
VM string
|
||||||
|
Interface string
|
||||||
|
IP string
|
||||||
|
Gateway string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c CreateFloatingIPCommand) Key() string { return floatingIPPrefix + c.Name }
|
||||||
|
|
||||||
|
func (c CreateFloatingIPCommand) Prepare(db *badger.DB, _ *configuration.Config) error {
|
||||||
|
if _, err := kv.GetFromDB(db, c.Key()+"/state"); err == nil {
|
||||||
|
return fmt.Errorf("floating ip %q already exists", c.Name)
|
||||||
|
}
|
||||||
|
public, _, err := parseNATGateway(c.IP, c.Gateway)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
vmState, err := state.Get(db, "vm/"+c.VM)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("vm %q not found", c.VM)
|
||||||
|
}
|
||||||
|
if vmState != state.Running {
|
||||||
|
return fmt.Errorf("vm %q is %s", c.VM, vmState)
|
||||||
|
}
|
||||||
|
if _, err := fip.Resolve(db, c.VM, c.Interface); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
others, err := floatingIPs(db)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for name, o := range others {
|
||||||
|
if o.vm == c.VM && o.iface == c.Interface {
|
||||||
|
return fmt.Errorf("interface %q of vm %q already has floating ip %q", c.Interface, c.VM, name)
|
||||||
|
}
|
||||||
|
if o.public != nil && o.public.Equal(public.IP) {
|
||||||
|
return fmt.Errorf("public ip %s is already used by floating ip %q", public.IP, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for key, value := range map[string]string{"vm": c.VM, "interface": c.Interface, "ip": c.IP, "gateway": c.Gateway} {
|
||||||
|
if err := kv.AddInDB(db, c.Key()+"/"+key, value); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return state.Set(db, c.Key(), state.Creating)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c CreateFloatingIPCommand) Execute(db *badger.DB, _ *configuration.Config) error {
|
||||||
|
public, gw, err := parseNATGateway(c.IP, c.Gateway)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
target, err := fip.Resolve(db, c.VM, c.Interface)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := fip.Create(target, public, gw); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return state.Set(db, c.Key(), state.Running)
|
||||||
|
}
|
||||||
|
|
||||||
|
type DeleteFloatingIPCommand struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c DeleteFloatingIPCommand) Key() string { return floatingIPPrefix + c.Name }
|
||||||
|
|
||||||
|
func (c DeleteFloatingIPCommand) Prepare(db *badger.DB, _ *configuration.Config) error {
|
||||||
|
current, err := state.Get(db, c.Key())
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("floating ip %q not found", c.Name)
|
||||||
|
}
|
||||||
|
if !state.CanDelete(current) {
|
||||||
|
return fmt.Errorf("floating ip %q cannot be deleted while %s", c.Name, current)
|
||||||
|
}
|
||||||
|
return state.Set(db, c.Key(), state.Deleting)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c DeleteFloatingIPCommand) Execute(db *badger.DB, _ *configuration.Config) error {
|
||||||
|
entries, err := kv.ListByPrefix(db, c.Key()+"/")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("floating ip %q: %w", c.Name, err)
|
||||||
|
}
|
||||||
|
prefix := c.Key() + "/"
|
||||||
|
public, _, err := parseNATGateway(entries[prefix+"ip"], entries[prefix+"gateway"])
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("floating ip %q: %w", c.Name, err)
|
||||||
|
}
|
||||||
|
target, err := fip.Resolve(db, entries[prefix+"vm"], entries[prefix+"interface"])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := fip.Delete(target, public); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := state.Set(db, c.Key(), state.Deleted); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return kv.DeleteInDB(db, c.Key())
|
||||||
|
}
|
||||||
|
|
||||||
|
type floatingIPRef struct {
|
||||||
|
vm string
|
||||||
|
iface string
|
||||||
|
public net.IP
|
||||||
|
}
|
||||||
|
|
||||||
|
func floatingIPs(db *badger.DB) (map[string]floatingIPRef, error) {
|
||||||
|
entries, err := kv.ListByPrefix(db, floatingIPPrefix)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to list floating ips: %w", err)
|
||||||
|
}
|
||||||
|
refs := map[string]floatingIPRef{}
|
||||||
|
for key, value := range entries {
|
||||||
|
parts := strings.Split(strings.TrimPrefix(key, floatingIPPrefix), "/")
|
||||||
|
if len(parts) != 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
r := refs[parts[0]]
|
||||||
|
switch parts[1] {
|
||||||
|
case "vm":
|
||||||
|
r.vm = value
|
||||||
|
case "interface":
|
||||||
|
r.iface = value
|
||||||
|
case "ip":
|
||||||
|
if ip, _, err := net.ParseCIDR(value); err == nil {
|
||||||
|
r.public = ip
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
refs[parts[0]] = r
|
||||||
|
}
|
||||||
|
return refs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func floatingIPOf(db *badger.DB, vmName string) (string, error) {
|
||||||
|
refs, err := floatingIPs(db)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
for name, r := range refs {
|
||||||
|
if r.vm == vmName {
|
||||||
|
return name, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
157
internal/dispatcher/agent/fip_commands_test.go
Normal file
157
internal/dispatcher/agent/fip_commands_test.go
Normal file
|
|
@ -0,0 +1,157 @@
|
||||||
|
package dispatcher
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||||
|
"github.com/dgraph-io/badger/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
func seedVMWithInterfaces(t *testing.T, db *badger.DB, name, st string) {
|
||||||
|
t.Helper()
|
||||||
|
for key, value := range map[string]string{
|
||||||
|
"vpc/vp-t47/cidr": "10.47.0.0/16",
|
||||||
|
"subnet/sn-a/vpc": "vp-t47",
|
||||||
|
"vm/" + name + "/state": st,
|
||||||
|
"vm/" + name + "/nic/0/name": "nic-a",
|
||||||
|
"vm/" + name + "/nic/0/subnet": "sn-a",
|
||||||
|
"vm/" + name + "/nic/0/ip": "10.47.1.10",
|
||||||
|
"vm/" + name + "/nic/1/name": "nic-ab",
|
||||||
|
"vm/" + name + "/nic/1/subnet": "sn-a",
|
||||||
|
"vm/" + name + "/nic/1/ip": "10.47.1.11",
|
||||||
|
} {
|
||||||
|
kv.AddInDB(db, key, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func validFloatingIP() CreateFloatingIPCommand {
|
||||||
|
return CreateFloatingIPCommand{Name: "fip-1", VM: "vm-1", Interface: "nic-a", IP: "240.0.0.47/24", Gateway: "240.0.0.1"}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateFloatingIPCommand_Prepare_WritesTheFloatingIP(t *testing.T) {
|
||||||
|
_, db := newTestDispatcher(t)
|
||||||
|
seedVMWithInterfaces(t, db, "vm-1", "running")
|
||||||
|
if err := validFloatingIP().Prepare(db, nil); err != nil {
|
||||||
|
t.Fatalf("Prepare a échoué : %v", err)
|
||||||
|
}
|
||||||
|
for key, want := range map[string]string{
|
||||||
|
"floating-ip/fip-1/state": "creating",
|
||||||
|
"floating-ip/fip-1/vm": "vm-1",
|
||||||
|
"floating-ip/fip-1/interface": "nic-a",
|
||||||
|
"floating-ip/fip-1/ip": "240.0.0.47/24",
|
||||||
|
"floating-ip/fip-1/gateway": "240.0.0.1",
|
||||||
|
} {
|
||||||
|
if got, err := kv.GetFromDB(db, key); err != nil || got != want {
|
||||||
|
t.Errorf("%s : %q (%v), %q attendu", key, got, err, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateFloatingIPCommand_Prepare_Refuses(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
label string
|
||||||
|
seed func(*badger.DB)
|
||||||
|
mutate func(*CreateFloatingIPCommand)
|
||||||
|
msg string
|
||||||
|
}{
|
||||||
|
{"IP flottante existante", func(db *badger.DB) { kv.AddInDB(db, "floating-ip/fip-1/state", "running") }, nil, `floating ip "fip-1" already exists`},
|
||||||
|
{"VM absente", nil, func(c *CreateFloatingIPCommand) { c.VM = "vm-x" }, `vm "vm-x" not found`},
|
||||||
|
{"VM en création", func(db *badger.DB) { seedVMWithInterfaces(t, db, "vm-2", "creating") }, func(c *CreateFloatingIPCommand) { c.VM = "vm-2" }, `vm "vm-2" is creating`},
|
||||||
|
{"interface inconnue", nil, func(c *CreateFloatingIPCommand) { c.Interface = "nic-z" }, `vm "vm-1" has no interface "nic-z"`},
|
||||||
|
{"interface qui a déjà une IP flottante", func(db *badger.DB) {
|
||||||
|
kv.AddInDB(db, "floating-ip/fip-0/state", "error")
|
||||||
|
kv.AddInDB(db, "floating-ip/fip-0/vm", "vm-1")
|
||||||
|
kv.AddInDB(db, "floating-ip/fip-0/interface", "nic-a")
|
||||||
|
kv.AddInDB(db, "floating-ip/fip-0/ip", "240.0.0.40/24")
|
||||||
|
}, nil, `interface "nic-a" of vm "vm-1" already has floating ip "fip-0"`},
|
||||||
|
{"IP publique déjà portée", func(db *badger.DB) {
|
||||||
|
kv.AddInDB(db, "floating-ip/fip-0/state", "running")
|
||||||
|
kv.AddInDB(db, "floating-ip/fip-0/vm", "vm-1")
|
||||||
|
kv.AddInDB(db, "floating-ip/fip-0/interface", "nic-ab")
|
||||||
|
kv.AddInDB(db, "floating-ip/fip-0/ip", "240.0.0.47/24")
|
||||||
|
}, nil, `public ip 240.0.0.47 is already used by floating ip "fip-0"`},
|
||||||
|
{"ip sans préfixe", nil, func(c *CreateFloatingIPCommand) { c.IP = "240.0.0.47" }, `invalid ip "240.0.0.47"`},
|
||||||
|
{"gateway invalide", nil, func(c *CreateFloatingIPCommand) { c.Gateway = "bordure" }, `invalid gateway "bordure"`},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
_, db := newTestDispatcher(t)
|
||||||
|
seedVMWithInterfaces(t, db, "vm-1", "running")
|
||||||
|
if c.seed != nil {
|
||||||
|
c.seed(db)
|
||||||
|
}
|
||||||
|
cmd := validFloatingIP()
|
||||||
|
if c.mutate != nil {
|
||||||
|
c.mutate(&cmd)
|
||||||
|
}
|
||||||
|
err := cmd.Prepare(db, nil)
|
||||||
|
if err == nil || !strings.Contains(err.Error(), c.msg) {
|
||||||
|
t.Errorf("%s : erreur %v, « %s » attendu", c.label, err, c.msg)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if c.label != "IP flottante existante" {
|
||||||
|
if _, err := kv.GetFromDB(db, "floating-ip/fip-1/state"); err == nil {
|
||||||
|
t.Errorf("%s : état écrit malgré le refus", c.label)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateFloatingIPCommand_Prepare_IgnoresNearbyNames(t *testing.T) {
|
||||||
|
_, db := newTestDispatcher(t)
|
||||||
|
seedVMWithInterfaces(t, db, "vm-1", "running")
|
||||||
|
seedVMWithInterfaces(t, db, "vm-10", "running")
|
||||||
|
kv.AddInDB(db, "floating-ip/fip-0/state", "running")
|
||||||
|
kv.AddInDB(db, "floating-ip/fip-0/vm", "vm-10")
|
||||||
|
kv.AddInDB(db, "floating-ip/fip-0/interface", "nic-a")
|
||||||
|
kv.AddInDB(db, "floating-ip/fip-0/ip", "240.0.0.40/24")
|
||||||
|
kv.AddInDB(db, "floating-ip/fip-2/state", "running")
|
||||||
|
kv.AddInDB(db, "floating-ip/fip-2/vm", "vm-1")
|
||||||
|
kv.AddInDB(db, "floating-ip/fip-2/interface", "nic-ab")
|
||||||
|
kv.AddInDB(db, "floating-ip/fip-2/ip", "240.0.0.41/24")
|
||||||
|
if err := validFloatingIP().Prepare(db, nil); err != nil {
|
||||||
|
t.Errorf("Prepare a échoué : %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDeleteFloatingIPCommand_Prepare(t *testing.T) {
|
||||||
|
for st, ok := range map[string]bool{"running": true, "error": true, "creating": false, "deleting": false} {
|
||||||
|
_, db := newTestDispatcher(t)
|
||||||
|
kv.AddInDB(db, "floating-ip/fip-1/state", st)
|
||||||
|
err := DeleteFloatingIPCommand{Name: "fip-1"}.Prepare(db, nil)
|
||||||
|
if ok != (err == nil) {
|
||||||
|
t.Errorf("état %s : erreur %v", st, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_, db := newTestDispatcher(t)
|
||||||
|
if err := (DeleteFloatingIPCommand{Name: "fip-x"}).Prepare(db, nil); err == nil || !strings.Contains(err.Error(), `floating ip "fip-x" not found`) {
|
||||||
|
t.Errorf("IP flottante absente : %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStopVMCommand_Prepare_RefusesWhileAFloatingIPIsAttached(t *testing.T) {
|
||||||
|
for _, st := range []string{"running", "error", "creating", "deleting"} {
|
||||||
|
_, db := newTestDispatcher(t)
|
||||||
|
seedVMWithInterfaces(t, db, "vm-1", "running")
|
||||||
|
kv.AddInDB(db, "floating-ip/fip-1/state", st)
|
||||||
|
kv.AddInDB(db, "floating-ip/fip-1/vm", "vm-1")
|
||||||
|
kv.AddInDB(db, "floating-ip/fip-1/interface", "nic-a")
|
||||||
|
err := StopVMCommand{Name: "vm-1"}.Prepare(db, nil)
|
||||||
|
if err == nil || !strings.Contains(err.Error(), `floating ip "fip-1" must be deleted before deleting vm "vm-1"`) {
|
||||||
|
t.Errorf("IP flottante %s : erreur %v", st, err)
|
||||||
|
}
|
||||||
|
if got, _ := kv.GetFromDB(db, "vm/vm-1/state"); got != "running" {
|
||||||
|
t.Errorf("IP flottante %s : VM passée en %q malgré le refus", st, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStopVMCommand_Prepare_IgnoresTheFloatingIPOfANearbyVM(t *testing.T) {
|
||||||
|
_, db := newTestDispatcher(t)
|
||||||
|
seedVMWithInterfaces(t, db, "vm-1", "running")
|
||||||
|
kv.AddInDB(db, "floating-ip/fip-1/state", "running")
|
||||||
|
kv.AddInDB(db, "floating-ip/fip-1/vm", "vm-10")
|
||||||
|
if err := (StopVMCommand{Name: "vm-1"}).Prepare(db, nil); err != nil {
|
||||||
|
t.Errorf("Prepare a échoué : %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -22,6 +22,7 @@ type VMDisk struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type VMNIC struct {
|
type VMNIC struct {
|
||||||
|
Name string
|
||||||
Subnet string
|
Subnet string
|
||||||
IP string
|
IP string
|
||||||
MAC string
|
MAC string
|
||||||
|
|
@ -57,6 +58,9 @@ func (c StartVMCommand) Prepare(db *badger.DB, _ *configuration.Config) error {
|
||||||
state.Set(db, c.Key(), state.Creating)
|
state.Set(db, c.Key(), state.Creating)
|
||||||
for i, n := range c.NICs {
|
for i, n := range c.NICs {
|
||||||
prefix := fmt.Sprintf("vm/%s/nic/%d/", c.Name, i)
|
prefix := fmt.Sprintf("vm/%s/nic/%d/", c.Name, i)
|
||||||
|
if err := kv.AddInDB(db, prefix+"name", n.Name); err != nil {
|
||||||
|
return fmt.Errorf("store nic %d name: %w", i, err)
|
||||||
|
}
|
||||||
if err := kv.AddInDB(db, prefix+"subnet", n.Subnet); err != nil {
|
if err := kv.AddInDB(db, prefix+"subnet", n.Subnet); err != nil {
|
||||||
return fmt.Errorf("store nic %d subnet: %w", i, err)
|
return fmt.Errorf("store nic %d subnet: %w", i, err)
|
||||||
}
|
}
|
||||||
|
|
@ -194,6 +198,13 @@ func (c StopVMCommand) Prepare(db *badger.DB, _ *configuration.Config) error {
|
||||||
if !state.CanDelete(current) {
|
if !state.CanDelete(current) {
|
||||||
return fmt.Errorf("vm %q cannot be stopped while %s", c.Name, current)
|
return fmt.Errorf("vm %q cannot be stopped while %s", c.Name, current)
|
||||||
}
|
}
|
||||||
|
floating, err := floatingIPOf(db, c.Name)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if floating != "" {
|
||||||
|
return fmt.Errorf("floating ip %q must be deleted before deleting vm %q", floating, c.Name)
|
||||||
|
}
|
||||||
return state.Set(db, c.Key(), state.Deleting)
|
return state.Set(db, c.Key(), state.Deleting)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
190
internal/fip/fip.go
Normal file
190
internal/fip/fip.go
Normal file
|
|
@ -0,0 +1,190 @@
|
||||||
|
package fip
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.g3e.fr/syonad/two/internal/netif"
|
||||||
|
"git.g3e.fr/syonad/two/internal/nft"
|
||||||
|
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||||
|
|
||||||
|
"github.com/dgraph-io/badger/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
publicBridge = "br-public"
|
||||||
|
mainTable = 254
|
||||||
|
prefMain = 100
|
||||||
|
prefOwn = 101
|
||||||
|
)
|
||||||
|
|
||||||
|
type Target struct {
|
||||||
|
VPC string
|
||||||
|
CIDR *net.IPNet
|
||||||
|
Private net.IP
|
||||||
|
}
|
||||||
|
|
||||||
|
type floatingIP struct {
|
||||||
|
netns string
|
||||||
|
vpcCIDR *net.IPNet
|
||||||
|
iface string
|
||||||
|
mac net.HardwareAddr
|
||||||
|
public *net.IPNet
|
||||||
|
private net.IP
|
||||||
|
gateway net.IP
|
||||||
|
table int
|
||||||
|
}
|
||||||
|
|
||||||
|
func Iface(public net.IP) (string, error) {
|
||||||
|
ip := public.To4()
|
||||||
|
if ip == nil {
|
||||||
|
return "", fmt.Errorf("public ip %v is not an IPv4 address", public)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("fip-%08x", binary.BigEndian.Uint32(ip)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func table(private net.IP) (int, error) {
|
||||||
|
ip := private.To4()
|
||||||
|
if ip == nil {
|
||||||
|
return 0, fmt.Errorf("private ip %v is not an IPv4 address", private)
|
||||||
|
}
|
||||||
|
t := int(binary.BigEndian.Uint32(ip))
|
||||||
|
if t < 256 {
|
||||||
|
return 0, fmt.Errorf("private ip %v gives a reserved routing table %d", private, t)
|
||||||
|
}
|
||||||
|
return t, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Resolve(db *badger.DB, vmName, ifaceName string) (Target, error) {
|
||||||
|
prefix := "vm/" + vmName + "/nic/"
|
||||||
|
entries, err := kv.ListByPrefix(db, prefix)
|
||||||
|
if err != nil {
|
||||||
|
return Target{}, fmt.Errorf("vm %q: listing interfaces: %w", vmName, err)
|
||||||
|
}
|
||||||
|
for key, value := range entries {
|
||||||
|
nic, ok := strings.CutSuffix(strings.TrimPrefix(key, prefix), "/name")
|
||||||
|
if !ok || strings.Contains(nic, "/") || value != ifaceName {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ip := net.ParseIP(entries[prefix+nic+"/ip"]).To4()
|
||||||
|
if ip == nil {
|
||||||
|
return Target{}, fmt.Errorf("vm %q: interface %q has no IPv4 address", vmName, ifaceName)
|
||||||
|
}
|
||||||
|
subnet := entries[prefix+nic+"/subnet"]
|
||||||
|
vpc, err := kv.GetFromDB(db, "subnet/"+subnet+"/vpc")
|
||||||
|
if err != nil {
|
||||||
|
return Target{}, fmt.Errorf("vm %q: interface %q: vpc of subnet %q: %w", vmName, ifaceName, subnet, err)
|
||||||
|
}
|
||||||
|
raw, err := kv.GetFromDB(db, "vpc/"+vpc+"/cidr")
|
||||||
|
if err != nil {
|
||||||
|
return Target{}, fmt.Errorf("vm %q: interface %q: cidr of vpc %q: %w", vmName, ifaceName, vpc, err)
|
||||||
|
}
|
||||||
|
_, cidr, err := net.ParseCIDR(raw)
|
||||||
|
if err != nil {
|
||||||
|
return Target{}, fmt.Errorf("vm %q: interface %q: invalid cidr %q of vpc %q", vmName, ifaceName, raw, vpc)
|
||||||
|
}
|
||||||
|
return Target{VPC: vpc, CIDR: cidr, Private: ip}, nil
|
||||||
|
}
|
||||||
|
return Target{}, fmt.Errorf("vm %q has no interface %q", vmName, ifaceName)
|
||||||
|
}
|
||||||
|
|
||||||
|
func build(t Target, public *net.IPNet, gw net.IP) (floatingIP, error) {
|
||||||
|
iface, err := Iface(public.IP)
|
||||||
|
if err != nil {
|
||||||
|
return floatingIP{}, err
|
||||||
|
}
|
||||||
|
mac, err := netif.FloatingIPMAC(public.IP)
|
||||||
|
if err != nil {
|
||||||
|
return floatingIP{}, err
|
||||||
|
}
|
||||||
|
tbl, err := table(t.Private)
|
||||||
|
if err != nil {
|
||||||
|
return floatingIP{}, err
|
||||||
|
}
|
||||||
|
return floatingIP{netns: t.VPC, vpcCIDR: t.CIDR, iface: iface, mac: mac, public: public, private: t.Private, gateway: gw, table: tbl}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Create(t Target, public *net.IPNet, gw net.IP) error {
|
||||||
|
f, err := build(t, public, gw)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return create(system{}, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Delete(t Target, public *net.IPNet) error {
|
||||||
|
f, err := build(t, public, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return remove(system{}, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
type step struct {
|
||||||
|
what string
|
||||||
|
run func() error
|
||||||
|
}
|
||||||
|
|
||||||
|
func runSteps(f floatingIP, steps []step) error {
|
||||||
|
for _, s := range steps {
|
||||||
|
if err := s.run(); err != nil {
|
||||||
|
return fmt.Errorf("floating ip %s of %s: %s: %w", f.public.IP, f.private, s.what, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f floatingIP) host() *net.IPNet {
|
||||||
|
return &net.IPNet{IP: f.public.IP.To4(), Mask: net.CIDRMask(32, 32)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f floatingIP) segment() *net.IPNet {
|
||||||
|
return &net.IPNet{IP: f.public.IP.Mask(f.public.Mask), Mask: f.public.Mask}
|
||||||
|
}
|
||||||
|
|
||||||
|
func sysctls(iface string) [][2]string {
|
||||||
|
return [][2]string{
|
||||||
|
{"net.ipv4.conf." + iface + ".arp_ignore", "1"},
|
||||||
|
{"net.ipv4.conf." + iface + ".arp_announce", "2"},
|
||||||
|
{"net.ipv4.conf." + iface + ".arp_notify", "1"},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func create(h host, f floatingIP) error {
|
||||||
|
ns := f.netns
|
||||||
|
rules := nft.FloatingIP{Iface: f.iface, Public: f.public.IP.String(), Private: f.private.String()}
|
||||||
|
steps := []step{
|
||||||
|
{"create " + f.iface, func() error { return h.CreateMacvlan(ns, f.iface, publicBridge, f.mac) }},
|
||||||
|
{"local fdb entry " + f.mac.String(), func() error { return h.BridgeSetLocalMAC(ns, publicBridge, f.mac) }},
|
||||||
|
}
|
||||||
|
for _, sc := range sysctls(f.iface) {
|
||||||
|
steps = append(steps, step{sc[0], func() error { return h.Sysctl(ns, sc[0], sc[1]) }})
|
||||||
|
}
|
||||||
|
steps = append(steps,
|
||||||
|
step{"translation rules", func() error { return h.SetFloatingIP(ns, rules) }},
|
||||||
|
step{"address " + f.host().String(), func() error { return h.AddrAdd(ns, f.iface, f.host()) }},
|
||||||
|
step{"set up " + f.iface, func() error { return h.LinkUp(ns, f.iface) }},
|
||||||
|
step{fmt.Sprintf("route %s of table %d", f.segment(), f.table), func() error {
|
||||||
|
return h.TableRouteDev(ns, f.table, f.segment(), f.iface)
|
||||||
|
}},
|
||||||
|
step{fmt.Sprintf("default route of table %d via %s", f.table, f.gateway), func() error {
|
||||||
|
return h.TableDefaultVia(ns, f.table, f.gateway, f.iface)
|
||||||
|
}},
|
||||||
|
step{"rule to the main table for " + f.vpcCIDR.String(), func() error { return h.RuleAdd(ns, f.private, f.vpcCIDR, mainTable, prefMain) }},
|
||||||
|
step{fmt.Sprintf("rule to table %d", f.table), func() error { return h.RuleAdd(ns, f.private, nil, f.table, prefOwn) }},
|
||||||
|
)
|
||||||
|
return runSteps(f, steps)
|
||||||
|
}
|
||||||
|
|
||||||
|
func remove(h host, f floatingIP) error {
|
||||||
|
ns := f.netns
|
||||||
|
return runSteps(f, []step{
|
||||||
|
{fmt.Sprintf("rule to table %d", f.table), func() error { return h.RuleDel(ns, f.private, nil, f.table, prefOwn) }},
|
||||||
|
{"rule to the main table for " + f.vpcCIDR.String(), func() error { return h.RuleDel(ns, f.private, f.vpcCIDR, mainTable, prefMain) }},
|
||||||
|
{"translation rules", func() error { return h.DeleteFloatingIP(ns, f.iface) }},
|
||||||
|
{"local fdb entry " + f.mac.String(), func() error { return h.BridgeDelLocalMAC(ns, publicBridge, f.mac) }},
|
||||||
|
{"delete " + f.iface, func() error { return h.DeleteLink(ns, f.iface) }},
|
||||||
|
})
|
||||||
|
}
|
||||||
235
internal/fip/fip_test.go
Normal file
235
internal/fip/fip_test.go
Normal file
|
|
@ -0,0 +1,235 @@
|
||||||
|
package fip
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.g3e.fr/syonad/two/internal/nft"
|
||||||
|
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||||
|
"github.com/dgraph-io/badger/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
type fakeHost struct {
|
||||||
|
calls []string
|
||||||
|
fail map[string]error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeHost) record(call string) error {
|
||||||
|
f.calls = append(f.calls, call)
|
||||||
|
for prefix, err := range f.fail {
|
||||||
|
if strings.HasPrefix(call, prefix) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeHost) CreateMacvlan(ns, name, parent string, mac net.HardwareAddr) error {
|
||||||
|
return f.record(fmt.Sprintf("macvlan [%s] %s parent %s mac %s", ns, name, parent, mac))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeHost) BridgeSetLocalMAC(ns, bridge string, mac net.HardwareAddr) error {
|
||||||
|
return f.record(fmt.Sprintf("fdb add [%s] %s %s", ns, bridge, mac))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeHost) BridgeDelLocalMAC(ns, bridge string, mac net.HardwareAddr) error {
|
||||||
|
return f.record(fmt.Sprintf("fdb del [%s] %s %s", ns, bridge, mac))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeHost) Sysctl(ns, key, value string) error {
|
||||||
|
return f.record(fmt.Sprintf("sysctl [%s] %s=%s", ns, key, value))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeHost) SetFloatingIP(ns string, r nft.FloatingIP) error {
|
||||||
|
return f.record(fmt.Sprintf("nft set [%s] %s %s<->%s", ns, r.Iface, r.Public, r.Private))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeHost) DeleteFloatingIP(ns, iface string) error {
|
||||||
|
return f.record(fmt.Sprintf("nft del [%s] %s", ns, iface))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeHost) AddrAdd(ns, iface string, prefix *net.IPNet) error {
|
||||||
|
return f.record(fmt.Sprintf("addr [%s] %s %s", ns, iface, prefix))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeHost) LinkUp(ns, iface string) error {
|
||||||
|
return f.record(fmt.Sprintf("up [%s] %s", ns, iface))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeHost) TableRouteDev(ns string, table int, dst *net.IPNet, iface string) error {
|
||||||
|
return f.record(fmt.Sprintf("table %d [%s] %s dev %s", table, ns, dst, iface))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeHost) TableDefaultVia(ns string, table int, gw net.IP, iface string) error {
|
||||||
|
return f.record(fmt.Sprintf("table %d [%s] default via %s dev %s", table, ns, gw, iface))
|
||||||
|
}
|
||||||
|
|
||||||
|
func rule(src net.IP, dst *net.IPNet, table, priority int) string {
|
||||||
|
to := "all"
|
||||||
|
if dst != nil {
|
||||||
|
to = dst.String()
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%d from %s to %s lookup %d", priority, src, to, table)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeHost) RuleAdd(ns string, src net.IP, dst *net.IPNet, table, priority int) error {
|
||||||
|
return f.record(fmt.Sprintf("rule add [%s] %s", ns, rule(src, dst, table, priority)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeHost) RuleDel(ns string, src net.IP, dst *net.IPNet, table, priority int) error {
|
||||||
|
return f.record(fmt.Sprintf("rule del [%s] %s", ns, rule(src, dst, table, priority)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeHost) DeleteLink(ns, name string) error {
|
||||||
|
return f.record(fmt.Sprintf("del [%s] %s", ns, name))
|
||||||
|
}
|
||||||
|
|
||||||
|
func labFloatingIP(t *testing.T) floatingIP {
|
||||||
|
t.Helper()
|
||||||
|
ip, public, err := net.ParseCIDR("240.0.0.47/24")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
public.IP = ip
|
||||||
|
_, cidr, _ := net.ParseCIDR("10.47.0.0/16")
|
||||||
|
f, err := build(Target{VPC: "vp-t47", CIDR: cidr, Private: net.ParseIP("10.47.1.10")}, public, net.ParseIP("240.0.0.1"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildDerivesInterfaceMACAndTable(t *testing.T) {
|
||||||
|
f := labFloatingIP(t)
|
||||||
|
if f.iface != "fip-f000002f" || f.mac.String() != "02:05:f0:00:00:2f" || f.table != 0x0a2f010a {
|
||||||
|
t.Errorf("interface %s, MAC %s, table %d", f.iface, f.mac, f.table)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildRefusesAReservedTable(t *testing.T) {
|
||||||
|
_, public, _ := net.ParseCIDR("240.0.0.47/24")
|
||||||
|
if _, err := build(Target{VPC: "vp-t47", Private: net.ParseIP("0.0.0.254")}, public, nil); err == nil || !strings.Contains(err.Error(), "reserved routing table 254") {
|
||||||
|
t.Errorf("erreur %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateTranslatesBeforeRoutingThroughTheFloatingIP(t *testing.T) {
|
||||||
|
f := &fakeHost{}
|
||||||
|
if err := create(f, labFloatingIP(t)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
want := []string{
|
||||||
|
"macvlan [vp-t47] fip-f000002f parent br-public mac 02:05:f0:00:00:2f",
|
||||||
|
"fdb add [vp-t47] br-public 02:05:f0:00:00:2f",
|
||||||
|
"sysctl [vp-t47] net.ipv4.conf.fip-f000002f.arp_ignore=1",
|
||||||
|
"sysctl [vp-t47] net.ipv4.conf.fip-f000002f.arp_announce=2",
|
||||||
|
"sysctl [vp-t47] net.ipv4.conf.fip-f000002f.arp_notify=1",
|
||||||
|
"nft set [vp-t47] fip-f000002f 240.0.0.47<->10.47.1.10",
|
||||||
|
"addr [vp-t47] fip-f000002f 240.0.0.47/32",
|
||||||
|
"up [vp-t47] fip-f000002f",
|
||||||
|
"table 170852618 [vp-t47] 240.0.0.0/24 dev fip-f000002f",
|
||||||
|
"table 170852618 [vp-t47] default via 240.0.0.1 dev fip-f000002f",
|
||||||
|
"rule add [vp-t47] 100 from 10.47.1.10 to 10.47.0.0/16 lookup 254",
|
||||||
|
"rule add [vp-t47] 101 from 10.47.1.10 to all lookup 170852618",
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(f.calls, want) {
|
||||||
|
t.Errorf("appels :\n%s\nattendu :\n%s", strings.Join(f.calls, "\n"), strings.Join(want, "\n"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateRoutesNothingThroughTheFloatingIPWithoutTranslation(t *testing.T) {
|
||||||
|
f := &fakeHost{fail: map[string]error{"nft set ": errors.New("nft absent")}}
|
||||||
|
err := create(f, labFloatingIP(t))
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "floating ip 240.0.0.47 of 10.47.1.10: translation rules: nft absent") {
|
||||||
|
t.Fatalf("erreur %v", err)
|
||||||
|
}
|
||||||
|
for _, c := range f.calls {
|
||||||
|
if strings.HasPrefix(c, "rule ") || strings.HasPrefix(c, "table ") || strings.HasPrefix(c, "addr ") {
|
||||||
|
t.Errorf("étape exécutée sans traduction : %q", c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRemoveDropsTheRulesFirstThenUndoesTheHost(t *testing.T) {
|
||||||
|
f := &fakeHost{}
|
||||||
|
if err := remove(f, labFloatingIP(t)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
want := []string{
|
||||||
|
"rule del [vp-t47] 101 from 10.47.1.10 to all lookup 170852618",
|
||||||
|
"rule del [vp-t47] 100 from 10.47.1.10 to 10.47.0.0/16 lookup 254",
|
||||||
|
"nft del [vp-t47] fip-f000002f",
|
||||||
|
"fdb del [vp-t47] br-public 02:05:f0:00:00:2f",
|
||||||
|
"del [vp-t47] fip-f000002f",
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(f.calls, want) {
|
||||||
|
t.Errorf("appels :\n%s\nattendu :\n%s", strings.Join(f.calls, "\n"), strings.Join(want, "\n"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRemoveStopsAtTheFirstFailure(t *testing.T) {
|
||||||
|
f := &fakeHost{fail: map[string]error{"rule del [vp-t47] 101": errors.New("netns absent")}}
|
||||||
|
if err := remove(f, labFloatingIP(t)); err == nil || len(f.calls) != 1 {
|
||||||
|
t.Errorf("erreur %v, appels %q", err, f.calls)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func seedVM(t *testing.T) *badger.DB {
|
||||||
|
t.Helper()
|
||||||
|
db := kv.InitDB(kv.Config{Path: t.TempDir()}, false)
|
||||||
|
t.Cleanup(func() { db.Close() })
|
||||||
|
for key, value := range map[string]string{
|
||||||
|
"vpc/vp-t47/cidr": "10.47.0.0/16",
|
||||||
|
"subnet/sn-a/vpc": "vp-t47",
|
||||||
|
"subnet/sn-b/vpc": "vp-t47",
|
||||||
|
"vm/vm-1/nic/0/name": "nic-a1b2",
|
||||||
|
"vm/vm-1/nic/0/subnet": "sn-a",
|
||||||
|
"vm/vm-1/nic/0/ip": "10.47.1.10",
|
||||||
|
"vm/vm-1/nic/1/name": "nic-c3d4",
|
||||||
|
"vm/vm-1/nic/1/subnet": "sn-b",
|
||||||
|
"vm/vm-1/nic/1/ip": "10.47.2.10",
|
||||||
|
"vm/vm-10/nic/0/name": "nic-a1b2",
|
||||||
|
"vm/vm-10/nic/0/subnet": "sn-a",
|
||||||
|
"vm/vm-10/nic/0/ip": "10.47.1.99",
|
||||||
|
} {
|
||||||
|
kv.AddInDB(db, key, value)
|
||||||
|
}
|
||||||
|
return db
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveFindsTheInterfaceByName(t *testing.T) {
|
||||||
|
got, err := Resolve(seedVM(t), "vm-1", "nic-c3d4")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if got.VPC != "vp-t47" || got.CIDR.String() != "10.47.0.0/16" || got.Private.String() != "10.47.2.10" {
|
||||||
|
t.Errorf("cible %+v", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveIgnoresAVMWhoseNameExtendsTheOne(t *testing.T) {
|
||||||
|
got, err := Resolve(seedVM(t), "vm-1", "nic-a1b2")
|
||||||
|
if err != nil || got.Private.String() != "10.47.1.10" {
|
||||||
|
t.Errorf("cible %+v, erreur %v", got, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolveRefusesAnUnknownInterface(t *testing.T) {
|
||||||
|
if _, err := Resolve(seedVM(t), "vm-1", "nic-zzzz"); err == nil || !strings.Contains(err.Error(), `vm "vm-1" has no interface "nic-zzzz"`) {
|
||||||
|
t.Errorf("erreur %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIfaceFitsTheKernelLimit(t *testing.T) {
|
||||||
|
name, err := Iface(net.ParseIP("255.255.255.255"))
|
||||||
|
if err != nil || name != "fip-ffffffff" || len(name) > 15 {
|
||||||
|
t.Errorf("%q (%d caractères), %v", name, len(name), err)
|
||||||
|
}
|
||||||
|
if _, err := Iface(net.ParseIP("2001:db8::1")); err == nil {
|
||||||
|
t.Error("IPv6 acceptée")
|
||||||
|
}
|
||||||
|
}
|
||||||
82
internal/fip/host.go
Normal file
82
internal/fip/host.go
Normal file
|
|
@ -0,0 +1,82 @@
|
||||||
|
package fip
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"git.g3e.fr/syonad/two/internal/netif"
|
||||||
|
"git.g3e.fr/syonad/two/internal/netns"
|
||||||
|
"git.g3e.fr/syonad/two/internal/nft"
|
||||||
|
"git.g3e.fr/syonad/two/internal/sysctl"
|
||||||
|
)
|
||||||
|
|
||||||
|
type host interface {
|
||||||
|
CreateMacvlan(ns, name, parent string, mac net.HardwareAddr) error
|
||||||
|
BridgeSetLocalMAC(ns, bridge string, mac net.HardwareAddr) error
|
||||||
|
BridgeDelLocalMAC(ns, bridge string, mac net.HardwareAddr) error
|
||||||
|
Sysctl(ns, key, value string) error
|
||||||
|
SetFloatingIP(ns string, f nft.FloatingIP) error
|
||||||
|
DeleteFloatingIP(ns, iface string) error
|
||||||
|
AddrAdd(ns, iface string, prefix *net.IPNet) error
|
||||||
|
LinkUp(ns, iface string) error
|
||||||
|
TableRouteDev(ns string, table int, dst *net.IPNet, iface string) error
|
||||||
|
TableDefaultVia(ns string, table int, gw net.IP, iface string) error
|
||||||
|
RuleAdd(ns string, src net.IP, dst *net.IPNet, table, priority int) error
|
||||||
|
RuleDel(ns string, src net.IP, dst *net.IPNet, table, priority int) error
|
||||||
|
DeleteLink(ns, name string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
var defaultRoute = &net.IPNet{IP: net.IPv4zero.To4(), Mask: net.CIDRMask(0, 32)}
|
||||||
|
|
||||||
|
type system struct{}
|
||||||
|
|
||||||
|
func (system) CreateMacvlan(ns, name, parent string, mac net.HardwareAddr) error {
|
||||||
|
return netns.Call(ns, func() error { return netif.CreateMacvlan(name, parent, mac) })
|
||||||
|
}
|
||||||
|
|
||||||
|
func (system) BridgeSetLocalMAC(ns, bridge string, mac net.HardwareAddr) error {
|
||||||
|
return netns.Call(ns, func() error { return netif.BridgeSetLocalMAC(bridge, mac) })
|
||||||
|
}
|
||||||
|
|
||||||
|
func (system) BridgeDelLocalMAC(ns, bridge string, mac net.HardwareAddr) error {
|
||||||
|
return netns.Call(ns, func() error { return netif.BridgeDelLocalMACIfExists(bridge, mac) })
|
||||||
|
}
|
||||||
|
|
||||||
|
func (system) Sysctl(ns, key, value string) error {
|
||||||
|
return netns.Call(ns, func() error { return sysctl.Set(key, value) })
|
||||||
|
}
|
||||||
|
|
||||||
|
func (system) SetFloatingIP(ns string, f nft.FloatingIP) error {
|
||||||
|
return netns.Call(ns, func() error { return nft.SetFloatingIP(f) })
|
||||||
|
}
|
||||||
|
|
||||||
|
func (system) DeleteFloatingIP(ns, iface string) error {
|
||||||
|
return netns.Call(ns, func() error { return nft.DeleteFloatingIP(iface) })
|
||||||
|
}
|
||||||
|
|
||||||
|
func (system) AddrAdd(ns, iface string, prefix *net.IPNet) error {
|
||||||
|
return netns.Call(ns, func() error { return netif.AddrAddPrefix(iface, prefix) })
|
||||||
|
}
|
||||||
|
|
||||||
|
func (system) LinkUp(ns, iface string) error {
|
||||||
|
return netns.Call(ns, func() error { return netif.LinkSetUp(iface) })
|
||||||
|
}
|
||||||
|
|
||||||
|
func (system) TableRouteDev(ns string, table int, dst *net.IPNet, iface string) error {
|
||||||
|
return netns.Call(ns, func() error { return netif.RouteReplaceDev(table, dst, iface) })
|
||||||
|
}
|
||||||
|
|
||||||
|
func (system) TableDefaultVia(ns string, table int, gw net.IP, iface string) error {
|
||||||
|
return netns.Call(ns, func() error { return netif.RouteReplaceVia(table, defaultRoute, gw, iface) })
|
||||||
|
}
|
||||||
|
|
||||||
|
func (system) RuleAdd(ns string, src net.IP, dst *net.IPNet, table, priority int) error {
|
||||||
|
return netns.Call(ns, func() error { return netif.RuleAddFrom(src, dst, table, priority) })
|
||||||
|
}
|
||||||
|
|
||||||
|
func (system) RuleDel(ns string, src net.IP, dst *net.IPNet, table, priority int) error {
|
||||||
|
return netns.Call(ns, func() error { return netif.RuleDelFromIfExists(src, dst, table, priority) })
|
||||||
|
}
|
||||||
|
|
||||||
|
func (system) DeleteLink(ns, name string) error {
|
||||||
|
return netns.Call(ns, func() error { return netif.DeleteLinkIfExists(name) })
|
||||||
|
}
|
||||||
43
internal/migration/nicname.go
Normal file
43
internal/migration/nicname.go
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
package migration
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||||
|
|
||||||
|
"github.com/dgraph-io/badger/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
func MigrateVMNICNames(db *badger.DB, log *slog.Logger) error {
|
||||||
|
vms, err := kv.ListByPrefix(db, "vm/")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("list vm/: %w", err)
|
||||||
|
}
|
||||||
|
for _, prefix := range nicsWithoutName(vms) {
|
||||||
|
name := "nic-" + strings.Split(prefix, "/")[3]
|
||||||
|
if err := kv.AddInDB(db, prefix+"name", name); err != nil {
|
||||||
|
return fmt.Errorf("store %sname: %w", prefix, err)
|
||||||
|
}
|
||||||
|
log.Info("vm nic name migrated", "resource", strings.TrimSuffix(prefix, "/"), "name", name, "reason", "interface name now required")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func nicsWithoutName(entries map[string]string) []string {
|
||||||
|
var prefixes []string
|
||||||
|
for key := range entries {
|
||||||
|
parts := strings.Split(key, "/")
|
||||||
|
if len(parts) != 5 || parts[2] != "nic" || parts[4] != "subnet" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
prefix := strings.TrimSuffix(key, "subnet")
|
||||||
|
if _, ok := entries[prefix+"name"]; !ok {
|
||||||
|
prefixes = append(prefixes, prefix)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Strings(prefixes)
|
||||||
|
return prefixes
|
||||||
|
}
|
||||||
52
internal/migration/nicname_test.go
Normal file
52
internal/migration/nicname_test.go
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
package migration
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMigrateVMNICNames_NamesEveryInterfaceByItsIndex(t *testing.T) {
|
||||||
|
db := newTestDB(t)
|
||||||
|
seedPreloadedVM(t, db, "vm-1", "running", map[int][2]string{0: {"sn-a", "10.0.0.10"}, 1: {"sn-b", "10.0.1.10"}})
|
||||||
|
if err := MigrateVMNICNames(db, discardLogger()); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
assertKey(t, db, "vm/vm-1/nic/0/name", "nic-0")
|
||||||
|
assertKey(t, db, "vm/vm-1/nic/1/name", "nic-1")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMigrateVMNICNames_KeepsAnExistingName(t *testing.T) {
|
||||||
|
db := newTestDB(t)
|
||||||
|
seedPreloadedVM(t, db, "vm-1", "running", map[int][2]string{0: {"sn-a", "10.0.0.10"}, 1: {"sn-b", "10.0.1.10"}})
|
||||||
|
kv.AddInDB(db, "vm/vm-1/nic/0/name", "nic-a1b2")
|
||||||
|
if err := MigrateVMNICNames(db, discardLogger()); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
assertKey(t, db, "vm/vm-1/nic/0/name", "nic-a1b2")
|
||||||
|
assertKey(t, db, "vm/vm-1/nic/1/name", "nic-1")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMigrateVMNICNames_IsIdempotent(t *testing.T) {
|
||||||
|
db := newTestDB(t)
|
||||||
|
seedPreloadedVM(t, db, "vm-1", "running", map[int][2]string{0: {"sn-a", "10.0.0.10"}})
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
if err := MigrateVMNICNames(db, discardLogger()); err != nil {
|
||||||
|
t.Fatalf("passage %d : %v", i+1, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertKey(t, db, "vm/vm-1/nic/0/name", "nic-0")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMigrateVMNICNames_IgnoresKeysOutsideInterfaces(t *testing.T) {
|
||||||
|
db := newTestDB(t)
|
||||||
|
kv.AddInDB(db, "vm/vm-1/state", "running")
|
||||||
|
kv.AddInDB(db, "vm/vm-1/disk/vda", "/data/vda.qcow2")
|
||||||
|
if err := MigrateVMNICNames(db, discardLogger()); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
entries, _ := kv.ListByPrefix(db, "vm/vm-1/")
|
||||||
|
if len(entries) != 2 {
|
||||||
|
t.Errorf("clés après migration : %v", entries)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -14,7 +14,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// prefixes énumère les familles de ressources portant une clé /state.
|
// prefixes énumère les familles de ressources portant une clé /state.
|
||||||
var prefixes = []string{"vpc/", "subnet/", "vm/", "nat-gateway/"}
|
var prefixes = []string{"vpc/", "subnet/", "vm/", "nat-gateway/", "floating-ip/"}
|
||||||
|
|
||||||
// legacyStates traduit l'ancien vocabulaire (états VPC/subnet d'avant
|
// legacyStates traduit l'ancien vocabulaire (états VPC/subnet d'avant
|
||||||
// l'unification, et états VM start/stop) vers l'enum courant.
|
// l'unification, et états VM start/stop) vers l'enum courant.
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,8 @@ func TestMigrateStates_LegacyMapping(t *testing.T) {
|
||||||
"nat creating": {"nat-gateway/ng-1", "creating", state.Error},
|
"nat creating": {"nat-gateway/ng-1", "creating", state.Error},
|
||||||
"nat deleting": {"nat-gateway/ng-2", "deleting", state.Error},
|
"nat deleting": {"nat-gateway/ng-2", "deleting", state.Error},
|
||||||
"nat running": {"nat-gateway/ng-3", "running", state.Running},
|
"nat running": {"nat-gateway/ng-3", "running", state.Running},
|
||||||
|
"fip creating": {"floating-ip/fip-1", "creating", state.Error},
|
||||||
|
"fip running": {"floating-ip/fip-2", "running", state.Running},
|
||||||
}
|
}
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
kv.AddInDB(db, c.resource+"/state", c.from)
|
kv.AddInDB(db, c.resource+"/state", c.from)
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ const (
|
||||||
RoleRouter byte = 0x02
|
RoleRouter byte = 0x02
|
||||||
RoleHostGateway byte = 0x03
|
RoleHostGateway byte = 0x03
|
||||||
RoleNATGateway byte = 0x04
|
RoleNATGateway byte = 0x04
|
||||||
|
RoleFloatingIP byte = 0x05
|
||||||
|
|
||||||
MaxVNI = 1<<24 - 1
|
MaxVNI = 1<<24 - 1
|
||||||
)
|
)
|
||||||
|
|
@ -28,6 +29,10 @@ func AnycastMAC(vni int) (net.HardwareAddr, error) {
|
||||||
return vniMAC(RoleAnycastGateway, vni)
|
return vniMAC(RoleAnycastGateway, vni)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FloatingIPMAC(public net.IP) (net.HardwareAddr, error) {
|
||||||
|
return HostMAC(RoleFloatingIP, public)
|
||||||
|
}
|
||||||
|
|
||||||
func NATGatewayMAC(l3vni int) (net.HardwareAddr, error) {
|
func NATGatewayMAC(l3vni int) (net.HardwareAddr, error) {
|
||||||
return vniMAC(RoleNATGateway, l3vni)
|
return vniMAC(RoleNATGateway, l3vni)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -141,3 +141,34 @@ func TestNATGatewayMACDiffersFromOtherRoles(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFloatingIPMAC(t *testing.T) {
|
||||||
|
cases := map[string]string{
|
||||||
|
"203.0.113.20": "02:05:cb:00:71:14",
|
||||||
|
"240.0.0.47": "02:05:f0:00:00:2f",
|
||||||
|
}
|
||||||
|
for ip, want := range cases {
|
||||||
|
got, err := FloatingIPMAC(net.ParseIP(ip))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s : erreur inattendue %v", ip, err)
|
||||||
|
}
|
||||||
|
if got.String() != want {
|
||||||
|
t.Errorf("%s : %s, %s attendu", ip, got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if mac, err := FloatingIPMAC(net.ParseIP("2001:db8::20")); err == nil {
|
||||||
|
t.Errorf("IPv6 acceptée : %s", mac)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFloatingIPMACDiffersFromOtherRoles(t *testing.T) {
|
||||||
|
fip, _ := FloatingIPMAC(net.ParseIP("192.168.14.11"))
|
||||||
|
for _, role := range []byte{RoleAnycastGateway, RoleRouter, RoleHostGateway, RoleNATGateway} {
|
||||||
|
if fip[1] == role {
|
||||||
|
t.Errorf("rôle %#x : même octet de rôle que l'IP flottante (%s)", role, fip)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if fip[1] != 0x05 {
|
||||||
|
t.Errorf("%s : octet de rôle 0x05 attendu (valeur publiée dans la doc)", fip)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,19 @@ func TableRoutesVia(table int, iface string) ([]*net.IPNet, error) {
|
||||||
return dsts, nil
|
return dsts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RouteReplaceDev(table int, dst *net.IPNet, iface string) error {
|
||||||
|
link, err := netlink.LinkByName(iface)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return netlink.RouteReplace(&netlink.Route{
|
||||||
|
LinkIndex: link.Attrs().Index,
|
||||||
|
Dst: dst,
|
||||||
|
Table: table,
|
||||||
|
Scope: netlink.SCOPE_LINK,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func RouteDelViaIfExists(table int, dst *net.IPNet, gw net.IP, iface string) error {
|
func RouteDelViaIfExists(table int, dst *net.IPNet, gw net.IP, iface string) error {
|
||||||
link, err := netlink.LinkByName(iface)
|
link, err := netlink.LinkByName(iface)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -34,3 +34,7 @@ func HasUnreachableDefault(_, _ int) (bool, error) {
|
||||||
func RouteDelViaIfExists(_ int, _ *net.IPNet, _ net.IP, _ string) error {
|
func RouteDelViaIfExists(_ int, _ *net.IPNet, _ net.IP, _ string) error {
|
||||||
return errors.New("not supported on this platform")
|
return errors.New("not supported on this platform")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RouteReplaceDev(_ int, _ *net.IPNet, _ string) error {
|
||||||
|
return errors.New("not supported on this platform")
|
||||||
|
}
|
||||||
|
|
|
||||||
32
internal/netif/rule_linux.go
Normal file
32
internal/netif/rule_linux.go
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
//go:build linux
|
||||||
|
|
||||||
|
package netif
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/vishvananda/netlink"
|
||||||
|
)
|
||||||
|
|
||||||
|
func sourceRule(src net.IP, dst *net.IPNet, table, priority int) *netlink.Rule {
|
||||||
|
r := netlink.NewRule()
|
||||||
|
r.Family = netlink.FAMILY_V4
|
||||||
|
r.Src = &net.IPNet{IP: src.To4(), Mask: net.CIDRMask(32, 32)}
|
||||||
|
r.Dst = dst
|
||||||
|
r.Table = table
|
||||||
|
r.Priority = priority
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func RuleAddFrom(src net.IP, dst *net.IPNet, table, priority int) error {
|
||||||
|
return netlink.RuleAdd(sourceRule(src, dst, table, priority))
|
||||||
|
}
|
||||||
|
|
||||||
|
func RuleDelFromIfExists(src net.IP, dst *net.IPNet, table, priority int) error {
|
||||||
|
if err := netlink.RuleDel(sourceRule(src, dst, table, priority)); err != nil && !errors.Is(err, syscall.ENOENT) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
22
internal/netif/rule_linux_test.go
Normal file
22
internal/netif/rule_linux_test.go
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
//go:build linux
|
||||||
|
|
||||||
|
package netif
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/vishvananda/netlink"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSourceRule(t *testing.T) {
|
||||||
|
r := sourceRule(net.ParseIP("10.46.1.10"), nil, 170786058, 101)
|
||||||
|
if r.Family != netlink.FAMILY_V4 || r.Src.String() != "10.46.1.10/32" || r.Dst != nil || r.Table != 170786058 || r.Priority != 101 {
|
||||||
|
t.Errorf("règle %+v", r)
|
||||||
|
}
|
||||||
|
_, vpc, _ := net.ParseCIDR("10.46.0.0/16")
|
||||||
|
toVPC := sourceRule(net.ParseIP("10.46.1.10"), vpc, 254, 100)
|
||||||
|
if toVPC.Dst.String() != "10.46.0.0/16" || toVPC.Table != 254 || toVPC.SuppressPrefixlen != -1 {
|
||||||
|
t.Errorf("règle vers le VPC %+v", toVPC)
|
||||||
|
}
|
||||||
|
}
|
||||||
16
internal/netif/rule_other.go
Normal file
16
internal/netif/rule_other.go
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
//go:build !linux
|
||||||
|
|
||||||
|
package netif
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RuleAddFrom(_ net.IP, _ *net.IPNet, _, _ int) error {
|
||||||
|
return errors.New("not supported on this platform")
|
||||||
|
}
|
||||||
|
|
||||||
|
func RuleDelFromIfExists(_ net.IP, _ *net.IPNet, _, _ int) error {
|
||||||
|
return errors.New("not supported on this platform")
|
||||||
|
}
|
||||||
58
internal/nft/fip.go
Normal file
58
internal/nft/fip.go
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
package nft
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type FloatingIP struct {
|
||||||
|
Iface string
|
||||||
|
Public string
|
||||||
|
Private string
|
||||||
|
}
|
||||||
|
|
||||||
|
var fipChains = []natChain{
|
||||||
|
{"pre-", "filter", "prerouting", "raw"},
|
||||||
|
{"post-", "filter", "postrouting", "srcnat"},
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f FloatingIP) rule(c natChain) string {
|
||||||
|
if c.hook == "prerouting" {
|
||||||
|
return fmt.Sprintf("iifname %q ip daddr %s ip daddr set %s", f.Iface, f.Public, f.Private)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("oifname %q ip saddr %s ip saddr set %s", f.Iface, f.Private, f.Public)
|
||||||
|
}
|
||||||
|
|
||||||
|
func fipSetScript(f FloatingIP) string {
|
||||||
|
var b strings.Builder
|
||||||
|
fmt.Fprintf(&b, "add table ip %s\n", Table)
|
||||||
|
for _, c := range fipChains {
|
||||||
|
name := c.declare(&b, f.Iface)
|
||||||
|
fmt.Fprintf(&b, "add rule ip %s %s %s\n", Table, name, f.rule(c))
|
||||||
|
}
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func fipDeleteScript(iface string) string {
|
||||||
|
var b strings.Builder
|
||||||
|
fmt.Fprintf(&b, "add table ip %s\n", Table)
|
||||||
|
for _, c := range fipChains {
|
||||||
|
name := c.declare(&b, iface)
|
||||||
|
fmt.Fprintf(&b, "delete chain ip %s %s\n", Table, name)
|
||||||
|
}
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetFloatingIP(f FloatingIP) error {
|
||||||
|
if err := apply(fipSetScript(f)); err != nil {
|
||||||
|
return fmt.Errorf("set floating ip %s: %w", f.Iface, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteFloatingIP(iface string) error {
|
||||||
|
if err := apply(fipDeleteScript(iface)); err != nil {
|
||||||
|
return fmt.Errorf("delete floating ip %s: %w", iface, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
53
internal/nft/fip_test.go
Normal file
53
internal/nft/fip_test.go
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
package nft
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func labFloatingIP() FloatingIP {
|
||||||
|
return FloatingIP{Iface: "fip-f000002f", Public: "240.0.0.47", Private: "10.46.1.10"}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFloatingIPSetScript(t *testing.T) {
|
||||||
|
want := `add table ip two
|
||||||
|
add chain ip two pre-fip-f000002f { type filter hook prerouting priority raw; policy accept; }
|
||||||
|
flush chain ip two pre-fip-f000002f
|
||||||
|
add rule ip two pre-fip-f000002f iifname "fip-f000002f" ip daddr 240.0.0.47 ip daddr set 10.46.1.10
|
||||||
|
add chain ip two post-fip-f000002f { type filter hook postrouting priority srcnat; policy accept; }
|
||||||
|
flush chain ip two post-fip-f000002f
|
||||||
|
add rule ip two post-fip-f000002f oifname "fip-f000002f" ip saddr 10.46.1.10 ip saddr set 240.0.0.47
|
||||||
|
`
|
||||||
|
if got := fipSetScript(labFloatingIP()); got != want {
|
||||||
|
t.Errorf("script :\n%s\nattendu :\n%s", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFloatingIPDeleteScript(t *testing.T) {
|
||||||
|
want := `add table ip two
|
||||||
|
add chain ip two pre-fip-f000002f { type filter hook prerouting priority raw; policy accept; }
|
||||||
|
flush chain ip two pre-fip-f000002f
|
||||||
|
delete chain ip two pre-fip-f000002f
|
||||||
|
add chain ip two post-fip-f000002f { type filter hook postrouting priority srcnat; policy accept; }
|
||||||
|
flush chain ip two post-fip-f000002f
|
||||||
|
delete chain ip two post-fip-f000002f
|
||||||
|
`
|
||||||
|
if got := fipDeleteScript("fip-f000002f"); got != want {
|
||||||
|
t.Errorf("script :\n%s\nattendu :\n%s", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFloatingIPScriptsDoNotTrackNorTouchOtherInterfaces(t *testing.T) {
|
||||||
|
script := fipSetScript(labFloatingIP()) + fipDeleteScript("fip-f000002f")
|
||||||
|
for _, line := range strings.Split(strings.TrimSpace(script), "\n") {
|
||||||
|
if !strings.Contains(line, " ip two") {
|
||||||
|
t.Errorf("ligne hors de la table ip two : %q", line)
|
||||||
|
}
|
||||||
|
if strings.Contains(line, "notrack") {
|
||||||
|
t.Errorf("notrack présent : %q", line)
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(line, "add rule") && !strings.Contains(line, `ifname "fip-f000002f"`) {
|
||||||
|
t.Errorf("règle qui ne vise pas fip-f000002f : %q", line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -19,6 +19,7 @@ type AgentCollector struct {
|
||||||
subnetsTotal *prometheus.Desc
|
subnetsTotal *prometheus.Desc
|
||||||
vmsTotal *prometheus.Desc
|
vmsTotal *prometheus.Desc
|
||||||
natGateways *prometheus.Desc
|
natGateways *prometheus.Desc
|
||||||
|
floatingIPs *prometheus.Desc
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAgentCollector(db *badger.DB) *AgentCollector {
|
func NewAgentCollector(db *badger.DB) *AgentCollector {
|
||||||
|
|
@ -44,6 +45,11 @@ func NewAgentCollector(db *badger.DB) *AgentCollector {
|
||||||
"Number of NAT gateways by state.",
|
"Number of NAT gateways by state.",
|
||||||
[]string{"state"}, nil,
|
[]string{"state"}, nil,
|
||||||
),
|
),
|
||||||
|
floatingIPs: prometheus.NewDesc(
|
||||||
|
"syonad_floating_ips_total",
|
||||||
|
"Number of floating IPs by state.",
|
||||||
|
[]string{"state"}, nil,
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -52,6 +58,7 @@ func (c *AgentCollector) Describe(ch chan<- *prometheus.Desc) {
|
||||||
ch <- c.subnetsTotal
|
ch <- c.subnetsTotal
|
||||||
ch <- c.vmsTotal
|
ch <- c.vmsTotal
|
||||||
ch <- c.natGateways
|
ch <- c.natGateways
|
||||||
|
ch <- c.floatingIPs
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *AgentCollector) Collect(ch chan<- prometheus.Metric) {
|
func (c *AgentCollector) Collect(ch chan<- prometheus.Metric) {
|
||||||
|
|
@ -59,6 +66,7 @@ func (c *AgentCollector) Collect(ch chan<- prometheus.Metric) {
|
||||||
c.collectStates(ch, "subnet/", c.subnetsTotal)
|
c.collectStates(ch, "subnet/", c.subnetsTotal)
|
||||||
c.collectStates(ch, "vm/", c.vmsTotal)
|
c.collectStates(ch, "vm/", c.vmsTotal)
|
||||||
c.collectStates(ch, "nat-gateway/", c.natGateways)
|
c.collectStates(ch, "nat-gateway/", c.natGateways)
|
||||||
|
c.collectStates(ch, "floating-ip/", c.floatingIPs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// collectStates counts resources under the given DB prefix by their state value
|
// collectStates counts resources under the given DB prefix by their state value
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,8 @@ func collect(t *testing.T, c *AgentCollector) map[string]map[string]float64 {
|
||||||
name = "vms"
|
name = "vms"
|
||||||
case strings.Contains(desc, "syonad_nat_gateways_total"):
|
case strings.Contains(desc, "syonad_nat_gateways_total"):
|
||||||
name = "nat_gateways"
|
name = "nat_gateways"
|
||||||
|
case strings.Contains(desc, "syonad_floating_ips_total"):
|
||||||
|
name = "floating_ips"
|
||||||
default:
|
default:
|
||||||
t.Fatalf("métrique inattendue : %s", desc)
|
t.Fatalf("métrique inattendue : %s", desc)
|
||||||
}
|
}
|
||||||
|
|
@ -64,13 +66,14 @@ func collect(t *testing.T, c *AgentCollector) map[string]map[string]float64 {
|
||||||
func TestCollector_CountsByState(t *testing.T) {
|
func TestCollector_CountsByState(t *testing.T) {
|
||||||
db := newTestDB(t)
|
db := newTestDB(t)
|
||||||
for resource, s := range map[string]state.State{
|
for resource, s := range map[string]state.State{
|
||||||
"vpc/vpc-1": state.Running,
|
"vpc/vpc-1": state.Running,
|
||||||
"vpc/vpc-2": state.Running,
|
"vpc/vpc-2": state.Running,
|
||||||
"vpc/vpc-3": state.Error,
|
"vpc/vpc-3": state.Error,
|
||||||
"subnet/sn-1": state.Creating,
|
"subnet/sn-1": state.Creating,
|
||||||
"vm/vm-1": state.Running,
|
"vm/vm-1": state.Running,
|
||||||
"vm/vm-2": state.Error,
|
"vm/vm-2": state.Error,
|
||||||
"nat-gateway/ng-1": state.Running,
|
"nat-gateway/ng-1": state.Running,
|
||||||
|
"floating-ip/fip-1": state.Error,
|
||||||
} {
|
} {
|
||||||
if err := state.Set(db, resource, s); err != nil {
|
if err := state.Set(db, resource, s); err != nil {
|
||||||
t.Fatalf("préparation du test : %v", err)
|
t.Fatalf("préparation du test : %v", err)
|
||||||
|
|
@ -92,6 +95,9 @@ func TestCollector_CountsByState(t *testing.T) {
|
||||||
t.Errorf("vms{state=%q} = %v, attendu %v", s, got["vms"][s], want)
|
t.Errorf("vms{state=%q} = %v, attendu %v", s, got["vms"][s], want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if got["floating_ips"]["error"] != 1 || got["floating_ips"]["running"] != 0 {
|
||||||
|
t.Errorf("floating_ips{state=error} = %v, {state=running} = %v, attendu 1 et 0", got["floating_ips"]["error"], got["floating_ips"]["running"])
|
||||||
|
}
|
||||||
if got["nat_gateways"]["running"] != 1 || got["vpcs"]["running"] != 2 {
|
if got["nat_gateways"]["running"] != 1 || got["vpcs"]["running"] != 2 {
|
||||||
t.Errorf("nat_gateways{state=running} = %v, vpcs{state=running} = %v, attendu 1 et 2", got["nat_gateways"]["running"], got["vpcs"]["running"])
|
t.Errorf("nat_gateways{state=running} = %v, vpcs{state=running} = %v, attendu 1 et 2", got["nat_gateways"]["running"], got["vpcs"]["running"])
|
||||||
}
|
}
|
||||||
|
|
@ -117,7 +123,7 @@ func TestCollector_IgnoresVMDiskKeys(t *testing.T) {
|
||||||
// côté Prometheus casse les alertes qui s'en servent.
|
// côté Prometheus casse les alertes qui s'en servent.
|
||||||
func TestCollector_EmitsEveryState(t *testing.T) {
|
func TestCollector_EmitsEveryState(t *testing.T) {
|
||||||
got := collect(t, NewAgentCollector(newTestDB(t)))
|
got := collect(t, NewAgentCollector(newTestDB(t)))
|
||||||
for _, family := range []string{"vpcs", "subnets", "vms", "nat_gateways"} {
|
for _, family := range []string{"vpcs", "subnets", "vms", "nat_gateways", "floating_ips"} {
|
||||||
if len(got[family]) != len(state.All()) {
|
if len(got[family]) != len(state.All()) {
|
||||||
t.Errorf("%s : %d séries, attendu %d", family, len(got[family]), len(state.All()))
|
t.Errorf("%s : %d séries, attendu %d", family, len(got[family]), len(state.All()))
|
||||||
}
|
}
|
||||||
|
|
@ -151,7 +157,7 @@ func TestCollector_Describe(t *testing.T) {
|
||||||
for range ch {
|
for range ch {
|
||||||
n++
|
n++
|
||||||
}
|
}
|
||||||
if n != 4 {
|
if n != 5 {
|
||||||
t.Errorf("%d descripteurs, attendu 4", n)
|
t.Errorf("%d descripteurs, attendu 5", n)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,13 @@
|
||||||
(`br-public` par défaut, jamais `default_interface`), au lieu d'un nom en dur
|
(`br-public` par défaut, jamais `default_interface`), au lieu d'un nom en dur
|
||||||
- Le watchdog signale un bridge public absent, éteint ou sans port VXLAN
|
- Le watchdog signale un bridge public absent, éteint ou sans port VXLAN
|
||||||
|
|
||||||
|
**Interfaces de VM nommées (#88)**
|
||||||
|
|
||||||
|
- Chaque interface d'une VM porte un `name`, **obligatoire** et unique dans la VM, choisi par
|
||||||
|
l'appelant (`nic-a1b2` par exemple) : 400 s'il est absent ou en double. Il est rendu par le GET.
|
||||||
|
C'est l'identifiant stable d'une interface dans l'API (une IP flottante la désigne par ce nom) ;
|
||||||
|
aucune interface Linux n'en tire son nom
|
||||||
|
|
||||||
**Passerelle NAT des VPC (#42)**
|
**Passerelle NAT des VPC (#42)**
|
||||||
|
|
||||||
- Nouvelle ressource `/nat-gateways` (`POST`, `GET`, `DELETE`) : `name`, `vpc`, `ip` (IP de
|
- Nouvelle ressource `/nat-gateways` (`POST`, `GET`, `DELETE`) : `name`, `vpc`, `ip` (IP de
|
||||||
|
|
@ -77,6 +84,24 @@
|
||||||
`syonad_nat_gateways_total` ; le watchdog attend la route par défaut d'une passerelle `running`
|
`syonad_nat_gateways_total` ; le watchdog attend la route par défaut d'une passerelle `running`
|
||||||
dans la VRF
|
dans la VRF
|
||||||
|
|
||||||
|
**IP flottante (#88)**
|
||||||
|
|
||||||
|
- Nouvelle ressource `/floating-ips` (`POST`, `GET`, `DELETE`) : `name`, `vm`, `interface` (nom de
|
||||||
|
l'interface de la VM), `ip` (IP publique avec son préfixe) et `gateway`, fournis par l'appelant
|
||||||
|
et non vérifiés. Créée sur l'hôte de la VM, elle rend l'interface joignable par l'IP publique ;
|
||||||
|
la VM sort par elle, même dans un VPC qui a une passerelle NAT. 409 si la VM n'est pas `running`
|
||||||
|
sur l'hôte, si l'interface est inconnue ou a déjà une IP flottante, ou si l'IP publique est déjà
|
||||||
|
portée
|
||||||
|
- Dans le netns du VPC : `macvlan` `fip-<hex>` sur `br-public`, de MAC `02:05` + IP publique (la
|
||||||
|
même sur tout hôte), IP publique en `/32` ; NAT 1:1 sans état en nftables (table `ip two`),
|
||||||
|
sans `notrack` : métadonnées, DNS et NTP restent servis ; routage par source vers une table
|
||||||
|
propre, le trafic interne au VPC restant dans `main`
|
||||||
|
- Le `GET` d'une VM affiche l'IP flottante de chaque interface (`floating_ip`, en lecture seule) ;
|
||||||
|
la suppression d'une VM est refusée (409) tant qu'une IP flottante y est attachée. Nouvelle
|
||||||
|
métrique `syonad_floating_ips_total`
|
||||||
|
- Déplacer l'IP flottante avec la VM : la supprimer sur l'ancien hôte, la créer sur le nouveau. Les
|
||||||
|
connexions en cours sont perdues
|
||||||
|
|
||||||
## Correctifs
|
## Correctifs
|
||||||
|
|
||||||
- **Chaque VRF de VPC porte une route `unreachable default` (#81).** Une recherche qui échouait
|
- **Chaque VRF de VPC porte une route `unreachable default` (#81).** Une recherche qui échouait
|
||||||
|
|
@ -150,6 +175,9 @@
|
||||||
- **`br-public` du netns (#42) : seuls les VPC créés par la 0.3.0 sont protégés.** L'agent ne
|
- **`br-public` du netns (#42) : seuls les VPC créés par la 0.3.0 sont protégés.** L'agent ne
|
||||||
reprend pas les VPC existants au démarrage : recréer chaque VPC pour fermer l'entrée par le
|
reprend pas les VPC existants au démarrage : recréer chaque VPC pour fermer l'entrée par le
|
||||||
segment public
|
segment public
|
||||||
|
- **`name` est obligatoire sur chaque interface de `POST /vms` (#88)** : un appel qui l'omet reçoit
|
||||||
|
un 400. Au démarrage, l'agent nomme `nic-<rang>` (`nic-0`, `nic-1`…) les interfaces des VM
|
||||||
|
existantes
|
||||||
- **Passerelle NAT (#42) : seuls les VPC créés par la 0.3.0 peuvent s'en servir.** Les VPC
|
- **Passerelle NAT (#42) : seuls les VPC créés par la 0.3.0 peuvent s'en servir.** Les VPC
|
||||||
routés existants n'envoient pas leur route par défaut vers la VRF et leur route-map n'exporte
|
routés existants n'envoient pas leur route par défaut vers la VRF et leur route-map n'exporte
|
||||||
pas celle de la passerelle ; l'agent ne les reprend pas au démarrage. Recréer le VPC avant de
|
pas celle de la passerelle ; l'agent ne les reprend pas au démarrage. Recréer le VPC avant de
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ STATE_DIR=/var/tmp/e2e
|
||||||
SCRATCH_DIR=/tmp
|
SCRATCH_DIR=/tmp
|
||||||
|
|
||||||
owned () {
|
owned () {
|
||||||
grep -E "^(vp-|sn-|ng-)?t${T}"
|
grep -E "^(vp-|sn-|ng-|fip-)?t${T}"
|
||||||
}
|
}
|
||||||
|
|
||||||
api_names () {
|
api_names () {
|
||||||
|
|
@ -39,11 +39,11 @@ clean_scratch () {
|
||||||
}
|
}
|
||||||
|
|
||||||
teardown_owned () {
|
teardown_owned () {
|
||||||
delete_owned vms && delete_owned subnets && delete_owned nat-gateways && delete_owned vpcs || return 1
|
delete_owned floating-ips && delete_owned vms && delete_owned subnets && delete_owned nat-gateways && delete_owned vpcs || return 1
|
||||||
rm -f "${VOLUMES}/t${T}"-*
|
rm -f "${VOLUMES}/t${T}"-*
|
||||||
clean_scratch || return 1
|
clean_scratch || return 1
|
||||||
local left="" kind names
|
local left="" kind names
|
||||||
for kind in vms subnets nat-gateways vpcs; do
|
for kind in floating-ips vms subnets nat-gateways vpcs; do
|
||||||
names=$(api_names "${kind}") || return 1
|
names=$(api_names "${kind}") || return 1
|
||||||
left="${left}$(owned <<<"${names}" | sed "s|^|${kind}/|" | tr '\n' ' ')"
|
left="${left}$(owned <<<"${names}" | sed "s|^|${kind}/|" | tr '\n' ' ')"
|
||||||
done
|
done
|
||||||
|
|
@ -67,7 +67,7 @@ host_state () {
|
||||||
prepare_test () {
|
prepare_test () {
|
||||||
mkdir -p "${STATE_DIR}"
|
mkdir -p "${STATE_DIR}"
|
||||||
curl -fsS -o /dev/null "${API}/vpcs" || echo "ÉCHOUÉ: ${HOST} : API de l'agent injoignable avant le test"
|
curl -fsS -o /dev/null "${API}/vpcs" || echo "ÉCHOUÉ: ${HOST} : API de l'agent injoignable avant le test"
|
||||||
if [ -n "$(for kind in vms subnets nat-gateways vpcs; do api_names "${kind}" | owned; done)$(scratch_netns)$(scratch_files)" ]; then
|
if [ -n "$(for kind in floating-ips vms subnets nat-gateways vpcs; do api_names "${kind}" | owned; done)$(scratch_netns)$(scratch_files)" ]; then
|
||||||
info "${HOST} : ressources, netns ou fichiers t${T} d'un passage précédent, suppression"
|
info "${HOST} : ressources, netns ou fichiers t${T} d'un passage précédent, suppression"
|
||||||
check "${HOST} : nettoyage des ressources t${T} restantes" teardown_owned
|
check "${HOST} : nettoyage des ressources t${T} restantes" teardown_owned
|
||||||
fi
|
fi
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ vm_create () {
|
||||||
api_state "vms/${name}" | grep -qx running && return 0
|
api_state "vms/${name}" | grep -qx running && return 0
|
||||||
[ -n "${key}" ] && meta="{\"sshkey\":\"${key}\"}"
|
[ -n "${key}" ] && meta="{\"sshkey\":\"${key}\"}"
|
||||||
vm_volume "${name}" || return 1
|
vm_volume "${name}" || return 1
|
||||||
api_post vms "{\"name\":\"${name}\",\"memory\":1024,\"cpus\":1,\"metadata\":${meta},\"interfaces\":[{\"subnet\":\"${subnet}\",\"ip\":\"${ip}\",\"mac\":\"$(mac_for_ip "${ip}")\",\"primary\":true}],\"storage\":[{\"path\":\"${VOLUMES}/${name}.qcow2\",\"dev\":\"vda\"}]}" \
|
api_post vms "{\"name\":\"${name}\",\"memory\":1024,\"cpus\":1,\"metadata\":${meta},\"interfaces\":[{\"name\":\"nic-0\",\"subnet\":\"${subnet}\",\"ip\":\"${ip}\",\"mac\":\"$(mac_for_ip "${ip}")\",\"primary\":true}],\"storage\":[{\"path\":\"${VOLUMES}/${name}.qcow2\",\"dev\":\"vda\"}]}" \
|
||||||
&& wait_state "vms/${name}" running
|
&& wait_state "vms/${name}" running
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -109,7 +109,7 @@ ping_rate () {
|
||||||
|
|
||||||
vm_body () {
|
vm_body () {
|
||||||
local name="${1}" subnet="${2}" ip="${3}" disk="${4:-${VOLUMES}/${1}.qcow2}" meta="${5:-{\}}" dev="${6:-vda}"
|
local name="${1}" subnet="${2}" ip="${3}" disk="${4:-${VOLUMES}/${1}.qcow2}" meta="${5:-{\}}" dev="${6:-vda}"
|
||||||
printf '{"name":"%s","memory":1024,"cpus":1,"metadata":%s,"interfaces":[{"subnet":"%s","ip":"%s","mac":"%s","primary":true}],"storage":[{"path":"%s","dev":"%s"}]}' \
|
printf '{"name":"%s","memory":1024,"cpus":1,"metadata":%s,"interfaces":[{"name":"nic-0","subnet":"%s","ip":"%s","mac":"%s","primary":true}],"storage":[{"path":"%s","dev":"%s"}]}' \
|
||||||
"${name}" "${meta}" "${subnet}" "${ip}" "$(mac_for_ip "${ip}")" "${disk}" "${dev}"
|
"${name}" "${meta}" "${subnet}" "${ip}" "$(mac_for_ip "${ip}")" "${disk}" "${dev}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -121,7 +121,7 @@ vm_json () {
|
||||||
--nic)
|
--nic)
|
||||||
sn=${2%%=*}; rest=${2#*=}; ip=${rest%%=*}; mac=$(mac_for_ip "${ip}")
|
sn=${2%%=*}; rest=${2#*=}; ip=${rest%%=*}; mac=$(mac_for_ip "${ip}")
|
||||||
[ "${rest}" != "${ip}" ] && mac=${rest#*=}
|
[ "${rest}" != "${ip}" ] && mac=${rest#*=}
|
||||||
nics=$(jq -c --arg s "${sn}" --arg ip "${ip}" --arg m "${mac}" --argjson p "${primary}" '. + [{subnet: $s, ip: $ip, mac: $m, primary: $p}]' <<<"${nics}"); primary=false; shift 2 ;;
|
nics=$(jq -c --arg s "${sn}" --arg ip "${ip}" --arg m "${mac}" --argjson p "${primary}" '. + [{name: ("nic-" + (length | tostring)), subnet: $s, ip: $ip, mac: $m, primary: $p}]' <<<"${nics}"); primary=false; shift 2 ;;
|
||||||
--dhcp-server) nics=$(jq -c '.[-1].dhcp_server = true' <<<"${nics}"); shift ;;
|
--dhcp-server) nics=$(jq -c '.[-1].dhcp_server = true' <<<"${nics}"); shift ;;
|
||||||
--disk) disks=$(jq -c --arg d "${2%%=*}" --arg p "${2#*=}" '. + [{path: $p, dev: $d}]' <<<"${disks}"); shift 2 ;;
|
--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 ;;
|
--sshkey) meta=$(jq -c --arg v "${2}" '. + {sshkey: $v}' <<<"${meta}"); shift 2 ;;
|
||||||
|
|
|
||||||
|
|
@ -399,19 +399,19 @@ test_matrix_lists_every_test_from_its_header () {
|
||||||
}
|
}
|
||||||
|
|
||||||
test_owned_keeps_only_the_resources_of_the_test () {
|
test_owned_keeps_only_the_resources_of_the_test () {
|
||||||
setup "owned : vp-tNN, sn-tNN, ng-tNN, tNN- seulement"
|
setup "owned : vp-tNN, sn-tNN, ng-tNN, fip-tNN, tNN- seulement"
|
||||||
[[ $(node 'printf "%s\n" vp-t41a sn-t41b ng-t41c t41-vm vp-t42a ng-t42 t4-x vp-s4 sn-s41 ng-s41 | T=41 owned | tr "\n" " "') == "vp-t41a sn-t41b ng-t41c t41-vm " ]] \
|
[[ $(node 'printf "%s\n" vp-t41a sn-t41b ng-t41c fip-t41d t41-vm vp-t42a ng-t42 fip-t42 t4-x vp-s4 sn-s41 ng-s41 fip-s41 | T=41 owned | tr "\n" " "') == "vp-t41a sn-t41b ng-t41c fip-t41d t41-vm " ]] \
|
||||||
|| fail "$(node 'printf "%s\n" vp-t41a sn-t41b ng-t41c t41-vm vp-t42a ng-t42 t4-x vp-s4 sn-s41 ng-s41 | T=41 owned | tr "\n" " "')"
|
|| fail "$(node 'printf "%s\n" vp-t41a sn-t41b ng-t41c fip-t41d t41-vm vp-t42a ng-t42 fip-t42 t4-x vp-s4 sn-s41 ng-s41 fip-s41 | T=41 owned | tr "\n" " "')"
|
||||||
teardown
|
teardown
|
||||||
}
|
}
|
||||||
|
|
||||||
test_teardown_deletes_vms_then_subnets_then_vpcs () {
|
test_teardown_deletes_vms_then_subnets_then_vpcs () {
|
||||||
setup "teardown_owned : VM, puis subnets, puis passerelles NAT, puis VPC, et rien d'autre"
|
setup "teardown_owned : IP flottantes, puis VM, subnets, passerelles NAT, VPC, et rien d'autre"
|
||||||
local stub='api_names () { case $1 in vms) [ -f /tmp/.t70.$$ ] || echo t41-a; echo t42-z ;; subnets) echo sn-t41a ;; nat-gateways) echo ng-t41a; echo ng-t42 ;; vpcs) echo vp-t41a ;; esac; }; api_delete () { echo "DEL $1"; [ "$1" = vms/t41-a ] && touch /tmp/.t70.$$; :; }; wait_state () { :; }; rm () { :; }; scratch_netns () { :; }; scratch_files () { :; }; '
|
local stub='api_names () { case $1 in floating-ips) echo fip-t41a; echo fip-t42 ;; vms) [ -f /tmp/.t70.$$ ] || echo t41-a; echo t42-z ;; subnets) echo sn-t41a ;; nat-gateways) echo ng-t41a; echo ng-t42 ;; vpcs) echo vp-t41a ;; esac; }; api_delete () { echo "DEL $1"; [ "$1" = vms/t41-a ] && touch /tmp/.t70.$$; :; }; wait_state () { :; }; rm () { :; }; scratch_netns () { :; }; scratch_files () { :; }; '
|
||||||
local out
|
local out
|
||||||
out=$(node "T=41; ${stub}teardown_owned; command rm -f /tmp/.t70.\$\$")
|
out=$(node "T=41; ${stub}teardown_owned; command rm -f /tmp/.t70.\$\$")
|
||||||
[[ $(grep '^DEL' <<< "${out}" | tr '\n' ' ') == "DEL vms/t41-a DEL subnets/sn-t41a DEL nat-gateways/ng-t41a DEL vpcs/vp-t41a " ]] || fail "ordre : ${out}"
|
[[ $(grep '^DEL' <<< "${out}" | tr '\n' ' ') == "DEL floating-ips/fip-t41a DEL vms/t41-a DEL subnets/sn-t41a DEL nat-gateways/ng-t41a DEL vpcs/vp-t41a " ]] || fail "ordre : ${out}"
|
||||||
grep -q 'encore présents : subnets/sn-t41a nat-gateways/ng-t41a vpcs/vp-t41a' <<< "${out}" || fail "restes non signalés : ${out}"
|
grep -q 'encore présents : floating-ips/fip-t41a subnets/sn-t41a nat-gateways/ng-t41a vpcs/vp-t41a' <<< "${out}" || fail "restes non signalés : ${out}"
|
||||||
teardown
|
teardown
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -447,7 +447,7 @@ test_api_field_reads_one_field_or_nothing () {
|
||||||
|
|
||||||
test_vm_body_builds_the_create_request () {
|
test_vm_body_builds_the_create_request () {
|
||||||
setup "vm_body : requête de création, disque par défaut, métadonnées et dev"
|
setup "vm_body : requête de création, disque par défaut, métadonnées et dev"
|
||||||
[[ $(node 'vm_body t11-a sn-t11a 10.11.1.10') == '{"name":"t11-a","memory":1024,"cpus":1,"metadata":{},"interfaces":[{"subnet":"sn-t11a","ip":"10.11.1.10","mac":"52:54:00:0b:01:0a","primary":true}],"storage":[{"path":"/var/lib/two/volumes/t11-a.qcow2","dev":"vda"}]}' ]] \
|
[[ $(node 'vm_body t11-a sn-t11a 10.11.1.10') == '{"name":"t11-a","memory":1024,"cpus":1,"metadata":{},"interfaces":[{"name":"nic-0","subnet":"sn-t11a","ip":"10.11.1.10","mac":"52:54:00:0b:01:0a","primary":true}],"storage":[{"path":"/var/lib/two/volumes/t11-a.qcow2","dev":"vda"}]}' ]] \
|
||||||
|| fail "défaut : $(node 'vm_body t11-a sn-t11a 10.11.1.10')"
|
|| fail "défaut : $(node 'vm_body t11-a sn-t11a 10.11.1.10')"
|
||||||
[[ $(node 'vm_body t11-b sn-t11a 10.11.1.11 /x.qcow2 "{\"user_data\":\"YQ==\"}" xvdb | jq -c "[.metadata.user_data, .storage[0].path, .storage[0].dev]"') == '["YQ==","/x.qcow2","xvdb"]' ]] \
|
[[ $(node 'vm_body t11-b sn-t11a 10.11.1.11 /x.qcow2 "{\"user_data\":\"YQ==\"}" xvdb | jq -c "[.metadata.user_data, .storage[0].path, .storage[0].dev]"') == '["YQ==","/x.qcow2","xvdb"]' ]] \
|
||||||
|| fail "options : $(node 'vm_body t11-b sn-t11a 10.11.1.11 /x.qcow2 "{\"user_data\":\"YQ==\"}" xvdb')"
|
|| fail "options : $(node 'vm_body t11-b sn-t11a 10.11.1.11 /x.qcow2 "{\"user_data\":\"YQ==\"}" xvdb')"
|
||||||
|
|
@ -458,10 +458,10 @@ test_vm_json_builds_nics_disks_metadata_and_uefi () {
|
||||||
setup "vm_json : NIC (la première primaire), disques, métadonnées, uefi"
|
setup "vm_json : NIC (la première primaire), disques, métadonnées, uefi"
|
||||||
local out
|
local out
|
||||||
out=$(node 'vm_json t25-m --disk vda=/v/a.qcow2 --nic sn-a=10.0.1.10 --nic sn-b=10.0.2.10 --disk xvda=/v/b.qcow2 --sshkey "ssh-ed25519 AAAA x" --password "\$6\$s\$h" --user-data "I2Nsb3Vk" --uefi')
|
out=$(node 'vm_json t25-m --disk vda=/v/a.qcow2 --nic sn-a=10.0.1.10 --nic sn-b=10.0.2.10 --disk xvda=/v/b.qcow2 --sshkey "ssh-ed25519 AAAA x" --password "\$6\$s\$h" --user-data "I2Nsb3Vk" --uefi')
|
||||||
[[ "${out}" == '{"name":"t25-m","memory":1024,"cpus":1,"metadata":{"sshkey":"ssh-ed25519 AAAA x","password":"$6$s$h","user_data":"I2Nsb3Vk"},"interfaces":[{"subnet":"sn-a","ip":"10.0.1.10","mac":"52:54:00:00:01:0a","primary":true},{"subnet":"sn-b","ip":"10.0.2.10","mac":"52:54:00:00:02:0a","primary":false}],"storage":[{"path":"/v/a.qcow2","dev":"vda"},{"path":"/v/b.qcow2","dev":"xvda"}],"uefi":true}' ]] \
|
[[ "${out}" == '{"name":"t25-m","memory":1024,"cpus":1,"metadata":{"sshkey":"ssh-ed25519 AAAA x","password":"$6$s$h","user_data":"I2Nsb3Vk"},"interfaces":[{"name":"nic-0","subnet":"sn-a","ip":"10.0.1.10","mac":"52:54:00:00:01:0a","primary":true},{"name":"nic-1","subnet":"sn-b","ip":"10.0.2.10","mac":"52:54:00:00:02:0a","primary":false}],"storage":[{"path":"/v/a.qcow2","dev":"vda"},{"path":"/v/b.qcow2","dev":"xvda"}],"uefi":true}' ]] \
|
||||||
|| fail "complet : ${out}"
|
|| fail "complet : ${out}"
|
||||||
out=$(node 'vm_json t21-a --nic sn-a=10.0.1.10')
|
out=$(node 'vm_json t21-a --nic sn-a=10.0.1.10')
|
||||||
[[ "${out}" == '{"name":"t21-a","memory":1024,"cpus":1,"metadata":{},"interfaces":[{"subnet":"sn-a","ip":"10.0.1.10","mac":"52:54:00:00:01:0a","primary":true}],"storage":[],"uefi":false}' ]] || fail "minimal : ${out}"
|
[[ "${out}" == '{"name":"t21-a","memory":1024,"cpus":1,"metadata":{},"interfaces":[{"name":"nic-0","subnet":"sn-a","ip":"10.0.1.10","mac":"52:54:00:00:01:0a","primary":true}],"storage":[],"uefi":false}' ]] || fail "minimal : ${out}"
|
||||||
out=$(node 'vm_json t21-m --nic sn-a=10.0.1.12=02:de:ad:be:ef:01 | jq -c ".interfaces[0] | [.ip, .mac]"')
|
out=$(node 'vm_json t21-m --nic sn-a=10.0.1.12=02:de:ad:be:ef:01 | jq -c ".interfaces[0] | [.ip, .mac]"')
|
||||||
[[ "${out}" == '["10.0.1.12","02:de:ad:be:ef:01"]' ]] || fail "mac forcée : ${out}"
|
[[ "${out}" == '["10.0.1.12","02:de:ad:be:ef:01"]' ]] || fail "mac forcée : ${out}"
|
||||||
out=$(node 'vm_json t76-p --nic sn-a=10.0.1.10 --nic sn-b=10.0.2.10 --dhcp-server | jq -c "[.interfaces[] | .dhcp_server]"')
|
out=$(node 'vm_json t76-p --nic sn-a=10.0.1.10 --nic sn-b=10.0.2.10 --dhcp-server | jq -c "[.interfaces[] | .dhcp_server]"')
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,8 @@ done
|
||||||
check "VM : subnet inconnu, 422" expect_code 422 api_post_code vms "$(vm_body t11-x sn-t11z 10.11.1.12 "${NONE}")"
|
check "VM : 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 : aucune interface primaire, 400" expect_code 400 api_post_code vms "$(vm_body t11-x sn-t11a 10.11.1.12 "${NONE}" | sed 's/"primary":true/"primary":false/')"
|
||||||
check "VM : interface sans mac, 400" expect_code 400 api_post_code vms "$(vm_body t11-x sn-t11a 10.11.1.12 "${NONE}" | jq -c 'del(.interfaces[0].mac)')"
|
check "VM : interface sans mac, 400" expect_code 400 api_post_code vms "$(vm_body t11-x sn-t11a 10.11.1.12 "${NONE}" | jq -c 'del(.interfaces[0].mac)')"
|
||||||
|
check "VM : interface sans nom, 400" expect_code 400 api_post_code vms "$(vm_body t11-x sn-t11a 10.11.1.12 "${NONE}" | jq -c 'del(.interfaces[0].name)')"
|
||||||
|
check "VM : deux interfaces de même nom, 400" expect_code 400 api_post_code vms "$(vm_json t11-x --disk "vda=${NONE}" --nic sn-t11a=10.11.1.12 --nic sn-t11a=10.11.1.13 | jq -c '.interfaces[1].name = "nic-0"')"
|
||||||
for dev in sdb vdb xvdaa; do
|
for dev in sdb vdb xvdaa; do
|
||||||
check "VM : disque additionnel ${dev}, 400" expect_code 400 api_post_code vms "$(vm_body t11-x sn-t11a 10.11.1.12 "${NONE}" | jq -c --arg d "${dev}" '.storage += [{"path":"/x.qcow2","dev":$d}]')"
|
check "VM : disque additionnel ${dev}, 400" expect_code 400 api_post_code vms "$(vm_body t11-x sn-t11a 10.11.1.12 "${NONE}" | jq -c --arg d "${dev}" '.storage += [{"path":"/x.qcow2","dev":$d}]')"
|
||||||
done
|
done
|
||||||
|
|
|
||||||
102
test/e2e/suite/47-ip-flottante.sh
Normal file
102
test/e2e/suite/47-ip-flottante.sh
Normal file
|
|
@ -0,0 +1,102 @@
|
||||||
|
# fonctionnalité: IP flottante d'une interface de VM — macvlan fip-<hex> sur le br-public du netns, MAC 02:05 + IP publique, NAT 1:1 sans état, routage par source (sortie par l'IP flottante même dans un VPC qui a une passerelle NAT), entrée neuve depuis le segment public, métadonnées servies, affichage sur la VM, refus 409, détachement, déplacement vers un autre hôte par mobilité MAC
|
||||||
|
# hôtes: hv1 hv2 hv3
|
||||||
|
# tickets: #88
|
||||||
|
|
||||||
|
on hv1 <<'NODE'
|
||||||
|
KEY=$(vm_key)
|
||||||
|
check "hv1 : VPC vp-t47, l3vni 4700" vpc_create vp-t47 10.47.0.0/16 4700
|
||||||
|
check "hv1 : subnet sn-t47a (VNI 4711)" subnet_create sn-t47a vp-t47 4711 10.47.1.0/24
|
||||||
|
check "hv1 : VM t47-a, interface nic-0" vm_create t47-a sn-t47a 10.47.1.10 "${KEY}"
|
||||||
|
check "hv1 : VM t47-c, sans IP flottante" vm_create t47-c sn-t47a 10.47.1.11 "${KEY}"
|
||||||
|
check "t47-a joignable" vm_wait vp-t47 10.47.1.10
|
||||||
|
check "t47-c joignable" vm_wait vp-t47 10.47.1.11
|
||||||
|
check "hv1 : passerelle NAT ng-t47 du VPC (240.0.0.57)" api_post nat-gateways '{"name":"ng-t47","vpc":"vp-t47","ip":"240.0.0.57/24","gateway":"240.0.0.1"}'
|
||||||
|
check "hv1 : ng-t47 running" wait_state nat-gateways/ng-t47 running
|
||||||
|
check "hv1 : IP flottante sur une interface inconnue refusée (409)" expect_code 409 api_post_code floating-ips '{"name":"fip-t47x","vm":"t47-a","interface":"nic-9","ip":"240.0.0.48/24","gateway":"240.0.0.1"}'
|
||||||
|
check "hv1 : IP flottante fip-t47 (240.0.0.47) sur nic-0 de t47-a" api_post floating-ips '{"name":"fip-t47","vm":"t47-a","interface":"nic-0","ip":"240.0.0.47/24","gateway":"240.0.0.1"}'
|
||||||
|
check "hv1 : fip-t47 running" wait_state floating-ips/fip-t47 running
|
||||||
|
check "hv1 : seconde IP flottante sur nic-0 refusée (409)" expect_code 409 api_post_code floating-ips '{"name":"fip-t47y","vm":"t47-a","interface":"nic-0","ip":"240.0.0.49/24","gateway":"240.0.0.1"}'
|
||||||
|
check "hv1 : GET de t47-a affiche fip-t47 sur nic-0" sh -c "curl -fsS ${API}/vms/t47-a | jq -e '.interfaces[0] | .name == \"nic-0\" and .floating_ip.name == \"fip-t47\" and .floating_ip.ip == \"240.0.0.47/24\"'"
|
||||||
|
check "hv1 : GET de t47-c sans floating_ip" sh -c "curl -fsS ${API}/vms/t47-c | jq -e '.interfaces[0] | has(\"floating_ip\") | not'"
|
||||||
|
check "hv1 : suppression de t47-a refusée tant que fip-t47 est attachée (409)" expect_code 409 api_code DELETE vms/t47-a
|
||||||
|
check "hv1 : macvlan fip-f000002f sur le br-public du netns, MAC 02:05:f0:00:00:2f" sh -c "ip -n vp-t47 -d -o link show fip-f000002f | grep -q 'fip-f000002f@br-public:.*link/ether 02:05:f0:00:00:2f .*macvlan mode bridge'"
|
||||||
|
check "hv1 : entrée FDB locale 02:05:f0:00:00:2f" sh -c "ip netns exec vp-t47 bridge fdb show br br-public | grep -q '^02:05:f0:00:00:2f dev br-public master br-public permanent'"
|
||||||
|
check "hv1 : 240.0.0.47 posée en /32" sh -c "ip -n vp-t47 -4 -o addr show dev fip-f000002f | grep -q ' 240.0.0.47/32 '"
|
||||||
|
check "hv1 : règle 100, trafic vers le VPC dans main" sh -c "ip -n vp-t47 rule show | grep -q '^100:.*from 10.47.1.10 to 10.47.0.0/16 lookup main'"
|
||||||
|
check "hv1 : règle 101 vers la table 170852618" sh -c "ip -n vp-t47 rule show | grep -q '^101:.*from 10.47.1.10 lookup 170852618'"
|
||||||
|
check "hv1 : table 170852618 : segment et route par défaut par fip-f000002f" sh -c "ip -n vp-t47 route show table 170852618 | grep -q '^default via 240.0.0.1 dev fip-f000002f' && ip -n vp-t47 route show table 170852618 | grep -q '^240.0.0.0/24 dev fip-f000002f'"
|
||||||
|
check "hv1 : main ne porte qu'une route du segment, celle de la passerelle" sh -c "[ \"\$(ip -n vp-t47 -4 route show 240.0.0.0/24 | grep -c .)\" = 1 ] && ip -n vp-t47 -4 route show 240.0.0.0/24 | grep -q ' dev ng-t47 '"
|
||||||
|
check "hv1 : traduction à l'entrée et à la sortie dans ip two" sh -c "ip netns exec vp-t47 nft list chain ip two pre-fip-f000002f | grep -q 'ip daddr 240.0.0.47 ip daddr set 10.47.1.10' && ip netns exec vp-t47 nft list chain ip two post-fip-f000002f | grep -q 'ip saddr 10.47.1.10 ip saddr set 240.0.0.47'"
|
||||||
|
S=$(mktemp -d)
|
||||||
|
sniff_start "${S}/fip" vp-t47-e 12 icmp-from 240.0.0.47
|
||||||
|
sniff_start "${S}/ng" vp-t47-e 12 icmp-from 240.0.0.57
|
||||||
|
sniff_start "${S}/private" vp-t47-e 12 icmp-from 10.47.1.10
|
||||||
|
for i in $(seq 1 10); do vm_ssh vp-t47 10.47.1.10 'ping -n -c 1 -W 2 240.255.0.1' >/dev/null 2>&1 && break; sleep 2; done
|
||||||
|
check "t47-a joint 240.255.0.1 derrière les bordures" vm_ssh vp-t47 10.47.1.10 'ping -n -c 3 -W 2 240.255.0.1'
|
||||||
|
wait
|
||||||
|
info "ICMP sortis du netns pendant le ping de t47-a : source 240.0.0.47 $(cat "${S}/fip"), 240.0.0.57 $(cat "${S}/ng"), 10.47.1.10 $(cat "${S}/private")"
|
||||||
|
check "t47-a sort avec son IP flottante 240.0.0.47" test "$(cat "${S}/fip")" -gt 0
|
||||||
|
check "t47-a ne sort pas par la passerelle NAT du VPC" test "$(cat "${S}/ng")" -eq 0
|
||||||
|
check "aucun ICMP de t47-a ne sort avec son adresse privée" test "$(cat "${S}/private")" -eq 0
|
||||||
|
sniff_start "${S}/ng-c" vp-t47-e 12 icmp-from 240.0.0.57
|
||||||
|
vm_ssh vp-t47 10.47.1.11 'ping -n -c 3 -W 2 240.255.0.1' >/dev/null 2>&1
|
||||||
|
wait
|
||||||
|
check "témoin : t47-c, sans IP flottante, sort par la passerelle (240.0.0.57)" test "$(cat "${S}/ng-c")" -gt 0
|
||||||
|
rm -r "${S}"
|
||||||
|
check "t47-a joint t47-c dans le VPC" vm_ssh vp-t47 10.47.1.10 'ping -n -c 2 -W 2 10.47.1.11'
|
||||||
|
check "t47-a reçoit toujours ses métadonnées (instance-id t47-a)" sh -c 'case "$1" in *t47-a*) exit 0 ;; esac; echo "meta-data : $1"; exit 1' _ "$(vm_meta vp-t47 10.47.1.10 meta-data)"
|
||||||
|
NODE
|
||||||
|
|
||||||
|
on hv3 <<'NODE'
|
||||||
|
NS=t47-pub
|
||||||
|
ip netns add "${NS}"
|
||||||
|
ip link add t47-pub-h type veth peer name t47-pub-n netns "${NS}"
|
||||||
|
ip link set t47-pub-h master br-public up
|
||||||
|
ip -n "${NS}" link set t47-pub-n up
|
||||||
|
ip -n "${NS}" addr add 240.0.0.147/24 dev t47-pub-n
|
||||||
|
check "hv3 : témoin, ${NS} joint la gateway des bordures" ip netns exec "${NS}" ping -n -c 3 -W 1 240.0.0.1
|
||||||
|
check "hv3 : 02:05:f0:00:00:2f apprise en type 2 sur le VNI 1 via hv1" wait_evpn_mac 1 02:05:f0:00:00:2f 192.168.14.11
|
||||||
|
check "hv3 : ${NS} joint t47-a par son IP flottante (entrée neuve)" ip netns exec "${NS}" ping -n -c 3 -W 2 240.0.0.47
|
||||||
|
check "hv3 : 240.0.0.47 résolue vers 02:05:f0:00:00:2f" sh -c "ip -n ${NS} neigh show 240.0.0.47 | grep -q 'lladdr 02:05:f0:00:00:2f'"
|
||||||
|
NODE
|
||||||
|
|
||||||
|
on hv1 <<'NODE'
|
||||||
|
check "hv1 : détachement, suppression de fip-t47" api_delete floating-ips/fip-t47
|
||||||
|
check "hv1 : fip-t47 supprimée" wait_state floating-ips/fip-t47 deleted
|
||||||
|
check "hv1 : plus d'interface fip-f000002f" sh -c "! ip -n vp-t47 link show fip-f000002f >/dev/null 2>&1"
|
||||||
|
check "hv1 : plus de règle pour 10.47.1.10" sh -c "! ip -n vp-t47 rule show | grep -q 'from 10.47.1.10'"
|
||||||
|
check "hv1 : table 170852618 vide" test -z "$(ip -n vp-t47 route show table 170852618 2>/dev/null)"
|
||||||
|
check "hv1 : plus d'entrée FDB 02:05:f0:00:00:2f" sh -c "! ip netns exec vp-t47 bridge fdb show br br-public | grep -q '^02:05:f0:00:00:2f '"
|
||||||
|
check "hv1 : plus de chaîne de l'IP flottante dans ip two" sh -c "! ip netns exec vp-t47 nft list table ip two | grep -q 'fip-f000002f'"
|
||||||
|
check "hv1 : GET de t47-a sans floating_ip" sh -c "curl -fsS ${API}/vms/t47-a | jq -e '.interfaces[0] | has(\"floating_ip\") | not'"
|
||||||
|
check "détachée, t47-a sort par la passerelle NAT" vm_ssh vp-t47 10.47.1.10 'ping -n -c 3 -W 2 240.255.0.1'
|
||||||
|
check "hv1 : déplacement, suppression de t47-a" vm_delete t47-a
|
||||||
|
NODE
|
||||||
|
|
||||||
|
on hv3 <<'NODE'
|
||||||
|
check "hv3 : détachée, t47-pub ne joint plus 240.0.0.47" sh -c "! ip netns exec t47-pub ping -n -c 3 -W 1 240.0.0.47 >/dev/null 2>&1"
|
||||||
|
NODE
|
||||||
|
|
||||||
|
on hv2 <<'NODE'
|
||||||
|
KEY=$(vm_key)
|
||||||
|
check "hv2 : VPC vp-t47, l3vni 4700" vpc_create vp-t47 10.47.0.0/16 4700
|
||||||
|
check "hv2 : subnet sn-t47a (VNI 4711)" subnet_create sn-t47a vp-t47 4711 10.47.1.0/24
|
||||||
|
check "hv2 : déplacement, t47-a recréée sur hv2, même adresse" vm_create t47-a sn-t47a 10.47.1.10 "${KEY}"
|
||||||
|
check "t47-a joignable sur hv2" vm_wait vp-t47 10.47.1.10
|
||||||
|
check "hv2 : fip-t47 recréée sur hv2, même IP" api_post floating-ips '{"name":"fip-t47","vm":"t47-a","interface":"nic-0","ip":"240.0.0.47/24","gateway":"240.0.0.1"}'
|
||||||
|
check "hv2 : fip-t47 running" wait_state floating-ips/fip-t47 running
|
||||||
|
check "hv2 : même MAC 02:05:f0:00:00:2f qu'avant le déplacement" sh -c "ip -n vp-t47 -o link show fip-f000002f | grep -q 'link/ether 02:05:f0:00:00:2f '"
|
||||||
|
NODE
|
||||||
|
|
||||||
|
on hv3 <<'NODE'
|
||||||
|
check "hv3 : 02:05:f0:00:00:2f a suivi la VM, apprise via hv2" wait_evpn_mac 1 02:05:f0:00:00:2f 192.168.14.12
|
||||||
|
START=$(date +%s)
|
||||||
|
for i in $(seq 1 30); do ip netns exec t47-pub ping -n -c 1 -W 1 240.0.0.47 >/dev/null 2>&1 && break; sleep 2; done
|
||||||
|
info "hv3 : 240.0.0.47 joignable $(( $(date +%s) - START )) s après l'apprentissage de la MAC via hv2"
|
||||||
|
check "hv3 : t47-pub joint t47-a sur hv2 par son IP flottante" ip netns exec t47-pub ping -n -c 3 -W 2 240.0.0.47
|
||||||
|
NODE
|
||||||
|
|
||||||
|
on hv2 <<'NODE'
|
||||||
|
for i in $(seq 1 15); do vm_ssh vp-t47 10.47.1.10 'ping -n -c 1 -W 2 240.255.0.1' >/dev/null 2>&1 && break; sleep 2; done
|
||||||
|
check "t47-a (hv2) sort par son IP flottante" vm_ssh vp-t47 10.47.1.10 'ping -n -c 3 -W 2 240.255.0.1'
|
||||||
|
NODE
|
||||||
Loading…
Add table
Add a link
Reference in a new issue