IPv6 support for hexCIDR()

Includes these changes:
- Modified so that IPv6 CIDRs can be converted correctly.
- Added test cases for IPv6 addresses.
- Split UTs for hexCIDR() and asciiCIDR() so that masking can be tested.
- Add UTs for failure cases.

Note: Some code that calls hexCIDR() builds a CIDR from the pod IP string
and the concatenation of "/32". These should, in the future, use "128",
if/when the pod IP is IPv6. Not addressed as part of this commit.
This commit is contained in:
Paul Michali
2017-05-16 20:53:06 +00:00
parent 0c25199117
commit 65342a0000
2 changed files with 81 additions and 9 deletions

View File

@@ -101,7 +101,7 @@ func hexCIDR(cidr string) (string, error) {
return "", err
}
ip = ip.Mask(ipnet.Mask)
hexIP := hex.EncodeToString([]byte(ip.To4()))
hexIP := hex.EncodeToString([]byte(ip))
hexMask := ipnet.Mask.String()
return hexIP + "/" + hexMask, nil
}
@@ -119,6 +119,9 @@ func asciiCIDR(cidr string) (string, error) {
ip := net.IP(ipData)
maskData, err := hex.DecodeString(parts[1])
if err != nil {
return "", err
}
mask := net.IPMask(maskData)
size, _ := mask.Size()