/
client.go
60 lines (51 loc) · 1.46 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package dockerapi
import (
"context"
"log/slog"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
// Client is a wrapper around the Docker client.
type Client struct {
client *client.Client
}
// NewClient returns a new Client.
func NewClient() (*Client, error) {
c, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return nil, err
}
return &Client{c}, nil
}
// LogValue logs customizable properties of the Docker client.
// These properties can be customized by setting environment variables.
// Read: client.FromEnv.
func (c Client) LogValue() slog.Value {
return slog.GroupValue(
slog.String("host", c.client.DaemonHost()),
slog.String("apiVersion", c.client.ClientVersion()))
}
// ContainerList returns the list of containers from the Docker client.
func (c *Client) ContainerList() ([]Container, error) {
clientContainers, err := c.client.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
return nil, err
}
containers := make([]Container, len(clientContainers))
for i, container := range clientContainers {
names := container.Names
networks := make(map[string]IPAddress)
for network, endpoint := range container.NetworkSettings.Networks {
networks[network] = IPAddress(endpoint.IPAddress)
}
containers[i] = Container{
Names: names,
Networks: networks,
}
}
return containers, nil
}
var (
_ slog.LogValuer = (*Client)(nil)
_ Docker = (*Client)(nil)
)