-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support IPv6 IP Pods during AZ detection (#2375)
- Loading branch information
Showing
9 changed files
with
780 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package networking | ||
|
||
import "inet.af/netaddr" | ||
|
||
// TODO: replace netaddr package with built-in netip package once golang 1.18 released: https://pkg.go.dev/net/netip@master#Prefix | ||
|
||
// ParseCIDRs will parse CIDRs in string format into parsed IPPrefix | ||
func ParseCIDRs(cidrs []string) ([]netaddr.IPPrefix, error) { | ||
var ipPrefixes []netaddr.IPPrefix | ||
for _, cidr := range cidrs { | ||
ipPrefix, err := netaddr.ParseIPPrefix(cidr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ipPrefixes = append(ipPrefixes, ipPrefix) | ||
} | ||
return ipPrefixes, nil | ||
} | ||
|
||
// IsIPWithinCIDRs checks whether specific IP is in IPv4 CIDR or IPv6 CIDRs. | ||
func IsIPWithinCIDRs(ip netaddr.IP, cidrs []netaddr.IPPrefix) bool { | ||
for _, cidr := range cidrs { | ||
if cidr.Contains(ip) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package networking | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
"inet.af/netaddr" | ||
"testing" | ||
) | ||
|
||
func TestParseCIDRs(t *testing.T) { | ||
type args struct { | ||
cidrs []string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []netaddr.IPPrefix | ||
wantErr error | ||
}{ | ||
{ | ||
name: "has one valid CIDR", | ||
args: args{ | ||
cidrs: []string{"192.168.5.100/16"}, | ||
}, | ||
want: []netaddr.IPPrefix{ | ||
netaddr.MustParseIPPrefix("192.168.5.100/16"), | ||
}, | ||
}, | ||
{ | ||
name: "has multiple valid CIDRs", | ||
args: args{ | ||
cidrs: []string{"192.168.5.100/16", "10.100.0.0/16"}, | ||
}, | ||
want: []netaddr.IPPrefix{ | ||
netaddr.MustParseIPPrefix("192.168.5.100/16"), | ||
netaddr.MustParseIPPrefix("10.100.0.0/16"), | ||
}, | ||
}, | ||
{ | ||
name: "has one invalid CIDR", | ||
args: args{ | ||
cidrs: []string{"192.168.5.100/16", "10.100.0.0"}, | ||
}, | ||
wantErr: errors.New("netaddr.ParseIPPrefix(\"10.100.0.0\"): no '/'"), | ||
}, | ||
{ | ||
name: "empty CIDRs", | ||
args: args{ | ||
cidrs: []string{}, | ||
}, | ||
want: nil, | ||
}, | ||
{ | ||
name: "nil CIDRs", | ||
args: args{ | ||
cidrs: nil, | ||
}, | ||
want: nil, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := ParseCIDRs(tt.args.cidrs) | ||
if tt.wantErr != nil { | ||
assert.EqualError(t, err, tt.wantErr.Error()) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.want, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestIsIPWithinCIDRs(t *testing.T) { | ||
type args struct { | ||
ip netaddr.IP | ||
cidrs []netaddr.IPPrefix | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want bool | ||
}{ | ||
{ | ||
name: "ipv4 address within CIDRs", | ||
args: args{ | ||
ip: netaddr.MustParseIP("192.168.1.42"), | ||
cidrs: []netaddr.IPPrefix{ | ||
netaddr.MustParseIPPrefix("10.100.0.0/16"), | ||
netaddr.MustParseIPPrefix("192.168.0.0/16"), | ||
netaddr.MustParseIPPrefix("2600:1f14:f8c:2700::/56"), | ||
}, | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "ipv4 address not within CIDRs", | ||
args: args{ | ||
ip: netaddr.MustParseIP("172.16.1.42"), | ||
cidrs: []netaddr.IPPrefix{ | ||
netaddr.MustParseIPPrefix("10.100.0.0/16"), | ||
netaddr.MustParseIPPrefix("192.168.0.0/16"), | ||
netaddr.MustParseIPPrefix("2600:1f14:f8c:2700::/56"), | ||
}, | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "ipv6 address within CIDRs", | ||
args: args{ | ||
ip: netaddr.MustParseIP("2600:1f14:f8c:2701:a740::"), | ||
cidrs: []netaddr.IPPrefix{ | ||
netaddr.MustParseIPPrefix("10.100.0.0/16"), | ||
netaddr.MustParseIPPrefix("2700:1f14:f8c:2700::/56"), | ||
netaddr.MustParseIPPrefix("2600:1f14:f8c:2700::/56"), | ||
}, | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "ipv6 address not within CIDRs", | ||
args: args{ | ||
ip: netaddr.MustParseIP("2800:1f14:f8c:2701:a740::"), | ||
cidrs: []netaddr.IPPrefix{ | ||
netaddr.MustParseIPPrefix("10.100.0.0/16"), | ||
netaddr.MustParseIPPrefix("2700:1f14:f8c:2700::/56"), | ||
netaddr.MustParseIPPrefix("2600:1f14:f8c:2700::/56"), | ||
}, | ||
}, | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := IsIPWithinCIDRs(tt.args.ip, tt.args.cidrs) | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.