-
Notifications
You must be signed in to change notification settings - Fork 395
/
podmutator.go
409 lines (360 loc) · 16.3 KB
/
podmutator.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
397
398
399
400
401
402
403
404
405
406
407
408
409
// Copyright The 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 instrumentation
import (
"context"
"errors"
"fmt"
"strings"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
"github.com/open-telemetry/opentelemetry-operator/internal/webhook/podmutation"
"github.com/open-telemetry/opentelemetry-operator/pkg/featuregate"
)
var (
errMultipleInstancesPossible = errors.New("multiple OpenTelemetry Instrumentation instances available, cannot determine which one to select")
errNoInstancesAvailable = errors.New("no OpenTelemetry Instrumentation instances available")
)
type instPodMutator struct {
Client client.Client
sdkInjector *sdkInjector
Logger logr.Logger
Recorder record.EventRecorder
config config.Config
}
type instrumentationWithContainers struct {
Instrumentation *v1alpha1.Instrumentation
Containers string
AdditionalAnnotations map[string]string
}
type languageInstrumentations struct {
Java instrumentationWithContainers
NodeJS instrumentationWithContainers
Python instrumentationWithContainers
DotNet instrumentationWithContainers
ApacheHttpd instrumentationWithContainers
Nginx instrumentationWithContainers
Go instrumentationWithContainers
Sdk instrumentationWithContainers
}
// Check if single instrumentation is configured for Pod and return which is configured.
func (langInsts languageInstrumentations) isSingleInstrumentationEnabled() bool {
count := 0
if langInsts.Java.Instrumentation != nil {
count++
}
if langInsts.NodeJS.Instrumentation != nil {
count++
}
if langInsts.Python.Instrumentation != nil {
count++
}
if langInsts.DotNet.Instrumentation != nil {
count++
}
if langInsts.ApacheHttpd.Instrumentation != nil {
count++
}
if langInsts.Nginx.Instrumentation != nil {
count++
}
if langInsts.Go.Instrumentation != nil {
count++
}
if langInsts.Sdk.Instrumentation != nil {
count++
}
return count == 1
}
// Check if specific containers are provided for configured instrumentation.
func (langInsts languageInstrumentations) areContainerNamesConfiguredForMultipleInstrumentations() (bool, error) {
var instrWithoutContainers int
var instrWithContainers int
var allContainers []string
// Check for instrumentations with and without containers.
if langInsts.Java.Instrumentation != nil {
instrWithContainers += isInstrWithContainers(langInsts.Java)
instrWithoutContainers += isInstrWithoutContainers(langInsts.Java)
allContainers = append(allContainers, langInsts.Java.Containers)
}
if langInsts.NodeJS.Instrumentation != nil {
instrWithContainers += isInstrWithContainers(langInsts.NodeJS)
instrWithoutContainers += isInstrWithoutContainers(langInsts.NodeJS)
allContainers = append(allContainers, langInsts.NodeJS.Containers)
}
if langInsts.Python.Instrumentation != nil {
instrWithContainers += isInstrWithContainers(langInsts.Python)
instrWithoutContainers += isInstrWithoutContainers(langInsts.Python)
allContainers = append(allContainers, langInsts.Python.Containers)
}
if langInsts.DotNet.Instrumentation != nil {
instrWithContainers += isInstrWithContainers(langInsts.DotNet)
instrWithoutContainers += isInstrWithoutContainers(langInsts.DotNet)
allContainers = append(allContainers, langInsts.DotNet.Containers)
}
if langInsts.ApacheHttpd.Instrumentation != nil {
instrWithContainers += isInstrWithContainers(langInsts.ApacheHttpd)
instrWithoutContainers += isInstrWithoutContainers(langInsts.ApacheHttpd)
allContainers = append(allContainers, langInsts.ApacheHttpd.Containers)
}
if langInsts.Nginx.Instrumentation != nil {
instrWithContainers += isInstrWithContainers(langInsts.Nginx)
instrWithoutContainers += isInstrWithoutContainers(langInsts.Nginx)
allContainers = append(allContainers, langInsts.Nginx.Containers)
}
if langInsts.Go.Instrumentation != nil {
instrWithContainers += isInstrWithContainers(langInsts.Go)
instrWithoutContainers += isInstrWithoutContainers(langInsts.Go)
allContainers = append(allContainers, langInsts.Go.Containers)
}
if langInsts.Sdk.Instrumentation != nil {
instrWithContainers += isInstrWithContainers(langInsts.Sdk)
instrWithoutContainers += isInstrWithoutContainers(langInsts.Sdk)
allContainers = append(allContainers, langInsts.Sdk.Containers)
}
// Look for duplicated containers.
containerDuplicates := findDuplicatedContainers(allContainers)
if containerDuplicates != nil {
return false, containerDuplicates
}
// Look for mixed multiple instrumentations with and without container names.
if instrWithoutContainers > 0 && instrWithContainers > 0 {
return false, fmt.Errorf("incorrect instrumentation configuration - please provide container names for all instrumentations")
}
// Look for multiple instrumentations without container names.
if instrWithoutContainers > 1 && instrWithContainers == 0 {
return false, fmt.Errorf("incorrect instrumentation configuration - please provide container names for all instrumentations")
}
if instrWithoutContainers == 0 && instrWithContainers == 0 {
return false, fmt.Errorf("instrumentation configuration not provided")
}
return true, nil
}
// Set containers for configured instrumentation.
func (langInsts *languageInstrumentations) setInstrumentationLanguageContainers(containers string) {
if langInsts.Java.Instrumentation != nil {
langInsts.Java.Containers = containers
}
if langInsts.NodeJS.Instrumentation != nil {
langInsts.NodeJS.Containers = containers
}
if langInsts.Python.Instrumentation != nil {
langInsts.Python.Containers = containers
}
if langInsts.DotNet.Instrumentation != nil {
langInsts.DotNet.Containers = containers
}
if langInsts.ApacheHttpd.Instrumentation != nil {
langInsts.ApacheHttpd.Containers = containers
}
if langInsts.Nginx.Instrumentation != nil {
langInsts.Nginx.Containers = containers
}
if langInsts.Go.Instrumentation != nil {
langInsts.Go.Containers = containers
}
if langInsts.Sdk.Instrumentation != nil {
langInsts.Sdk.Containers = containers
}
}
var _ podmutation.PodMutator = (*instPodMutator)(nil)
func NewMutator(logger logr.Logger, client client.Client, recorder record.EventRecorder, cfg config.Config) *instPodMutator {
return &instPodMutator{
Logger: logger,
Client: client,
sdkInjector: &sdkInjector{
logger: logger,
client: client,
},
Recorder: recorder,
config: cfg,
}
}
func (pm *instPodMutator) Mutate(ctx context.Context, ns corev1.Namespace, pod corev1.Pod) (corev1.Pod, error) {
logger := pm.Logger.WithValues("namespace", pod.Namespace, "name", pod.Name)
// We check if Pod is already instrumented.
if isAutoInstrumentationInjected(pod) {
logger.Info("Skipping pod instrumentation - already instrumented")
return pod, nil
}
var inst *v1alpha1.Instrumentation
var err error
insts := languageInstrumentations{}
// We bail out if any annotation fails to process.
if inst, err = pm.getInstrumentationInstance(ctx, ns, pod, annotationInjectJava); err != nil {
// we still allow the pod to be created, but we log a message to the operator's logs
logger.Error(err, "failed to select an OpenTelemetry Instrumentation instance for this pod")
return pod, err
}
if featuregate.EnableJavaAutoInstrumentationSupport.IsEnabled() || inst == nil {
insts.Java.Instrumentation = inst
} else {
logger.Error(nil, "support for Java auto instrumentation is not enabled")
pm.Recorder.Event(pod.DeepCopy(), "Warning", "InstrumentationRequestRejected", "support for Java auto instrumentation is not enabled")
}
if inst, err = pm.getInstrumentationInstance(ctx, ns, pod, annotationInjectNodeJS); err != nil {
// we still allow the pod to be created, but we log a message to the operator's logs
logger.Error(err, "failed to select an OpenTelemetry Instrumentation instance for this pod")
return pod, err
}
if featuregate.EnableNodeJSAutoInstrumentationSupport.IsEnabled() || inst == nil {
insts.NodeJS.Instrumentation = inst
} else {
logger.Error(nil, "support for NodeJS auto instrumentation is not enabled")
pm.Recorder.Event(pod.DeepCopy(), "Warning", "InstrumentationRequestRejected", "support for NodeJS auto instrumentation is not enabled")
}
if inst, err = pm.getInstrumentationInstance(ctx, ns, pod, annotationInjectPython); err != nil {
// we still allow the pod to be created, but we log a message to the operator's logs
logger.Error(err, "failed to select an OpenTelemetry Instrumentation instance for this pod")
return pod, err
}
if featuregate.EnablePythonAutoInstrumentationSupport.IsEnabled() || inst == nil {
insts.Python.Instrumentation = inst
} else {
logger.Error(nil, "support for Python auto instrumentation is not enabled")
pm.Recorder.Event(pod.DeepCopy(), "Warning", "InstrumentationRequestRejected", "support for Python auto instrumentation is not enabled")
}
if inst, err = pm.getInstrumentationInstance(ctx, ns, pod, annotationInjectDotNet); err != nil {
// we still allow the pod to be created, but we log a message to the operator's logs
logger.Error(err, "failed to select an OpenTelemetry Instrumentation instance for this pod")
return pod, err
}
if pm.config.EnableDotNetAutoInstrumentation() || inst == nil {
insts.DotNet.Instrumentation = inst
insts.DotNet.AdditionalAnnotations = map[string]string{annotationDotNetRuntime: annotationValue(ns.ObjectMeta, pod.ObjectMeta, annotationDotNetRuntime)}
} else {
logger.Error(nil, "support for .NET auto instrumentation is not enabled")
pm.Recorder.Event(pod.DeepCopy(), "Warning", "InstrumentationRequestRejected", "support for .NET auto instrumentation is not enabled")
}
if inst, err = pm.getInstrumentationInstance(ctx, ns, pod, annotationInjectGo); err != nil {
// we still allow the pod to be created, but we log a message to the operator's logs
logger.Error(err, "failed to select an OpenTelemetry Instrumentation instance for this pod")
return pod, err
}
if featuregate.EnableGoAutoInstrumentationSupport.IsEnabled() || inst == nil {
insts.Go.Instrumentation = inst
} else {
logger.Error(err, "support for Go auto instrumentation is not enabled")
pm.Recorder.Event(pod.DeepCopy(), "Warning", "InstrumentationRequestRejected", "support for Go auto instrumentation is not enabled")
}
if inst, err = pm.getInstrumentationInstance(ctx, ns, pod, annotationInjectApacheHttpd); err != nil {
// we still allow the pod to be created, but we log a message to the operator's logs
logger.Error(err, "failed to select an OpenTelemetry Instrumentation instance for this pod")
return pod, err
}
if pm.config.EnableApacheHttpdAutoInstrumentation() || inst == nil {
insts.ApacheHttpd.Instrumentation = inst
} else {
logger.Error(nil, "support for Apache HTTPD auto instrumentation is not enabled")
pm.Recorder.Event(pod.DeepCopy(), "Warning", "InstrumentationRequestRejected", "support for Apache HTTPD auto instrumentation is not enabled")
}
if inst, err = pm.getInstrumentationInstance(ctx, ns, pod, annotationInjectNginx); err != nil {
// we still allow the pod to be created, but we log a message to the operator's logs
logger.Error(err, "failed to select an OpenTelemetry Instrumentation instance for this pod")
return pod, err
}
if featuregate.EnableNginxAutoInstrumentationSupport.IsEnabled() || inst == nil {
insts.Nginx.Instrumentation = inst
} else {
logger.Error(nil, "support for Nginx auto instrumentation is not enabled")
pm.Recorder.Event(pod.DeepCopy(), "Warning", "InstrumentationRequestRejected", "support for Nginx auto instrumentation is not enabled")
}
if inst, err = pm.getInstrumentationInstance(ctx, ns, pod, annotationInjectSdk); err != nil {
// we still allow the pod to be created, but we log a message to the operator's logs
logger.Error(err, "failed to select an OpenTelemetry Instrumentation instance for this pod")
return pod, err
}
insts.Sdk.Instrumentation = inst
if insts.Java.Instrumentation == nil && insts.NodeJS.Instrumentation == nil && insts.Python.Instrumentation == nil &&
insts.DotNet.Instrumentation == nil && insts.Go.Instrumentation == nil && insts.ApacheHttpd.Instrumentation == nil &&
insts.Nginx.Instrumentation == nil &&
insts.Sdk.Instrumentation == nil {
logger.V(1).Info("annotation not present in deployment, skipping instrumentation injection")
return pod, nil
}
// We retrieve the annotation for podname
if pm.config.EnableMultiInstrumentation() {
// We use annotations specific for instrumentation language
insts.Java.Containers = annotationValue(ns.ObjectMeta, pod.ObjectMeta, annotationInjectJavaContainersName)
insts.NodeJS.Containers = annotationValue(ns.ObjectMeta, pod.ObjectMeta, annotationInjectNodeJSContainersName)
insts.Python.Containers = annotationValue(ns.ObjectMeta, pod.ObjectMeta, annotationInjectPythonContainersName)
insts.DotNet.Containers = annotationValue(ns.ObjectMeta, pod.ObjectMeta, annotationInjectDotnetContainersName)
insts.Go.Containers = annotationValue(ns.ObjectMeta, pod.ObjectMeta, annotationInjectGoContainersName)
insts.ApacheHttpd.Containers = annotationValue(ns.ObjectMeta, pod.ObjectMeta, annotationInjectApacheHttpdContainersName)
insts.Nginx.Containers = annotationValue(ns.ObjectMeta, pod.ObjectMeta, annotationInjectNginxContainersName)
insts.Sdk.Containers = annotationValue(ns.ObjectMeta, pod.ObjectMeta, annotationInjectSdkContainersName)
// We check if provided annotations and instrumentations are valid
ok, msg := insts.areContainerNamesConfiguredForMultipleInstrumentations()
if !ok {
logger.V(1).Error(msg, "skipping instrumentation injection")
return pod, nil
}
} else {
// We use general annotation for container names
// only when multi instrumentation is disabled
singleInstrEnabled := insts.isSingleInstrumentationEnabled()
if singleInstrEnabled {
generalContainerNames := annotationValue(ns.ObjectMeta, pod.ObjectMeta, annotationInjectContainerName)
insts.setInstrumentationLanguageContainers(generalContainerNames)
} else {
logger.V(1).Error(fmt.Errorf("multiple injection annotations present"), "skipping instrumentation injection")
return pod, nil
}
}
// once it's been determined that instrumentation is desired, none exists yet, and we know which instance it should talk to,
// we should inject the instrumentation.
modifiedPod := pod
modifiedPod = pm.sdkInjector.inject(ctx, insts, ns, modifiedPod, pm.config)
return modifiedPod, nil
}
func (pm *instPodMutator) getInstrumentationInstance(ctx context.Context, ns corev1.Namespace, pod corev1.Pod, instAnnotation string) (*v1alpha1.Instrumentation, error) {
instValue := annotationValue(ns.ObjectMeta, pod.ObjectMeta, instAnnotation)
if len(instValue) == 0 || strings.EqualFold(instValue, "false") {
return nil, nil
}
if strings.EqualFold(instValue, "true") {
return pm.selectInstrumentationInstanceFromNamespace(ctx, ns)
}
var instNamespacedName types.NamespacedName
if instNamespace, instName, namespaced := strings.Cut(instValue, "/"); namespaced {
instNamespacedName = types.NamespacedName{Name: instName, Namespace: instNamespace}
} else {
instNamespacedName = types.NamespacedName{Name: instValue, Namespace: ns.Name}
}
otelInst := &v1alpha1.Instrumentation{}
err := pm.Client.Get(ctx, instNamespacedName, otelInst)
if err != nil {
return nil, err
}
return otelInst, nil
}
func (pm *instPodMutator) selectInstrumentationInstanceFromNamespace(ctx context.Context, ns corev1.Namespace) (*v1alpha1.Instrumentation, error) {
var otelInsts v1alpha1.InstrumentationList
if err := pm.Client.List(ctx, &otelInsts, client.InNamespace(ns.Name)); err != nil {
return nil, err
}
switch s := len(otelInsts.Items); {
case s == 0:
return nil, errNoInstancesAvailable
case s > 1:
return nil, errMultipleInstancesPossible
default:
return &otelInsts.Items[0], nil
}
}