Skip to content

Commit

Permalink
feat(gateway-optional): This commit makes gw definition optional for …
Browse files Browse the repository at this point in the history
…additionalNetworkDevices (#130)

* 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.

* fix(tests): Fix regression in test

* test: Add testing for noGW feature

---------

Co-authored-by: Mohamed Chiheb Ben Jemaa <[email protected]>
  • Loading branch information
CiraciNicolo and mcbenjemaa authored Mar 14, 2024
1 parent 9ab4021 commit a745a6a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
8 changes: 5 additions & 3 deletions pkg/cloudinit/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -52,6 +53,7 @@ const (
via: '{{ $element.Gateway6 }}'
{{- end }}
{{- end }}
{{- end }}
{{- if $element.DNSServers }}
nameservers:
addresses:
Expand Down Expand Up @@ -131,7 +133,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)
Expand All @@ -158,7 +160,7 @@ func (r *NetworkConfig) validate() error {
if err != nil {
return err
}
if d.Gateway == "" {
if d.Gateway == "" && i == 0 {
return ErrMissingGateway
}
}
Expand All @@ -168,7 +170,7 @@ func (r *NetworkConfig) validate() error {
if err6 != nil {
return err6
}
if d.Gateway6 == "" {
if d.Gateway6 == "" && i == 0 {
return ErrMissingGateway
}
}
Expand Down
56 changes: 56 additions & 0 deletions pkg/cloudinit/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit a745a6a

Please sign in to comment.