From 109c2460fa46d17d07db38e43ff07b0796722d0d Mon Sep 17 00:00:00 2001 From: yusing Date: Mon, 23 Sep 2024 20:34:46 +0800 Subject: [PATCH] fixed container being excluded in host network_mode --- .gitignore | 4 ++- 0001-fixed-nil-dereferencing.patch | 39 ---------------------- src/docker/container.go | 19 ++++++----- src/models/raw_entry.go | 2 +- src/proxy/provider/docker_provider_test.go | 21 ++++++++++++ 5 files changed, 36 insertions(+), 49 deletions(-) delete mode 100644 0001-fixed-nil-dereferencing.patch diff --git a/.gitignore b/.gitignore index 0e3ca51d..0e090036 100755 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,6 @@ go.work.sum !src/**/ -todo.md \ No newline at end of file +todo.md + +.*.swp \ No newline at end of file diff --git a/0001-fixed-nil-dereferencing.patch b/0001-fixed-nil-dereferencing.patch deleted file mode 100644 index 12a2e84d..00000000 --- a/0001-fixed-nil-dereferencing.patch +++ /dev/null @@ -1,39 +0,0 @@ -From 6728bc39d2d7ded19c1ca288691ec787c02ff666 Mon Sep 17 00:00:00 2001 -From: yusing -Date: Mon, 23 Sep 2024 07:19:47 +0800 -Subject: [PATCH] fixed nil dereferencing - ---- - frontend | 2 +- - src/error/builder.go | 6 ++++-- - 2 files changed, 5 insertions(+), 3 deletions(-) - -diff --git a/frontend b/frontend -index d0e5963..441fd70 160000 ---- a/frontend -+++ b/frontend -@@ -1 +1 @@ --Subproject commit d0e59630d6e0beb1c22d2f242f556464a5056c1f -+Subproject commit 441fd708dbed89c59bf72a742431a78ff2f02bc3 -diff --git a/src/error/builder.go b/src/error/builder.go -index 38a1bf2..16d2816 100644 ---- a/src/error/builder.go -+++ b/src/error/builder.go -@@ -60,10 +60,12 @@ func (b Builder) Build() NestedError { - } - - func (b Builder) To(ptr *NestedError) { -- if *ptr == nil { -+ if ptr == nil { -+ return -+ } else if *ptr == nil { - *ptr = b.Build() - } else { -- **ptr = *b.Build() -+ (*ptr).With(b.Build()) - } - } - --- -2.46.1 - diff --git a/src/docker/container.go b/src/docker/container.go index eabf3287..e825e6b1 100644 --- a/src/docker/container.go +++ b/src/docker/container.go @@ -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 { @@ -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), diff --git a/src/models/raw_entry.go b/src/models/raw_entry.go index 00f3cdbd..3d3e0b68 100644 --- a/src/models/raw_entry.go +++ b/src/models/raw_entry.go @@ -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 { diff --git a/src/proxy/provider/docker_provider_test.go b/src/proxy/provider/docker_provider_test.go index 06bdd30e..353fe079 100644 --- a/src/proxy/provider/docker_provider_test.go +++ b/src/proxy/provider/docker_provider_test.go @@ -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) +}