@@ -17,7 +17,6 @@ package instrumentation
17
17
import (
18
18
"fmt"
19
19
20
- "github.com/go-logr/logr"
21
20
corev1 "k8s.io/api/core/v1"
22
21
23
22
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
@@ -40,11 +39,17 @@ const (
40
39
dotNetStartupHookPath = "/otel-auto-instrumentation/netcoreapp3.1/OpenTelemetry.AutoInstrumentation.StartupHook.dll"
41
40
)
42
41
43
- func injectDotNetSDK (logger logr.Logger , dotNetSpec v1alpha1.DotNet , pod corev1.Pod , index int ) corev1.Pod {
44
- // caller checks if there is at least one container
45
- container := pod .Spec .Containers [index ]
42
+ func injectDotNetSDK (dotNetSpec v1alpha1.DotNet , pod corev1.Pod , index int ) (corev1.Pod , error ) {
46
43
47
- // inject env vars
44
+ // caller checks if there is at least one container.
45
+ container := & pod .Spec .Containers [index ]
46
+
47
+ err := validateContainerEnv (container .Env , envDotNetStartupHook , envDotNetAdditionalDeps , envDotNetSharedStore )
48
+ if err != nil {
49
+ return pod , err
50
+ }
51
+
52
+ // inject .NET instrumentation spec env vars.
48
53
for _ , env := range dotNetSpec .Env {
49
54
idx := getIndexOfEnv (container .Env , env .Name )
50
55
if idx == - 1 {
@@ -57,40 +62,26 @@ func injectDotNetSDK(logger logr.Logger, dotNetSpec v1alpha1.DotNet, pod corev1.
57
62
concatEnvValues = true
58
63
)
59
64
60
- if ! trySetEnvVar (logger , & container , envDotNetCoreClrEnableProfiling , dotNetCoreClrEnableProfilingEnabled , doNotConcatEnvValues ) {
61
- return pod
62
- }
65
+ setDotNetEnvVar (container , envDotNetCoreClrEnableProfiling , dotNetCoreClrEnableProfilingEnabled , doNotConcatEnvValues )
63
66
64
- if ! trySetEnvVar (logger , & container , envDotNetCoreClrProfiler , dotNetCoreClrProfilerId , doNotConcatEnvValues ) {
65
- return pod
66
- }
67
+ setDotNetEnvVar (container , envDotNetCoreClrProfiler , dotNetCoreClrProfilerId , doNotConcatEnvValues )
67
68
68
- if ! trySetEnvVar (logger , & container , envDotNetCoreClrProfilerPath , dotNetCoreClrProfilerPath , doNotConcatEnvValues ) {
69
- return pod
70
- }
69
+ setDotNetEnvVar (container , envDotNetCoreClrProfilerPath , dotNetCoreClrProfilerPath , doNotConcatEnvValues )
71
70
72
- if ! trySetEnvVar (logger , & container , envDotNetStartupHook , dotNetStartupHookPath , concatEnvValues ) {
73
- return pod
74
- }
71
+ setDotNetEnvVar (container , envDotNetStartupHook , dotNetStartupHookPath , concatEnvValues )
75
72
76
- if ! trySetEnvVar (logger , & container , envDotNetAdditionalDeps , dotNetAdditionalDepsPath , concatEnvValues ) {
77
- return pod
78
- }
73
+ setDotNetEnvVar (container , envDotNetAdditionalDeps , dotNetAdditionalDepsPath , concatEnvValues )
79
74
80
- if ! trySetEnvVar (logger , & container , envDotNetOTelAutoHome , dotNetOTelAutoHomePath , doNotConcatEnvValues ) {
81
- return pod
82
- }
75
+ setDotNetEnvVar (container , envDotNetOTelAutoHome , dotNetOTelAutoHomePath , doNotConcatEnvValues )
83
76
84
- if ! trySetEnvVar (logger , & container , envDotNetSharedStore , dotNetSharedStorePath , concatEnvValues ) {
85
- return pod
86
- }
77
+ setDotNetEnvVar (container , envDotNetSharedStore , dotNetSharedStorePath , concatEnvValues )
87
78
88
79
container .VolumeMounts = append (container .VolumeMounts , corev1.VolumeMount {
89
80
Name : volumeName ,
90
81
MountPath : "/otel-auto-instrumentation" ,
91
82
})
92
83
93
- // We just inject Volumes and init containers for the first processed container
84
+ // We just inject Volumes and init containers for the first processed container.
94
85
if isInitContainerMissing (pod ) {
95
86
pod .Spec .Volumes = append (pod .Spec .Volumes , corev1.Volume {
96
87
Name : volumeName ,
@@ -108,30 +99,22 @@ func injectDotNetSDK(logger logr.Logger, dotNetSpec v1alpha1.DotNet, pod corev1.
108
99
}},
109
100
})
110
101
}
111
-
112
- pod .Spec .Containers [index ] = container
113
- return pod
102
+ return pod , nil
114
103
}
115
104
116
- func trySetEnvVar (logger logr.Logger , container * corev1.Container , envVarName string , envVarValue string , concatValues bool ) bool {
105
+ // setDotNetEnvVar function sets env var to the container if not exist already.
106
+ // value of concatValues should be set to true if the env var supports multiple values separated by :.
107
+ // If it is set to false, the original container's env var value has priority.
108
+ func setDotNetEnvVar (container * corev1.Container , envVarName string , envVarValue string , concatValues bool ) {
117
109
idx := getIndexOfEnv (container .Env , envVarName )
118
110
if idx < 0 {
119
111
container .Env = append (container .Env , corev1.EnvVar {
120
112
Name : envVarName ,
121
113
Value : envVarValue ,
122
114
})
123
- return true
115
+ return
124
116
}
125
-
126
- if container .Env [idx ].ValueFrom != nil {
127
- // TODO add to status object or submit it as an event
128
- logger .Info ("Skipping DotNet SDK injection, the container defines env var value via ValueFrom" , "envVar" , envVarName , "container" , container .Name )
129
- return false
130
- }
131
-
132
117
if concatValues {
133
118
container .Env [idx ].Value = fmt .Sprintf ("%s:%s" , container .Env [idx ].Value , envVarValue )
134
119
}
135
-
136
- return true
137
120
}
0 commit comments