Skip to content

Commit

Permalink
fixed container being excluded in host network_mode
Browse files Browse the repository at this point in the history
  • Loading branch information
yusing committed Sep 23, 2024
1 parent 71e8e4a commit 109c246
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 49 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ go.work.sum

!src/**/

todo.md
todo.md

.*.swp
39 changes: 0 additions & 39 deletions 0001-fixed-nil-dereferencing.patch

This file was deleted.

19 changes: 11 additions & 8 deletions src/docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ type ProxyProperties struct {
ImageName string `yaml:"-" json:"image_name"`
PublicPortMapping PortMapping `yaml:"-" json:"public_port_mapping"` // non-zero publicPort:types.Port
PrivatePortMapping PortMapping `yaml:"-" json:"private_port_mapping"` // privatePort:types.Port
Aliases []string `yaml:"-" json:"aliases"`
IsExcluded bool `yaml:"-" json:"is_excluded"`
IdleTimeout string `yaml:"-" json:"idle_timeout"`
WakeTimeout string `yaml:"-" json:"wake_timeout"`
StopMethod string `yaml:"-" json:"stop_method"`
StopTimeout string `yaml:"-" json:"stop_timeout"` // stop_method = "stop" only
StopSignal string `yaml:"-" json:"stop_signal"` // stop_method = "stop" | "kill" only
Running bool `yaml:"-" json:"running"`
NetworkMode string `yaml:"-" json:"network_mode"`

Aliases []string `yaml:"-" json:"aliases"`
IsExcluded bool `yaml:"-" json:"is_excluded"`
IdleTimeout string `yaml:"-" json:"idle_timeout"`
WakeTimeout string `yaml:"-" json:"wake_timeout"`
StopMethod string `yaml:"-" json:"stop_method"`
StopTimeout string `yaml:"-" json:"stop_timeout"` // stop_method = "stop" only
StopSignal string `yaml:"-" json:"stop_signal"` // stop_method = "stop" | "kill" only
Running bool `yaml:"-" json:"running"`
}

type Container struct {
Expand All @@ -40,6 +42,7 @@ func FromDocker(c *types.Container, dockerHost string) (res Container) {
ImageName: res.getImageName(),
PublicPortMapping: res.getPublicPortMapping(),
PrivatePortMapping: res.getPrivatePortMapping(),
NetworkMode: c.HostConfig.NetworkMode,
Aliases: res.getAliases(),
IsExcluded: U.ParseBool(res.getDeleteLabel(LabelExclude)),
IdleTimeout: res.getDeleteLabel(LabelIdleTimeout),
Expand Down
2 changes: 1 addition & 1 deletion src/models/raw_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (e *RawEntry) FillMissingFields() bool {
}
}

if e.PublicPortMapping != nil {
if e.PublicPortMapping != nil && e.NetworkMode != "host" {
if _, ok := e.PublicPortMapping[e.Port]; !ok { // port is not exposed, but specified
// try to fallback to first public port
if len(e.PublicPortMapping) == 0 {
Expand Down
21 changes: 21 additions & 0 deletions src/proxy/provider/docker_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,24 @@ func TestExcludeNonExposedPort(t *testing.T) {
_, ok := entries.Load("redis")
ExpectFalse(t, ok)
}

func TestNotExcludeNonExposedPortHostNetwork(t *testing.T) {
var p DockerProvider
cont := &types.Container{
Image: "redis",
Names: []string{"redis"},
Ports: []types.Port{
{Type: "tcp", PrivatePort: 6379, PublicPort: 0}, // not exposed
},
Labels: map[string]string{
"proxy.redis.port": "6379:6379", // should be excluded even specified
},
}
cont.HostConfig.NetworkMode = "host"

entries, err := p.entriesFromContainerLabels(D.FromDocker(cont, ""))
ExpectNoError(t, err.Error())

_, ok := entries.Load("redis")
ExpectTrue(t, ok)
}

0 comments on commit 109c246

Please sign in to comment.