forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker.go
150 lines (128 loc) · 3.06 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
package docker
import (
"github.com/elastic/beats/libbeat/autodiscover"
"github.com/elastic/beats/libbeat/autodiscover/template"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/bus"
"github.com/elastic/beats/libbeat/common/docker"
"github.com/elastic/beats/libbeat/logp"
)
func init() {
autodiscover.ProviderRegistry.AddProvider("docker", AutodiscoverBuilder)
}
// Provider implements autodiscover provider for docker containers
type Provider struct {
config *Config
bus bus.Bus
watcher docker.Watcher
templates *template.Mapper
stop chan interface{}
startListener bus.Listener
stopListener bus.Listener
}
// AutodiscoverBuilder builds and returns an autodiscover provider
func AutodiscoverBuilder(bus bus.Bus, c *common.Config) (autodiscover.Provider, error) {
config := defaultConfig()
err := c.Unpack(&config)
if err != nil {
return nil, err
}
mapper, err := template.NewConfigMapper(config.Templates)
if err != nil {
return nil, err
}
watcher, err := docker.NewWatcher(config.Host, config.TLS)
if err != nil {
return nil, err
}
start := watcher.ListenStart()
stop := watcher.ListenStop()
if err := watcher.Start(); err != nil {
return nil, err
}
return &Provider{
config: config,
bus: bus,
templates: mapper,
watcher: watcher,
stop: make(chan interface{}),
startListener: start,
stopListener: stop,
}, nil
}
// Start the autodiscover process
func (d *Provider) Start() {
go func() {
for {
select {
case <-d.stop:
d.startListener.Stop()
d.stopListener.Stop()
return
case event := <-d.startListener.Events():
d.emitContainer(event, "start")
case event := <-d.stopListener.Events():
d.emitContainer(event, "stop")
}
}
}()
}
func (d *Provider) emitContainer(event bus.Event, flag string) {
container, ok := event["container"].(*docker.Container)
if !ok {
logp.Err("Couldn't get a container from watcher event")
return
}
var host string
if len(container.IPAddresses) > 0 {
host = container.IPAddresses[0]
}
labelMap := common.MapStr{}
for k, v := range container.Labels {
labelMap[k] = v
}
meta := common.MapStr{
"container": common.MapStr{
"id": container.ID,
"name": container.Name,
"image": container.Image,
"labels": labelMap,
},
}
// Emit container info
d.publish(bus.Event{
flag: true,
"host": host,
"docker": meta,
"meta": common.MapStr{
"docker": meta,
},
})
// Emit container private ports
for _, port := range container.Ports {
event := bus.Event{
flag: true,
"host": host,
"port": port.PrivatePort,
"docker": meta,
"meta": common.MapStr{
"docker": meta,
},
}
d.publish(event)
}
}
func (d *Provider) publish(event bus.Event) {
// Try to match a config
if config := d.templates.GetConfig(event); config != nil {
event["config"] = config
}
d.bus.Publish(event)
}
// Stop the autodiscover process
func (d *Provider) Stop() {
close(d.stop)
}
func (d *Provider) String() string {
return "docker"
}