Skip to content

Commit

Permalink
Ensures that InterfaceInfo.CIDRAddress returns a not-found error when
Browse files Browse the repository at this point in the history
either address or CIDR is empty.

This specific error is handled in cloud-init generation where DHCP is
configured for the device if encountered.
  • Loading branch information
manadart committed May 15, 2020
1 parent dc52117 commit d28e9a4
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
8 changes: 1 addition & 7 deletions apiserver/common/networkingcommon/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,7 @@ func NetworkInterfacesToStateArgs(ifaces corenetwork.InterfaceInfos) (

cidrAddress, err := iface.CIDRAddress()
if err != nil {
logger.Warningf("ignoring unexpected address/CIDR format: %q/%q, %v",
iface.PrimaryAddress(), iface.CIDR, err)
continue
}
if cidrAddress == "" {
logger.Tracef("skipping empty CIDR %q and/or Address %q of %q",
iface.CIDR, iface.PrimaryAddress(), iface.InterfaceName)
logger.Warningf("ignoring address for device %q: %v", iface.InterfaceName, err)
continue
}

Expand Down
4 changes: 2 additions & 2 deletions cloudconfig/cloudinit/network_ubuntu.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func GenerateNetplan(interfaces corenetwork.InterfaceInfos) (string, error) {
for _, info := range interfaces {
var iface netplan.Ethernet
cidr, err := info.CIDRAddress()
if err != nil {
if err != nil && !errors.IsNotFound(err) {
return "", errors.Trace(err)
}
if cidr != "" {
Expand Down Expand Up @@ -255,7 +255,7 @@ func PrepareNetworkConfigFromInterfaces(interfaces corenetwork.InterfaceInfos) (
}

cidr, err := info.CIDRAddress()
if err != nil {
if err != nil && !errors.IsNotFound(err) {
return nil, errors.Trace(err)
}
if cidr != "" {
Expand Down
6 changes: 6 additions & 0 deletions cloudconfig/cloudinit/network_ubuntu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,12 @@ func (s *NetworkUbuntuSuite) TestPrepareNetworkConfigFromInterfacesBadCIDRError(
c.Assert(err, gc.ErrorMatches, `invalid CIDR address: invalid`)
}

func (s *NetworkUbuntuSuite) TestGenerateNetplanBadAddressError(c *gc.C) {
s.fakeInterfaces[0].Addresses[0].Value = "invalid"
_, err := cloudinit.PrepareNetworkConfigFromInterfaces(s.fakeInterfaces)
c.Assert(err, gc.ErrorMatches, `cannot parse IP address "invalid"`)
}

func (s *NetworkUbuntuSuite) runENIScriptWithAllPythons(c *gc.C, ipCommand, input, expectedOutput string, wait, retries int) {
for _, python := range s.pythonVersions {
c.Logf("test using %s", python)
Expand Down
6 changes: 5 additions & 1 deletion core/network/nic.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,17 +213,21 @@ func (i *InterfaceInfo) IsVLAN() bool {
// CIDRAddress returns Address.Value combined with CIDR mask.
func (i *InterfaceInfo) CIDRAddress() (string, error) {
primaryAddr := i.PrimaryAddress()

if i.CIDR == "" || primaryAddr.Value == "" {
return "", nil
return "", errors.NotFoundf("address and CIDR pair (%q, %q)", primaryAddr.Value, i.CIDR)
}

_, ipNet, err := net.ParseCIDR(i.CIDR)
if err != nil {
return "", errors.Trace(err)
}

ip := primaryAddr.IP()
if ip == nil {
return "", errors.Errorf("cannot parse IP address %q", primaryAddr.Value)
}

ipNet.IP = ip
return ipNet.String(), nil
}
Expand Down
5 changes: 3 additions & 2 deletions core/network/nic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package network_test

import (
"github.com/juju/errors"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"

Expand Down Expand Up @@ -79,14 +80,14 @@ func (*nicSuite) TestCIDRAddress(c *gc.C) {
Addresses: network.NewProviderAddresses("10.0.0.10"),
}
addr, err = dev.CIDRAddress()
c.Assert(err, jc.ErrorIsNil)
c.Assert(err, jc.Satisfies, errors.IsNotFound)
c.Check(addr, gc.Equals, "")

dev = network.InterfaceInfo{
CIDR: "10.0.0.0/24",
}
addr, err = dev.CIDRAddress()
c.Assert(err, jc.ErrorIsNil)
c.Assert(err, jc.Satisfies, errors.IsNotFound)
c.Check(addr, gc.Equals, "")

dev = network.InterfaceInfo{
Expand Down

0 comments on commit d28e9a4

Please sign in to comment.