Add Static Route Backend

This commit is contained in:
Samuel Lorch 2023-04-08 17:07:26 +02:00
parent dec123e9ee
commit 3197f291fa
4 changed files with 40 additions and 6 deletions

View file

@ -1,5 +1,6 @@
package definitions package definitions
type Network struct { 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"`
} }

View file

@ -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"`
}

View file

@ -16,7 +16,8 @@ type NetworkdConfigFile struct {
type InterfaceWithName struct { type InterfaceWithName struct {
Name string Name string
definitions.Interface definitions.Interface
Vlans []string Vlans []string
StaticRoutes []definitions.StaticRoute
} }
type BondMembership struct { type BondMembership struct {
@ -128,6 +129,7 @@ func GenerateNetworkdConfiguration(conf definitions.Config) ([]NetworkdConfigFil
// Step 6 Generate addressing network files // Step 6 Generate addressing network files
for name, inter := range conf.Network.Interfaces { for name, inter := range conf.Network.Interfaces {
// Vlans
vlans := []string{} vlans := []string{}
if inter.Type != definitions.Vlan { if inter.Type != definitions.Vlan {
vlans := []string{} vlans := []string{}
@ -141,11 +143,20 @@ func GenerateNetworkdConfiguration(conf definitions.Config) ([]NetworkdConfigFil
slog.Info("Vlans on interface", "interface", name, "count", len(vlans)) 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) buf := new(bytes.Buffer)
err := templates.ExecuteTemplate(buf, "config-addressing.network.tmpl", InterfaceWithName{ err := templates.ExecuteTemplate(buf, "config-addressing.network.tmpl", InterfaceWithName{
Name: name, Name: name,
Interface: inter, Interface: inter,
Vlans: vlans, Vlans: vlans,
StaticRoutes: staticRoutes,
}) })
if err != nil { if err != nil {
return nil, fmt.Errorf("executing config-addressing.network.tmpl template: %w", err) return nil, fmt.Errorf("executing config-addressing.network.tmpl template: %w", err)

View file

@ -14,4 +14,13 @@ DHCP=yes
{{- end }} {{- end }}
{{- range .Vlans }} {{- range .Vlans }}
VLAN={{ . }} VLAN={{ . }}
{{- end}} {{- end}}
{{- range .StaticRoutes }}
[Route]
Destination={{ Destination }}
Gateway={{ Gateway }}
{{- if ne .Metric 0 }}
Metric={{ Metric }}
{{- end }}
{{end}}