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()))
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

View file

@ -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

View file

@ -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()
}

View file

@ -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)
})
}

View file

@ -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)
}