forked from SumoLogic/sumologic-otel-collector
-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.go
384 lines (354 loc) · 11 KB
/
options.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
// Copyright 2020 OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package k8sprocessor
import (
"fmt"
"os"
"regexp"
"strings"
"k8s.io/apimachinery/pkg/selection"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/k8sprocessor/kube"
)
const (
filterOPEquals = "equals"
filterOPNotEquals = "not-equals"
filterOPExists = "exists"
filterOPDoesNotExist = "does-not-exist"
metadataContainerID = "containerId"
metadataContainerName = "containerName"
metadataContainerImage = "containerImage"
metadataCronJobName = "cronJobName"
metadataDaemonSetName = "daemonSetName"
metadataDeploymentName = "deploymentName"
metadataHostName = "hostName"
metadataJobName = "jobName"
metadataNamespace = "namespace"
metadataNodeName = "nodeName"
metadataPodID = "podId"
metadataPodName = "podName"
metadataReplicaSetName = "replicaSetName"
metadataServiceName = "serviceName"
metadataStartTime = "startTime"
metadataStatefulSetName = "statefulSetName"
deprecatedMetadataClusterName = "clusterName"
)
// Option represents a configuration option that can be passes.
// to the k8s-tagger
type Option func(*kubernetesprocessor) error
// WithAPIConfig provides k8s API related configuration to the processor.
// It defaults the authentication method to in-cluster auth using service accounts.
func WithAPIConfig(cfg k8sconfig.APIConfig) Option {
return func(p *kubernetesprocessor) error {
p.apiConfig = cfg
return p.apiConfig.Validate()
}
}
// WithPassthrough enables passthrough mode. In passthrough mode, the processor
// only detects and tags the pod IP and does not invoke any k8s APIs.
func WithPassthrough() Option {
return func(p *kubernetesprocessor) error {
p.passthroughMode = true
return nil
}
}
// WithOwnerLookupEnabled makes the processor pull additional owner data from K8S API
func WithOwnerLookupEnabled() Option {
return func(p *kubernetesprocessor) error {
p.rules.OwnerLookupEnabled = true
return nil
}
}
// WithExtractMetadata allows specifying options to control extraction of pod metadata.
// If no fields explicitly provided, all metadata extracted by default.
func WithExtractMetadata(fields ...string) Option {
return func(p *kubernetesprocessor) error {
if len(fields) == 0 {
fields = []string{
metadataContainerID,
metadataContainerImage,
metadataContainerName,
metadataDaemonSetName,
metadataDeploymentName,
metadataHostName,
metadataNamespace,
metadataNodeName,
metadataPodName,
metadataPodID,
metadataReplicaSetName,
metadataServiceName,
metadataStartTime,
metadataStatefulSetName,
}
}
for _, field := range fields {
switch field {
case metadataContainerID:
p.rules.ContainerID = true
case metadataContainerImage:
p.rules.ContainerImage = true
case metadataContainerName:
p.rules.ContainerName = true
case metadataCronJobName:
p.rules.CronJobName = true
case metadataDaemonSetName:
p.rules.DaemonSetName = true
case metadataDeploymentName:
p.rules.DeploymentName = true
case metadataHostName:
p.rules.HostName = true
case metadataJobName:
p.rules.JobName = true
case metadataNamespace:
p.rules.Namespace = true
case metadataNodeName:
p.rules.NodeName = true
case metadataPodID:
p.rules.PodUID = true
case metadataPodName:
p.rules.PodName = true
case metadataReplicaSetName:
p.rules.ReplicaSetName = true
case metadataServiceName:
p.rules.ServiceName = true
case metadataStartTime:
p.rules.StartTime = true
case metadataStatefulSetName:
p.rules.StatefulSetName = true
case deprecatedMetadataClusterName:
p.logger.Warn("clusterName metadata field has been deprecated and will be removed soon")
default:
return fmt.Errorf("\"%s\" is not a supported metadata field", field)
}
}
return nil
}
}
// WithExtractTags allows specifying custom tag names
func WithExtractTags(tagsMap map[string]string) Option {
return func(p *kubernetesprocessor) error {
var tags = kube.NewExtractionFieldTags()
for field, tag := range tagsMap {
switch field {
case strings.ToLower(metadataContainerID):
tags.ContainerID = tag
case strings.ToLower(metadataContainerName):
tags.ContainerName = tag
case strings.ToLower(metadataContainerImage):
tags.ContainerImage = tag
case strings.ToLower(metadataDaemonSetName):
tags.DaemonSetName = tag
case strings.ToLower(metadataDeploymentName):
tags.DeploymentName = tag
case strings.ToLower(metadataHostName):
tags.HostName = tag
case strings.ToLower(metadataNamespace):
tags.Namespace = tag
case strings.ToLower(metadataNodeName):
tags.NodeName = tag
case strings.ToLower(metadataPodID):
tags.PodUID = tag
case strings.ToLower(metadataPodName):
tags.PodName = tag
case strings.ToLower(metadataReplicaSetName):
tags.ReplicaSetName = tag
case strings.ToLower(metadataServiceName):
tags.ServiceName = tag
case strings.ToLower(metadataStartTime):
tags.StartTime = tag
case strings.ToLower(metadataStatefulSetName):
tags.StatefulSetName = tag
case strings.ToLower(deprecatedMetadataClusterName):
p.logger.Warn("clusterName metadata field has been deprecated and will be removed soon")
default:
return fmt.Errorf("\"%s\" is not a supported metadata field", field)
}
}
p.rules.Tags = tags
return nil
}
}
// WithExtractLabels allows specifying options to control extraction of pod labels.
func WithExtractLabels(labels ...FieldExtractConfig) Option {
return func(p *kubernetesprocessor) error {
labels, err := extractFieldRules("labels", labels...)
if err != nil {
return err
}
p.rules.Labels = labels
return nil
}
}
// WithExtractNamespaceLabels allows specifying options to control extraction of namespace labels.
func WithExtractNamespaceLabels(labels ...FieldExtractConfig) Option {
return func(p *kubernetesprocessor) error {
labels, err := extractFieldRules("namespace_labels", labels...)
if err != nil {
return err
}
p.rules.NamespaceLabels = labels
return nil
}
}
// WithExtractAnnotations allows specifying options to control extraction of pod annotations tags.
func WithExtractAnnotations(annotations ...FieldExtractConfig) Option {
return func(p *kubernetesprocessor) error {
annotations, err := extractFieldRules("annotations", annotations...)
if err != nil {
return err
}
p.rules.Annotations = annotations
return nil
}
}
func extractFieldRules(fieldType string, fields ...FieldExtractConfig) ([]kube.FieldExtractionRule, error) {
rules := []kube.FieldExtractionRule{}
for _, a := range fields {
name := a.TagName
if name == "" {
if a.Key == "*" {
name = fmt.Sprintf("k8s.%s.%%s", fieldType)
} else {
name = fmt.Sprintf("k8s.%s.%s", fieldType, a.Key)
}
}
var r *regexp.Regexp
if a.Regex != "" {
var err error
r, err = regexp.Compile(a.Regex)
if err != nil {
return rules, err
}
names := r.SubexpNames()
if len(names) != 2 || names[1] != "value" {
return rules, fmt.Errorf("regex must contain exactly one named submatch (value)")
}
}
rules = append(rules, kube.FieldExtractionRule{
Name: name, Key: a.Key, Regex: r,
})
}
return rules, nil
}
// WithFilterNode allows specifying options to control filtering pods by a node/host.
func WithFilterNode(node, nodeFromEnvVar string) Option {
return func(p *kubernetesprocessor) error {
if nodeFromEnvVar != "" {
p.filters.Node = os.Getenv(nodeFromEnvVar)
return nil
}
p.filters.Node = node
return nil
}
}
// WithFilterNamespace allows specifying options to control filtering pods by a namespace.
func WithFilterNamespace(ns string) Option {
return func(p *kubernetesprocessor) error {
p.filters.Namespace = ns
return nil
}
}
// WithFilterLabels allows specifying options to control filtering pods by pod labels.
func WithFilterLabels(filters ...FieldFilterConfig) Option {
return func(p *kubernetesprocessor) error {
labels := []kube.FieldFilter{}
for _, f := range filters {
if f.Op == "" {
f.Op = filterOPEquals
}
var op selection.Operator
switch f.Op {
case filterOPEquals:
op = selection.Equals
case filterOPNotEquals:
op = selection.NotEquals
case filterOPExists:
op = selection.Exists
case filterOPDoesNotExist:
op = selection.DoesNotExist
default:
return fmt.Errorf("'%s' is not a valid label filter operation for key=%s, value=%s", f.Op, f.Key, f.Value)
}
labels = append(labels, kube.FieldFilter{
Key: f.Key,
Value: f.Value,
Op: op,
})
}
p.filters.Labels = labels
return nil
}
}
// WithFilterFields allows specifying options to control filtering pods by pod fields.
func WithFilterFields(filters ...FieldFilterConfig) Option {
return func(p *kubernetesprocessor) error {
fields := []kube.FieldFilter{}
for _, f := range filters {
if f.Op == "" {
f.Op = filterOPEquals
}
var op selection.Operator
switch f.Op {
case filterOPEquals:
op = selection.Equals
case filterOPNotEquals:
op = selection.NotEquals
default:
return fmt.Errorf("'%s' is not a valid field filter operation for key=%s, value=%s", f.Op, f.Key, f.Value)
}
fields = append(fields, kube.FieldFilter{
Key: f.Key,
Value: f.Value,
Op: op,
})
}
p.filters.Fields = fields
return nil
}
}
// WithExtractPodAssociations allows specifying options to associate pod metadata with incoming resource
func WithExtractPodAssociations(podAssociations ...PodAssociationConfig) Option {
return func(p *kubernetesprocessor) error {
associations := make([]kube.Association, 0, len(podAssociations))
for _, association := range podAssociations {
associations = append(associations, kube.Association{
From: association.From,
Name: association.Name,
})
}
p.podAssociations = associations
return nil
}
}
// WithDelimiter sets delimiter to use by kubernetesprocessor
func WithDelimiter(delimiter string) Option {
return func(p *kubernetesprocessor) error {
p.delimiter = delimiter
return nil
}
}
// WithExcludes allows specifying pods to exclude
func WithExcludes(excludeConfig ExcludeConfig) Option {
return func(p *kubernetesprocessor) error {
excludes := kube.Excludes{}
names := excludeConfig.Pods
for _, name := range names {
excludes.Pods = append(excludes.Pods, kube.ExcludePods{
Name: regexp.MustCompile(name.Name)},
)
}
p.podIgnore = excludes
return nil
}
}