From fac5f9c7cd1ae0fea54db7f7c5acf97d1b95aaaa Mon Sep 17 00:00:00 2001 From: "Felix Wischke (65278)" Date: Sun, 31 Mar 2024 22:46:35 +0200 Subject: [PATCH] cloudinit: fix multiple vrf device templating --- pkg/cloudinit/network.go | 8 +-- pkg/cloudinit/network_test.go | 130 ++++++++++++++++++++++++---------- 2 files changed, 96 insertions(+), 42 deletions(-) diff --git a/pkg/cloudinit/network.go b/pkg/cloudinit/network.go index 27e941d9..0f72cb22 100644 --- a/pkg/cloudinit/network.go +++ b/pkg/cloudinit/network.go @@ -41,17 +41,17 @@ const ( {{- end }} {{- if and $element.IPV6Address (not $element.DHCP6)}} - '{{ $element.IPV6Address }}' - {{- end }} + {{- end }} {{- if or (and $element.Gateway (not $element.DHCP4)) (and $element.Gateway6 (not $element.DHCP6)) }} routes: {{- if and $element.Gateway (not $element.DHCP4) }} - to: 0.0.0.0/0 via: {{ $element.Gateway }} - {{- end }} + {{- end }} {{- if and $element.Gateway6 (not $element.DHCP6) }} - to: '::/0' via: '{{ $element.Gateway6 }}' - {{- end }} + {{- end }} {{- end }} {{- end }} {{- if $element.DNSServers }} @@ -68,7 +68,7 @@ const ( {{- if eq $element.Type "vrf" }} {{- if eq $vrf 0 }} vrfs: - {{- $vrf := 1 }} + {{- $vrf = 1 }} {{- end }} {{$element.Name}}: table: {{ $element.Table }} diff --git a/pkg/cloudinit/network_test.go b/pkg/cloudinit/network_test.go index 8a54bffa..f6eb9d11 100644 --- a/pkg/cloudinit/network_test.go +++ b/pkg/cloudinit/network_test.go @@ -237,17 +237,7 @@ const ( - eth0 - eth1` - expectedValidNetworkConfigValidFIBRule = `network: - version: 2 - renderer: networkd - ethernets: - vrfs: - vrf-blue: - table: 500 - routing-policy: - - { "from": "10.10.0.0/16", }` - - expectedValidNetworkNotGateway = `network: + expectedValidNetworkConfigMultipleNicsMultipleVRF = `network: version: 2 renderer: networkd ethernets: @@ -272,10 +262,39 @@ const ( dhcp6: false addresses: - 196.168.100.124/24 + routes: + - to: 0.0.0.0/0 + via: 196.168.100.254 nameservers: addresses: - '8.8.8.8' - - '8.8.4.4'` + - '8.8.4.4' + vrfs: + vrf-blue: + table: 500 + routes: + - { "to": "default", "via": "192.168.178.1", "metric": 100, "table": 100, } + - { "to": "10.10.10.0/24", "via": "192.168.178.254", "metric": 100, } + routing-policy: + - { "to": "0.0.0.0/0", "from": "192.168.178.1/24", "priority": 999, "table": 100, } + interfaces: + - eth0 + vrf-red: + table: 501 + routing-policy: + - { "to": "0.0.0.0/0", "from": "192.168.100.0/24", "priority": 999, "table": 101, } + interfaces: + - eth1` + + expectedValidNetworkConfigValidFIBRule = `network: + version: 2 + renderer: networkd + ethernets: + vrfs: + vrf-blue: + table: 500 + routing-policy: + - { "from": "10.10.0.0/16", }` ) func TestNetworkConfig_Render(t *testing.T) { @@ -625,6 +644,67 @@ func TestNetworkConfig_Render(t *testing.T) { err: nil, }, }, + "ValidNetworkConfigMultipleNicsMultipleVRF": { + reason: "valid config multiple nics enslaved to multiple VRFs", + args: args{ + nics: []NetworkConfigData{ + { + Type: "ethernet", + Name: "eth0", + MacAddress: "92:60:a0:5b:22:c2", + IPAddress: "10.10.10.12/24", + Gateway: "10.10.10.1", + DNSServers: []string{"8.8.8.8", "8.8.4.4"}, + }, + { + Type: "ethernet", + Name: "eth1", + MacAddress: "b4:87:18:bf:a3:60", + IPAddress: "196.168.100.124/24", + Gateway: "196.168.100.254", + DNSServers: []string{"8.8.8.8", "8.8.4.4"}, + }, + { + Type: "vrf", + Name: "vrf-blue", + Table: 500, + Interfaces: []string{"eth0"}, + Routes: []RoutingData{{ + To: "default", + Via: "192.168.178.1", + Metric: 100, + Table: 100, + }, { + To: "10.10.10.0/24", + Via: "192.168.178.254", + Metric: 100, + }}, + FIBRules: []FIBRuleData{{ + To: "0.0.0.0/0", + From: "192.168.178.1/24", + Priority: 999, + Table: 100, + }}, + }, + { + Type: "vrf", + Name: "vrf-red", + Table: 501, + Interfaces: []string{"eth1"}, + FIBRules: []FIBRuleData{{ + To: "0.0.0.0/0", + From: "192.168.100.0/24", + Priority: 999, + Table: 101, + }}, + }, + }, + }, + want: want{ + network: expectedValidNetworkConfigMultipleNicsMultipleVRF, + err: nil, + }, + }, "ValidNetworkConfigValidFIBRule": { reason: "valid config valid routing policy", args: args{ @@ -664,32 +744,6 @@ func TestNetworkConfig_Render(t *testing.T) { err: ErrMalformedRoute, }, }, - "AdditionalNicNoGateway": { - reason: "missing route is okay", - args: args{ - nics: []NetworkConfigData{ - { - Type: "ethernet", - Name: "eth0", - MacAddress: "92:60:a0:5b:22:c2", - IPAddress: "10.10.10.12/24", - Gateway: "10.10.10.1", - DNSServers: []string{"8.8.8.8", "8.8.4.4"}, - }, - { - Type: "ethernet", - Name: "eth1", - MacAddress: "b4:87:18:bf:a3:60", - IPAddress: "196.168.100.124/24", - DNSServers: []string{"8.8.8.8", "8.8.4.4"}, - }, - }, - }, - want: want{ - network: expectedValidNetworkNotGateway, - err: nil, - }, - }, } for n, tc := range cases {