@@ -198,42 +198,39 @@ func ParsePortSpec(rawPort string) ([]PortMapping, error) {
198198 return nil , errors .New ("invalid containerPort: " + containerPort )
199199 }
200200
201- var startHostPort , endHostPort uint64 = 0 , 0
201+ var startHostPort , endHostPort uint64
202202 if hostPort != "" {
203203 startHostPort , endHostPort , err = ParsePortRange (hostPort )
204204 if err != nil {
205205 return nil , errors .New ("invalid hostPort: " + hostPort )
206206 }
207- }
208-
209- if hostPort != "" && (endPort - startPort ) != (endHostPort - startHostPort ) {
210- // Allow host port range iff containerPort is not a range.
211- // In this case, use the host port range as the dynamic
212- // host port range to allocate into.
213- if endPort != startPort {
214- return nil , fmt .Errorf ("invalid ranges specified for container and host Ports: %s and %s" , containerPort , hostPort )
207+ if (endPort - startPort ) != (endHostPort - startHostPort ) {
208+ // Allow host port range iff containerPort is not a range.
209+ // In this case, use the host port range as the dynamic
210+ // host port range to allocate into.
211+ if endPort != startPort {
212+ return nil , fmt .Errorf ("invalid ranges specified for container and host Ports: %s and %s" , containerPort , hostPort )
213+ }
215214 }
216215 }
217216
218- ports := []PortMapping {}
219- for i := uint64 (0 ); i <= (endPort - startPort ); i ++ {
220- containerPort = strconv .FormatUint (startPort + i , 10 )
217+ count := endPort - startPort + 1
218+ ports := make ([]PortMapping , 0 , count )
219+
220+ for i := uint64 (0 ); i < count ; i ++ {
221+ cPort := Port (strconv .FormatUint (startPort + i , 10 ) + "/" + proto )
222+ hPort := ""
221223 if hostPort != "" {
222- hostPort = strconv .FormatUint (startHostPort + i , 10 )
223- }
224- // Set hostPort to a range only if there is a single container port
225- // and a dynamic host port.
226- if startPort == endPort && startHostPort != endHostPort {
227- hostPort = hostPort + "-" + strconv .FormatUint (endHostPort , 10 )
228- }
229- port , err := NewPort (proto , containerPort )
230- if err != nil {
231- return nil , err
224+ hPort = strconv .FormatUint (startHostPort + i , 10 )
225+ // Set hostPort to a range only if there is a single container port
226+ // and a dynamic host port.
227+ if count == 1 && startHostPort != endHostPort {
228+ hPort += "-" + strconv .FormatUint (endHostPort , 10 )
229+ }
232230 }
233-
234231 ports = append (ports , PortMapping {
235- Port : port ,
236- Binding : PortBinding {HostIP : ip , HostPort : hostPort },
232+ Port : cPort ,
233+ Binding : PortBinding {HostIP : ip , HostPort : hPort },
237234 })
238235 }
239236 return ports , nil
0 commit comments