@@ -253,36 +253,26 @@ 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
- idx := getIndexOfEnv (container .Env , constants .EnvPodIP )
257
- if idx == - 1 {
258
- container .Env = append ([]corev1.EnvVar {{
256
+ container .Env = appendIfNotSet (container .Env ,
257
+ corev1.EnvVar {
259
258
Name : constants .EnvPodIP ,
260
259
ValueFrom : & corev1.EnvVarSource {
261
260
FieldRef : & corev1.ObjectFieldSelector {
262
261
FieldPath : "status.podIP" ,
263
262
},
264
263
},
265
- }}, container .Env ... )
266
- }
267
-
268
- idx = getIndexOfEnv (container .Env , constants .EnvNodeIP )
269
- if idx == - 1 {
270
- container .Env = append ([]corev1.EnvVar {{
264
+ },
265
+ corev1.EnvVar {
271
266
Name : constants .EnvNodeIP ,
272
267
ValueFrom : & corev1.EnvVarSource {
273
268
FieldRef : & corev1.ObjectFieldSelector {
274
269
FieldPath : "status.hostIP" ,
275
270
},
276
271
},
277
- }}, container . Env ... )
278
- }
272
+ },
273
+ )
279
274
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
- }
275
+ container .Env = appendIfNotSet (container .Env , otelinst .Spec .Env ... )
286
276
return pod
287
277
}
288
278
@@ -297,13 +287,10 @@ func (i *sdkInjector) injectCommonSDKConfig(ctx context.Context, otelinst v1alph
297
287
container := & pod .Spec .Containers [agentIndex ]
298
288
useLabelsForResourceAttributes := otelinst .Spec .Defaults .UseLabelsForResourceAttributes
299
289
resourceMap := i .createResourceMap (ctx , otelinst , ns , pod , appIndex )
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
- }
290
+ container .Env = appendIfNotSet (container .Env , corev1.EnvVar {
291
+ Name : constants .EnvOTELServiceName ,
292
+ Value : chooseServiceName (pod , useLabelsForResourceAttributes , resourceMap , appIndex ),
293
+ })
307
294
configureExporter (otelinst .Spec .Exporter , & pod , container )
308
295
309
296
// Always retrieve the pod name from the Downward API. Ensure that the OTEL_RESOURCE_ATTRIBUTES_POD_NAME env exists.
@@ -332,7 +319,7 @@ func (i *sdkInjector) injectCommonSDKConfig(ctx context.Context, otelinst v1alph
332
319
}
333
320
}
334
321
335
- idx = getIndexOfEnv (container .Env , constants .EnvOTELResourceAttrs )
322
+ idx : = getIndexOfEnv (container .Env , constants .EnvOTELResourceAttrs )
336
323
if idx == - 1 || ! strings .Contains (container .Env [idx ].Value , string (semconv .ServiceVersionKey )) {
337
324
vsn := chooseServiceVersion (pod , useLabelsForResourceAttributes , appIndex )
338
325
if vsn != "" {
@@ -661,6 +648,20 @@ func getIndexOfEnv(envs []corev1.EnvVar, name string) int {
661
648
return - 1
662
649
}
663
650
651
+ func appendIfNotSet (envs []corev1.EnvVar , newEnvVars ... corev1.EnvVar ) []corev1.EnvVar {
652
+ keys := make (map [string ]struct {}, len (envs ))
653
+ for _ , e := range envs {
654
+ keys [e .Name ] = struct {}{}
655
+ }
656
+ for _ , envVar := range newEnvVars {
657
+ if _ , ok := keys [envVar .Name ]; ! ok {
658
+ envs = append (envs , envVar )
659
+ keys [envVar .Name ] = struct {}{}
660
+ }
661
+ }
662
+ return envs
663
+ }
664
+
664
665
func moveEnvToListEnd (envs []corev1.EnvVar , idx int ) []corev1.EnvVar {
665
666
if idx >= 0 && idx < len (envs ) {
666
667
envToMove := envs [idx ]
0 commit comments