Skip to content

Commit

Permalink
Merge pull request #3217 from THLIVSQAZ/compose-ipv6-port
Browse files Browse the repository at this point in the history
Fix compose up failed when ipv6 as hostIP
  • Loading branch information
AkihiroSuda authored Jul 16, 2024
2 parents 80bfa8e + e7b567c commit 31352aa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
23 changes: 11 additions & 12 deletions pkg/portutil/portutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,19 @@ import (

// return respectively ip, hostPort, containerPort
func splitParts(rawport string) (string, string, string) {
parts := strings.Split(rawport, ":")
n := len(parts)
containerport := parts[n-1]
lastIndex := strings.LastIndex(rawport, ":")
containerPort := rawport[lastIndex+1:]
if lastIndex == -1 {
return "", "", containerPort
}

switch n {
case 1:
return "", "", containerport
case 2:
return "", parts[0], containerport
case 3:
return parts[0], parts[1], containerport
default:
return strings.Join(parts[:n-2], ":"), parts[n-2], containerport
hostAddrPort := rawport[:lastIndex]
addr, port, err := net.SplitHostPort(hostAddrPort)
if err != nil {
return "", hostAddrPort, containerPort
}

return addr, port, containerPort
}

// ParseFlagP parse port mapping pair, like "127.0.0.1:3000:8080/tcp",
Expand Down
15 changes: 15 additions & 0 deletions pkg/portutil/portutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,21 @@ func TestParseFlagP(t *testing.T) {
},
wantErr: false,
},
{
name: "with ipv6 host ip",
args: args{
s: "[::0]:8080:80/tcp",
},
want: []gocni.PortMapping{
{
HostPort: 8080,
ContainerPort: 80,
Protocol: "tcp",
HostIP: "::0",
},
},
wantErr: false,
},
{
name: "with invalid protocol",
args: args{
Expand Down

0 comments on commit 31352aa

Please sign in to comment.