-
Notifications
You must be signed in to change notification settings - Fork 67
/
docker.go
171 lines (137 loc) · 4.62 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package docker
import (
"context"
"errors"
"time"
"github.com/portainer/agent"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/system"
"github.com/docker/docker/client"
"github.com/rs/zerolog/log"
)
const (
serviceNameLabel = "com.docker.swarm.service.name"
clientTimeout = 1 * time.Minute
)
// DockerInfoService is a service used to retrieve information from a Docker environment
// using the Docker library.
type InfoService struct{}
// NewInfoService returns a pointer to an instance of DockerInfoService
func NewInfoService() *InfoService {
return &InfoService{}
}
// GetRuntimeConfigurationFromDockerEngine retrieves information from a Docker environment
// and returns a map of labels.
func (service *InfoService) GetRuntimeConfigurationFromDockerEngine() (*agent.RuntimeConfig, error) {
cli, err := NewClient()
if err != nil {
return nil, err
}
defer cli.Close()
dockerInfo, err := cli.Info(context.Background())
if err != nil {
return nil, err
}
runtimeConfig := &agent.RuntimeConfig{
NodeName: dockerInfo.Name,
DockerConfig: agent.DockerRuntimeConfig{},
}
if dockerInfo.Swarm.NodeID == "" {
getStandaloneConfig(runtimeConfig)
} else {
err := getSwarmConfig(runtimeConfig, dockerInfo, cli)
if err != nil {
return nil, err
}
}
return runtimeConfig, nil
}
// GetContainerIpFromDockerEngine is used to retrieve the IP address of the container through Docker.
// It will inspect the container to retrieve the networks associated to the container and returns the IP associated
// to the first network found that is not an ingress network. If the ignoreNonSwarmNetworks parameter is specified,
// it will also ignore non Swarm scoped networks.
func (service *InfoService) GetContainerIpFromDockerEngine(containerName string, ignoreNonSwarmNetworks bool) (string, error) {
cli, err := NewClient()
if err != nil {
return "", err
}
defer cli.Close()
containerInspect, err := cli.ContainerInspect(context.Background(), containerName)
if err != nil {
return "", err
}
if len(containerInspect.NetworkSettings.Networks) > 1 {
log.Warn().
Int("network_count", len(containerInspect.NetworkSettings.Networks)).
Msg("agent container running in more than a single Docker network. This might cause communication issues")
}
for networkName, network := range containerInspect.NetworkSettings.Networks {
networkInspect, err := cli.NetworkInspect(context.Background(), network.NetworkID, types.NetworkInspectOptions{})
if err != nil {
return "", err
}
if networkInspect.Ingress || (ignoreNonSwarmNetworks && networkInspect.Scope != "swarm") {
log.Debug().
Str("network_name", networkInspect.Name).
Str("scope", networkInspect.Scope).
Bool("ingress", networkInspect.Ingress).
Msg("skipping invalid container network")
continue
}
if network.IPAddress != "" {
log.Debug().
Str("ip_address", network.IPAddress).
Str("network_name", networkName).
Msg("retrieving IP address from container network")
return network.IPAddress, nil
}
}
return "", errors.New("unable to retrieve the address on which the agent can advertise. Check your network settings")
}
// GetServiceNameFromDockerEngine is used to return the name of the Swarm service the agent is part of.
// The service name is retrieved through container labels.
func (service *InfoService) GetServiceNameFromDockerEngine(containerName string) (string, error) {
cli, err := NewClient()
if err != nil {
return "", err
}
defer cli.Close()
containerInspect, err := cli.ContainerInspect(context.Background(), containerName)
if err != nil {
return "", err
}
return containerInspect.Config.Labels[serviceNameLabel], nil
}
func getStandaloneConfig(config *agent.RuntimeConfig) {
config.DockerConfig.EngineType = agent.EngineTypeStandalone
}
func getSwarmConfig(config *agent.RuntimeConfig, dockerInfo system.Info, cli *client.Client) error {
config.DockerConfig.EngineType = agent.EngineTypeSwarm
config.DockerConfig.NodeRole = agent.NodeRoleWorker
if dockerInfo.Swarm.ControlAvailable {
config.DockerConfig.NodeRole = agent.NodeRoleManager
node, _, err := cli.NodeInspectWithRaw(context.Background(), dockerInfo.Swarm.NodeID)
if err != nil {
return err
}
if node.ManagerStatus.Leader {
config.DockerConfig.Leader = true
}
}
return nil
}
func NewClient() (*client.Client, error) {
return client.NewClientWithOpts(
client.FromEnv,
client.WithAPIVersionNegotiation(),
client.WithTimeout(clientTimeout),
)
}
func withCli(callback func(cli *client.Client) error) error {
cli, err := NewClient()
if err != nil {
return err
}
defer cli.Close()
return callback(cli)
}