1
1
/*
2
- Copyright (C) nerdctl authors.
3
- Copyright (C) containerd authors.
2
+ Copyright The containerd Authors.
4
3
5
4
Licensed under the Apache License, Version 2.0 (the "License");
6
5
you may not use this file except in compliance with the License.
@@ -26,6 +25,23 @@ import (
26
25
"github.com/pkg/errors"
27
26
)
28
27
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
+
29
45
func ParseFlagP (s string ) (* gocni.PortMapping , error ) {
30
46
proto := "tcp"
31
47
splitBySlash := strings .Split (s , "/" )
@@ -48,39 +64,41 @@ func ParseFlagP(s string) (*gocni.PortMapping, error) {
48
64
HostIP : "0.0.0.0" ,
49
65
}
50
66
51
- splitByColon := strings .Split (splitBySlash [0 ], ":" )
52
- switch len (splitByColon ) {
53
- case 1 :
54
- return nil , errors .Errorf ("automatic host port assignment is not supported yet (FIXME)" )
55
- case 2 :
56
- i , err := strconv .Atoi (splitByColon [0 ])
57
- if err != nil {
58
- return nil , err
59
- }
60
- res .HostPort = int32 (i )
61
- i , err = strconv .Atoi (splitByColon [1 ])
62
- if err != nil {
63
- return nil , err
64
- }
65
- res .ContainerPort = int32 (i )
66
- return res , nil
67
- case 3 :
68
- res .HostIP = splitByColon [0 ]
69
- if net .ParseIP (res .HostIP ) == nil {
70
- return nil , errors .Errorf ("invalid IP %q" , res .HostIP )
71
- }
72
- i , err := strconv .Atoi (splitByColon [1 ])
73
- if err != nil {
74
- return nil , err
75
- }
76
- res .HostPort = int32 (i )
77
- i , err = strconv .Atoi (splitByColon [2 ])
67
+ ip , hostPort , containerPort := splitParts (splitBySlash [0 ])
68
+
69
+ if ip != "" && ip [0 ] == '[' {
70
+ // Strip [] from IPV6 addresses
71
+ rawIP , _ , err := net .SplitHostPort (ip + ":" )
78
72
if err != nil {
79
- return nil , err
73
+ return nil , errors . Errorf ( "Invalid ip address %v: %s" , ip , err )
80
74
}
81
- res .ContainerPort = int32 (i )
82
- return res , nil
83
- default :
84
- return nil , errors .Errorf ("failed to parse %q, unexpected colons" , s )
75
+ ip = rawIP
76
+ }
77
+
78
+ if ip != "" && net .ParseIP (ip ) == nil {
79
+ return nil , errors .Errorf ("Invalid ip address: %s" , ip )
80
+ }
81
+ if containerPort == "" {
82
+ return nil , errors .Errorf ("No port specified: %s<empty>" , splitBySlash [0 ])
83
+ }
84
+
85
+ if hostPort == "" {
86
+ return nil , errors .Errorf ("automatic host port assignment is not supported yet (FIXME)" )
87
+ }
88
+
89
+ i , err := strconv .Atoi (hostPort )
90
+ if err != nil {
91
+ return nil , err
85
92
}
93
+ res .HostPort = int32 (i )
94
+
95
+ i , err = strconv .Atoi (containerPort )
96
+ if err != nil {
97
+ return nil , err
98
+ }
99
+ res .ContainerPort = int32 (i )
100
+
101
+ res .HostIP = ip
102
+ return res , nil
103
+
86
104
}
0 commit comments