From 776fa5e76f842a388340f7a7294d9af065d3f14f Mon Sep 17 00:00:00 2001 From: SataQiu <1527062125@qq.com> Date: Tue, 10 Mar 2020 18:03:53 +0800 Subject: [PATCH] use utilnet.GetIndexedIP instead of replicating the function locally --- pkg/registry/core/service/ipallocator/BUILD | 1 + .../core/service/ipallocator/allocator.go | 17 +++++------------ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/pkg/registry/core/service/ipallocator/BUILD b/pkg/registry/core/service/ipallocator/BUILD index 10db164981f5..67b931e3f6df 100644 --- a/pkg/registry/core/service/ipallocator/BUILD +++ b/pkg/registry/core/service/ipallocator/BUILD @@ -13,6 +13,7 @@ go_library( deps = [ "//pkg/apis/core:go_default_library", "//pkg/registry/core/service/allocator:go_default_library", + "//vendor/k8s.io/utils/net:go_default_library", ], ) diff --git a/pkg/registry/core/service/ipallocator/allocator.go b/pkg/registry/core/service/ipallocator/allocator.go index fb421e4e9c5c..732a089ac389 100644 --- a/pkg/registry/core/service/ipallocator/allocator.go +++ b/pkg/registry/core/service/ipallocator/allocator.go @@ -19,10 +19,12 @@ package ipallocator import ( "errors" "fmt" - api "k8s.io/kubernetes/pkg/apis/core" - "k8s.io/kubernetes/pkg/registry/core/service/allocator" "math/big" "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 @@ -187,7 +189,7 @@ func (r *Range) Release(ip net.IP) error { // ForEach calls the provided function for each allocated IP. func (r *Range) ForEach(fn func(net.IP)) { 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) }) } @@ -282,12 +284,3 @@ func RangeSize(subnet *net.IPNet) int64 { 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 -}