@@ -253,26 +253,36 @@ func getContainerIndex(containerName string, pod corev1.Pod) int {
253
253
func (i * sdkInjector ) injectCommonEnvVar (otelinst v1alpha1.Instrumentation , pod corev1.Pod , index int ) corev1.Pod {
254
254
container := & pod .Spec .Containers [index ]
255
255
256
- container .Env = appendIfNotSet (container .Env ,
257
- corev1.EnvVar {
256
+ idx := getIndexOfEnv (container .Env , constants .EnvPodIP )
257
+ if idx == - 1 {
258
+ container .Env = append ([]corev1.EnvVar {{
258
259
Name : constants .EnvPodIP ,
259
260
ValueFrom : & corev1.EnvVarSource {
260
261
FieldRef : & corev1.ObjectFieldSelector {
261
262
FieldPath : "status.podIP" ,
262
263
},
263
264
},
264
- },
265
- corev1.EnvVar {
265
+ }}, container .Env ... )
266
+ }
267
+
268
+ idx = getIndexOfEnv (container .Env , constants .EnvNodeIP )
269
+ if idx == - 1 {
270
+ container .Env = append ([]corev1.EnvVar {{
266
271
Name : constants .EnvNodeIP ,
267
272
ValueFrom : & corev1.EnvVarSource {
268
273
FieldRef : & corev1.ObjectFieldSelector {
269
274
FieldPath : "status.hostIP" ,
270
275
},
271
276
},
272
- },
273
- )
277
+ }}, container . Env ... )
278
+ }
274
279
275
- container .Env = appendIfNotSet (container .Env , otelinst .Spec .Env ... )
280
+ for _ , env := range otelinst .Spec .Env {
281
+ idx := getIndexOfEnv (container .Env , env .Name )
282
+ if idx == - 1 {
283
+ container .Env = append (container .Env , env )
284
+ }
285
+ }
276
286
return pod
277
287
}
278
288
@@ -287,10 +297,13 @@ func (i *sdkInjector) injectCommonSDKConfig(ctx context.Context, otelinst v1alph
287
297
container := & pod .Spec .Containers [agentIndex ]
288
298
useLabelsForResourceAttributes := otelinst .Spec .Defaults .UseLabelsForResourceAttributes
289
299
resourceMap := i .createResourceMap (ctx , otelinst , ns , pod , appIndex )
290
- container .Env = appendIfNotSet (container .Env , corev1.EnvVar {
291
- Name : constants .EnvOTELServiceName ,
292
- Value : chooseServiceName (pod , useLabelsForResourceAttributes , resourceMap , appIndex ),
293
- })
300
+ idx := getIndexOfEnv (container .Env , constants .EnvOTELServiceName )
301
+ if idx == - 1 {
302
+ container .Env = append (container .Env , corev1.EnvVar {
303
+ Name : constants .EnvOTELServiceName ,
304
+ Value : chooseServiceName (pod , useLabelsForResourceAttributes , resourceMap , appIndex ),
305
+ })
306
+ }
294
307
configureExporter (otelinst .Spec .Exporter , & pod , container )
295
308
296
309
// Always retrieve the pod name from the Downward API. Ensure that the OTEL_RESOURCE_ATTRIBUTES_POD_NAME env exists.
@@ -319,7 +332,7 @@ func (i *sdkInjector) injectCommonSDKConfig(ctx context.Context, otelinst v1alph
319
332
}
320
333
}
321
334
322
- idx : = getIndexOfEnv (container .Env , constants .EnvOTELResourceAttrs )
335
+ idx = getIndexOfEnv (container .Env , constants .EnvOTELResourceAttrs )
323
336
if idx == - 1 || ! strings .Contains (container .Env [idx ].Value , string (semconv .ServiceVersionKey )) {
324
337
vsn := chooseServiceVersion (pod , useLabelsForResourceAttributes , appIndex )
325
338
if vsn != "" {
0 commit comments