f-28: refactor dhcp config to use VPCRoute and DefaultGateway
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
c0caf1a24c
commit
1b56a42627
4 changed files with 61 additions and 24 deletions
|
|
@ -51,12 +51,15 @@ func TestIncrementIP_Carry(t *testing.T) {
|
|||
func newConf(t *testing.T, cidr string) Config {
|
||||
t.Helper()
|
||||
_, network, _ := net.ParseCIDR(cidr)
|
||||
_, vpcNet, _ := net.ParseCIDR("10.0.0.0/16")
|
||||
gw := net.ParseIP("192.168.1.1").To4()
|
||||
return Config{
|
||||
Network: network,
|
||||
Gateway: net.ParseIP("192.168.1.1").To4(),
|
||||
DefaultRoute: true,
|
||||
Name: "test",
|
||||
ConfDir: t.TempDir(),
|
||||
Network: network,
|
||||
VPCGateway: gw,
|
||||
VPCRoute: vpcNet,
|
||||
DefaultGateway: gw,
|
||||
Name: "test",
|
||||
ConfDir: t.TempDir(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -85,24 +88,45 @@ func TestGenerateConfig_FilenameMatchesName(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestGenerateConfig_ContainsGateway(t *testing.T) {
|
||||
func TestGenerateConfig_ContainsDefaultGateway(t *testing.T) {
|
||||
conf := newConf(t, "192.168.1.0/29")
|
||||
path, _, _ := GenerateConfig(conf)
|
||||
content, _ := os.ReadFile(path)
|
||||
|
||||
if !strings.Contains(string(content), "dhcp-option=3,192.168.1.1") {
|
||||
t.Errorf("gateway absente du fichier généré :\n%s", content)
|
||||
t.Errorf("dhcp-option=3 absente du fichier généré :\n%s", content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateConfig_NoDefaultRoute(t *testing.T) {
|
||||
func TestGenerateConfig_NoDefaultGateway(t *testing.T) {
|
||||
conf := newConf(t, "192.168.1.0/29")
|
||||
conf.DefaultRoute = false
|
||||
conf.DefaultGateway = nil
|
||||
path, _, _ := GenerateConfig(conf)
|
||||
content, _ := os.ReadFile(path)
|
||||
|
||||
if strings.Contains(string(content), "dhcp-option=3,") {
|
||||
t.Errorf("dhcp-option=3 présente alors que DefaultRoute=false :\n%s", content)
|
||||
t.Errorf("dhcp-option=3 présente alors que DefaultGateway=nil :\n%s", content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateConfig_ContainsVPCRoute(t *testing.T) {
|
||||
conf := newConf(t, "192.168.1.0/29")
|
||||
path, _, _ := GenerateConfig(conf)
|
||||
content, _ := os.ReadFile(path)
|
||||
|
||||
if !strings.Contains(string(content), "dhcp-option=121,10.0.0.0/16,192.168.1.1") {
|
||||
t.Errorf("dhcp-option=121 absente ou incorrecte :\n%s", content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateConfig_NoVPCRoute(t *testing.T) {
|
||||
conf := newConf(t, "192.168.1.0/29")
|
||||
conf.VPCRoute = nil
|
||||
path, _, _ := GenerateConfig(conf)
|
||||
content, _ := os.ReadFile(path)
|
||||
|
||||
if strings.Contains(string(content), "dhcp-option=121,") {
|
||||
t.Errorf("dhcp-option=121 présente alors que VPCRoute=nil :\n%s", content)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -110,7 +134,6 @@ func TestGenerateConfig_ContainsDhcpRange(t *testing.T) {
|
|||
_, network, _ := net.ParseCIDR("10.10.0.0/24")
|
||||
conf := Config{
|
||||
Network: network,
|
||||
Gateway: net.ParseIP("10.10.0.1").To4(),
|
||||
Name: "vpc1",
|
||||
ConfDir: t.TempDir(),
|
||||
}
|
||||
|
|
@ -156,7 +179,6 @@ func TestGenerateConfig_CreatesConfDir(t *testing.T) {
|
|||
_, network, _ := net.ParseCIDR("10.0.0.0/30")
|
||||
conf := Config{
|
||||
Network: network,
|
||||
Gateway: net.ParseIP("10.0.0.1").To4(),
|
||||
Name: "net",
|
||||
ConfDir: dir,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,8 +14,11 @@ func GenerateConfig(c Config) (string, map[string]string, error) {
|
|||
var sb strings.Builder
|
||||
fmt.Fprintf(&sb, "no-resolv\n")
|
||||
fmt.Fprintf(&sb, "dhcp-range=%s,static,%s,12h\n", c.Network.IP.String(), mask)
|
||||
if c.DefaultRoute {
|
||||
fmt.Fprintf(&sb, "dhcp-option=3,%s\n", c.Gateway.String())
|
||||
if c.VPCRoute != nil {
|
||||
fmt.Fprintf(&sb, "dhcp-option=121,%s,%s\n", c.VPCRoute.String(), c.VPCGateway.String())
|
||||
}
|
||||
if c.DefaultGateway != nil {
|
||||
fmt.Fprintf(&sb, "dhcp-option=3,%s\n", c.DefaultGateway.String())
|
||||
}
|
||||
fmt.Fprintf(&sb, "dhcp-option=6,1.1.1.1,8.8.8.8\n\n")
|
||||
|
||||
|
|
|
|||
|
|
@ -5,9 +5,10 @@ import (
|
|||
)
|
||||
|
||||
type Config struct {
|
||||
Network *net.IPNet
|
||||
Gateway net.IP
|
||||
DefaultRoute bool
|
||||
Name string
|
||||
ConfDir string
|
||||
Network *net.IPNet
|
||||
VPCGateway net.IP // next-hop for VPCRoute (option 121)
|
||||
VPCRoute *net.IPNet // if non-nil, emit dhcp-option=121,VPCRoute,VPCGateway
|
||||
DefaultGateway net.IP // if non-nil, emit dhcp-option=3,DefaultGateway
|
||||
Name string
|
||||
ConfDir string
|
||||
}
|
||||
|
|
|
|||
|
|
@ -136,11 +136,22 @@ func setupVxlanHost(d subnetData, vethE string) error {
|
|||
|
||||
func startDHCP(db *badger.DB, subnetName string, d subnetData) error {
|
||||
conf := dhcp.Config{
|
||||
Network: d.cidr,
|
||||
Gateway: d.interfaceIP,
|
||||
DefaultRoute: d.mode == "vxlan",
|
||||
Name: d.vpc + "_" + d.bridge,
|
||||
ConfDir: "/etc/dnsmasq.d",
|
||||
Network: d.cidr,
|
||||
Name: d.vpc + "_" + d.bridge,
|
||||
ConfDir: "/etc/dnsmasq.d",
|
||||
}
|
||||
switch d.mode {
|
||||
case "vxlan":
|
||||
conf.VPCGateway = d.interfaceIP
|
||||
conf.VPCRoute = d.vpcCIDR
|
||||
case "bridge":
|
||||
if d.defaultRoute {
|
||||
gw, err := netif.GetDefaultGateway()
|
||||
if err != nil {
|
||||
return fmt.Errorf("get default gateway: %w", err)
|
||||
}
|
||||
conf.DefaultGateway = gw
|
||||
}
|
||||
}
|
||||
_, entries, err := dhcp.GenerateConfig(conf)
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue