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

Say the valid IP range in IP errors #52693

Merged
merged 1 commit into from
Sep 20, 2017
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
11 changes: 9 additions & 2 deletions pkg/registry/core/service/ipallocator/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,18 @@ type Interface interface {

var (
ErrFull = errors.New("range is full")
ErrNotInRange = errors.New("provided IP is not in the valid range")
ErrAllocated = errors.New("provided IP is already allocated")
ErrMismatchedNetwork = errors.New("the provided network does not match the current range")
)

type ErrNotInRange struct {
ValidRange string
}

func (e *ErrNotInRange) Error() string {
return fmt.Sprintf("provided IP is not in the valid range. The range of valid IPs is %s", e.ValidRange)
Copy link
Member

Choose a reason for hiding this comment

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

Should this print the provided IP?

Copy link
Contributor

Choose a reason for hiding this comment

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

No, the provided IP is printed outside this error. See the current message:

Message: "Invalid value: \"127.1.2.3\": provided IP is not in the valid range",

The PR is expected to provide the valid IP range when we get an error of provided IP is not in the valid range for debugging.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for clarifying, feel free to lgtm. We need to move forward with the reference issue quickly!

}

// Range is a contiguous block of IPs that can be allocated atomically.
//
// The internal structure of the range is:
Expand Down Expand Up @@ -135,7 +142,7 @@ func (r *Range) CIDR() net.IPNet {
func (r *Range) Allocate(ip net.IP) error {
ok, offset := r.contains(ip)
if !ok {
return ErrNotInRange
return &ErrNotInRange{r.net.String()}
}

allocated, err := r.alloc.Allocate(offset)
Expand Down
9 changes: 6 additions & 3 deletions pkg/registry/core/service/ipallocator/allocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,19 @@ func TestAllocate(t *testing.T) {
if err := r.Release(released); err != nil {
t.Fatal(err)
}
if err := r.Allocate(net.ParseIP("192.168.0.1")); err != ErrNotInRange {
err = r.Allocate(net.ParseIP("192.168.0.1"))
if _, ok := err.(*ErrNotInRange); !ok {
t.Fatal(err)
}
if err := r.Allocate(net.ParseIP("192.168.1.1")); err != ErrAllocated {
t.Fatal(err)
}
if err := r.Allocate(net.ParseIP("192.168.1.0")); err != ErrNotInRange {
err = r.Allocate(net.ParseIP("192.168.1.0"))
if _, ok := err.(*ErrNotInRange); !ok {
t.Fatal(err)
}
if err := r.Allocate(net.ParseIP("192.168.1.255")); err != ErrNotInRange {
err = r.Allocate(net.ParseIP("192.168.1.255"))
if _, ok := err.(*ErrNotInRange); !ok {
t.Fatal(err)
}
if f := r.Free(); f != 1 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/registry/core/service/ipallocator/controller/repair.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (c *Repair) runOnce() error {
// TODO: send event
// cluster IP is duplicate
runtime.HandleError(fmt.Errorf("the cluster IP %s for service %s/%s was assigned to multiple services; please recreate", ip, svc.Name, svc.Namespace))
case ipallocator.ErrNotInRange:
case err.(*ipallocator.ErrNotInRange):
// TODO: send event
// cluster IP is out of range
runtime.HandleError(fmt.Errorf("the cluster IP %s for service %s/%s is not within the service CIDR %s; please recreate", ip, svc.Name, svc.Namespace, c.network))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ func TestEmpty(t *testing.T) {
func TestErrors(t *testing.T) {
_, storage, _, _, destroyFunc := newStorage(t)
defer destroyFunc()
if err := storage.Allocate(net.ParseIP("192.168.0.0")); err != ipallocator.ErrNotInRange {
err := storage.Allocate(net.ParseIP("192.168.0.0"))
if _, ok := err.(*ipallocator.ErrNotInRange); !ok {
t.Fatal(err)
}
}
Expand Down