mirror of
https://github.com/yusing/godoxy.git
synced 2025-07-06 06:24:03 +02:00
chore: reduce memory usage of docker container info
This commit is contained in:
parent
49ee9c908a
commit
5d2b700cb2
3 changed files with 25 additions and 15 deletions
|
@ -15,7 +15,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
PortMapping = map[int]container.Port
|
PortMapping = map[int]*container.Port
|
||||||
Container struct {
|
Container struct {
|
||||||
_ U.NoCopy
|
_ U.NoCopy
|
||||||
|
|
||||||
|
@ -58,21 +58,12 @@ 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{c}
|
helper := containerHelper{c}
|
||||||
for lbl := range c.Labels {
|
|
||||||
if strings.HasPrefix(lbl, NSProxy+".") {
|
|
||||||
isExplicit = true
|
|
||||||
} else {
|
|
||||||
delete(c.Labels, lbl)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
res = &Container{
|
res = &Container{
|
||||||
DockerHost: dockerHost,
|
DockerHost: dockerHost,
|
||||||
Image: helper.parseImage(),
|
Image: helper.parseImage(),
|
||||||
ContainerName: helper.getName(),
|
ContainerName: helper.getName(),
|
||||||
ContainerID: c.ID,
|
ContainerID: c.ID,
|
||||||
|
|
||||||
Labels: c.Labels,
|
|
||||||
|
|
||||||
Mounts: helper.getMounts(),
|
Mounts: helper.getMounts(),
|
||||||
|
|
||||||
PublicPortMapping: helper.getPublicPortMapping(),
|
PublicPortMapping: helper.getPublicPortMapping(),
|
||||||
|
@ -100,11 +91,20 @@ func FromDocker(c *container.Summary, dockerHost string) (res *Container) {
|
||||||
|
|
||||||
res.setPrivateHostname(helper)
|
res.setPrivateHostname(helper)
|
||||||
res.setPublicHostname()
|
res.setPublicHostname()
|
||||||
|
|
||||||
|
for lbl := range c.Labels {
|
||||||
|
if strings.HasPrefix(lbl, NSProxy+".") {
|
||||||
|
isExplicit = true
|
||||||
|
} else {
|
||||||
|
delete(c.Labels, lbl)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res.RouteConfig = utils.FitMap(c.Labels)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func FromInspectResponse(json container.InspectResponse, dockerHost string) *Container {
|
func FromInspectResponse(json container.InspectResponse, dockerHost string) *Container {
|
||||||
ports := make([]container.Port, 0)
|
ports := make([]container.Port, 0, len(json.NetworkSettings.Ports))
|
||||||
for k, bindings := range json.NetworkSettings.Ports {
|
for k, bindings := range json.NetworkSettings.Ports {
|
||||||
proto, privPortStr := nat.SplitProtoPort(string(k))
|
proto, privPortStr := nat.SplitProtoPort(string(k))
|
||||||
privPort, _ := strconv.ParseUint(privPortStr, 10, 16)
|
privPort, _ := strconv.ParseUint(privPortStr, 10, 16)
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types/container"
|
"github.com/docker/docker/api/types/container"
|
||||||
|
"github.com/yusing/go-proxy/internal/utils"
|
||||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -62,15 +63,15 @@ func (c containerHelper) getPublicPortMapping() PortMapping {
|
||||||
if v.PublicPort == 0 {
|
if v.PublicPort == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
res[int(v.PublicPort)] = v
|
res[int(v.PublicPort)] = &v
|
||||||
}
|
}
|
||||||
return res
|
return utils.FitMap(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c containerHelper) getPrivatePortMapping() PortMapping {
|
func (c containerHelper) getPrivatePortMapping() PortMapping {
|
||||||
res := make(PortMapping)
|
res := make(PortMapping, len(c.Ports))
|
||||||
for _, v := range c.Ports {
|
for _, v := range c.Ports {
|
||||||
res[int(v.PrivatePort)] = v
|
res[int(v.PrivatePort)] = &v
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
9
internal/utils/maps.go
Normal file
9
internal/utils/maps.go
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
func FitMap[K comparable, V any](m map[K]V) map[K]V {
|
||||||
|
res := make(map[K]V, len(m))
|
||||||
|
for k, v := range m {
|
||||||
|
res[k] = v
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue