Skip to content

Commit 7f60d19

Browse files
author
externalreality
committed
Fix errors with network get cidrs.
1 parent 11201fb commit 7f60d19

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

apiserver/common/firewall/egressaddresswatcher.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ func (w *EgressAddressWatcher) loop() error {
142142
}
143143
changed = false
144144
if !setEquals(addresses, lastAddresses) {
145-
addressesCIDR = network.FormatAsCIDR(addresses.Values())
145+
addressesCIDR, err = network.FormatAsCIDR(addresses.Values())
146+
if err != nil {
147+
return errors.Trace(err)
148+
}
146149
ready = ready || sentInitial
147150
}
148151
}

apiserver/facades/agent/uniter/uniter.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2114,7 +2114,10 @@ func (u *UniterAPI) NetworkInfo(args params.NetworkInfoParams) (params.NetworkIn
21142114
// default to the first ingress address. This matches the behaviour when
21152115
// there's a relation in place.
21162116
if len(info.EgressSubnets) == 0 && len(info.IngressAddresses) > 0 {
2117-
info.EgressSubnets = network.FormatAsCIDR([]string{info.IngressAddresses[0]})
2117+
info.EgressSubnets, err = network.FormatAsCIDR([]string{info.IngressAddresses[0]})
2118+
if err != nil {
2119+
return result, errors.Trace(err)
2120+
}
21182121
}
21192122

21202123
result.Results[binding] = info

network/network.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -610,20 +610,23 @@ func SubnetInAnyRange(cidrs []*net.IPNet, subnet *net.IPNet) bool {
610610

611611
// FormatAsCIDR converts the specified IP addresses to
612612
// a slice of CIDRs.
613-
func FormatAsCIDR(addresses []string) []string {
613+
func FormatAsCIDR(addresses []string) ([]string, error) {
614614
result := make([]string, len(addresses))
615615
for i, a := range addresses {
616616
cidr := a
617617
// If address is not already a cidr, add a /32 (ipv4) or /128 (ipv6).
618618
if _, _, err := net.ParseCIDR(a); err != nil {
619-
ip := net.ParseIP(a)
620-
if ip.To4() != nil {
621-
cidr = a + "/32"
619+
address, err := net.ResolveIPAddr("ip", a)
620+
if err != nil {
621+
return nil, errors.Trace(err)
622+
}
623+
if address.IP.To4() != nil {
624+
cidr = address.String() + "/32"
622625
} else {
623-
cidr = a + "/128"
626+
cidr = address.String() + "/128"
624627
}
625628
}
626629
result[i] = cidr
627630
}
628-
return result
631+
return result, nil
629632
}

state/relationunit.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,10 @@ func NetworksForRelation(
586586

587587
// If no egress subnets defined, We default to the ingress address.
588588
if len(egress) == 0 && len(ingress) > 0 {
589-
egress = network.FormatAsCIDR([]string{ingress[0]})
589+
egress, err = network.FormatAsCIDR([]string{ingress[0]})
590+
if err != nil {
591+
return "", nil, nil, errors.Trace(err)
592+
}
590593
}
591594
return boundSpace, ingress, egress, nil
592595
}

0 commit comments

Comments
 (0)