-
Notifications
You must be signed in to change notification settings - Fork 490
/
Copy pathnodejs.go
64 lines (53 loc) · 2.03 KB
/
nodejs.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package instrumentation
import (
corev1 "k8s.io/api/core/v1"
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
)
const (
envNodeOptions = "NODE_OPTIONS"
nodeRequireArgument = " --require /otel-auto-instrumentation-nodejs/autoinstrumentation.js"
nodejsInitContainerName = initContainerName + "-nodejs"
nodejsVolumeName = volumeName + "-nodejs"
nodejsInstrMountPath = "/otel-auto-instrumentation-nodejs"
)
func injectNodeJSSDK(nodeJSSpec v1alpha1.NodeJS, pod corev1.Pod, index int) (corev1.Pod, error) {
volume := instrVolume(nodeJSSpec.VolumeClaimTemplate, nodejsVolumeName, nodeJSSpec.VolumeSizeLimit)
// caller checks if there is at least one container.
container := &pod.Spec.Containers[index]
err := validateContainerEnv(container.Env, envNodeOptions)
if err != nil {
return pod, err
}
// inject NodeJS instrumentation spec env vars.
container.Env = appendIfNotSet(container.Env, nodeJSSpec.Env...)
idx := getIndexOfEnv(container.Env, envNodeOptions)
if idx == -1 {
container.Env = append(container.Env, corev1.EnvVar{
Name: envNodeOptions,
Value: nodeRequireArgument,
})
} else if idx > -1 {
container.Env[idx].Value = container.Env[idx].Value + nodeRequireArgument
}
container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{
Name: volume.Name,
MountPath: nodejsInstrMountPath,
})
// We just inject Volumes and init containers for the first processed container
if isInitContainerMissing(pod, nodejsInitContainerName) {
pod.Spec.Volumes = append(pod.Spec.Volumes, volume)
pod.Spec.InitContainers = append(pod.Spec.InitContainers, corev1.Container{
Name: nodejsInitContainerName,
Image: nodeJSSpec.Image,
Command: []string{"cp", "-r", "/autoinstrumentation/.", nodejsInstrMountPath},
Resources: nodeJSSpec.Resources,
VolumeMounts: []corev1.VolumeMount{{
Name: volume.Name,
MountPath: nodejsInstrMountPath,
}},
})
}
return pod, nil
}