-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
service.go
258 lines (222 loc) · 7.71 KB
/
service.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
// 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 listeners
import (
"context"
"fmt"
"reflect"
"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/tagger"
"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/kubernetes/kubelet"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
// service implements the Service interface and stores data collected from
// workloadmeta.Store.
type service struct {
entity workloadmeta.Entity
adIdentifiers []string
hosts map[string]string
ports []ContainerPort
pid int
hostname string
ready bool
checkNames []string
extraConfig map[string]string
metricsExcluded bool
logsExcluded bool
}
var _ Service = &service{}
// GetServiceID returns the AD entity ID of the service.
func (s *service) GetServiceID() string {
switch e := s.entity.(type) {
case *workloadmeta.Container:
return containers.BuildEntityName(string(e.Runtime), e.ID)
case *workloadmeta.KubernetesPod:
return kubelet.PodUIDToEntityName(e.ID)
default:
entityID := s.entity.GetID()
log.Errorf("cannot build AD entity ID for kind %q, ID %q", entityID.Kind, entityID.ID)
return ""
}
}
// GetTaggerEntity returns the Tagger entity ID of the service.
func (s *service) GetTaggerEntity() string {
switch e := s.entity.(type) {
case *workloadmeta.Container:
return containers.BuildTaggerEntityName(e.ID)
case *workloadmeta.KubernetesPod:
return kubelet.PodUIDToTaggerEntityName(e.ID)
default:
entityID := s.entity.GetID()
log.Errorf("cannot build AD entity ID for kind %q, ID %q", entityID.Kind, entityID.ID)
return ""
}
}
// GetADIdentifiers returns the service's AD identifiers.
func (s *service) GetADIdentifiers(_ context.Context) ([]string, error) {
return s.adIdentifiers, nil
}
// GetHosts returns the service's IPs for each host.
func (s *service) GetHosts(_ context.Context) (map[string]string, error) {
return s.hosts, nil
}
// GetPorts returns the ports exposed by the service's containers.
func (s *service) GetPorts(_ context.Context) ([]ContainerPort, error) {
return s.ports, nil
}
// GetTags returns the tags associated with the service.
func (s *service) GetTags() ([]string, error) {
return tagger.Tag(s.GetTaggerEntity(), tagger.ChecksCardinality)
}
// GetPid returns the process ID of the service.
func (s *service) GetPid(_ context.Context) (int, error) {
return s.pid, nil
}
// GetHostname returns the service's hostname.
func (s *service) GetHostname(_ context.Context) (string, error) {
return s.hostname, nil
}
// IsReady returns whether the service is ready.
func (s *service) IsReady(_ context.Context) bool {
return s.ready
}
// GetCheckNames returns the check names of the service.
func (s *service) GetCheckNames(_ context.Context) []string {
return s.checkNames
}
// HasFilter returns whether the service should not collect certain data (logs
// or metrics) due to filtering applied by filter.
func (s *service) HasFilter(filter containers.FilterType) bool {
switch filter {
case containers.MetricsFilter:
return s.metricsExcluded
case containers.LogsFilter:
return s.logsExcluded
}
return false
}
// FilterTemplates implements Service#FilterTemplates.
func (s *service) FilterTemplates(configs map[string]integration.Config) {
// These two overrides are handled in
// comp/core/autodiscovery/configresolver/configresolver.go
s.filterTemplatesEmptyOverrides(configs)
s.filterTemplatesOverriddenChecks(configs)
s.filterTemplatesContainerCollectAll(configs)
}
// filterTemplatesEmptyOverrides drops file-based templates if this service is a container
// or pod and has an empty check_names label/annotation.
func (s *service) filterTemplatesEmptyOverrides(configs map[string]integration.Config) {
// Empty check names on k8s annotations or container labels override the check config from file
// Used to deactivate unneeded OOTB autodiscovery checks defined in files
// The checkNames slice is considered empty also if it contains one single empty string
if s.checkNames != nil && (len(s.checkNames) == 0 || (len(s.checkNames) == 1 && s.checkNames[0] == "")) {
// ...remove all file-based templates
for digest, config := range configs {
if config.Provider == names.File {
log.Debugf(
"Ignoring config from %s, as the service %s defines an empty set of checkNames",
config.Source, s.GetServiceID())
delete(configs, digest)
}
}
}
}
// filterTemplatesOverriddenChecks drops file-based templates if this service's
// labels/annotations specify a check of the same name.
func (s *service) filterTemplatesOverriddenChecks(configs map[string]integration.Config) {
for digest, config := range configs {
if config.Provider != names.File {
continue // only override file configs
}
for _, checkName := range s.checkNames {
if config.Name == checkName {
// Ignore config from file when the same check is activated on
// the same service via other config providers (k8s annotations
// or container labels)
log.Debugf("Ignoring config from %s: the service %s overrides check %s",
config.Source, s.GetServiceID(), config.Name)
delete(configs, digest)
}
}
}
}
// filterTemplatesContainerCollectAll drops the container-collect-all template
// added by the config provider (AddContainerCollectAllConfigs) if the service
// has any other templates containing logs config.
func (s *service) filterTemplatesContainerCollectAll(configs map[string]integration.Config) {
if !config.Datadog.GetBool("logs_config.container_collect_all") {
return
}
var ccaDigest string
foundLogsConfig := false
for digest, config := range configs {
if config.Name == "container_collect_all" {
ccaDigest = digest
continue
}
if config.LogsConfig != nil {
foundLogsConfig = true
}
}
if foundLogsConfig && ccaDigest != "" {
delete(configs, ccaDigest)
}
}
// GetExtraConfig returns extra configuration associated with the service.
func (s *service) GetExtraConfig(key string) (string, error) {
result, found := s.extraConfig[key]
if !found {
return "", fmt.Errorf("extra config %q is not supported", key)
}
return result, nil
}
// svcEqual checks that two Services are equal to each other by doing a deep
// equality check on data returned by most of Service's methods. Methods not
// checked are HasFilter and GetExtraConfig.
func svcEqual(a, b Service) bool {
ctx := context.Background()
var (
errA error
errB error
)
entityA := a.GetServiceID()
entityB := b.GetServiceID()
if entityA != entityB {
return false
}
hostsA, errA := a.GetHosts(ctx)
hostsB, errB := b.GetHosts(ctx)
if errA != errB || !reflect.DeepEqual(hostsA, hostsB) {
return false
}
portsA, errA := a.GetPorts(ctx)
portsB, errB := b.GetPorts(ctx)
if errA != errB && !reflect.DeepEqual(portsA, portsB) {
return false
}
adA, errA := a.GetADIdentifiers(ctx)
adB, errB := b.GetADIdentifiers(ctx)
if errA != errB || !reflect.DeepEqual(adA, adB) {
return false
}
if !reflect.DeepEqual(a.GetCheckNames(ctx), b.GetCheckNames(ctx)) {
return false
}
hostnameA, errA := a.GetHostname(ctx)
hostnameB, errB := b.GetHostname(ctx)
if errA != errB || hostnameA != hostnameB {
return false
}
pidA, errA := a.GetPid(ctx)
pidB, errB := b.GetPid(ctx)
if errA != errB || pidA != pidB {
return false
}
return a.IsReady(ctx) == b.IsReady(ctx)
}