mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Merge pull request #89001 from SataQiu/cleanup-20200310
Use utilnet.GetIndexedIP instead of replicating the function locally
This commit is contained in:
commit
34f03ae9d4
@ -13,6 +13,7 @@ go_library(
|
|||||||
deps = [
|
deps = [
|
||||||
"//pkg/apis/core:go_default_library",
|
"//pkg/apis/core:go_default_library",
|
||||||
"//pkg/registry/core/service/allocator:go_default_library",
|
"//pkg/registry/core/service/allocator:go_default_library",
|
||||||
|
"//vendor/k8s.io/utils/net:go_default_library",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -19,10 +19,12 @@ package ipallocator
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
api "k8s.io/kubernetes/pkg/apis/core"
|
|
||||||
"k8s.io/kubernetes/pkg/registry/core/service/allocator"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
|
api "k8s.io/kubernetes/pkg/apis/core"
|
||||||
|
"k8s.io/kubernetes/pkg/registry/core/service/allocator"
|
||||||
|
utilnet "k8s.io/utils/net"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Interface manages the allocation of IP addresses out of a range. Interface
|
// Interface manages the allocation of IP addresses out of a range. Interface
|
||||||
@ -187,7 +189,7 @@ func (r *Range) Release(ip net.IP) error {
|
|||||||
// ForEach calls the provided function for each allocated IP.
|
// ForEach calls the provided function for each allocated IP.
|
||||||
func (r *Range) ForEach(fn func(net.IP)) {
|
func (r *Range) ForEach(fn func(net.IP)) {
|
||||||
r.alloc.ForEach(func(offset int) {
|
r.alloc.ForEach(func(offset int) {
|
||||||
ip, _ := GetIndexedIP(r.net, offset+1) // +1 because Range doesn't store IP 0
|
ip, _ := utilnet.GetIndexedIP(r.net, offset+1) // +1 because Range doesn't store IP 0
|
||||||
fn(ip)
|
fn(ip)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -282,12 +284,3 @@ func RangeSize(subnet *net.IPNet) int64 {
|
|||||||
return int64(1) << uint(bits-ones)
|
return int64(1) << uint(bits-ones)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetIndexedIP returns a net.IP that is subnet.IP + index in the contiguous IP space.
|
|
||||||
func GetIndexedIP(subnet *net.IPNet, index int) (net.IP, error) {
|
|
||||||
ip := addIPOffset(bigForIP(subnet.IP), index)
|
|
||||||
if !subnet.Contains(ip) {
|
|
||||||
return nil, fmt.Errorf("can't generate IP with index %d from subnet. subnet too small. subnet: %q", index, subnet)
|
|
||||||
}
|
|
||||||
return ip, nil
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user