From 779e8afa9162853ecbe6a8f8868956815a814974 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ciraci?= Date: Thu, 7 Mar 2024 15:06:44 +0100 Subject: [PATCH 1/3] feat(gateway-optional): This commit makes gw definition optional for additionalNetworkDevices There is no need to have a mandatory gateway for secondary network interfaces, this commit edits the validation and go template that is in charge of generating the cloudinit files. --- pkg/cloudinit/network.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/cloudinit/network.go b/pkg/cloudinit/network.go index 01914052..092e424b 100644 --- a/pkg/cloudinit/network.go +++ b/pkg/cloudinit/network.go @@ -42,6 +42,7 @@ const ( {{- if and $element.IPV6Address (not $element.DHCP6)}} - '{{ $element.IPV6Address }}' {{- 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 @@ -52,6 +53,7 @@ const ( via: '{{ $element.Gateway6 }}' {{- end }} {{- end }} + {{- end }} {{- if $element.DNSServers }} nameservers: addresses: @@ -91,7 +93,6 @@ const ( {{- end }} {{- end -}} {{- define "routes" }} - routes: {{- range $index, $route := .Routes }} - { {{- if $route.To }} "to": "{{$route.To}}", {{ end -}} @@ -131,7 +132,7 @@ func (r *NetworkConfig) validate() error { if len(r.data.NetworkConfigData) == 0 { return ErrMissingNetworkConfigData } - for _, d := range r.data.NetworkConfigData { + for i, d := range r.data.NetworkConfigData { // TODO: refactor this when network configuration is unified if d.Type != "ethernet" { err := validRoutes(d.Routes) @@ -158,7 +159,7 @@ func (r *NetworkConfig) validate() error { if err != nil { return err } - if d.Gateway == "" { + if d.Gateway == "" && i == 0 { return ErrMissingGateway } } @@ -168,7 +169,7 @@ func (r *NetworkConfig) validate() error { if err6 != nil { return err6 } - if d.Gateway6 == "" { + if d.Gateway6 == "" && i == 0 { return ErrMissingGateway } } From 0cac29699dd750563259d6401d2d191aec40c461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ciraci?= Date: Thu, 7 Mar 2024 15:19:39 +0100 Subject: [PATCH 2/3] fix(tests): Fix regression in test --- pkg/cloudinit/network.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/cloudinit/network.go b/pkg/cloudinit/network.go index 092e424b..27e941d9 100644 --- a/pkg/cloudinit/network.go +++ b/pkg/cloudinit/network.go @@ -93,6 +93,7 @@ const ( {{- end }} {{- end -}} {{- define "routes" }} + routes: {{- range $index, $route := .Routes }} - { {{- if $route.To }} "to": "{{$route.To}}", {{ end -}} From fce5554eb9489fe1e088b727a7379bea1848d34e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ciraci?= Date: Thu, 7 Mar 2024 15:24:02 +0100 Subject: [PATCH 3/3] test: Add testing for noGW feature --- pkg/cloudinit/network_test.go | 56 +++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/pkg/cloudinit/network_test.go b/pkg/cloudinit/network_test.go index e391a171..8a54bffa 100644 --- a/pkg/cloudinit/network_test.go +++ b/pkg/cloudinit/network_test.go @@ -246,6 +246,36 @@ const ( table: 500 routing-policy: - { "from": "10.10.0.0/16", }` + + expectedValidNetworkNotGateway = `network: + version: 2 + renderer: networkd + ethernets: + eth0: + match: + macaddress: 92:60:a0:5b:22:c2 + dhcp4: false + dhcp6: false + addresses: + - 10.10.10.12/24 + routes: + - to: 0.0.0.0/0 + via: 10.10.10.1 + nameservers: + addresses: + - '8.8.8.8' + - '8.8.4.4' + eth1: + match: + macaddress: b4:87:18:bf:a3:60 + dhcp4: false + dhcp6: false + addresses: + - 196.168.100.124/24 + nameservers: + addresses: + - '8.8.8.8' + - '8.8.4.4'` ) func TestNetworkConfig_Render(t *testing.T) { @@ -634,6 +664,32 @@ 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 {