Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cloudinit: fix multiple vrf device templating #159

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pkg/cloudinit/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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 }}
Expand Down
130 changes: 92 additions & 38 deletions pkg/cloudinit/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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) {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down
Loading