mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 14:37:00 +00:00
Add an IPFamily() method to ipallocator
This commit is contained in:
parent
89b633d353
commit
5970c4671c
@ -35,6 +35,7 @@ type Interface interface {
|
|||||||
Release(net.IP) error
|
Release(net.IP) error
|
||||||
ForEach(func(net.IP))
|
ForEach(func(net.IP))
|
||||||
CIDR() net.IPNet
|
CIDR() net.IPNet
|
||||||
|
IPFamily() api.IPFamily
|
||||||
|
|
||||||
// For testing
|
// For testing
|
||||||
Has(ip net.IP) bool
|
Has(ip net.IP) bool
|
||||||
@ -76,6 +77,8 @@ type Range struct {
|
|||||||
base *big.Int
|
base *big.Int
|
||||||
// max is the maximum size of the usable addresses in the range
|
// max is the maximum size of the usable addresses in the range
|
||||||
max int
|
max int
|
||||||
|
// family is the IP family of this range
|
||||||
|
family api.IPFamily
|
||||||
|
|
||||||
alloc allocator.Interface
|
alloc allocator.Interface
|
||||||
}
|
}
|
||||||
@ -85,13 +88,16 @@ func NewAllocatorCIDRRange(cidr *net.IPNet, allocatorFactory allocator.Allocator
|
|||||||
max := utilnet.RangeSize(cidr)
|
max := utilnet.RangeSize(cidr)
|
||||||
base := utilnet.BigForIP(cidr.IP)
|
base := utilnet.BigForIP(cidr.IP)
|
||||||
rangeSpec := cidr.String()
|
rangeSpec := cidr.String()
|
||||||
|
var family api.IPFamily
|
||||||
|
|
||||||
if utilnet.IsIPv6CIDR(cidr) {
|
if utilnet.IsIPv6CIDR(cidr) {
|
||||||
|
family = api.IPv6Protocol
|
||||||
// Limit the max size, since the allocator keeps a bitmap of that size.
|
// Limit the max size, since the allocator keeps a bitmap of that size.
|
||||||
if max > 65536 {
|
if max > 65536 {
|
||||||
max = 65536
|
max = 65536
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
family = api.IPv4Protocol
|
||||||
// Don't use the IPv4 network's broadcast address.
|
// Don't use the IPv4 network's broadcast address.
|
||||||
max--
|
max--
|
||||||
}
|
}
|
||||||
@ -104,6 +110,7 @@ func NewAllocatorCIDRRange(cidr *net.IPNet, allocatorFactory allocator.Allocator
|
|||||||
net: cidr,
|
net: cidr,
|
||||||
base: base,
|
base: base,
|
||||||
max: maximum(0, int(max)),
|
max: maximum(0, int(max)),
|
||||||
|
family: family,
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
r.alloc, err = allocatorFactory(r.max, rangeSpec)
|
r.alloc, err = allocatorFactory(r.max, rangeSpec)
|
||||||
@ -219,6 +226,11 @@ func (r *Range) Has(ip net.IP) bool {
|
|||||||
return r.alloc.Has(offset)
|
return r.alloc.Has(offset)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IPFamily returns the IP family of this range.
|
||||||
|
func (r *Range) IPFamily() api.IPFamily {
|
||||||
|
return r.family
|
||||||
|
}
|
||||||
|
|
||||||
// Snapshot saves the current state of the pool.
|
// Snapshot saves the current state of the pool.
|
||||||
func (r *Range) Snapshot(dst *api.RangeAllocation) error {
|
func (r *Range) Snapshot(dst *api.RangeAllocation) error {
|
||||||
snapshottable, ok := r.alloc.(allocator.Snapshottable)
|
snapshottable, ok := r.alloc.(allocator.Snapshottable)
|
||||||
|
@ -28,6 +28,7 @@ func TestAllocate(t *testing.T) {
|
|||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
cidr string
|
cidr string
|
||||||
|
family api.IPFamily
|
||||||
free int
|
free int
|
||||||
released string
|
released string
|
||||||
outOfRange []string
|
outOfRange []string
|
||||||
@ -36,6 +37,7 @@ func TestAllocate(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "IPv4",
|
name: "IPv4",
|
||||||
cidr: "192.168.1.0/24",
|
cidr: "192.168.1.0/24",
|
||||||
|
family: api.IPv4Protocol,
|
||||||
free: 254,
|
free: 254,
|
||||||
released: "192.168.1.5",
|
released: "192.168.1.5",
|
||||||
outOfRange: []string{
|
outOfRange: []string{
|
||||||
@ -49,6 +51,7 @@ func TestAllocate(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "IPv6",
|
name: "IPv6",
|
||||||
cidr: "2001:db8:1::/48",
|
cidr: "2001:db8:1::/48",
|
||||||
|
family: api.IPv6Protocol,
|
||||||
free: 65535,
|
free: 65535,
|
||||||
released: "2001:db8:1::5",
|
released: "2001:db8:1::5",
|
||||||
outOfRange: []string{
|
outOfRange: []string{
|
||||||
@ -79,6 +82,10 @@ func TestAllocate(t *testing.T) {
|
|||||||
t.Errorf("allocator returned a different cidr")
|
t.Errorf("allocator returned a different cidr")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if r.IPFamily() != tc.family {
|
||||||
|
t.Errorf("allocator returned wrong IP family")
|
||||||
|
}
|
||||||
|
|
||||||
if f := r.Used(); f != 0 {
|
if f := r.Used(); f != 0 {
|
||||||
t.Errorf("Test %s unexpected used %d", tc.name, f)
|
t.Errorf("Test %s unexpected used %d", tc.name, f)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user