/
container.go
316 lines (259 loc) · 9.62 KB
/
container.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build !serverless
package providers
import (
"context"
"fmt"
"strings"
"sync"
"github.com/DataDog/datadog-agent/comp/core/autodiscovery/common/utils"
"github.com/DataDog/datadog-agent/comp/core/autodiscovery/integration"
"github.com/DataDog/datadog-agent/comp/core/autodiscovery/providers/names"
"github.com/DataDog/datadog-agent/comp/core/autodiscovery/telemetry"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
"github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/util/containers"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
// ContainerConfigProvider implements the ConfigProvider interface for both pods and containers
type ContainerConfigProvider struct {
workloadmetaStore workloadmeta.Component
configErrors map[string]ErrorMsgSet // map[entity name]ErrorMsgSet
configCache map[string]map[string]integration.Config // map[entity name]map[config digest]integration.Config
mu sync.RWMutex
}
// NewContainerConfigProvider returns a new ConfigProvider subscribed to both container
// and pods
func NewContainerConfigProvider(_ *config.ConfigurationProviders, wmeta workloadmeta.Component) (ConfigProvider, error) {
return &ContainerConfigProvider{
workloadmetaStore: wmeta,
configCache: make(map[string]map[string]integration.Config),
configErrors: make(map[string]ErrorMsgSet),
}, nil
}
// String returns a string representation of the ContainerConfigProvider
func (k *ContainerConfigProvider) String() string {
return names.KubeContainer
}
// Stream starts listening to workloadmeta to generate configs as they come
// instead of relying on a periodic call to Collect.
func (k *ContainerConfigProvider) Stream(ctx context.Context) <-chan integration.ConfigChanges {
const name = "ad-kubecontainerprovider"
// outCh must be unbuffered. processing of workloadmeta events must not
// proceed until the config is processed by autodiscovery, as configs
// need to be generated before any associated services.
outCh := make(chan integration.ConfigChanges)
filterParams := workloadmeta.FilterParams{
Kinds: []workloadmeta.Kind{workloadmeta.KindKubernetesPod, workloadmeta.KindContainer},
Source: workloadmeta.SourceAll,
EventType: workloadmeta.EventTypeAll,
}
inCh := k.workloadmetaStore.Subscribe(name, workloadmeta.ConfigProviderPriority, workloadmeta.NewFilter(&filterParams))
go func() {
for {
select {
case <-ctx.Done():
k.workloadmetaStore.Unsubscribe(inCh)
case evBundle, ok := <-inCh:
if !ok {
return
}
// send changes even when they're empty, as we
// need to signal that an event has been
// received, for flow control reasons
outCh <- k.processEvents(evBundle)
evBundle.Acknowledge()
}
}
}()
return outCh
}
func (k *ContainerConfigProvider) processEvents(evBundle workloadmeta.EventBundle) integration.ConfigChanges {
k.mu.Lock()
defer k.mu.Unlock()
changes := integration.ConfigChanges{}
for _, event := range evBundle.Events {
entityName := buildEntityName(event.Entity)
switch event.Type {
case workloadmeta.EventTypeSet:
configs, err := k.generateConfig(event.Entity)
if err != nil {
k.configErrors[entityName] = err
} else {
delete(k.configErrors, entityName)
}
configCache, ok := k.configCache[entityName]
if !ok {
configCache = make(map[string]integration.Config)
k.configCache[entityName] = configCache
}
configsToUnschedule := make(map[string]integration.Config)
for digest, config := range configCache {
configsToUnschedule[digest] = config
}
for _, config := range configs {
digest := config.Digest()
if _, ok := configCache[digest]; ok {
delete(configsToUnschedule, digest)
} else {
configCache[digest] = config
changes.ScheduleConfig(config)
}
}
for oldDigest, oldConfig := range configsToUnschedule {
delete(configCache, oldDigest)
changes.UnscheduleConfig(oldConfig)
}
case workloadmeta.EventTypeUnset:
oldConfigs, found := k.configCache[entityName]
if !found {
log.Debugf("entity %q removed from workloadmeta store but not found in cache. skipping", entityName)
continue
}
for _, oldConfig := range oldConfigs {
changes.UnscheduleConfig(oldConfig)
}
delete(k.configCache, entityName)
delete(k.configErrors, entityName)
default:
log.Errorf("cannot handle event of type %d", event.Type)
}
}
telemetry.Errors.Set(float64(len(k.configErrors)), names.KubeContainer)
return changes
}
func (k *ContainerConfigProvider) generateConfig(e workloadmeta.Entity) ([]integration.Config, ErrorMsgSet) {
var (
errMsgSet ErrorMsgSet
errs []error
configs []integration.Config
)
switch entity := e.(type) {
case *workloadmeta.Container:
// kubernetes containers need to be handled together with their
// pod, so they generate a single []integration.Config.
// otherwise, it's possible for a container that belongs to an
// AD-annotated pod to briefly be scheduled without its
// annotations.
if !findKubernetesInLabels(entity.Labels) {
configs, errs = k.generateContainerConfig(entity)
containerID := entity.ID
containerEntityName := containers.BuildEntityName(string(entity.Runtime), containerID)
configs = utils.AddContainerCollectAllConfigs(configs, containerEntityName)
for idx := range configs {
configs[idx].Source = names.Container + ":" + containerEntityName
}
}
case *workloadmeta.KubernetesPod:
containerIdentifiers := map[string]struct{}{}
containerNames := map[string]struct{}{}
for _, podContainer := range entity.GetAllContainers() {
container, err := k.workloadmetaStore.GetContainer(podContainer.ID)
if err != nil {
log.Debugf("Pod %q has reference to non-existing container %q", entity.Name, podContainer.ID)
continue
}
var (
c []integration.Config
errors []error
)
c, errors = k.generateContainerConfig(container)
configs = append(configs, c...)
errs = append(errs, errors...)
adIdentifier := podContainer.Name
if customADID, found := utils.ExtractCheckIDFromPodAnnotations(entity.Annotations, podContainer.Name); found {
adIdentifier = customADID
}
containerEntity := containers.BuildEntityName(string(container.Runtime), container.ID)
c, errors = utils.ExtractTemplatesFromAnnotations(
containerEntity,
entity.Annotations,
adIdentifier,
)
// container_collect_all configs must be added after
// configs generated from annotations, since services
// are reconciled against configs one-by-one instead of
// as a set, so if a container_collect_all config
// appears before an annotation one, it'll cause a logs
// config to be scheduled as container_collect_all,
// unscheduled, and then re-scheduled correctly.
c = utils.AddContainerCollectAllConfigs(c, containerEntity)
if len(errors) > 0 {
errs = append(errs, errors...)
if len(c) == 0 {
// Only got errors, no valid configs so
// let's move on to the next container.
continue
}
}
containerIdentifiers[adIdentifier] = struct{}{}
containerNames[podContainer.Name] = struct{}{}
for idx := range c {
c[idx].Source = names.Container + ":" + containerEntity
}
configs = append(configs, c...)
}
errs = append(errs, utils.ValidateAnnotationsMatching(
entity.Annotations,
containerIdentifiers,
containerNames)...)
default:
log.Errorf("cannot handle entity of kind %s", e.GetID().Kind)
}
if len(errs) > 0 {
errMsgSet = make(ErrorMsgSet)
for _, err := range errs {
errMsgSet[err.Error()] = struct{}{}
}
}
return configs, errMsgSet
}
func (k *ContainerConfigProvider) generateContainerConfig(container *workloadmeta.Container) ([]integration.Config, []error) {
var (
errs []error
configs []integration.Config
)
containerID := container.ID
containerEntityName := containers.BuildEntityName(string(container.Runtime), containerID)
configs, errs = utils.ExtractTemplatesFromContainerLabels(containerEntityName, container.Labels)
return configs, errs
}
// GetConfigErrors returns a map of configuration errors for each namespace/pod
func (k *ContainerConfigProvider) GetConfigErrors() map[string]ErrorMsgSet {
k.mu.RLock()
defer k.mu.RUnlock()
errors := make(map[string]ErrorMsgSet, len(k.configErrors))
for entity, errset := range k.configErrors {
errors[entity] = errset
}
return errors
}
// buildEntityName is also used as display key in `agent status` "Configuration Errors" display.
// (for instance, incorrect annotation syntax or unknown container name).
// That's why it does not simply use Kind + ID.
// It needs to be unique over time.
// (for instance, namespace+name is not unique for a POD as it can be deleted/created with a different UID, see STS rollout)
func buildEntityName(e workloadmeta.Entity) string {
entityID := e.GetID()
switch entity := e.(type) {
case *workloadmeta.KubernetesPod:
return fmt.Sprintf("%s/%s (%s)", entity.Namespace, entity.Name, entity.ID)
case *workloadmeta.Container:
return containers.BuildEntityName(string(entity.Runtime), entityID.ID)
default:
return fmt.Sprintf("%s://%s", entityID.Kind, entityID.ID)
}
}
// findKubernetesInLabels traverses a map of container labels and
// returns true if a kubernetes label is detected
func findKubernetesInLabels(labels map[string]string) bool {
for name := range labels {
if strings.HasPrefix(name, "io.kubernetes.") {
return true
}
}
return false
}