Skip to content

Commit d832a3a

Browse files
committed
Properly surfaces errors when attempting to parse a subnet-mask-suffixed
address from NetworkInfo.
1 parent 2158c26 commit d832a3a

File tree

4 files changed

+34
-26
lines changed

4 files changed

+34
-26
lines changed

apiserver/common/networkingcommon/types.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -187,25 +187,17 @@ func NetworkInterfacesToStateArgs(ifaces corenetwork.InterfaceInfos) (
187187
devicesArgs = append(devicesArgs, args)
188188
}
189189

190-
if iface.CIDR == "" || iface.PrimaryAddress().Value == "" {
191-
logger.Tracef(
192-
"skipping empty CIDR %q and/or Address %q of %q",
193-
iface.CIDR, iface.PrimaryAddress(), iface.InterfaceName,
194-
)
195-
continue
196-
}
197-
_, ipNet, err := net.ParseCIDR(iface.CIDR)
190+
cidrAddress, err := iface.CIDRAddress()
198191
if err != nil {
199-
logger.Warningf("FIXME: ignoring unexpected CIDR format %q: %v", iface.CIDR, err)
192+
logger.Warningf("ignoring unexpected address/CIDR format: %q/%q, %v",
193+
iface.PrimaryAddress(), iface.CIDR, err)
200194
continue
201195
}
202-
ipAddr := net.ParseIP(iface.PrimaryAddress().Value)
203-
if ipAddr == nil {
204-
logger.Warningf("FIXME: ignoring unexpected Address format %q", iface.PrimaryAddress().Value)
196+
if cidrAddress == "" {
197+
logger.Tracef("skipping empty CIDR %q and/or Address %q of %q",
198+
iface.CIDR, iface.PrimaryAddress(), iface.InterfaceName)
205199
continue
206200
}
207-
ipNet.IP = ipAddr
208-
cidrAddress := ipNet.String()
209201

210202
var derivedConfigMethod corenetwork.AddressConfigMethod
211203
switch method := corenetwork.AddressConfigMethod(iface.ConfigType); method {

cloudconfig/cloudinit/network_ubuntu.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ func GenerateENITemplate(interfaces corenetwork.InterfaceInfos) (string, error)
3434
}
3535
logger.Debugf("generating /e/n/i template from %#v", interfaces)
3636

37-
prepared := PrepareNetworkConfigFromInterfaces(interfaces)
37+
prepared, err := PrepareNetworkConfigFromInterfaces(interfaces)
38+
if err != nil {
39+
return "", errors.Trace(err)
40+
}
3841

3942
var output bytes.Buffer
4043
gateway4Handled := false
@@ -147,7 +150,11 @@ func GenerateNetplan(interfaces corenetwork.InterfaceInfos) (string, error) {
147150
netPlan.Network.Version = 2
148151
for _, info := range interfaces {
149152
var iface netplan.Ethernet
150-
if cidr := info.CIDRAddress(); cidr != "" {
153+
cidr, err := info.CIDRAddress()
154+
if err != nil {
155+
return "", errors.Trace(err)
156+
}
157+
if cidr != "" {
151158
iface.Addresses = append(iface.Addresses, cidr)
152159
} else if info.ConfigType == corenetwork.ConfigDHCP {
153160
t := true
@@ -210,7 +217,7 @@ type PreparedConfig struct {
210217
// PrepareNetworkConfigFromInterfaces collects the necessary information to
211218
// render a persistent network config from the given slice of
212219
// network.InterfaceInfo. The result always includes the loopback interface.
213-
func PrepareNetworkConfigFromInterfaces(interfaces corenetwork.InterfaceInfos) *PreparedConfig {
220+
func PrepareNetworkConfigFromInterfaces(interfaces corenetwork.InterfaceInfos) (*PreparedConfig, error) {
214221
dnsServers := set.NewStrings()
215222
dnsSearchDomains := set.NewStrings()
216223
gateway4Address := ""
@@ -247,7 +254,11 @@ func PrepareNetworkConfigFromInterfaces(interfaces corenetwork.InterfaceInfos) *
247254
autoStarted.Add(ifaceName)
248255
}
249256

250-
if cidr := info.CIDRAddress(); cidr != "" {
257+
cidr, err := info.CIDRAddress()
258+
if err != nil {
259+
return nil, errors.Trace(err)
260+
}
261+
if cidr != "" {
251262
nameToAddress[ifaceName] = cidr
252263
} else if info.ConfigType == corenetwork.ConfigDHCP {
253264
nameToAddress[ifaceName] = string(corenetwork.ConfigDHCP)
@@ -290,7 +301,7 @@ func PrepareNetworkConfigFromInterfaces(interfaces corenetwork.InterfaceInfos) *
290301
}
291302

292303
logger.Debugf("prepared network config for rendering: %+v", prepared)
293-
return prepared
304+
return prepared, nil
294305
}
295306

296307
// AddNetworkConfig adds configuration scripts for specified interfaces

core/network/address.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ func (a MachineAddress) String() string {
139139
return prefix + a.Value
140140
}
141141

142+
// IP returns the net.IP representation of this address.
143+
func (a MachineAddress) IP() net.IP {
144+
return net.ParseIP(a.Value)
145+
}
146+
142147
// sortOrder calculates the "weight" of the address when sorting:
143148
// - public IPs first;
144149
// - hostnames after that, but "localhost" will be last if present;

core/network/nic.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ type InterfaceInfo struct {
162162
DNSServers ProviderAddresses
163163

164164
// MTU is the Maximum Transmission Unit controlling the maximum size of the
165-
// protocol packats that the interface can pass through. It is only used
165+
// protocol packets that the interface can pass through. It is only used
166166
// when > 0.
167167
MTU int
168168

@@ -211,21 +211,21 @@ func (i *InterfaceInfo) IsVLAN() bool {
211211
}
212212

213213
// CIDRAddress returns Address.Value combined with CIDR mask.
214-
func (i *InterfaceInfo) CIDRAddress() string {
214+
func (i *InterfaceInfo) CIDRAddress() (string, error) {
215215
primaryAddr := i.PrimaryAddress()
216216
if i.CIDR == "" || primaryAddr.Value == "" {
217-
return ""
217+
return "", nil
218218
}
219219
_, ipNet, err := net.ParseCIDR(i.CIDR)
220220
if err != nil {
221-
return errors.Trace(err).Error()
221+
return "", errors.Trace(err)
222222
}
223-
ip := net.ParseIP(primaryAddr.Value)
223+
ip := primaryAddr.IP()
224224
if ip == nil {
225-
return errors.Errorf("cannot parse IP address %q", primaryAddr.Value).Error()
225+
return "", errors.Errorf("cannot parse IP address %q", primaryAddr.Value)
226226
}
227227
ipNet.IP = ip
228-
return ipNet.String()
228+
return ipNet.String(), nil
229229
}
230230

231231
// PrimaryAddress returns the primary address for the interface.

0 commit comments

Comments
 (0)