mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 21:17:23 +00:00
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:
parent
0c25199117
commit
65342a0000
@ -101,7 +101,7 @@ func hexCIDR(cidr string) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
ip = ip.Mask(ipnet.Mask)
|
ip = ip.Mask(ipnet.Mask)
|
||||||
hexIP := hex.EncodeToString([]byte(ip.To4()))
|
hexIP := hex.EncodeToString([]byte(ip))
|
||||||
hexMask := ipnet.Mask.String()
|
hexMask := ipnet.Mask.String()
|
||||||
return hexIP + "/" + hexMask, nil
|
return hexIP + "/" + hexMask, nil
|
||||||
}
|
}
|
||||||
@ -119,6 +119,9 @@ func asciiCIDR(cidr string) (string, error) {
|
|||||||
ip := net.IP(ipData)
|
ip := net.IP(ipData)
|
||||||
|
|
||||||
maskData, err := hex.DecodeString(parts[1])
|
maskData, err := hex.DecodeString(parts[1])
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
mask := net.IPMask(maskData)
|
mask := net.IPMask(maskData)
|
||||||
size, _ := mask.Size()
|
size, _ := mask.Size()
|
||||||
|
|
||||||
|
@ -94,19 +94,33 @@ func TestNextClassID(t *testing.T) {
|
|||||||
|
|
||||||
func TestHexCIDR(t *testing.T) {
|
func TestHexCIDR(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
|
name string
|
||||||
input string
|
input string
|
||||||
output string
|
output string
|
||||||
expectErr bool
|
expectErr bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
input: "1.2.0.0/16",
|
name: "IPv4 masked",
|
||||||
|
input: "1.2.3.4/16",
|
||||||
output: "01020000/ffff0000",
|
output: "01020000/ffff0000",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "IPv4 host",
|
||||||
input: "172.17.0.2/32",
|
input: "172.17.0.2/32",
|
||||||
output: "ac110002/ffffffff",
|
output: "ac110002/ffffffff",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
name: "IPv6 masked",
|
||||||
|
input: "2001:dead:beef::cafe/64",
|
||||||
|
output: "2001deadbeef00000000000000000000/ffffffffffffffff0000000000000000",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "IPv6 host",
|
||||||
|
input: "2001::5/128",
|
||||||
|
output: "20010000000000000000000000000005/ffffffffffffffffffffffffffffffff",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid CIDR",
|
||||||
input: "foo",
|
input: "foo",
|
||||||
expectErr: true,
|
expectErr: true,
|
||||||
},
|
},
|
||||||
@ -115,21 +129,76 @@ func TestHexCIDR(t *testing.T) {
|
|||||||
output, err := hexCIDR(test.input)
|
output, err := hexCIDR(test.input)
|
||||||
if test.expectErr {
|
if test.expectErr {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("unexpected non-error")
|
t.Errorf("case %s: unexpected non-error", test.name)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("case %s: unexpected error: %v", test.name, err)
|
||||||
}
|
}
|
||||||
if output != test.output {
|
if output != test.output {
|
||||||
t.Errorf("expected: %s, saw: %s", test.output, output)
|
t.Errorf("case %s: expected: %s, saw: %s",
|
||||||
|
test.name, test.output, output)
|
||||||
}
|
}
|
||||||
input, err := asciiCIDR(output)
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAsciiCIDR(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
output string
|
||||||
|
expectErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "IPv4",
|
||||||
|
input: "01020000/ffff0000",
|
||||||
|
output: "1.2.0.0/16",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "IPv4 host",
|
||||||
|
input: "ac110002/ffffffff",
|
||||||
|
output: "172.17.0.2/32",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "IPv6",
|
||||||
|
input: "2001deadbeef00000000000000000000/ffffffffffffffff0000000000000000",
|
||||||
|
output: "2001:dead:beef::/64",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "IPv6 host",
|
||||||
|
input: "20010000000000000000000000000005/ffffffffffffffffffffffffffffffff",
|
||||||
|
output: "2001::5/128",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid CIDR",
|
||||||
|
input: "malformed",
|
||||||
|
expectErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "non-hex IP",
|
||||||
|
input: "nonhex/32",
|
||||||
|
expectErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "non-hex mask",
|
||||||
|
input: "01020000/badmask",
|
||||||
|
expectErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
output, err := asciiCIDR(test.input)
|
||||||
|
if test.expectErr {
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("case %s: unexpected non-error", test.name)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("case %s: unexpected error: %v", test.name, err)
|
||||||
}
|
}
|
||||||
if input != test.input {
|
if output != test.output {
|
||||||
t.Errorf("expected: %s, saw: %s", test.input, input)
|
t.Errorf("case %s: expected: %s, saw: %s",
|
||||||
|
test.name, test.output, output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user