f-57: dhcp: écoute sur le macvlan de la gateway anycast, dnsmasq et serveur intégré #57

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-13 14:51:40 +02:00
commit 12d69798fb
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
9 changed files with 116 additions and 4 deletions

View file

@ -316,3 +316,27 @@ func TestGenerateConfig_MissingInterfaceIPIsAnError(t *testing.T) {
t.Error("sans interface_ip aucune route metadata n'est possible : il faut échouer, pas écrire une conf muette")
}
}
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)
content, _ := os.ReadFile(path)
for _, line := range []string{"\ninterface=gw-s9r1\n", "\nexcept-interface=br-s9r1\n"} {
if !strings.Contains(string(content), line) {
t.Errorf("%q absente du fichier généré :\n%s", strings.TrimSpace(line), content)
}
}
}
func TestGenerateConfig_NoInterfaceLinesByDefault(t *testing.T) {
conf := newConf(t, "192.168.1.0/29")
path, _, _ := GenerateConfig(conf)
content, _ := os.ReadFile(path)
if strings.Contains(string(content), "interface=") {
t.Errorf("aucune directive d'interface attendue sans Listen :\n%s", content)
}
}

View file

@ -18,6 +18,12 @@ func GenerateConfig(c Config) (string, map[string]string, error) {
var sb strings.Builder
fmt.Fprintf(&sb, "no-resolv\n")
if c.Listen != "" {
fmt.Fprintf(&sb, "interface=%s\n", c.Listen)
}
if c.Except != "" {
fmt.Fprintf(&sb, "except-interface=%s\n", c.Except)
}
fmt.Fprintf(&sb, "dhcp-range=%s,static,%s,12h\n", c.Network.IP.String(), mask)
fmt.Fprintf(&sb, "dhcp-option=121,%s\n", strings.Join(classlessRoutes(c), ","))
if c.DefaultGateway != nil {

View file

@ -13,4 +13,6 @@ type Config struct {
DefaultGateway net.IP // if non-nil, default route via option 3 and 0.0.0.0/0 in option 121
Name string
ConfDir string
Listen string
Except string
}

View file

@ -11,6 +11,7 @@ type Subnet struct {
Name string
VPC string
Bridge string
Listen string
Network *net.IPNet
InterfaceIP net.IP
VPCRoute *net.IPNet

View file

@ -28,7 +28,7 @@ func (Dnsmasq) Unit(s Subnet) string {
}
func (b Dnsmasq) config(s Subnet) dhcp.Config {
return dhcp.Config{
c := dhcp.Config{
Network: s.Network,
Name: s.Instance(),
ConfDir: b.confDir(),
@ -36,6 +36,11 @@ func (b Dnsmasq) config(s Subnet) dhcp.Config {
VPCRoute: s.VPCRoute,
DefaultGateway: s.DefaultGateway,
}
if s.Listen != "" && s.Listen != s.Bridge {
c.Listen = s.Listen
c.Except = s.Bridge
}
return c
}
func (b Dnsmasq) ConfigureSubnet(s Subnet) error {

View file

@ -111,3 +111,22 @@ func TestDnsmasqSetVM_RejectsAVMWithoutReservation(t *testing.T) {
t.Fatal("a vm with no reservation would get no address: that must be reported")
}
}
func TestDnsmasqConfig_ListenOnGatewayExcludesTheBridge(t *testing.T) {
c := Dnsmasq{}.config(Subnet{VPC: "vp-s9", Bridge: "br-s9r1", Listen: "gw-s9r1"})
if c.Listen != "gw-s9r1" || c.Except != "br-s9r1" {
t.Errorf("Listen %q, Except %q : gw-s9r1 et br-s9r1 attendus", c.Listen, c.Except)
}
if c.Name != "vp-s9_br-s9r1" {
t.Errorf("instance %q : le nom doit rester celui du bridge", c.Name)
}
}
func TestDnsmasqConfig_NoListenKeepsTheCommandLineInterface(t *testing.T) {
for _, listen := range []string{"", "br-s9r1"} {
c := Dnsmasq{}.config(Subnet{VPC: "vp-s9", Bridge: "br-s9r1", Listen: listen})
if c.Listen != "" || c.Except != "" {
t.Errorf("Listen=%q : Listen %q, Except %q, vides attendus", listen, c.Listen, c.Except)
}
}
}

View file

@ -175,10 +175,15 @@ func startDHCP(db *badger.DB, subnetName string, d subnetData, backend dhcpbacke
return fmt.Errorf("store dhcp entries: %w", err)
}
listen := ""
if d.anycast() {
listen = d.gatewayLink()
}
return backend.ConfigureSubnet(dhcpbackend.Subnet{
Name: subnetName,
VPC: d.vpc,
Bridge: d.bridge,
Listen: listen,
Network: d.cidr,
InterfaceIP: d.interfaceIP,
VPCRoute: vpcRoute,