Merge pull request #52693 from thockin/better-error-msg

Automatic merge from submit-queue (batch tested with PRs 39620, 52693). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>..

Say the valid IP range in IP errors

This is now the same as portallocator.

@ericchiang @xiangpengzhao 

xref #50274
This commit is contained in:
Kubernetes Submit Queue 2017-09-19 23:56:41 -07:00 committed by GitHub
commit 02f48b6846
4 changed files with 18 additions and 7 deletions

View File

@ -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)
}
// Range is a contiguous block of IPs that can be allocated atomically.
//
// The internal structure of the range is:
@ -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)

View File

@ -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 {

View File

@ -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))

View File

@ -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)
}
}