diff --git a/internal/definitions/network.go b/internal/definitions/network.go index d42d594..c3840ce 100644 --- a/internal/definitions/network.go +++ b/internal/definitions/network.go @@ -1,5 +1,6 @@ package definitions type Network struct { - Interfaces map[string]Interface `json:"interfaces" validate:"required,dive"` + Interfaces map[string]Interface `json:"interfaces" validate:"required,dive"` + StaticRoutes []StaticRoute `json:"static_routes" validate:"required,dive"` } diff --git a/internal/definitions/static_route.go b/internal/definitions/static_route.go new file mode 100644 index 0000000..c5b6b52 --- /dev/null +++ b/internal/definitions/static_route.go @@ -0,0 +1,13 @@ +package definitions + +import ( + "net/netip" +) + +type StaticRoute struct { + Name string `json:"name,omitempty"` + Interface string `json:"interface,omitempty"` + Gateway netip.Addr `json:"gateway,omitempty"` + Destination IPNet `json:"destination,omitempty"` + Metric uint `json:"metric,omitempty"` +} diff --git a/internal/networkd/configuration.go b/internal/networkd/configuration.go index a32a74f..128b59e 100644 --- a/internal/networkd/configuration.go +++ b/internal/networkd/configuration.go @@ -16,7 +16,8 @@ type NetworkdConfigFile struct { type InterfaceWithName struct { Name string definitions.Interface - Vlans []string + Vlans []string + StaticRoutes []definitions.StaticRoute } type BondMembership struct { @@ -128,6 +129,7 @@ func GenerateNetworkdConfiguration(conf definitions.Config) ([]NetworkdConfigFil // Step 6 Generate addressing network files for name, inter := range conf.Network.Interfaces { + // Vlans vlans := []string{} if inter.Type != definitions.Vlan { vlans := []string{} @@ -141,11 +143,20 @@ func GenerateNetworkdConfiguration(conf definitions.Config) ([]NetworkdConfigFil slog.Info("Vlans on interface", "interface", name, "count", len(vlans)) } + // Static Routes + staticRoutes := []definitions.StaticRoute{} + for _, route := range conf.Network.StaticRoutes { + if route.Interface == name { + staticRoutes = append(staticRoutes, route) + } + } + buf := new(bytes.Buffer) err := templates.ExecuteTemplate(buf, "config-addressing.network.tmpl", InterfaceWithName{ - Name: name, - Interface: inter, - Vlans: vlans, + Name: name, + Interface: inter, + Vlans: vlans, + StaticRoutes: staticRoutes, }) if err != nil { return nil, fmt.Errorf("executing config-addressing.network.tmpl template: %w", err) diff --git a/internal/networkd/template/config-addressing.network.tmpl b/internal/networkd/template/config-addressing.network.tmpl index f010605..1633e4c 100644 --- a/internal/networkd/template/config-addressing.network.tmpl +++ b/internal/networkd/template/config-addressing.network.tmpl @@ -14,4 +14,13 @@ DHCP=yes {{- end }} {{- range .Vlans }} VLAN={{ . }} -{{- end}} \ No newline at end of file +{{- end}} + +{{- range .StaticRoutes }} +[Route] +Destination={{ Destination }} +Gateway={{ Gateway }} +{{- if ne .Metric 0 }} +Metric={{ Metric }} +{{- end }} +{{end}} \ No newline at end of file