From 1d16d514c7b01e4711f12b722f87d0316131d2d5 Mon Sep 17 00:00:00 2001 From: yusing Date: Mon, 24 Feb 2025 08:42:10 +0800 Subject: [PATCH] fix empty homepage name, incorrect image parsing, refactor --- internal/api/v1/favicon/favicon.go | 2 +- internal/docker/container.go | 24 +++++++++++++++--------- internal/docker/container_helper.go | 28 ++++++---------------------- internal/docker/container_test.go | 4 ++-- internal/route/route.go | 6 +++--- 5 files changed, 27 insertions(+), 37 deletions(-) diff --git a/internal/api/v1/favicon/favicon.go b/internal/api/v1/favicon/favicon.go index 20fa986..c4e0ffd 100644 --- a/internal/api/v1/favicon/favicon.go +++ b/internal/api/v1/favicon/favicon.go @@ -190,7 +190,7 @@ func findIcon(r route.HTTPRoute, req *http.Request, uri string) *fetchResult { result := fetchIcon("png", sanitizeName(r.TargetName())) cont := r.ContainerInfo() if !result.OK() && cont != nil { - result = fetchIcon("png", sanitizeName(cont.ImageName)) + result = fetchIcon("png", sanitizeName(cont.Image.Name)) } if !result.OK() { // fallback to parse html diff --git a/internal/docker/container.go b/internal/docker/container.go index ac93325..5e66d80 100644 --- a/internal/docker/container.go +++ b/internal/docker/container.go @@ -18,10 +18,10 @@ type ( Container struct { _ U.NoCopy - DockerHost string `json:"docker_host"` - ContainerName string `json:"container_name"` - ContainerID string `json:"container_id"` - ImageName string `json:"image_name"` + DockerHost string `json:"docker_host"` + Image *ContainerImage `json:"image"` + ContainerName string `json:"container_name"` + ContainerID string `json:"container_id"` Agent *agent.AgentConfig `json:"agent"` @@ -42,8 +42,11 @@ type ( StopSignal string `json:"stop_signal,omitempty"` // stop_method = "stop" | "kill" only StartEndpoint string `json:"start_endpoint,omitempty"` Running bool `json:"running"` - - containerHelper + } + ContainerImage struct { + Author string `json:"author,omitempty"` + Name string `json:"name"` + Tag string `json:"tag,omitempty"` } ) @@ -51,7 +54,7 @@ var DummyContainer = new(Container) func FromDocker(c *container.Summary, dockerHost string) (res *Container) { isExplicit := false - helper := containerHelper{Summary: c} + helper := containerHelper{c} for lbl := range c.Labels { if strings.HasPrefix(lbl, NSProxy+".") { isExplicit = true @@ -61,6 +64,7 @@ func FromDocker(c *container.Summary, dockerHost string) (res *Container) { } res = &Container{ DockerHost: dockerHost, + Image: helper.parseImage(), ContainerName: helper.getName(), ContainerID: c.ID, @@ -79,8 +83,6 @@ func FromDocker(c *container.Summary, dockerHost string) (res *Container) { StopSignal: helper.getDeleteLabel(LabelStopSignal), StartEndpoint: helper.getDeleteLabel(LabelStartEndpoint), Running: c.Status == "running" || c.State == "running", - - containerHelper: helper, } if agent.IsDockerHostAgent(dockerHost) { @@ -131,6 +133,10 @@ func FromInspectResponse(json container.InspectResponse, dockerHost string) *Con return cont } +func (c *Container) IsBlacklisted() bool { + return c.Image.IsBlacklisted() +} + func (c *Container) setPublicHostname() { if !c.Running { return diff --git a/internal/docker/container_helper.go b/internal/docker/container_helper.go index d78cd21..c6366fe 100644 --- a/internal/docker/container_helper.go +++ b/internal/docker/container_helper.go @@ -7,17 +7,9 @@ import ( "github.com/yusing/go-proxy/internal/utils/strutils" ) -type ( - containerHelper struct { - *container.Summary - image *ContainerImage - } - ContainerImage struct { - Author string - Name string - Tag string - } -) +type containerHelper struct { + *container.Summary +} // getDeleteLabel gets the value of a label and then deletes it from the container. // If the label does not exist, an empty string is returned. @@ -45,11 +37,10 @@ func (c containerHelper) parseImage() *ContainerImage { slashSep := strutils.SplitRune(colonSep[0], '/') im := new(ContainerImage) if len(slashSep) > 1 { - im.Author = slashSep[len(slashSep)-1] - im.Name = strings.Join(slashSep[:len(slashSep)-1], "/") + im.Author = strings.Join(slashSep[:len(slashSep)-1], "/") + im.Name = slashSep[len(slashSep)-1] } else { - im.Author = "library" - im.Name = colonSep[0] + im.Name = slashSep[0] } if len(colonSep) > 1 { im.Tag = colonSep[1] @@ -75,10 +66,3 @@ func (c containerHelper) getPrivatePortMapping() PortMapping { } return res } - -func (c containerHelper) IsBlacklisted() bool { - if c.image == nil { - c.image = c.parseImage() - } - return c.image.IsBlacklisted() -} diff --git a/internal/docker/container_test.go b/internal/docker/container_test.go index 4ecd475..753ebac 100644 --- a/internal/docker/container_test.go +++ b/internal/docker/container_test.go @@ -3,7 +3,7 @@ package docker import ( "testing" - "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/container" . "github.com/yusing/go-proxy/internal/utils/testing" ) @@ -36,7 +36,7 @@ func TestContainerExplicit(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - c := FromDocker(&types.Container{Names: []string{"test"}, State: "test", Labels: tt.labels}, "") + c := FromDocker(&container.Summary{Names: []string{"test"}, State: "test", Labels: tt.labels}, "") ExpectEqual(t, c.IsExplicit, tt.isExplicit) }) } diff --git a/internal/route/route.go b/internal/route/route.go index 6902341..897f2a9 100644 --- a/internal/route/route.go +++ b/internal/route/route.go @@ -267,7 +267,7 @@ func (r *Route) Finalize() { lp, pp := r.Port.Listening, r.Port.Proxy if isDocker { - scheme, port, ok := getSchemePortByImageName(cont.ImageName, pp) + scheme, port, ok := getSchemePortByImageName(cont.Image.Name, pp) if ok { if r.Scheme == "" { r.Scheme = types.Scheme(scheme) @@ -381,7 +381,7 @@ func (r *Route) FinalizeHomepageConfig() { var key string if hp.Name == "" { if r.Container != nil { - key = r.Container.ImageName + key = r.Container.Image.Name } else { key = r.Alias } @@ -401,7 +401,7 @@ func (r *Route) FinalizeHomepageConfig() { if hp.Category == "" { if config.GetInstance().Value().Homepage.UseDefaultCategories { if isDocker { - key = r.Container.ImageName + key = r.Container.Image.Name } else { key = strings.ToLower(r.Alias) }