@@ -27,69 +27,69 @@ type PortSet map[Port]struct{}
2727type Port string
2828
2929// NewPort creates a new instance of a Port given a protocol and port number or port range
30- func NewPort (proto , port string ) (Port , error ) {
31- // Check for parsing issues on "port" now so we can avoid having
32- // to check it later on.
33-
34- portStartInt , portEndInt , err := ParsePortRangeToInt (port )
30+ func NewPort (proto , portOrRange string ) (Port , error ) {
31+ start , end , err := parsePortRange (portOrRange )
3532 if err != nil {
3633 return "" , err
3734 }
38-
39- if portStartInt == portEndInt {
40- return Port (fmt .Sprintf ("%d/%s" , portStartInt , proto )), nil
35+ if start == end {
36+ return Port (fmt .Sprintf ("%d/%s" , start , proto )), nil
4137 }
42- return Port (fmt .Sprintf ("%d-%d/%s" , portStartInt , portEndInt , proto )), nil
38+ return Port (fmt .Sprintf ("%d-%d/%s" , start , end , proto )), nil
4339}
4440
4541// ParsePort parses the port number string and returns an int
4642func ParsePort (rawPort string ) (int , error ) {
4743 if rawPort == "" {
4844 return 0 , nil
4945 }
50- port , err := strconv . ParseUint (rawPort , 10 , 16 )
46+ port , err := parsePortNumber (rawPort )
5147 if err != nil {
52- return 0 , fmt .Errorf ("invalid port '%s': %w" , rawPort , errors . Unwrap ( err ) )
48+ return 0 , fmt .Errorf ("invalid port '%s': %w" , rawPort , err )
5349 }
54- return int ( port ) , nil
50+ return port , nil
5551}
5652
5753// ParsePortRangeToInt parses the port range string and returns start/end ints
58- func ParsePortRangeToInt (rawPort string ) (int , int , error ) {
54+ func ParsePortRangeToInt (rawPort string ) (startPort , endPort int , _ error ) {
5955 if rawPort == "" {
56+ // TODO(thaJeztah): consider making this an error; this was kept to keep existing behavior.
6057 return 0 , 0 , nil
6158 }
62- start , end , err := ParsePortRange (rawPort )
63- if err != nil {
64- return 0 , 0 , err
65- }
66- return int (start ), int (end ), nil
59+ return parsePortRange (rawPort )
6760}
6861
6962// Proto returns the protocol of a Port
7063func (p Port ) Proto () string {
71- proto , _ := SplitProtoPort (string (p ))
64+ _ , proto , _ := strings .Cut (string (p ), "/" )
65+ if proto == "" {
66+ proto = "tcp"
67+ }
7268 return proto
7369}
7470
7571// Port returns the port number of a Port
7672func (p Port ) Port () string {
77- _ , port := SplitProtoPort (string (p ))
73+ port , _ , _ := strings . Cut (string (p ), "/" )
7874 return port
7975}
8076
81- // Int returns the port number of a Port as an int
77+ // Int returns the port number of a Port as an int. It assumes [Port]
78+ // is valid, and returns 0 otherwise.
8279func (p Port ) Int () int {
83- portStr := p .Port ()
8480 // We don't need to check for an error because we're going to
85- // assume that any error would have been found, and reported, in NewPort()
86- port , _ := ParsePort ( portStr )
81+ // assume that any error would have been found, and reported, in [ NewPort]
82+ port , _ := parsePortNumber ( p . Port () )
8783 return port
8884}
8985
9086// Range returns the start/end port numbers of a Port range as ints
9187func (p Port ) Range () (int , int , error ) {
92- return ParsePortRangeToInt (p .Port ())
88+ portRange := p .Port ()
89+ if portRange == "" {
90+ return 0 , 0 , nil
91+ }
92+ return parsePortRange (portRange )
9393}
9494
9595// SplitProtoPort splits a port(range) and protocol, formatted as "<portnum>/[<proto>]"
@@ -194,14 +194,14 @@ func ParsePortSpec(rawPort string) ([]PortMapping, error) {
194194 return nil , errors .New ("invalid IP address: " + ip )
195195 }
196196
197- startPort , endPort , err := ParsePortRange (containerPort )
197+ startPort , endPort , err := parsePortRange (containerPort )
198198 if err != nil {
199199 return nil , errors .New ("invalid containerPort: " + containerPort )
200200 }
201201
202- var startHostPort , endHostPort uint64
202+ var startHostPort , endHostPort int
203203 if hostPort != "" {
204- startHostPort , endHostPort , err = ParsePortRange (hostPort )
204+ startHostPort , endHostPort , err = parsePortRange (hostPort )
205205 if err != nil {
206206 return nil , errors .New ("invalid hostPort: " + hostPort )
207207 }
@@ -218,19 +218,18 @@ func ParsePortSpec(rawPort string) ([]PortMapping, error) {
218218 count := endPort - startPort + 1
219219 ports := make ([]PortMapping , 0 , count )
220220
221- for i := uint64 (0 ); i < count ; i ++ {
222- cPort := Port (strconv .FormatUint (startPort + i , 10 ) + "/" + proto )
221+ for i := 0 ; i < count ; i ++ {
223222 hPort := ""
224223 if hostPort != "" {
225- hPort = strconv .FormatUint (startHostPort + i , 10 )
224+ hPort = strconv .Itoa (startHostPort + i )
226225 // Set hostPort to a range only if there is a single container port
227226 // and a dynamic host port.
228227 if count == 1 && startHostPort != endHostPort {
229- hPort += "-" + strconv .FormatUint (endHostPort , 10 )
228+ hPort += "-" + strconv .Itoa (endHostPort )
230229 }
231230 }
232231 ports = append (ports , PortMapping {
233- Port : cPort ,
232+ Port : Port ( strconv . Itoa ( startPort + i ) + "/" + proto ) ,
234233 Binding : PortBinding {HostIP : ip , HostPort : hPort },
235234 })
236235 }
0 commit comments