fix empty homepage name, incorrect image parsing, refactor

This commit is contained in:
yusing 2025-02-24 08:42:10 +08:00
parent bda547198e
commit 1d16d514c7
5 changed files with 27 additions and 37 deletions

View file

@ -190,7 +190,7 @@ func findIcon(r route.HTTPRoute, req *http.Request, uri string) *fetchResult {
result := fetchIcon("png", sanitizeName(r.TargetName())) result := fetchIcon("png", sanitizeName(r.TargetName()))
cont := r.ContainerInfo() cont := r.ContainerInfo()
if !result.OK() && cont != nil { if !result.OK() && cont != nil {
result = fetchIcon("png", sanitizeName(cont.ImageName)) result = fetchIcon("png", sanitizeName(cont.Image.Name))
} }
if !result.OK() { if !result.OK() {
// fallback to parse html // fallback to parse html

View file

@ -19,9 +19,9 @@ type (
_ U.NoCopy _ U.NoCopy
DockerHost string `json:"docker_host"` DockerHost string `json:"docker_host"`
Image *ContainerImage `json:"image"`
ContainerName string `json:"container_name"` ContainerName string `json:"container_name"`
ContainerID string `json:"container_id"` ContainerID string `json:"container_id"`
ImageName string `json:"image_name"`
Agent *agent.AgentConfig `json:"agent"` Agent *agent.AgentConfig `json:"agent"`
@ -42,8 +42,11 @@ type (
StopSignal string `json:"stop_signal,omitempty"` // stop_method = "stop" | "kill" only StopSignal string `json:"stop_signal,omitempty"` // stop_method = "stop" | "kill" only
StartEndpoint string `json:"start_endpoint,omitempty"` StartEndpoint string `json:"start_endpoint,omitempty"`
Running bool `json:"running"` 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) { func FromDocker(c *container.Summary, dockerHost string) (res *Container) {
isExplicit := false isExplicit := false
helper := containerHelper{Summary: c} helper := containerHelper{c}
for lbl := range c.Labels { for lbl := range c.Labels {
if strings.HasPrefix(lbl, NSProxy+".") { if strings.HasPrefix(lbl, NSProxy+".") {
isExplicit = true isExplicit = true
@ -61,6 +64,7 @@ func FromDocker(c *container.Summary, dockerHost string) (res *Container) {
} }
res = &Container{ res = &Container{
DockerHost: dockerHost, DockerHost: dockerHost,
Image: helper.parseImage(),
ContainerName: helper.getName(), ContainerName: helper.getName(),
ContainerID: c.ID, ContainerID: c.ID,
@ -79,8 +83,6 @@ func FromDocker(c *container.Summary, dockerHost string) (res *Container) {
StopSignal: helper.getDeleteLabel(LabelStopSignal), StopSignal: helper.getDeleteLabel(LabelStopSignal),
StartEndpoint: helper.getDeleteLabel(LabelStartEndpoint), StartEndpoint: helper.getDeleteLabel(LabelStartEndpoint),
Running: c.Status == "running" || c.State == "running", Running: c.Status == "running" || c.State == "running",
containerHelper: helper,
} }
if agent.IsDockerHostAgent(dockerHost) { if agent.IsDockerHostAgent(dockerHost) {
@ -131,6 +133,10 @@ func FromInspectResponse(json container.InspectResponse, dockerHost string) *Con
return cont return cont
} }
func (c *Container) IsBlacklisted() bool {
return c.Image.IsBlacklisted()
}
func (c *Container) setPublicHostname() { func (c *Container) setPublicHostname() {
if !c.Running { if !c.Running {
return return

View file

@ -7,17 +7,9 @@ import (
"github.com/yusing/go-proxy/internal/utils/strutils" "github.com/yusing/go-proxy/internal/utils/strutils"
) )
type ( type containerHelper struct {
containerHelper struct {
*container.Summary *container.Summary
image *ContainerImage
} }
ContainerImage struct {
Author string
Name string
Tag string
}
)
// getDeleteLabel gets the value of a label and then deletes it from the container. // 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. // 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], '/') slashSep := strutils.SplitRune(colonSep[0], '/')
im := new(ContainerImage) im := new(ContainerImage)
if len(slashSep) > 1 { if len(slashSep) > 1 {
im.Author = slashSep[len(slashSep)-1] im.Author = strings.Join(slashSep[:len(slashSep)-1], "/")
im.Name = strings.Join(slashSep[:len(slashSep)-1], "/") im.Name = slashSep[len(slashSep)-1]
} else { } else {
im.Author = "library" im.Name = slashSep[0]
im.Name = colonSep[0]
} }
if len(colonSep) > 1 { if len(colonSep) > 1 {
im.Tag = colonSep[1] im.Tag = colonSep[1]
@ -75,10 +66,3 @@ func (c containerHelper) getPrivatePortMapping() PortMapping {
} }
return res return res
} }
func (c containerHelper) IsBlacklisted() bool {
if c.image == nil {
c.image = c.parseImage()
}
return c.image.IsBlacklisted()
}

View file

@ -3,7 +3,7 @@ package docker
import ( import (
"testing" "testing"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container"
. "github.com/yusing/go-proxy/internal/utils/testing" . "github.com/yusing/go-proxy/internal/utils/testing"
) )
@ -36,7 +36,7 @@ func TestContainerExplicit(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { 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) ExpectEqual(t, c.IsExplicit, tt.isExplicit)
}) })
} }

View file

@ -267,7 +267,7 @@ func (r *Route) Finalize() {
lp, pp := r.Port.Listening, r.Port.Proxy lp, pp := r.Port.Listening, r.Port.Proxy
if isDocker { if isDocker {
scheme, port, ok := getSchemePortByImageName(cont.ImageName, pp) scheme, port, ok := getSchemePortByImageName(cont.Image.Name, pp)
if ok { if ok {
if r.Scheme == "" { if r.Scheme == "" {
r.Scheme = types.Scheme(scheme) r.Scheme = types.Scheme(scheme)
@ -381,7 +381,7 @@ func (r *Route) FinalizeHomepageConfig() {
var key string var key string
if hp.Name == "" { if hp.Name == "" {
if r.Container != nil { if r.Container != nil {
key = r.Container.ImageName key = r.Container.Image.Name
} else { } else {
key = r.Alias key = r.Alias
} }
@ -401,7 +401,7 @@ func (r *Route) FinalizeHomepageConfig() {
if hp.Category == "" { if hp.Category == "" {
if config.GetInstance().Value().Homepage.UseDefaultCategories { if config.GetInstance().Value().Homepage.UseDefaultCategories {
if isDocker { if isDocker {
key = r.Container.ImageName key = r.Container.Image.Name
} else { } else {
key = strings.ToLower(r.Alias) key = strings.ToLower(r.Alias)
} }