Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
a92c8b68
Commit
a92c8b68
authored
Sep 15, 2015
by
Jeff Lowdermilk
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #13384 from ZJU-SEL/portsbindings
Allow multiple host ports map to the same container port
parents
9ed2d842
7b2e2e56
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
87 additions
and
36 deletions
+87
-36
docker_test.go
pkg/kubelet/dockertools/docker_test.go
+71
-31
manager.go
pkg/kubelet/dockertools/manager.go
+16
-5
No files found.
pkg/kubelet/dockertools/docker_test.go
View file @
a92c8b68
...
@@ -22,6 +22,7 @@ import (
...
@@ -22,6 +22,7 @@ import (
"hash/adler32"
"hash/adler32"
"reflect"
"reflect"
"sort"
"sort"
"strconv"
"strings"
"strings"
"testing"
"testing"
...
@@ -698,42 +699,81 @@ func TestMakePortsAndBindings(t *testing.T) {
...
@@ -698,42 +699,81 @@ func TestMakePortsAndBindings(t *testing.T) {
HostPort
:
445
,
HostPort
:
445
,
Protocol
:
"foobar"
,
Protocol
:
"foobar"
,
},
},
{
ContainerPort
:
443
,
HostPort
:
446
,
Protocol
:
"tcp"
,
},
{
ContainerPort
:
443
,
HostPort
:
446
,
Protocol
:
"udp"
,
},
}
}
exposedPorts
,
bindings
:=
makePortsAndBindings
(
ports
)
exposedPorts
,
bindings
:=
makePortsAndBindings
(
ports
)
if
len
(
ports
)
!=
len
(
exposedPorts
)
||
len
(
ports
)
!=
len
(
bindings
)
{
// Count the expected exposed ports and bindings
expectedExposedPorts
:=
map
[
string
]
struct
{}{}
for
_
,
binding
:=
range
ports
{
dockerKey
:=
strconv
.
Itoa
(
binding
.
ContainerPort
)
+
"/"
+
string
(
binding
.
Protocol
)
expectedExposedPorts
[
dockerKey
]
=
struct
{}{}
}
// Should expose right ports in docker
if
len
(
expectedExposedPorts
)
!=
len
(
exposedPorts
)
{
t
.
Errorf
(
"Unexpected ports and bindings, %#v %#v %#v"
,
ports
,
exposedPorts
,
bindings
)
t
.
Errorf
(
"Unexpected ports and bindings, %#v %#v %#v"
,
ports
,
exposedPorts
,
bindings
)
}
}
for
key
,
value
:=
range
bindings
{
switch
value
[
0
]
.
HostPort
{
// Construct expected bindings
case
"8080"
:
expectPortBindings
:=
map
[
string
][]
docker
.
PortBinding
{
if
!
reflect
.
DeepEqual
(
docker
.
Port
(
"80/tcp"
),
key
)
{
"80/tcp"
:
{
t
.
Errorf
(
"Unexpected docker port: %#v"
,
key
)
docker
.
PortBinding
{
}
HostPort
:
"8080"
,
if
value
[
0
]
.
HostIP
!=
"127.0.0.1"
{
HostIP
:
"127.0.0.1"
,
t
.
Errorf
(
"Unexpected host IP: %s"
,
value
[
0
]
.
HostIP
)
},
}
},
case
"443"
:
"443/tcp"
:
{
if
!
reflect
.
DeepEqual
(
docker
.
Port
(
"443/tcp"
),
key
)
{
docker
.
PortBinding
{
t
.
Errorf
(
"Unexpected docker port: %#v"
,
key
)
HostPort
:
"443"
,
}
HostIP
:
""
,
if
value
[
0
]
.
HostIP
!=
""
{
},
t
.
Errorf
(
"Unexpected host IP: %s"
,
value
[
0
]
.
HostIP
)
docker
.
PortBinding
{
}
HostPort
:
"446"
,
case
"444"
:
HostIP
:
""
,
if
!
reflect
.
DeepEqual
(
docker
.
Port
(
"444/udp"
),
key
)
{
},
t
.
Errorf
(
"Unexpected docker port: %#v"
,
key
)
},
}
"443/udp"
:
{
if
value
[
0
]
.
HostIP
!=
""
{
docker
.
PortBinding
{
t
.
Errorf
(
"Unexpected host IP: %s"
,
value
[
0
]
.
HostIP
)
HostPort
:
"446"
,
}
HostIP
:
""
,
case
"445"
:
},
if
!
reflect
.
DeepEqual
(
docker
.
Port
(
"445/tcp"
),
key
)
{
},
t
.
Errorf
(
"Unexpected docker port: %#v"
,
key
)
"444/udp"
:
{
}
docker
.
PortBinding
{
if
value
[
0
]
.
HostIP
!=
""
{
HostPort
:
"444"
,
t
.
Errorf
(
"Unexpected host IP: %s"
,
value
[
0
]
.
HostIP
)
HostIP
:
""
,
},
},
"445/tcp"
:
{
docker
.
PortBinding
{
HostPort
:
"445"
,
HostIP
:
""
,
},
},
}
// interate the bindings by dockerPort, and check its portBindings
for
dockerPort
,
portBindings
:=
range
bindings
{
switch
dockerPort
{
case
"80/tcp"
,
"443/tcp"
,
"443/udp"
,
"444/udp"
,
"445/tcp"
:
if
!
reflect
.
DeepEqual
(
expectPortBindings
[
string
(
dockerPort
)],
portBindings
)
{
t
.
Errorf
(
"Unexpected portbindings for %#v, expected: %#v, but got: %#v"
,
dockerPort
,
expectPortBindings
[
string
(
dockerPort
)],
portBindings
)
}
}
default
:
t
.
Errorf
(
"Unexpected docker port: %#v with portbindings: %#v"
,
dockerPort
,
portBindings
)
}
}
}
}
}
}
...
...
pkg/kubelet/dockertools/manager.go
View file @
a92c8b68
...
@@ -584,13 +584,24 @@ func makePortsAndBindings(portMappings []kubecontainer.PortMapping) (map[docker.
...
@@ -584,13 +584,24 @@ func makePortsAndBindings(portMappings []kubecontainer.PortMapping) (map[docker.
glog
.
Warningf
(
"Unknown protocol %q: defaulting to TCP"
,
port
.
Protocol
)
glog
.
Warningf
(
"Unknown protocol %q: defaulting to TCP"
,
port
.
Protocol
)
protocol
=
"/tcp"
protocol
=
"/tcp"
}
}
dockerPort
:=
docker
.
Port
(
strconv
.
Itoa
(
interiorPort
)
+
protocol
)
dockerPort
:=
docker
.
Port
(
strconv
.
Itoa
(
interiorPort
)
+
protocol
)
exposedPorts
[
dockerPort
]
=
struct
{}{}
exposedPorts
[
dockerPort
]
=
struct
{}{}
portBindings
[
dockerPort
]
=
[]
docker
.
PortBinding
{
{
hostBinding
:=
docker
.
PortBinding
{
HostPort
:
strconv
.
Itoa
(
exteriorPort
),
HostPort
:
strconv
.
Itoa
(
exteriorPort
),
HostIP
:
port
.
HostIP
,
HostIP
:
port
.
HostIP
,
},
}
// Allow multiple host ports bind to same docker port
if
existedBindings
,
ok
:=
portBindings
[
dockerPort
];
ok
{
// If a docker port already map to a host port, just append the host ports
portBindings
[
dockerPort
]
=
append
(
existedBindings
,
hostBinding
)
}
else
{
// Otherwise, it's fresh new port binding
portBindings
[
dockerPort
]
=
[]
docker
.
PortBinding
{
hostBinding
,
}
}
}
}
}
return
exposedPorts
,
portBindings
return
exposedPorts
,
portBindings
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment