replace deprecated docker types

This commit is contained in:
yusing 2025-02-24 06:10:46 +08:00
parent baebede816
commit 080c1cee4f
3 changed files with 16 additions and 14 deletions

View file

@ -5,7 +5,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container"
"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"
@ -14,7 +14,7 @@ import (
) )
type ( type (
PortMapping = map[int]types.Port PortMapping = map[int]container.Port
Container struct { Container struct {
_ U.NoCopy _ U.NoCopy
@ -35,7 +35,6 @@ type (
Aliases []string `json:"aliases"` Aliases []string `json:"aliases"`
IsExcluded bool `json:"is_excluded"` IsExcluded bool `json:"is_excluded"`
IsExplicit bool `json:"is_explicit"` IsExplicit bool `json:"is_explicit"`
IsDatabase bool `json:"is_database"`
IdleTimeout string `json:"idle_timeout,omitempty"` IdleTimeout string `json:"idle_timeout,omitempty"`
WakeTimeout string `json:"wake_timeout,omitempty"` WakeTimeout string `json:"wake_timeout,omitempty"`
StopMethod string `json:"stop_method,omitempty"` StopMethod string `json:"stop_method,omitempty"`
@ -43,12 +42,14 @@ 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
} }
) )
var DummyContainer = new(Container) var DummyContainer = new(Container)
func FromDocker(c *types.Container, 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 { for lbl := range c.Labels {
@ -72,7 +73,6 @@ func FromDocker(c *types.Container, dockerHost string) (res *Container) {
Aliases: helper.getAliases(), Aliases: helper.getAliases(),
IsExcluded: strutils.ParseBool(helper.getDeleteLabel(LabelExclude)), IsExcluded: strutils.ParseBool(helper.getDeleteLabel(LabelExclude)),
IsExplicit: isExplicit, IsExplicit: isExplicit,
IsDatabase: helper.isDatabase(),
IdleTimeout: helper.getDeleteLabel(LabelIdleTimeout), IdleTimeout: helper.getDeleteLabel(LabelIdleTimeout),
WakeTimeout: helper.getDeleteLabel(LabelWakeTimeout), WakeTimeout: helper.getDeleteLabel(LabelWakeTimeout),
StopMethod: helper.getDeleteLabel(LabelStopMethod), StopMethod: helper.getDeleteLabel(LabelStopMethod),
@ -80,6 +80,8 @@ func FromDocker(c *types.Container, 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) {
@ -95,18 +97,18 @@ func FromDocker(c *types.Container, dockerHost string) (res *Container) {
return return
} }
func FromJSON(json types.ContainerJSON, dockerHost string) *Container { func FromInspectResponse(json container.InspectResponse, dockerHost string) *Container {
ports := make([]types.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() privPortStr, proto := k.Port(), k.Proto()
privPort, _ := strconv.ParseUint(privPortStr, 10, 16) privPort, _ := strconv.ParseUint(privPortStr, 10, 16)
ports = append(ports, types.Port{ ports = append(ports, container.Port{
PrivatePort: uint16(privPort), PrivatePort: uint16(privPort),
Type: proto, Type: proto,
}) })
for _, v := range bindings { for _, v := range bindings {
pubPort, _ := strconv.ParseUint(v.HostPort, 10, 16) pubPort, _ := strconv.ParseUint(v.HostPort, 10, 16)
ports = append(ports, types.Port{ ports = append(ports, container.Port{
IP: v.HostIP, IP: v.HostIP,
PublicPort: uint16(pubPort), PublicPort: uint16(pubPort),
PrivatePort: uint16(privPort), PrivatePort: uint16(privPort),
@ -114,7 +116,7 @@ func FromJSON(json types.ContainerJSON, dockerHost string) *Container {
}) })
} }
} }
cont := FromDocker(&types.Container{ cont := FromDocker(&container.Summary{
ID: json.ID, ID: json.ID,
Names: []string{strings.TrimPrefix(json.Name, "/")}, Names: []string{strings.TrimPrefix(json.Name, "/")},
Image: json.Image, Image: json.Image,
@ -123,7 +125,7 @@ func FromJSON(json types.ContainerJSON, dockerHost string) *Container {
State: json.State.Status, State: json.State.Status,
Status: json.State.Status, Status: json.State.Status,
Mounts: json.Mounts, Mounts: json.Mounts,
NetworkSettings: &types.SummaryNetworkSettings{ NetworkSettings: &container.NetworkSettingsSummary{
Networks: json.NetworkSettings.Networks, Networks: json.NetworkSettings.Networks,
}, },
}, dockerHost) }, dockerHost)

View file

@ -3,12 +3,12 @@ package docker
import ( import (
"strings" "strings"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container"
"github.com/yusing/go-proxy/internal/utils/strutils" "github.com/yusing/go-proxy/internal/utils/strutils"
) )
type containerHelper struct { type containerHelper struct {
*types.Container *container.Summary
} }
// 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.

View file

@ -24,5 +24,5 @@ func (c *SharedClient) Inspect(containerID string) (*Container, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return FromJSON(json, c.key), nil return FromInspectResponse(json, c.key), nil
} }