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

feat(gateway-optional): This commit makes gw definition optional for additionalNetworkDevices #130

Merged
merged 6 commits into from
Mar 14, 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: 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