fixed container being excluded in host network_mode

This commit is contained in:
yusing 2024-09-23 20:34:46 +08:00
parent 71e8e4a462
commit 109c2460fa
5 changed files with 36 additions and 49 deletions

2
.gitignore vendored
View file

@ -16,3 +16,5 @@ go.work.sum
!src/**/ !src/**/
todo.md todo.md
.*.swp

View file

@ -1,39 +0,0 @@
From 6728bc39d2d7ded19c1ca288691ec787c02ff666 Mon Sep 17 00:00:00 2001
From: yusing <yusing@6uo.me>
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

View file

@ -15,6 +15,8 @@ type ProxyProperties struct {
ImageName string `yaml:"-" json:"image_name"` ImageName string `yaml:"-" json:"image_name"`
PublicPortMapping PortMapping `yaml:"-" json:"public_port_mapping"` // non-zero publicPort:types.Port PublicPortMapping PortMapping `yaml:"-" json:"public_port_mapping"` // non-zero publicPort:types.Port
PrivatePortMapping PortMapping `yaml:"-" json:"private_port_mapping"` // privatePort:types.Port PrivatePortMapping PortMapping `yaml:"-" json:"private_port_mapping"` // privatePort:types.Port
NetworkMode string `yaml:"-" json:"network_mode"`
Aliases []string `yaml:"-" json:"aliases"` Aliases []string `yaml:"-" json:"aliases"`
IsExcluded bool `yaml:"-" json:"is_excluded"` IsExcluded bool `yaml:"-" json:"is_excluded"`
IdleTimeout string `yaml:"-" json:"idle_timeout"` IdleTimeout string `yaml:"-" json:"idle_timeout"`
@ -40,6 +42,7 @@ func FromDocker(c *types.Container, dockerHost string) (res Container) {
ImageName: res.getImageName(), ImageName: res.getImageName(),
PublicPortMapping: res.getPublicPortMapping(), PublicPortMapping: res.getPublicPortMapping(),
PrivatePortMapping: res.getPrivatePortMapping(), PrivatePortMapping: res.getPrivatePortMapping(),
NetworkMode: c.HostConfig.NetworkMode,
Aliases: res.getAliases(), Aliases: res.getAliases(),
IsExcluded: U.ParseBool(res.getDeleteLabel(LabelExclude)), IsExcluded: U.ParseBool(res.getDeleteLabel(LabelExclude)),
IdleTimeout: res.getDeleteLabel(LabelIdleTimeout), IdleTimeout: res.getDeleteLabel(LabelIdleTimeout),

View file

@ -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 if _, ok := e.PublicPortMapping[e.Port]; !ok { // port is not exposed, but specified
// try to fallback to first public port // try to fallback to first public port
if len(e.PublicPortMapping) == 0 { if len(e.PublicPortMapping) == 0 {

View file

@ -288,3 +288,24 @@ func TestExcludeNonExposedPort(t *testing.T) {
_, ok := entries.Load("redis") _, ok := entries.Load("redis")
ExpectFalse(t, ok) 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)
}