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:
parent
6c35ebdff4
commit
12d69798fb
9 changed files with 116 additions and 4 deletions
32
cmd/dhcp/listen_test.go
Normal file
32
cmd/dhcp/listen_test.go
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestListenInterface(t *testing.T) {
|
||||||
|
present := func(names ...string) func(string) bool {
|
||||||
|
return func(name string) bool {
|
||||||
|
for _, n := range names {
|
||||||
|
if n == name {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cases := []struct {
|
||||||
|
label string
|
||||||
|
bridge string
|
||||||
|
exists func(string) bool
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{"gateway anycast présente", "br-s9r1", present("br-s9r1", "gw-s9r1"), "gw-s9r1"},
|
||||||
|
{"ancien modèle, pas de gateway", "br-s9r1", present("br-s9r1"), "br-s9r1"},
|
||||||
|
{"gateway d'un autre subnet", "br-s9r1", present("br-s9r1", "gw-s9r2"), "br-s9r1"},
|
||||||
|
{"bridge sans préfixe br-", "vpc1br0", present("gw-vpc1br0"), "vpc1br0"},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
if got := listenInterface(c.bridge, c.exists); got != c.want {
|
||||||
|
t.Errorf("%s : %s, %s attendu", c.label, got, c.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
dhcpapi "git.g3e.fr/syonad/two/internal/api/dhcp"
|
dhcpapi "git.g3e.fr/syonad/two/internal/api/dhcp"
|
||||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||||
|
|
@ -66,13 +67,30 @@ func run() error {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
conn, err := server4.NewIPv4UDPConn(*iface, &net.UDPAddr{Port: dhcpv4.ServerPort})
|
listen := listenInterface(*iface, interfaceExists)
|
||||||
|
conn, err := server4.NewIPv4UDPConn(listen, &net.UDPAddr{Port: dhcpv4.ServerPort})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("bind udp/%d on %s: %w", dhcpv4.ServerPort, *iface, err)
|
return fmt.Errorf("bind udp/%d on %s: %w", dhcpv4.ServerPort, listen, err)
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
log.Info("dhcp server started", "state", store.Path(), "socket", control.Addr())
|
log.Info("dhcp server started", "listen", listen, "state", store.Path(), "socket", control.Addr())
|
||||||
|
|
||||||
return store.Serve(conn, log)
|
return store.Serve(conn, log)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func interfaceExists(name string) bool {
|
||||||
|
_, err := net.InterfaceByName(name)
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func listenInterface(bridge string, exists func(string) bool) string {
|
||||||
|
id, ok := strings.CutPrefix(bridge, "br-")
|
||||||
|
if !ok {
|
||||||
|
return bridge
|
||||||
|
}
|
||||||
|
if gateway := "gw-" + id; exists(gateway) {
|
||||||
|
return gateway
|
||||||
|
}
|
||||||
|
return bridge
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -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")
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,12 @@ func GenerateConfig(c Config) (string, map[string]string, error) {
|
||||||
|
|
||||||
var sb strings.Builder
|
var sb strings.Builder
|
||||||
fmt.Fprintf(&sb, "no-resolv\n")
|
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-range=%s,static,%s,12h\n", c.Network.IP.String(), mask)
|
||||||
fmt.Fprintf(&sb, "dhcp-option=121,%s\n", strings.Join(classlessRoutes(c), ","))
|
fmt.Fprintf(&sb, "dhcp-option=121,%s\n", strings.Join(classlessRoutes(c), ","))
|
||||||
if c.DefaultGateway != nil {
|
if c.DefaultGateway != nil {
|
||||||
|
|
|
||||||
|
|
@ -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
|
DefaultGateway net.IP // if non-nil, default route via option 3 and 0.0.0.0/0 in option 121
|
||||||
Name string
|
Name string
|
||||||
ConfDir string
|
ConfDir string
|
||||||
|
Listen string
|
||||||
|
Except string
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ type Subnet struct {
|
||||||
Name string
|
Name string
|
||||||
VPC string
|
VPC string
|
||||||
Bridge string
|
Bridge string
|
||||||
|
Listen string
|
||||||
Network *net.IPNet
|
Network *net.IPNet
|
||||||
InterfaceIP net.IP
|
InterfaceIP net.IP
|
||||||
VPCRoute *net.IPNet
|
VPCRoute *net.IPNet
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ func (Dnsmasq) Unit(s Subnet) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b Dnsmasq) config(s Subnet) dhcp.Config {
|
func (b Dnsmasq) config(s Subnet) dhcp.Config {
|
||||||
return dhcp.Config{
|
c := dhcp.Config{
|
||||||
Network: s.Network,
|
Network: s.Network,
|
||||||
Name: s.Instance(),
|
Name: s.Instance(),
|
||||||
ConfDir: b.confDir(),
|
ConfDir: b.confDir(),
|
||||||
|
|
@ -36,6 +36,11 @@ func (b Dnsmasq) config(s Subnet) dhcp.Config {
|
||||||
VPCRoute: s.VPCRoute,
|
VPCRoute: s.VPCRoute,
|
||||||
DefaultGateway: s.DefaultGateway,
|
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 {
|
func (b Dnsmasq) ConfigureSubnet(s Subnet) error {
|
||||||
|
|
|
||||||
|
|
@ -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")
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -175,10 +175,15 @@ func startDHCP(db *badger.DB, subnetName string, d subnetData, backend dhcpbacke
|
||||||
return fmt.Errorf("store dhcp entries: %w", err)
|
return fmt.Errorf("store dhcp entries: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
listen := ""
|
||||||
|
if d.anycast() {
|
||||||
|
listen = d.gatewayLink()
|
||||||
|
}
|
||||||
return backend.ConfigureSubnet(dhcpbackend.Subnet{
|
return backend.ConfigureSubnet(dhcpbackend.Subnet{
|
||||||
Name: subnetName,
|
Name: subnetName,
|
||||||
VPC: d.vpc,
|
VPC: d.vpc,
|
||||||
Bridge: d.bridge,
|
Bridge: d.bridge,
|
||||||
|
Listen: listen,
|
||||||
Network: d.cidr,
|
Network: d.cidr,
|
||||||
InterfaceIP: d.interfaceIP,
|
InterfaceIP: d.interfaceIP,
|
||||||
VPCRoute: vpcRoute,
|
VPCRoute: vpcRoute,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue