@@ -25,7 +25,24 @@ import (
25
25
"github.com/pkg/errors"
26
26
)
27
27
28
- func ParseFlagP (s string ) (* gocni.PortMapping , error ) {
28
+ func splitParts (rawport string ) (string , string , string ) {
29
+ parts := strings .Split (rawport , ":" )
30
+ n := len (parts )
31
+ containerport := parts [n - 1 ]
32
+
33
+ switch n {
34
+ case 1 :
35
+ return "" , "" , containerport
36
+ case 2 :
37
+ return "" , parts [0 ], containerport
38
+ case 3 :
39
+ return parts [0 ], parts [1 ], containerport
40
+ default :
41
+ return strings .Join (parts [:n - 2 ], ":" ), parts [n - 2 ], containerport
42
+ }
43
+ }
44
+
45
+ func ParseFlagP (s string ) ([]gocni.PortMapping , error ) {
29
46
proto := "tcp"
30
47
splitBySlash := strings .Split (s , "/" )
31
48
switch len (splitBySlash ) {
@@ -42,44 +59,54 @@ func ParseFlagP(s string) (*gocni.PortMapping, error) {
42
59
return nil , errors .Errorf ("failed to parse %q, unexpected slashes" , s )
43
60
}
44
61
45
- res := & gocni.PortMapping {
62
+ res := gocni.PortMapping {
46
63
Protocol : proto ,
47
- HostIP : "0.0.0.0" ,
48
64
}
49
65
50
- splitByColon := strings .Split (splitBySlash [0 ], ":" )
51
- switch len (splitByColon ) {
52
- case 1 :
66
+ multi_res := []gocni.PortMapping {}
67
+
68
+ ip , hostPort , containerPort := splitParts (splitBySlash [0 ])
69
+
70
+ if containerPort == "" {
71
+ return nil , errors .Errorf ("No port specified: %s<empty>" , splitBySlash [0 ])
72
+ }
73
+
74
+ if hostPort == "" {
53
75
return nil , errors .Errorf ("automatic host port assignment is not supported yet (FIXME)" )
54
- case 2 :
55
- i , err := strconv .Atoi (splitByColon [0 ])
56
- if err != nil {
57
- return nil , err
58
- }
59
- res .HostPort = int32 (i )
60
- i , err = strconv .Atoi (splitByColon [1 ])
61
- if err != nil {
62
- return nil , err
63
- }
64
- res .ContainerPort = int32 (i )
65
- return res , nil
66
- case 3 :
67
- res .HostIP = splitByColon [0 ]
68
- if net .ParseIP (res .HostIP ) == nil {
69
- return nil , errors .Errorf ("invalid IP %q" , res .HostIP )
70
- }
71
- i , err := strconv .Atoi (splitByColon [1 ])
72
- if err != nil {
73
- return nil , err
76
+ }
77
+
78
+ i , err := strconv .Atoi (hostPort )
79
+ if err != nil {
80
+ return nil , err
81
+ }
82
+ res .HostPort = int32 (i )
83
+
84
+ i , err = strconv .Atoi (containerPort )
85
+ if err != nil {
86
+ return nil , err
87
+ }
88
+ res .ContainerPort = int32 (i )
89
+
90
+ if ip == "" {
91
+ res .HostIP = "0.0.0.0"
92
+ multi_res = append (multi_res , res )
93
+ res .HostIP = "::"
94
+ multi_res = append (multi_res , res )
95
+ } else {
96
+ if ip [0 ] == '[' {
97
+ // Strip [] from IPV6 addresses
98
+ rawIP , _ , err := net .SplitHostPort (ip + ":" )
99
+ if err != nil {
100
+ return nil , errors .Errorf ("Invalid ip address %v: %s" , ip , err )
101
+ }
102
+ ip = rawIP
74
103
}
75
- res .HostPort = int32 (i )
76
- i , err = strconv .Atoi (splitByColon [2 ])
77
- if err != nil {
78
- return nil , err
104
+
105
+ if net .ParseIP (ip ) == nil {
106
+ return nil , errors .Errorf ("Invalid ip address: %s" , ip )
79
107
}
80
- res .ContainerPort = int32 (i )
81
- return res , nil
82
- default :
83
- return nil , errors .Errorf ("failed to parse %q, unexpected colons" , s )
108
+ res .HostIP = ip
109
+ multi_res = append (multi_res , res )
84
110
}
111
+ return multi_res , nil
85
112
}
0 commit comments