Skip to content

Commit 0b77dc2

Browse files
committed
order env vars to match existing
1 parent 4f7cbe8 commit 0b77dc2

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

pkg/instrumentation/sdk.go

+25-12
Original file line numberDiff line numberDiff line change
@@ -253,26 +253,36 @@ func getContainerIndex(containerName string, pod corev1.Pod) int {
253253
func (i *sdkInjector) injectCommonEnvVar(otelinst v1alpha1.Instrumentation, pod corev1.Pod, index int) corev1.Pod {
254254
container := &pod.Spec.Containers[index]
255255

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{{
258259
Name: constants.EnvPodIP,
259260
ValueFrom: &corev1.EnvVarSource{
260261
FieldRef: &corev1.ObjectFieldSelector{
261262
FieldPath: "status.podIP",
262263
},
263264
},
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{{
266271
Name: constants.EnvNodeIP,
267272
ValueFrom: &corev1.EnvVarSource{
268273
FieldRef: &corev1.ObjectFieldSelector{
269274
FieldPath: "status.hostIP",
270275
},
271276
},
272-
},
273-
)
277+
}}, container.Env...)
278+
}
274279

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+
}
276286
return pod
277287
}
278288

@@ -287,10 +297,13 @@ func (i *sdkInjector) injectCommonSDKConfig(ctx context.Context, otelinst v1alph
287297
container := &pod.Spec.Containers[agentIndex]
288298
useLabelsForResourceAttributes := otelinst.Spec.Defaults.UseLabelsForResourceAttributes
289299
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+
}
294307
configureExporter(otelinst.Spec.Exporter, &pod, container)
295308

296309
// 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
319332
}
320333
}
321334

322-
idx := getIndexOfEnv(container.Env, constants.EnvOTELResourceAttrs)
335+
idx = getIndexOfEnv(container.Env, constants.EnvOTELResourceAttrs)
323336
if idx == -1 || !strings.Contains(container.Env[idx].Value, string(semconv.ServiceVersionKey)) {
324337
vsn := chooseServiceVersion(pod, useLabelsForResourceAttributes, appIndex)
325338
if vsn != "" {

0 commit comments

Comments
 (0)