mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-19 20:32:35 +02:00
fix(docker): host network_mode port selection
This commit is contained in:
parent
4daefa19d1
commit
1e80ad2a44
3 changed files with 48 additions and 6 deletions
|
@ -7,6 +7,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/go-connections/nat"
|
||||
"github.com/yusing/go-proxy/agent/pkg/agent"
|
||||
config "github.com/yusing/go-proxy/internal/config/types"
|
||||
"github.com/yusing/go-proxy/internal/gperr"
|
||||
|
@ -218,13 +219,15 @@ func (c *Container) UpdatePorts() error {
|
|||
}
|
||||
|
||||
for port := range inspect.Config.ExposedPorts {
|
||||
if port.Int() == 0 {
|
||||
proto, portStr := nat.SplitProtoPort(string(port))
|
||||
portInt, _ := nat.ParsePort(portStr)
|
||||
if portInt == 0 {
|
||||
continue
|
||||
}
|
||||
c.PublicPortMapping[port.Int()] = container.Port{
|
||||
PublicPort: uint16(port.Int()),
|
||||
PrivatePort: uint16(port.Int()),
|
||||
Type: port.Proto(),
|
||||
c.PublicPortMapping[portInt] = container.Port{
|
||||
PublicPort: uint16(portInt),
|
||||
PrivatePort: uint16(portInt),
|
||||
Type: proto,
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
|
|
@ -41,3 +41,38 @@ func TestContainerExplicit(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainerHostNetworkMode(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
container *container.SummaryTrimmed
|
||||
isHostNetworkMode bool
|
||||
}{
|
||||
{
|
||||
name: "host network mode",
|
||||
container: &container.SummaryTrimmed{
|
||||
Names: []string{"test"},
|
||||
State: "test",
|
||||
HostConfig: struct {
|
||||
NetworkMode string "json:\",omitempty\""
|
||||
}{
|
||||
NetworkMode: "host",
|
||||
},
|
||||
},
|
||||
isHostNetworkMode: true,
|
||||
},
|
||||
{
|
||||
name: "not host network mode",
|
||||
container: &container.SummaryTrimmed{
|
||||
Names: []string{"test"},
|
||||
State: "test",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := FromDocker(tt.container, "")
|
||||
ExpectEqual(t, c.IsHostNetworkMode, tt.isHostNetworkMode)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -409,7 +409,11 @@ func (r *Route) Finalize() {
|
|||
if pp == 0 {
|
||||
switch {
|
||||
case isDocker:
|
||||
pp = preferredPort(cont.PrivatePortMapping)
|
||||
if cont.IsHostNetworkMode {
|
||||
pp = preferredPort(cont.PublicPortMapping)
|
||||
} else {
|
||||
pp = preferredPort(cont.PrivatePortMapping)
|
||||
}
|
||||
case r.Scheme == "https":
|
||||
pp = 443
|
||||
default:
|
||||
|
|
Loading…
Add table
Reference in a new issue