mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 05:03:09 +00:00
make portallocator.ErrNotInRange a type
This commit is contained in:
parent
6b83f89d47
commit
cfbdcec7d6
@ -134,7 +134,7 @@ func NewCmdCreateServiceNodePort(f *cmdutil.Factory, cmdOut io.Writer) *cobra.Co
|
|||||||
cmdutil.AddValidateFlags(cmd)
|
cmdutil.AddValidateFlags(cmd)
|
||||||
cmdutil.AddPrinterFlags(cmd)
|
cmdutil.AddPrinterFlags(cmd)
|
||||||
cmdutil.AddGeneratorFlags(cmd, cmdutil.ServiceNodePortGeneratorV1Name)
|
cmdutil.AddGeneratorFlags(cmd, cmdutil.ServiceNodePortGeneratorV1Name)
|
||||||
cmd.Flags().Int32("node-port", 0, "Port used to expose the service on each node in a cluster.")
|
cmd.Flags().Int("node-port", 0, "Port used to expose the service on each node in a cluster.")
|
||||||
addPortFlags(cmd)
|
addPortFlags(cmd)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
@ -153,7 +153,7 @@ func CreateServiceNodePort(f *cmdutil.Factory, cmdOut io.Writer, cmd *cobra.Comm
|
|||||||
TCP: cmdutil.GetFlagStringSlice(cmd, "tcp"),
|
TCP: cmdutil.GetFlagStringSlice(cmd, "tcp"),
|
||||||
Type: api.ServiceTypeNodePort,
|
Type: api.ServiceTypeNodePort,
|
||||||
ClusterIP: "",
|
ClusterIP: "",
|
||||||
NodePort: cmdutil.GetFlagInt32(cmd, "node-port"),
|
NodePort: cmdutil.GetFlagInt(cmd, "node-port"),
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return cmdutil.UsageError(cmd, fmt.Sprintf("Generator: %s not supported.", generatorName))
|
return cmdutil.UsageError(cmd, fmt.Sprintf("Generator: %s not supported.", generatorName))
|
||||||
|
@ -31,7 +31,7 @@ type ServiceCommonGeneratorV1 struct {
|
|||||||
TCP []string
|
TCP []string
|
||||||
Type api.ServiceType
|
Type api.ServiceType
|
||||||
ClusterIP string
|
ClusterIP string
|
||||||
NodePort int32
|
NodePort int
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServiceClusterIPGeneratorV1 struct {
|
type ServiceClusterIPGeneratorV1 struct {
|
||||||
@ -183,7 +183,7 @@ func (s ServiceCommonGeneratorV1) StructuredGenerate() (runtime.Object, error) {
|
|||||||
Port: port,
|
Port: port,
|
||||||
TargetPort: targetPort,
|
TargetPort: targetPort,
|
||||||
Protocol: api.Protocol("TCP"),
|
Protocol: api.Protocol("TCP"),
|
||||||
NodePort: s.NodePort,
|
NodePort: int32(s.NodePort),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,11 +37,18 @@ type Interface interface {
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
ErrFull = errors.New("range is full")
|
ErrFull = errors.New("range is full")
|
||||||
ErrNotInRange = errors.New("provided port is not in the valid range")
|
|
||||||
ErrAllocated = errors.New("provided port is already allocated")
|
ErrAllocated = errors.New("provided port is already allocated")
|
||||||
ErrMismatchedNetwork = errors.New("the provided port range does not match the current port range")
|
ErrMismatchedNetwork = errors.New("the provided port range does not match the current port range")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ErrNotInRange struct {
|
||||||
|
ValidPorts string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *ErrNotInRange) Error() string {
|
||||||
|
return fmt.Sprintf("provided port is not in the valid range. The range of valid ports is %s", e.ValidPorts)
|
||||||
|
}
|
||||||
|
|
||||||
type PortAllocator struct {
|
type PortAllocator struct {
|
||||||
portRange net.PortRange
|
portRange net.PortRange
|
||||||
|
|
||||||
@ -84,8 +91,7 @@ func (r *PortAllocator) Allocate(port int) error {
|
|||||||
if !ok {
|
if !ok {
|
||||||
// include valid port range in error
|
// include valid port range in error
|
||||||
validPorts := r.portRange.String()
|
validPorts := r.portRange.String()
|
||||||
msg := fmt.Sprintf("Valid ports range is %s", validPorts)
|
return &ErrNotInRange{validPorts}
|
||||||
return fmt.Errorf("%v. %s", ErrNotInRange, msg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
allocated, err := r.alloc.Allocate(offset)
|
allocated, err := r.alloc.Allocate(offset)
|
||||||
|
@ -73,16 +73,23 @@ 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(1); err != ErrNotInRange {
|
|
||||||
|
err = r.Allocate(1)
|
||||||
|
if _, ok := err.(*ErrNotInRange); !ok {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := r.Allocate(10001); err != ErrAllocated {
|
if err := r.Allocate(10001); err != ErrAllocated {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := r.Allocate(20000); err != ErrNotInRange {
|
|
||||||
|
err = r.Allocate(20000)
|
||||||
|
if _, ok := err.(*ErrNotInRange); !ok {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := r.Allocate(10201); err != ErrNotInRange {
|
|
||||||
|
err = r.Allocate(10201)
|
||||||
|
if _, ok := err.(*ErrNotInRange); !ok {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if f := r.Free(); f != 1 {
|
if f := r.Free(); f != 1 {
|
||||||
|
@ -114,7 +114,7 @@ func (c *Repair) runOnce() error {
|
|||||||
// TODO: send event
|
// TODO: send event
|
||||||
// port is broken, reallocate
|
// port is broken, reallocate
|
||||||
runtime.HandleError(fmt.Errorf("the port %d for service %s/%s was assigned to multiple services; please recreate", port, svc.Name, svc.Namespace))
|
runtime.HandleError(fmt.Errorf("the port %d for service %s/%s was assigned to multiple services; please recreate", port, svc.Name, svc.Namespace))
|
||||||
case portallocator.ErrNotInRange:
|
case err.(*portallocator.ErrNotInRange):
|
||||||
// TODO: send event
|
// TODO: send event
|
||||||
// port is broken, reallocate
|
// port is broken, reallocate
|
||||||
runtime.HandleError(fmt.Errorf("the port %d for service %s/%s is not within the port range %v; please recreate", port, svc.Name, svc.Namespace, c.portRange))
|
runtime.HandleError(fmt.Errorf("the port %d for service %s/%s is not within the port range %v; please recreate", port, svc.Name, svc.Namespace, c.portRange))
|
||||||
|
Loading…
Reference in New Issue
Block a user