-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker.go
81 lines (68 loc) · 1.76 KB
/
docker.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package docker
import (
"context"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
)
func NewClient() (*Client, error) {
c, err := client.NewClientWithOpts()
if err != nil {
return nil, err
}
return &Client{c}, err
}
type Client struct {
*client.Client
}
func (c *Client) Scan(ctx context.Context, serviceLabelFilter string) (map[string]string, error) {
const statusOK = "running"
containers, err := c.ContainerList(ctx, types.ContainerListOptions{
Filters: filters.NewArgs(filters.KeyValuePair{
Key: "name",
Value: serviceLabelFilter,
}),
})
if err != nil {
return nil, err
}
var o = make(map[string]string)
for _, container := range containers {
if container.State == statusOK {
if container.NetworkSettings != nil {
for _, network := range container.NetworkSettings.Networks {
o[container.ID] = network.IPAddress
// use the first found IP for now
break
}
}
}
}
return o, err
}
func (c *Client) Read(ctx context.Context, instanceID string) (string, string, error) {
info, err := c.ContainerInspect(ctx, instanceID)
if err != nil {
return "", "", err
}
accessKeyID, secretAccessKey := readAccessCredentialsFromEnv(info.Config.Env)
return accessKeyID, secretAccessKey, nil
}
func readAccessCredentialsFromEnv(envVars []string) (accessKeyID, secretAccessKey string) {
for _, kvPair := range envVars {
// stop scanning env variable when the credentials are found
if accessKeyID != "" && secretAccessKey != "" {
break
}
const cntElements = 2
els := strings.SplitN(kvPair, "=", cntElements)
switch els[0] {
case "MINIO_SECRET_KEY":
secretAccessKey = els[1]
case "MINIO_ACCESS_KEY":
accessKeyID = els[1]
}
}
return
}