f-62: vm: MAC fournie à la création par interface, fin de la table préchargée, migration #62
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
c26606787e
commit
077141443e
20 changed files with 338 additions and 171 deletions
|
|
@ -1,19 +0,0 @@
|
|||
package dhcp
|
||||
|
||||
import (
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
||||
func StoreDHCPEntries(db *badger.DB, subnetName string, entries map[string]string) error {
|
||||
for ip, mac := range entries {
|
||||
if err := kv.AddInDB(db, "subnet/"+subnetName+"/dhcp/"+ip, mac); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetMACForIP(db *badger.DB, subnetName, ip string) (string, error) {
|
||||
return kv.GetFromDB(db, "subnet/"+subnetName+"/dhcp/"+ip)
|
||||
}
|
||||
|
|
@ -17,35 +17,6 @@ func parseNet(t *testing.T, cidr string) *net.IPNet {
|
|||
return network
|
||||
}
|
||||
|
||||
// --- cloneIP ---
|
||||
|
||||
func TestCloneIP_IsIndependent(t *testing.T) {
|
||||
ip := net.ParseIP("10.0.0.1").To4()
|
||||
clone := cloneIP(ip)
|
||||
clone[3] = 99
|
||||
if ip[3] == 99 {
|
||||
t.Error("cloneIP devrait retourner une copie indépendante")
|
||||
}
|
||||
}
|
||||
|
||||
// --- incrementIP ---
|
||||
|
||||
func TestIncrementIP_Simple(t *testing.T) {
|
||||
ip := net.ParseIP("10.0.0.1").To4()
|
||||
incrementIP(ip)
|
||||
if ip.String() != "10.0.0.2" {
|
||||
t.Errorf("attendu 10.0.0.2, obtenu %s", ip)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIncrementIP_Carry(t *testing.T) {
|
||||
ip := net.ParseIP("10.0.0.255").To4()
|
||||
incrementIP(ip)
|
||||
if ip.String() != "10.0.1.0" {
|
||||
t.Errorf("attendu 10.0.1.0, obtenu %s", ip)
|
||||
}
|
||||
}
|
||||
|
||||
// --- GenerateConfig ---
|
||||
|
||||
func newConf(t *testing.T, cidr string) Config {
|
||||
|
|
@ -65,7 +36,7 @@ func newConf(t *testing.T, cidr string) Config {
|
|||
|
||||
func TestGenerateConfig_CreatesFile(t *testing.T) {
|
||||
conf := newConf(t, "192.168.1.0/29") // 6 hôtes
|
||||
path, _, err := GenerateConfig(conf)
|
||||
path, err := GenerateConfig(conf)
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateConfig a échoué : %v", err)
|
||||
}
|
||||
|
|
@ -77,7 +48,7 @@ func TestGenerateConfig_CreatesFile(t *testing.T) {
|
|||
|
||||
func TestGenerateConfig_FilenameMatchesName(t *testing.T) {
|
||||
conf := newConf(t, "192.168.1.0/29")
|
||||
path, _, err := GenerateConfig(conf)
|
||||
path, err := GenerateConfig(conf)
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateConfig a échoué : %v", err)
|
||||
}
|
||||
|
|
@ -90,7 +61,7 @@ func TestGenerateConfig_FilenameMatchesName(t *testing.T) {
|
|||
|
||||
func TestGenerateConfig_ContainsDefaultGateway(t *testing.T) {
|
||||
conf := newConf(t, "192.168.1.0/29")
|
||||
path, _, _ := GenerateConfig(conf)
|
||||
path, _ := GenerateConfig(conf)
|
||||
content, _ := os.ReadFile(path)
|
||||
|
||||
if !strings.Contains(string(content), "dhcp-option=3,192.168.1.1") {
|
||||
|
|
@ -101,7 +72,7 @@ func TestGenerateConfig_ContainsDefaultGateway(t *testing.T) {
|
|||
func TestGenerateConfig_NoDefaultGateway(t *testing.T) {
|
||||
conf := newConf(t, "192.168.1.0/29")
|
||||
conf.DefaultGateway = nil
|
||||
path, _, _ := GenerateConfig(conf)
|
||||
path, _ := GenerateConfig(conf)
|
||||
content, _ := os.ReadFile(path)
|
||||
|
||||
if strings.Contains(string(content), "dhcp-option=3,") {
|
||||
|
|
@ -112,7 +83,7 @@ func TestGenerateConfig_NoDefaultGateway(t *testing.T) {
|
|||
func TestGenerateConfig_NoDefaultGatewaySuppressesRouterOption(t *testing.T) {
|
||||
conf := newConf(t, "192.168.1.0/29")
|
||||
conf.DefaultGateway = nil
|
||||
path, _, _ := GenerateConfig(conf)
|
||||
path, _ := GenerateConfig(conf)
|
||||
content, _ := os.ReadFile(path)
|
||||
|
||||
if !strings.Contains(string(content), "\ndhcp-option=3\n") {
|
||||
|
|
@ -127,7 +98,7 @@ func TestGenerateConfig_NilDefaultGatewayEmitsNoRoute(t *testing.T) {
|
|||
conf := newConf(t, "192.168.1.0/29")
|
||||
conf.DefaultGateway = nil
|
||||
|
||||
path, _, _ := GenerateConfig(conf)
|
||||
path, _ := GenerateConfig(conf)
|
||||
content, _ := os.ReadFile(path)
|
||||
|
||||
if !strings.Contains(string(content), "dhcp-option=121,") {
|
||||
|
|
@ -163,7 +134,7 @@ func TestGenerateConfig_ContainsDhcpRange(t *testing.T) {
|
|||
Name: "vpc1",
|
||||
ConfDir: t.TempDir(),
|
||||
}
|
||||
path, _, _ := GenerateConfig(conf)
|
||||
path, _ := GenerateConfig(conf)
|
||||
content, _ := os.ReadFile(path)
|
||||
|
||||
if !strings.Contains(string(content), "dhcp-range=10.10.0.0,static,255.255.255.0,12h") {
|
||||
|
|
@ -171,18 +142,6 @@ func TestGenerateConfig_ContainsDhcpRange(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestGenerateConfig_OneEntryPerIP(t *testing.T) {
|
||||
// /29 = 8 adresses. Les entrées ne vont plus dans le fichier dnsmasq mais
|
||||
// dans la map retournée, que StoreDHCPEntries écrit en base pour GetMACForIP.
|
||||
_, entries, err := GenerateConfig(newConf(t, "10.0.0.0/29"))
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateConfig : %v", err)
|
||||
}
|
||||
if len(entries) != 8 {
|
||||
t.Errorf("attendu 8 entrées ip->mac, obtenu %d", len(entries))
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateConfig_NoPreGeneratedHosts(t *testing.T) {
|
||||
// Une entrée dhcp-host pré-générée fait rejeter celle du dhcp-hostsdir
|
||||
// (« duplicate dhcp-host IP address »), sans erreur : la VM reçoit alors
|
||||
|
|
@ -212,18 +171,6 @@ func TestGenerateConfig_PointsToDirs(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestGenerateConfig_MACPrefix(t *testing.T) {
|
||||
_, entries, err := GenerateConfig(newConf(t, "10.0.0.0/30"))
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateConfig : %v", err)
|
||||
}
|
||||
for ip, mac := range entries {
|
||||
if !strings.HasPrefix(mac, "00:22:33:") {
|
||||
t.Errorf("mac de %s sans le préfixe 00:22:33: : %s", ip, mac)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateConfig_CreatesConfDir(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "sous", "dossier")
|
||||
_, network, _ := net.ParseCIDR("10.0.0.0/30")
|
||||
|
|
@ -233,7 +180,7 @@ func TestGenerateConfig_CreatesConfDir(t *testing.T) {
|
|||
Name: "net",
|
||||
ConfDir: dir,
|
||||
}
|
||||
if _, _, err := GenerateConfig(conf); err != nil {
|
||||
if _, err := GenerateConfig(conf); err != nil {
|
||||
t.Fatalf("GenerateConfig devrait créer les répertoires manquants : %v", err)
|
||||
}
|
||||
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
||||
|
|
@ -245,7 +192,7 @@ func TestGenerateConfig_CreatesConfDir(t *testing.T) {
|
|||
|
||||
func confLines(t *testing.T, c Config) string {
|
||||
t.Helper()
|
||||
path, _, err := GenerateConfig(c)
|
||||
path, err := GenerateConfig(c)
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateConfig : %v", err)
|
||||
}
|
||||
|
|
@ -312,7 +259,7 @@ func TestGenerateConfig_MissingInterfaceIPIsAnError(t *testing.T) {
|
|||
conf := newConf(t, "192.168.1.0/29")
|
||||
conf.InterfaceIP = nil
|
||||
|
||||
if _, _, err := GenerateConfig(conf); err == nil {
|
||||
if _, err := GenerateConfig(conf); err == nil {
|
||||
t.Error("sans interface_ip aucune route metadata n'est possible : il faut échouer, pas écrire une conf muette")
|
||||
}
|
||||
}
|
||||
|
|
@ -321,7 +268,7 @@ func TestGenerateConfig_ListensOnTheAnycastGateway(t *testing.T) {
|
|||
conf := newConf(t, "192.168.1.0/29")
|
||||
conf.Listen = "gw-s9r1"
|
||||
conf.Except = "br-s9r1"
|
||||
path, _, _ := GenerateConfig(conf)
|
||||
path, _ := GenerateConfig(conf)
|
||||
content, _ := os.ReadFile(path)
|
||||
|
||||
for _, line := range []string{"\ninterface=gw-s9r1\n", "\nexcept-interface=br-s9r1\n"} {
|
||||
|
|
@ -333,7 +280,7 @@ func TestGenerateConfig_ListensOnTheAnycastGateway(t *testing.T) {
|
|||
|
||||
func TestGenerateConfig_NoInterfaceLinesByDefault(t *testing.T) {
|
||||
conf := newConf(t, "192.168.1.0/29")
|
||||
path, _, _ := GenerateConfig(conf)
|
||||
path, _ := GenerateConfig(conf)
|
||||
content, _ := os.ReadFile(path)
|
||||
|
||||
if strings.Contains(string(content), "interface=") {
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ package dhcp
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
|
@ -10,9 +9,9 @@ import (
|
|||
"git.g3e.fr/syonad/two/internal/metadata"
|
||||
)
|
||||
|
||||
func GenerateConfig(c Config) (string, map[string]string, error) {
|
||||
func GenerateConfig(c Config) (string, error) {
|
||||
if c.InterfaceIP == nil {
|
||||
return "", nil, fmt.Errorf("interface ip is required: guests would have no route to the metadata server")
|
||||
return "", fmt.Errorf("interface ip is required: guests would have no route to the metadata server")
|
||||
}
|
||||
mask := fmt.Sprintf("%d.%d.%d.%d", c.Network.Mask[0], c.Network.Mask[1], c.Network.Mask[2], c.Network.Mask[3])
|
||||
|
||||
|
|
@ -35,26 +34,14 @@ func GenerateConfig(c Config) (string, map[string]string, error) {
|
|||
fmt.Fprintf(&sb, "dhcp-hostsdir=%s\n", HostsDir(c.ConfDir, c.Name))
|
||||
fmt.Fprintf(&sb, "dhcp-optsdir=%s\n", OptsDir(c.ConfDir, c.Name))
|
||||
|
||||
entries := Entries(c.Network)
|
||||
|
||||
for _, dir := range []string{c.ConfDir, HostsDir(c.ConfDir, c.Name), OptsDir(c.ConfDir, c.Name)} {
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return "", nil, fmt.Errorf("create %s: %w", dir, err)
|
||||
return "", fmt.Errorf("create %s: %w", dir, err)
|
||||
}
|
||||
}
|
||||
|
||||
outPath := filepath.Join(c.ConfDir, c.Name+".conf")
|
||||
return outPath, entries, os.WriteFile(outPath, []byte(sb.String()), 0644)
|
||||
}
|
||||
|
||||
func Entries(network *net.IPNet) map[string]string {
|
||||
entries := make(map[string]string)
|
||||
i := 0
|
||||
for ip := cloneIP(network.IP); network.Contains(ip); incrementIP(ip) {
|
||||
entries[ip.String()] = fmt.Sprintf("00:22:33:%02X:%02X:%02X", (i>>16)&0xFF, (i>>8)&0xFF, i&0xFF)
|
||||
i++
|
||||
}
|
||||
return entries
|
||||
return outPath, os.WriteFile(outPath, []byte(sb.String()), 0644)
|
||||
}
|
||||
|
||||
func classlessRoutes(c Config) []string {
|
||||
|
|
@ -69,18 +56,3 @@ func classlessRoutes(c Config) []string {
|
|||
}
|
||||
return routes
|
||||
}
|
||||
|
||||
func incrementIP(ip net.IP) {
|
||||
for j := len(ip) - 1; j >= 0; j-- {
|
||||
ip[j]++
|
||||
if ip[j] != 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func cloneIP(ip net.IP) net.IP {
|
||||
clone := make(net.IP, len(ip))
|
||||
copy(clone, ip)
|
||||
return clone
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue