-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathconfig.go
396 lines (333 loc) · 12.1 KB
/
config.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// 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.
package integration
import (
"fmt"
"hash/fnv"
"sort"
"strconv"
yaml "gopkg.in/yaml.v2"
"github.com/DataDog/datadog-agent/pkg/util/containers"
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/util/tmplvar"
)
// Data contains YAML code
type Data []byte
// RawMap is the generic type to hold YAML configurations
type RawMap map[interface{}]interface{}
// JSONMap is the generic type to hold JSON configurations
type JSONMap map[string]interface{}
// Config is a generic container for configuration data specific to an
// integration. It contains snippets of configuration for various agent
// components, in fields of type Data.
//
// The Data fields contain YAML data except when config.Provider is
// names.Container or names.Kubernetes, in which case the configuration is in
// JSON.
type Config struct {
// When a new field is added to this struct, please evaluate whether it
// should be computed in the config Digest and update the field's
// documentation and the Digest method accordingly
// Name of the integration
Name string `json:"check_name"` // (include in digest: true)
// Instances is the list of instances in YAML or JSON.
Instances []Data `json:"instances"` // (include in digest: true)
// InitConfig is the init_config in YAML or JSON
InitConfig Data `json:"init_config"` // (include in digest: true)
// MetricConfig is the metric config in YAML or JSON (jmx check only)
MetricConfig Data `json:"metric_config"` // (include in digest: false)
// LogsConfig is the logs config in YAML or JSON (logs-agent only)
LogsConfig Data `json:"logs"` // (include in digest: true)
// ADIdentifiers is the list of AutoDiscovery identifiers for this
// integration. If either ADIdentifiers or AdvancedADIdentifiers are
// present, then this config is a template and will be resolved when a
// matching service is discovered. Otherwise, the config will be scheduled
// immediately. (optional)
ADIdentifiers []string `json:"ad_identifiers"` // (include in digest: true)
// AdvancedADIdentifiers is the list of advanced AutoDiscovery identifiers;
// see ADIdentifiers. (optional)
AdvancedADIdentifiers []AdvancedADIdentifier `json:"advanced_ad_identifiers"` // (include in digest: false)
// Provider is the name of the config provider that issued the config. If
// this is "", then the config is a service config, representing a serivce
// discovered by a listener.
Provider string `json:"provider"` // (include in digest: false)
// ServiceID is the ID of the service (set only for resolved templates and
// for service configs)
ServiceID string `json:"-"` // (include in digest: true)
// TaggerEntity is the tagger entity ID
TaggerEntity string `json:"-"` // (include in digest: false)
// ClusterCheck is cluster-check configuration flag
ClusterCheck bool `json:"cluster_check"` // (include in digest: false)
// NodeName is node name in case of an endpoint check backed by a pod
NodeName string `json:"node_name"` // (include in digest: true)
// Source is the source of the configuration
Source string `json:"source"` // (include in digest: false)
// IgnoreAutodiscoveryTags is used to ignore tags coming from autodiscovery
IgnoreAutodiscoveryTags bool `json:"ignore_autodiscovery_tags"` // (include in digest: true)
// MetricsExcluded is whether metrics collection is disabled (set by
// container listeners only)
MetricsExcluded bool `json:"metrics_excluded"` // (include in digest: false)
// LogsExcluded is whether logs collection is disabled (set by container
// listeners only)
LogsExcluded bool `json:"logs_excluded"` // (include in digest: false)
}
// CommonInstanceConfig holds the reserved fields for the yaml instance data
type CommonInstanceConfig struct {
MinCollectionInterval int `yaml:"min_collection_interval"`
EmptyDefaultHostname bool `yaml:"empty_default_hostname"`
Tags []string `yaml:"tags"`
Service string `yaml:"service"`
Name string `yaml:"name"`
Namespace string `yaml:"namespace"`
}
// CommonGlobalConfig holds the reserved fields for the yaml init_config data
type CommonGlobalConfig struct {
Service string `yaml:"service"`
}
// AdvancedADIdentifier contains user-defined autodiscovery information
// It replaces ADIdentifiers for advanced use-cases. Typically, file-based k8s service and endpoint checks.
type AdvancedADIdentifier struct {
KubeService KubeNamespacedName `yaml:"kube_service,omitempty"`
KubeEndpoints KubeNamespacedName `yaml:"kube_endpoints,omitempty"`
}
// KubeNamespacedName identifies a kubernetes object.
type KubeNamespacedName struct {
Name string `yaml:"name"`
Namespace string `yaml:"namespace"`
}
// IsEmpty returns true if the KubeNamespacedName is empty
func (k KubeNamespacedName) IsEmpty() bool {
return k.Name == "" && k.Namespace == ""
}
// Equal determines whether the passed config is the same
func (c *Config) Equal(cfg *Config) bool {
if cfg == nil {
return false
}
return c.Digest() == cfg.Digest()
}
// String constructs the YAML representation of the config.
func (c *Config) String() string {
rawConfig := make(map[interface{}]interface{})
var initConfig interface{}
var instances []interface{}
var logsConfig interface{}
rawConfig["check_name"] = c.Name
yaml.Unmarshal(c.InitConfig, &initConfig) //nolint:errcheck
rawConfig["init_config"] = initConfig
for _, i := range c.Instances {
var instance interface{}
yaml.Unmarshal(i, &instance) //nolint:errcheck
instances = append(instances, instance)
}
rawConfig["instances"] = instances
yaml.Unmarshal(c.LogsConfig, &logsConfig) //nolint:errcheck
rawConfig["logs_config"] = logsConfig
buffer, err := yaml.Marshal(&rawConfig)
if err != nil {
log.Error(err)
}
return string(buffer)
}
// IsTemplate returns if the config has AD identifiers
func (c *Config) IsTemplate() bool {
return len(c.ADIdentifiers) > 0 || len(c.AdvancedADIdentifiers) > 0
}
// IsCheckConfig returns true if the config is a node-agent check configuration,
func (c *Config) IsCheckConfig() bool {
return !c.ClusterCheck && len(c.Instances) > 0
}
// IsLogConfig returns true if config contains a logs config.
func (c *Config) IsLogConfig() bool {
return c.LogsConfig != nil
}
// HasFilter returns true if metrics or logs collection must be disabled for this config.
func (c *Config) HasFilter(filter containers.FilterType) bool {
// no containers.GlobalFilter case here because we don't create services
// that are globally excluded in AD
switch filter {
case containers.MetricsFilter:
return c.MetricsExcluded
case containers.LogsFilter:
return c.LogsExcluded
}
return false
}
// AddMetrics adds metrics to a check configuration
func (c *Config) AddMetrics(metrics Data) error {
var rawInitConfig RawMap
err := yaml.Unmarshal(c.InitConfig, &rawInitConfig)
if err != nil {
return err
}
var rawMetricsConfig []interface{}
err = yaml.Unmarshal(metrics, &rawMetricsConfig)
if err != nil {
return err
}
// Grab any metrics currently in init_config
var conf []interface{}
currMetrics := make(map[string]bool)
if _, ok := rawInitConfig["conf"]; ok {
if currentMetrics, ok := rawInitConfig["conf"].([]interface{}); ok {
for _, metric := range currentMetrics {
conf = append(conf, metric)
if metricS, e := yaml.Marshal(metric); e == nil {
currMetrics[string(metricS)] = true
}
}
}
}
// Add new metrics, skip duplicates
for _, metric := range rawMetricsConfig {
if metricS, e := yaml.Marshal(metric); e == nil {
if !currMetrics[string(metricS)] {
conf = append(conf, metric)
}
}
}
// JMX fetch expects the metrics to be a part of init_config, under "conf"
rawInitConfig["conf"] = conf
initConfig, err := yaml.Marshal(rawInitConfig)
if err != nil {
return err
}
c.InitConfig = initConfig
return nil
}
// GetTemplateVariablesForInstance returns a slice of raw template variables
// it found in a config instance template.
func (c *Config) GetTemplateVariablesForInstance(i int) []tmplvar.TemplateVar {
if len(c.Instances) < i {
return nil
}
return tmplvar.Parse(c.Instances[i])
}
// GetNameForInstance returns the name from an instance if specified, fallback on namespace
func (c *Data) GetNameForInstance() string {
commonOptions := CommonInstanceConfig{}
err := yaml.Unmarshal(*c, &commonOptions)
if err != nil {
log.Errorf("invalid instance section: %s", err)
return ""
}
if commonOptions.Name != "" {
return commonOptions.Name
}
// Fallback on `namespace` if we don't find `name`, can be empty
return commonOptions.Namespace
}
// SetNameForInstance set name for instance
func (c *Data) SetNameForInstance(name string) error {
commonOptions := CommonInstanceConfig{}
err := yaml.Unmarshal(*c, &commonOptions)
if err != nil {
return fmt.Errorf("invalid instance section: %s", err)
}
commonOptions.Name = name
// modify original config
out, err := yaml.Marshal(&commonOptions)
if err != nil {
return err
}
*c = Data(out)
return nil
}
// MergeAdditionalTags merges additional tags to possible existing config tags
func (c *Data) MergeAdditionalTags(tags []string) error {
rawConfig := RawMap{}
err := yaml.Unmarshal(*c, &rawConfig)
if err != nil {
return err
}
rTags, _ := rawConfig["tags"].([]interface{})
// convert raw tags to string
cTags := make([]string, len(rTags))
for i, t := range rTags {
cTags[i] = fmt.Sprint(t)
}
tagList := append(cTags, tags...)
if len(tagList) == 0 {
return nil
}
// use set keys to remove duplicate
tagSet := make(map[string]struct{})
for _, t := range tagList {
tagSet[t] = struct{}{}
}
// override config tags
rawConfig["tags"] = []string{}
for k := range tagSet {
rawConfig["tags"] = append(rawConfig["tags"].([]string), k)
}
// modify original config
out, err := yaml.Marshal(&rawConfig)
if err != nil {
return err
}
*c = Data(out)
return nil
}
// SetField allows to set an arbitrary field to a given value,
// overriding the existing value if present
func (c *Data) SetField(key string, value interface{}) error {
rawConfig := RawMap{}
err := yaml.Unmarshal(*c, &rawConfig)
if err != nil {
return err
}
rawConfig[key] = value
out, err := yaml.Marshal(&rawConfig)
if err != nil {
return err
}
*c = Data(out)
return nil
}
// Digest returns an hash value representing the data stored in this configuration.
// The ClusterCheck field is intentionally left out to keep a stable digest
// between the cluster-agent and the node-agents
func (c *Config) Digest() string {
h := fnv.New64()
h.Write([]byte(c.Name)) //nolint:errcheck
for _, i := range c.Instances {
inst := RawMap{}
err := yaml.Unmarshal(i, &inst)
if err != nil {
log.Debugf("Error while calculating config digest for %s, skipping: %v", c.Name, err)
continue
}
if val, found := inst["tags"]; found {
// sort the list of tags so the digest stays stable for
// identical configs with the same tags but with different order
tagsInterface, ok := val.([]interface{})
if !ok {
log.Debugf("Error while calculating config digest for %s, skipping: cannot read tags from config", c.Name)
continue
}
tags := make([]string, len(tagsInterface))
for i, tag := range tagsInterface {
tags[i] = fmt.Sprint(tag)
}
sort.Strings(tags)
inst["tags"] = tags
}
out, err := yaml.Marshal(&inst)
if err != nil {
log.Debugf("Error while calculating config digest for %s, skipping: %v", c.Name, err)
continue
}
h.Write(out) //nolint:errcheck
}
h.Write([]byte(c.InitConfig)) //nolint:errcheck
for _, i := range c.ADIdentifiers {
h.Write([]byte(i)) //nolint:errcheck
}
h.Write([]byte(c.NodeName)) //nolint:errcheck
h.Write([]byte(c.LogsConfig)) //nolint:errcheck
h.Write([]byte(c.ServiceID)) //nolint:errcheck
h.Write([]byte(strconv.FormatBool(c.IgnoreAutodiscoveryTags))) //nolint:errcheck
return strconv.FormatUint(h.Sum64(), 16)
}