mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 13:37:30 +00:00
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:
commit
02f48b6846
@ -40,11 +40,18 @@ type Interface interface {
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
ErrFull = errors.New("range is full")
|
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")
|
ErrAllocated = errors.New("provided IP is already allocated")
|
||||||
ErrMismatchedNetwork = errors.New("the provided network does not match the current range")
|
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.
|
// Range is a contiguous block of IPs that can be allocated atomically.
|
||||||
//
|
//
|
||||||
// The internal structure of the range is:
|
// 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 {
|
func (r *Range) Allocate(ip net.IP) error {
|
||||||
ok, offset := r.contains(ip)
|
ok, offset := r.contains(ip)
|
||||||
if !ok {
|
if !ok {
|
||||||
return ErrNotInRange
|
return &ErrNotInRange{r.net.String()}
|
||||||
}
|
}
|
||||||
|
|
||||||
allocated, err := r.alloc.Allocate(offset)
|
allocated, err := r.alloc.Allocate(offset)
|
||||||
|
@ -78,16 +78,19 @@ func TestAllocate(t *testing.T) {
|
|||||||
if err := r.Release(released); err != nil {
|
if err := r.Release(released); err != nil {
|
||||||
t.Fatal(err)
|
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)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := r.Allocate(net.ParseIP("192.168.1.1")); err != ErrAllocated {
|
if err := r.Allocate(net.ParseIP("192.168.1.1")); err != ErrAllocated {
|
||||||
t.Fatal(err)
|
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)
|
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)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if f := r.Free(); f != 1 {
|
if f := r.Free(); f != 1 {
|
||||||
|
@ -154,7 +154,7 @@ func (c *Repair) runOnce() error {
|
|||||||
// TODO: send event
|
// TODO: send event
|
||||||
// cluster IP is duplicate
|
// 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))
|
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
|
// TODO: send event
|
||||||
// cluster IP is out of range
|
// 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))
|
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))
|
||||||
|
@ -78,7 +78,8 @@ func TestEmpty(t *testing.T) {
|
|||||||
func TestErrors(t *testing.T) {
|
func TestErrors(t *testing.T) {
|
||||||
_, storage, _, _, destroyFunc := newStorage(t)
|
_, storage, _, _, destroyFunc := newStorage(t)
|
||||||
defer destroyFunc()
|
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)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user