forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubernetes.go
185 lines (156 loc) · 4.51 KB
/
kubernetes.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
package kubernetes
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/kubernetes"
"github.com/elastic/beats/libbeat/logp"
)
func init() {
autodiscover.ProviderRegistry.AddProvider("kubernetes", AutodiscoverBuilder)
}
// Provider implements autodiscover provider for docker containers
type Provider struct {
config *Config
bus bus.Bus
watcher kubernetes.Watcher
metagen kubernetes.MetaGenerator
templates *template.Mapper
stop chan interface{}
startListener bus.Listener
stopListener bus.Listener
updateListener 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
}
client, err := kubernetes.GetKubernetesClient(config.InCluster, config.KubeConfig)
if err != nil {
return nil, err
}
metagen := kubernetes.NewMetaGenerator(config.IncludeAnnotations, config.IncludeLabels, config.ExcludeLabels)
config.Host = kubernetes.DiscoverKubernetesNode(config.Host, client)
watcher := kubernetes.NewWatcher(client.CoreV1(), config.SyncPeriod, config.CleanupTimeout, config.Host)
start := watcher.ListenStart()
stop := watcher.ListenStop()
update := watcher.ListenUpdate()
if err := watcher.Start(); err != nil {
return nil, err
}
return &Provider{
config: config,
bus: bus,
templates: mapper,
metagen: metagen,
watcher: watcher,
stop: make(chan interface{}),
startListener: start,
stopListener: stop,
updateListener: update,
}, nil
}
// Start the autodiscover provider. Start and stop listeners work the
// conventional way. Update listener triggers a stop and then a start
// to simulate an update.
func (p *Provider) Start() {
go func() {
for {
select {
case <-p.stop:
p.startListener.Stop()
p.stopListener.Stop()
return
case event := <-p.startListener.Events():
p.emit(event, "start")
case event := <-p.stopListener.Events():
p.emit(event, "stop")
case event := <-p.updateListener.Events():
//On updates, first send a stop signal followed by a start signal to simulate a restart
p.emit(event, "stop")
p.emit(event, "start")
}
}
}()
}
func (p *Provider) emit(event bus.Event, flag string) {
pod, ok := event["pod"].(*kubernetes.Pod)
if !ok {
logp.Err("Couldn't get a pod from watcher event")
return
}
host := pod.Status.PodIP
containerIDs := map[string]string{}
// Emit pod container IDs
for _, c := range append(pod.Status.ContainerStatuses, pod.Status.InitContainerStatuses...) {
cid := c.GetContainerID()
containerIDs[c.Name] = cid
cmeta := common.MapStr{
"id": cid,
"name": c.Name,
"image": c.Image,
}
// Metadata appended to each event
meta := p.metagen.ContainerMetadata(pod, c.Name)
// Information that can be used in discovering a workload
kubemeta := meta.Clone()
kubemeta["container"] = cmeta
// Emit container info
p.publish(bus.Event{
flag: true,
"host": host,
"kubernetes": kubemeta,
"meta": common.MapStr{
"kubernetes": meta,
},
})
}
// Emit pod ports
for _, c := range pod.Spec.Containers {
cmeta := common.MapStr{
"id": containerIDs[c.Name],
"name": c.Name,
"image": c.Image,
}
// Metadata appended to each event
meta := p.metagen.ContainerMetadata(pod, c.Name)
// Information that can be used in discovering a workload
kubemeta := meta.Clone()
kubemeta["container"] = cmeta
for _, port := range c.Ports {
event := bus.Event{
flag: true,
"host": host,
"port": port.ContainerPort,
"kubernetes": kubemeta,
"meta": common.MapStr{
"kubernetes": meta,
},
}
p.publish(event)
}
}
}
func (p *Provider) publish(event bus.Event) {
// Try to match a config
if config := p.templates.GetConfig(event); config != nil {
event["config"] = config
}
p.bus.Publish(event)
}
// Stop signals the stop channel to force the watch loop routine to stop.
func (p *Provider) Stop() {
close(p.stop)
}
// String returns a description of kubernetes autodiscover provider.
func (p *Provider) String() string {
return "kubernetes"
}