mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 20:52: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"
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types/container"
|
"github.com/docker/docker/api/types/container"
|
||||||
|
"github.com/docker/go-connections/nat"
|
||||||
"github.com/yusing/go-proxy/agent/pkg/agent"
|
"github.com/yusing/go-proxy/agent/pkg/agent"
|
||||||
config "github.com/yusing/go-proxy/internal/config/types"
|
config "github.com/yusing/go-proxy/internal/config/types"
|
||||||
"github.com/yusing/go-proxy/internal/logging"
|
"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 {
|
func FromInspectResponse(json container.InspectResponse, dockerHost string) *Container {
|
||||||
ports := make([]container.Port, 0)
|
ports := make([]container.Port, 0)
|
||||||
for k, bindings := range json.NetworkSettings.Ports {
|
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)
|
privPort, _ := strconv.ParseUint(privPortStr, 10, 16)
|
||||||
ports = append(ports, container.Port{
|
ports = append(ports, container.Port{
|
||||||
PrivatePort: uint16(privPort),
|
PrivatePort: uint16(privPort),
|
||||||
|
@ -137,6 +138,10 @@ func FromInspectResponse(json container.InspectResponse, dockerHost string) *Con
|
||||||
return cont
|
return cont
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Container) IsBlacklisted() bool {
|
||||||
|
return c.Image.IsBlacklisted() || c.isDatabase()
|
||||||
|
}
|
||||||
|
|
||||||
var databaseMPs = map[string]struct{}{
|
var databaseMPs = map[string]struct{}{
|
||||||
"/var/lib/postgresql/data": {},
|
"/var/lib/postgresql/data": {},
|
||||||
"/var/lib/mysql": {},
|
"/var/lib/mysql": {},
|
||||||
|
@ -163,10 +168,6 @@ func (c *Container) isDatabase() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Container) IsBlacklisted() bool {
|
|
||||||
return c.Image.IsBlacklisted() || c.isDatabase()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Container) setPublicHostname() {
|
func (c *Container) setPublicHostname() {
|
||||||
if !c.Running {
|
if !c.Running {
|
||||||
return
|
return
|
||||||
|
|
|
@ -21,17 +21,11 @@ var listOptions = container.ListOptions{
|
||||||
All: true,
|
All: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
func ListContainers(clientHost string) ([]container.Summary, error) {
|
func (c *SharedClient) ListContainers() ([]container.Summary, error) {
|
||||||
dockerClient, err := NewClient(clientHost)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer dockerClient.Close()
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeoutCause(context.Background(), 3*time.Second, errors.New("list containers timeout"))
|
ctx, cancel := context.WithTimeoutCause(context.Background(), 3*time.Second, errors.New("list containers timeout"))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
containers, err := dockerClient.ContainerList(ctx, listOptions)
|
containers, err := c.ContainerList(ctx, listOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,7 +61,13 @@ func (p *DockerProvider) NewWatcher() watcher.Watcher {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *DockerProvider) loadRoutesImpl() (route.Routes, gperr.Error) {
|
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 {
|
if err != nil {
|
||||||
return nil, gperr.Wrap(err)
|
return nil, gperr.Wrap(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue