This repository has been archived by the owner on Aug 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
swarm.go
372 lines (302 loc) · 10.4 KB
/
swarm.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"reflect"
"strconv"
"time"
"github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/util/strutil"
"github.com/spf13/cobra"
)
const (
implicit = "implicit"
explicit = "explicit"
portLabel = "prometheus.port"
includeLabel = "prometheus.scan"
excludeLabel = "prometheus.ignore"
)
var logger = logrus.New()
var options = Options{}
// allocateIP returns the 3rd last IP in the network range.
func allocateIP(netCIDR *net.IPNet) string {
allocIP := net.IP(make([]byte, 4))
for i := range netCIDR.IP {
allocIP[i] = netCIDR.IP[i] | ^netCIDR.Mask[i]
}
allocIP[3] = allocIP[3] - 2
return allocIP.String()
}
func connectNetworks(networks map[string]swarm.Network, containerID string) error {
cli, err := client.NewEnvClient()
if err != nil {
return err
}
prometheusContainer, err := cli.ContainerInspect(context.Background(), containerID)
if err != nil {
return err
}
promNetworks := make(map[string]*network.EndpointSettings)
for _, cntnet := range prometheusContainer.NetworkSettings.Networks {
promNetworks[cntnet.NetworkID] = cntnet
}
for id, netwrk := range networks {
if _, ok := promNetworks[id]; ok || len(netwrk.IPAMOptions.Configs) == 0 {
continue
}
_, netCIDR, err := net.ParseCIDR(netwrk.IPAMOptions.Configs[0].Subnet)
if err != nil {
logger.Error(err)
continue
}
prometheusIP := allocateIP(netCIDR)
logger.Info("Connecting network ", netwrk.Spec.Name, "(", netCIDR.IP, ") to ", containerID, "(", prometheusIP, ")")
netconfig := &network.EndpointSettings{
IPAMConfig: &network.EndpointIPAMConfig{
IPv4Address: prometheusIP,
},
}
err = cli.NetworkConnect(context.Background(), netwrk.ID, containerID, netconfig)
if err != nil {
logger.Error("Could not connect container ", containerID, " to network ", netwrk.ID, ": ", err)
continue
}
}
return nil
}
func writeSDConfig(scrapeTasks []scrapeTask, output string) {
jsonScrapeConfig, err := json.MarshalIndent(scrapeTasks, "", " ")
if err != nil {
panic(err)
}
logger.Debug("Writing Prometheus config file")
err = ioutil.WriteFile(output, jsonScrapeConfig, 0644)
if err != nil {
panic(err)
}
}
func findPrometheusContainer(serviceName string) (string, error) {
cli, err := client.NewEnvClient()
if err != nil {
panic(err)
}
taskFilters := filters.NewArgs()
taskFilters.Add("desired-state", string(swarm.TaskStateRunning))
taskFilters.Add("service", serviceName)
promTasks, err := cli.TaskList(context.Background(), types.TaskListOptions{Filters: taskFilters})
if err != nil {
return "", err
}
if len(promTasks) == 0 || promTasks[0].Status.ContainerStatus.ContainerID == "" {
return "", fmt.Errorf("Could not find container for service %s", serviceName)
}
return promTasks[0].Status.ContainerStatus.ContainerID, nil
}
func cleanNetworks(prometheusContainerID string) {
cli, err := client.NewEnvClient()
networkFilters := filters.NewArgs()
networkFilters.Add("driver", "overlay")
networks, err := cli.NetworkList(context.Background(), types.NetworkListOptions{Filters: networkFilters})
if err != nil {
panic(err)
}
for _, network := range networks {
if network.Name == "ingress" {
continue
}
if len(network.Containers) == 1 && reflect.ValueOf(network.Containers).MapKeys()[0].String() == prometheusContainerID {
logger.Info("Network ", network.Name, " contains only the Prometheus container. Disconnecting and removing network.")
cli.NetworkDisconnect(context.Background(), network.ID, prometheusContainerID, true)
if err != nil {
logger.Error(err)
}
err := cli.NetworkRemove(context.Background(), network.ID)
if err != nil {
logger.Error(err)
}
}
}
}
type scrapeService struct {
ServiceName string
scrapeTasks scrapeTask
}
type scrapeTask struct {
Targets []string
Labels map[string]string
}
// collectPorts builds a map of ports collected from container exposed ports and/or from ports defined
// as container labels
func collectPorts(task swarm.Task, serviceIDMap map[string]swarm.Service, discoveryType string) map[int]struct{} {
ports := make(map[int]struct{})
// collects port defined in the container labels
if portstr, ok := task.Spec.ContainerSpec.Labels[portLabel]; ok {
if port, err := strconv.Atoi(portstr); err == nil {
ports[port] = struct{}{}
}
}
// collects exposed ports, but only if we use implicit discovery
if discoveryType == implicit {
for _, port := range serviceIDMap[task.ServiceID].Spec.EndpointSpec.Ports {
ports[int(port.TargetPort)] = struct{}{}
}
} else {
logger.Debugf("Exposed ports on Service %s ignored by Prometheus", task.ServiceID)
}
return ports
}
func collectIPs(task swarm.Task) ([]net.IP, map[string]swarm.Network) {
var containerIPs []net.IP
taskNetworks := make(map[string]swarm.Network)
for _, netatt := range task.NetworksAttachments {
if netatt.Network.Spec.Name == "ingress" || netatt.Network.DriverState.Name != "overlay" {
continue
}
for _, ipcidr := range netatt.Addresses {
ip, _, err := net.ParseCIDR(ipcidr)
if err != nil {
logger.Error(err)
continue
}
containerIPs = append(containerIPs, ip)
}
taskNetworks[netatt.Network.ID] = netatt.Network
}
return containerIPs, taskNetworks
}
func taskLabels(task swarm.Task, serviceIDMap map[string]swarm.Service) map[string]string {
service := serviceIDMap[task.ServiceID]
labels := map[string]string{
model.JobLabel: service.Spec.Name,
model.MetaLabelPrefix + "docker_task_name": task.Name,
model.MetaLabelPrefix + "docker_task_desired_state": string(task.DesiredState),
}
for k, v := range task.Spec.ContainerSpec.Labels {
labels[strutil.SanitizeLabelName(model.MetaLabelPrefix+"docker_task_label_"+k)] = v
}
for k, v := range service.Spec.Labels {
labels[strutil.SanitizeLabelName(model.MetaLabelPrefix+"docker_service_label_"+k)] = v
}
return labels
}
func discoverSwarm(prometheusContainerID string, outputFile string, discoveryType string) {
cli, err := client.NewEnvClient()
if err != nil {
panic(err)
}
services, err := cli.ServiceList(context.Background(), types.ServiceListOptions{})
if err != nil {
panic(err)
}
serviceIDMap := make(map[string]swarm.Service)
for _, service := range services {
serviceIDMap[service.ID] = service
}
taskFilters := filters.NewArgs()
taskFilters.Add("desired-state", string(swarm.TaskStateRunning))
tasks, err := cli.TaskList(context.Background(), types.TaskListOptions{Filters: taskFilters})
if err != nil {
panic(err)
}
var scrapeTasks []scrapeTask
allNetworks := make(map[string]swarm.Network)
for _, task := range tasks {
if discoveryType == implicit {
if _, ok := task.Spec.ContainerSpec.Labels[excludeLabel]; ok {
logger.Debugf("Task %s ignored by Prometheus", task.ID)
continue
}
} else if discoveryType == explicit {
if _, ok := task.Spec.ContainerSpec.Labels[includeLabel]; ok {
logger.Debugf("Task %s should be scanned by Prometheus", task.ID)
} else {
continue
}
}
ports := collectPorts(task, serviceIDMap, discoveryType)
containerIPs, taskNetworks := collectIPs(task)
var taskEndpoints []string
for k, v := range taskNetworks {
allNetworks[k] = v
}
// if exposed ports are found, or ports defined through labels, add them to the Prometheus target.
// if not, add only the container IP as a target, and Prometheus will use the default port (80).
for _, ip := range containerIPs {
if len(ports) > 0 {
for port := range ports {
taskEndpoints = append(taskEndpoints, fmt.Sprintf("%s:%d", ip.String(), port))
}
} else {
taskEndpoints = append(taskEndpoints, ip.String())
}
}
logger.Debugf("Found task %s with IPs %s", task.ID, taskEndpoints)
scrapetask := scrapeTask{
Targets: taskEndpoints,
Labels: taskLabels(task, serviceIDMap),
}
scrapeTasks = append(scrapeTasks, scrapetask)
}
err = connectNetworks(allNetworks, prometheusContainerID)
if err != nil {
logger.Error("Could not connect container ,", prometheusContainerID, ": ", err)
}
writeSDConfig(scrapeTasks, outputFile)
}
func discoveryProcess(cmd *cobra.Command, args []string) {
level, err := logrus.ParseLevel(options.logLevel)
if err != nil {
logger.Fatal(err)
}
logger.Level = level
if options.discovery != implicit && options.discovery != explicit {
logger.Fatal("Invalid discovery type: ", options.discovery)
}
logger.Info("Starting service discovery process using Prometheus service [", options.prometheusService, "]")
for {
time.Sleep(time.Duration(options.discoveryInterval) * time.Second)
prometheusContainerID, err := findPrometheusContainer(options.prometheusService)
if err != nil {
logger.Warn(err)
continue
}
discoverSwarm(prometheusContainerID, options.output, options.discovery)
if options.clean {
cleanNetworks(prometheusContainerID)
}
}
}
// Options structure for all the cmd line flags
type Options struct {
prometheusService string
discoveryInterval int
discovery string
logLevel string
output string
clean bool
}
func main() {
var cmdDiscover = &cobra.Command{
Use: "discover",
Short: "Starts Swarm service discovery",
Run: discoveryProcess,
}
cmdDiscover.Flags().StringVarP(&options.prometheusService, "prometheus", "p", "prometheus", "Name of the Prometheus service")
cmdDiscover.Flags().IntVarP(&options.discoveryInterval, "interval", "i", 30, "The interval, in seconds, at which the discovery process is kicked off")
cmdDiscover.Flags().StringVarP(&options.logLevel, "loglevel", "l", "info", "Specify log level: debug, info, warn, error")
cmdDiscover.Flags().StringVarP(&options.output, "output", "o", "swarm-endpoints.json", "Output file that contains the Prometheus endpoints.")
cmdDiscover.Flags().BoolVarP(&options.clean, "clean", "c", true, "Disconnects unused networks from the Prometheus container, and deletes them.")
cmdDiscover.Flags().StringVarP(&options.discovery, "discovery", "d", "implicit", "Discovery time: implicit or explicit. Implicit scans all the services found while explicit scans only labeled services.")
var rootCmd = &cobra.Command{Use: "promswarm"}
rootCmd.AddCommand(cmdDiscover)
rootCmd.Execute()
}