mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 04:42:33 +02:00
docker: refactor container related code
This commit is contained in:
parent
1739afae24
commit
1078731f2d
3 changed files with 15 additions and 14 deletions
|
@ -6,6 +6,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/go-connections/nat"
|
||||
"github.com/yusing/go-proxy/agent/pkg/agent"
|
||||
config "github.com/yusing/go-proxy/internal/config/types"
|
||||
"github.com/yusing/go-proxy/internal/logging"
|
||||
|
@ -105,7 +106,7 @@ func FromDocker(c *container.Summary, dockerHost string) (res *Container) {
|
|||
func FromInspectResponse(json container.InspectResponse, dockerHost string) *Container {
|
||||
ports := make([]container.Port, 0)
|
||||
for k, bindings := range json.NetworkSettings.Ports {
|
||||
privPortStr, proto := k.Port(), k.Proto()
|
||||
proto, privPortStr := nat.SplitProtoPort(string(k))
|
||||
privPort, _ := strconv.ParseUint(privPortStr, 10, 16)
|
||||
ports = append(ports, container.Port{
|
||||
PrivatePort: uint16(privPort),
|
||||
|
@ -137,6 +138,10 @@ func FromInspectResponse(json container.InspectResponse, dockerHost string) *Con
|
|||
return cont
|
||||
}
|
||||
|
||||
func (c *Container) IsBlacklisted() bool {
|
||||
return c.Image.IsBlacklisted() || c.isDatabase()
|
||||
}
|
||||
|
||||
var databaseMPs = map[string]struct{}{
|
||||
"/var/lib/postgresql/data": {},
|
||||
"/var/lib/mysql": {},
|
||||
|
@ -163,10 +168,6 @@ func (c *Container) isDatabase() bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func (c *Container) IsBlacklisted() bool {
|
||||
return c.Image.IsBlacklisted() || c.isDatabase()
|
||||
}
|
||||
|
||||
func (c *Container) setPublicHostname() {
|
||||
if !c.Running {
|
||||
return
|
||||
|
|
|
@ -21,17 +21,11 @@ var listOptions = container.ListOptions{
|
|||
All: true,
|
||||
}
|
||||
|
||||
func ListContainers(clientHost string) ([]container.Summary, error) {
|
||||
dockerClient, err := NewClient(clientHost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer dockerClient.Close()
|
||||
|
||||
func (c *SharedClient) ListContainers() ([]container.Summary, error) {
|
||||
ctx, cancel := context.WithTimeoutCause(context.Background(), 3*time.Second, errors.New("list containers timeout"))
|
||||
defer cancel()
|
||||
|
||||
containers, err := dockerClient.ContainerList(ctx, listOptions)
|
||||
containers, err := c.ContainerList(ctx, listOptions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -61,7 +61,13 @@ func (p *DockerProvider) NewWatcher() watcher.Watcher {
|
|||
}
|
||||
|
||||
func (p *DockerProvider) loadRoutesImpl() (route.Routes, gperr.Error) {
|
||||
containers, err := docker.ListContainers(p.dockerHost)
|
||||
dockerClient, err := docker.NewClient(p.dockerHost)
|
||||
if err != nil {
|
||||
return nil, gperr.Wrap(err)
|
||||
}
|
||||
defer dockerClient.Close()
|
||||
|
||||
containers, err := dockerClient.ListContainers()
|
||||
if err != nil {
|
||||
return nil, gperr.Wrap(err)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue