2017-07-28 06:12:29 +08:00
|
|
|
package docker
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2017-07-28 08:18:44 +08:00
|
|
|
"crypto/tls"
|
|
|
|
|
"net/http"
|
2017-07-28 06:12:29 +08:00
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2024-01-24 00:54:50 +08:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2017-10-04 05:36:26 +08:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2024-01-24 00:54:50 +08:00
|
|
|
"github.com/docker/docker/api/types/system"
|
2021-06-21 23:07:52 +08:00
|
|
|
dockerClient "github.com/docker/docker/client"
|
2017-07-28 06:12:29 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
defaultHeaders = map[string]string{"User-Agent": "engine-api-cli-1.0"}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Client interface {
|
2024-01-24 00:54:50 +08:00
|
|
|
Info(ctx context.Context) (system.Info, error)
|
|
|
|
|
ContainerList(ctx context.Context, options container.ListOptions) ([]types.Container, error)
|
2024-07-31 23:54:08 +08:00
|
|
|
ContainerStats(ctx context.Context, containerID string, stream bool) (container.StatsResponseReader, error)
|
2017-07-28 06:12:29 +08:00
|
|
|
ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error)
|
2017-10-04 05:36:26 +08:00
|
|
|
ServiceList(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error)
|
|
|
|
|
TaskList(ctx context.Context, options types.TaskListOptions) ([]swarm.Task, error)
|
|
|
|
|
NodeList(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error)
|
2023-09-28 04:28:36 +08:00
|
|
|
DiskUsage(ctx context.Context, options types.DiskUsageOptions) (types.DiskUsage, error)
|
|
|
|
|
ClientVersion() string
|
2021-06-15 12:23:39 +08:00
|
|
|
Close() error
|
2017-07-28 06:12:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewEnvClient() (Client, error) {
|
2021-06-21 23:07:52 +08:00
|
|
|
client, err := dockerClient.NewClientWithOpts(dockerClient.FromEnv)
|
2017-07-28 06:12:29 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &SocketClient{client}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-28 08:18:44 +08:00
|
|
|
func NewClient(host string, tlsConfig *tls.Config) (Client, error) {
|
|
|
|
|
transport := &http.Transport{
|
|
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
|
}
|
|
|
|
|
httpClient := &http.Client{Transport: transport}
|
|
|
|
|
|
2021-06-21 23:07:52 +08:00
|
|
|
client, err := dockerClient.NewClientWithOpts(
|
|
|
|
|
dockerClient.WithHTTPHeaders(defaultHeaders),
|
|
|
|
|
dockerClient.WithHTTPClient(httpClient),
|
2023-09-28 04:28:36 +08:00
|
|
|
dockerClient.WithAPIVersionNegotiation(),
|
2021-06-21 23:07:52 +08:00
|
|
|
dockerClient.WithHost(host))
|
2017-07-28 06:12:29 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2018-07-26 07:10:28 +08:00
|
|
|
|
2017-07-28 06:12:29 +08:00
|
|
|
return &SocketClient{client}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SocketClient struct {
|
2021-06-21 23:07:52 +08:00
|
|
|
client *dockerClient.Client
|
2017-07-28 06:12:29 +08:00
|
|
|
}
|
|
|
|
|
|
2024-01-24 00:54:50 +08:00
|
|
|
func (c *SocketClient) Info(ctx context.Context) (system.Info, error) {
|
2017-07-28 06:12:29 +08:00
|
|
|
return c.client.Info(ctx)
|
|
|
|
|
}
|
2024-01-24 00:54:50 +08:00
|
|
|
func (c *SocketClient) ContainerList(ctx context.Context, options container.ListOptions) ([]types.Container, error) {
|
2017-07-28 06:12:29 +08:00
|
|
|
return c.client.ContainerList(ctx, options)
|
|
|
|
|
}
|
2024-07-31 23:54:08 +08:00
|
|
|
func (c *SocketClient) ContainerStats(ctx context.Context, containerID string, stream bool) (container.StatsResponseReader, error) {
|
2017-07-28 06:12:29 +08:00
|
|
|
return c.client.ContainerStats(ctx, containerID, stream)
|
|
|
|
|
}
|
|
|
|
|
func (c *SocketClient) ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error) {
|
|
|
|
|
return c.client.ContainerInspect(ctx, containerID)
|
|
|
|
|
}
|
2017-10-04 05:36:26 +08:00
|
|
|
func (c *SocketClient) ServiceList(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) {
|
|
|
|
|
return c.client.ServiceList(ctx, options)
|
|
|
|
|
}
|
|
|
|
|
func (c *SocketClient) TaskList(ctx context.Context, options types.TaskListOptions) ([]swarm.Task, error) {
|
|
|
|
|
return c.client.TaskList(ctx, options)
|
|
|
|
|
}
|
|
|
|
|
func (c *SocketClient) NodeList(ctx context.Context, options types.NodeListOptions) ([]swarm.Node, error) {
|
|
|
|
|
return c.client.NodeList(ctx, options)
|
|
|
|
|
}
|
2023-09-28 04:28:36 +08:00
|
|
|
func (c *SocketClient) DiskUsage(ctx context.Context, options types.DiskUsageOptions) (types.DiskUsage, error) {
|
|
|
|
|
return c.client.DiskUsage(ctx, options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *SocketClient) ClientVersion() string {
|
|
|
|
|
return c.client.ClientVersion()
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-15 12:23:39 +08:00
|
|
|
func (c *SocketClient) Close() error {
|
|
|
|
|
return c.client.Close()
|
|
|
|
|
}
|