Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use utilnet.GetIndexedIP instead of replicating the function locally #89001

Merged
merged 1 commit into from Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/registry/core/service/ipallocator/BUILD
Expand Up @@ -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",
],
)

Expand Down
17 changes: 5 additions & 12 deletions pkg/registry/core/service/ipallocator/allocator.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
Copy link
Member

@aojea aojea Mar 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it also fixes the bug you fixed in the utils repo, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, after that PR gets merged, we can fix the bug by bumping the utils version. 😄

Copy link
Member

@aojea aojea Mar 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we hold this PR until kubernetes/utils#145 merges then?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh ok, I got it, we bump utils in a follow-up PR so it automatically gets the fix
scratch las comment

ip, _ := utilnet.GetIndexedIP(r.net, offset+1) // +1 because Range doesn't store IP 0
fn(ip)
})
}
Expand Down Expand Up @@ -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
}