Skip to content

Commit feddaf0

Browse files
Merge pull request #2098 from joelanford/fix/subscription-env-merge
fix environment variable merging from subscription.config
2 parents d3ba073 + 75326e5 commit feddaf0

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

pkg/controller/operators/olm/overrides/inject/inject.go

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,40 @@ func InjectEnvIntoDeployment(podSpec *corev1.PodSpec, envVars []corev1.EnvVar) e
2525
return nil
2626
}
2727

28-
func mergeEnvVars(containerEnvVars []corev1.EnvVar, newEnvVars []corev1.EnvVar) (merged []corev1.EnvVar) {
29-
merged = containerEnvVars
30-
31-
for _, newEnvVar := range newEnvVars {
32-
existing, found := findEnvVar(containerEnvVars, newEnvVar.Name)
33-
if found {
34-
existing.Value = newEnvVar.Value
28+
func mergeEnvVars(containerEnvVars []corev1.EnvVar, newEnvVars []corev1.EnvVar) []corev1.EnvVar {
29+
// Build a map of environment variables.
30+
// newEnvVars always overrides containerEnvVars.
31+
mergedMap := map[string]corev1.EnvVar{}
32+
for _, envVar := range containerEnvVars {
33+
mergedMap[envVar.Name] = envVar
34+
}
35+
for _, envVar := range newEnvVars {
36+
mergedMap[envVar.Name] = envVar
37+
}
38+
39+
// To keep things in the expected order, always put the
40+
// original environment variable names into the merged
41+
// output in place first.
42+
merged := make([]corev1.EnvVar, 0, len(mergedMap))
43+
for _, e := range containerEnvVars {
44+
envVar := mergedMap[e.Name]
45+
merged = append(merged, envVar)
46+
delete(mergedMap, e.Name)
47+
}
48+
49+
// Then for any remaining newEnvVars (i.e. env vars
50+
// that weren't present in the containerEnvVars), add
51+
// them at the end in the order they were provided in
52+
// the subscription.
53+
for _, e := range newEnvVars {
54+
envVar, ok := mergedMap[e.Name]
55+
if !ok {
3556
continue
3657
}
37-
38-
merged = append(merged, corev1.EnvVar{
39-
Name: newEnvVar.Name,
40-
Value: newEnvVar.Value,
41-
})
42-
}
43-
44-
return
45-
}
46-
47-
func findEnvVar(proxyEnvVar []corev1.EnvVar, name string) (foundEnvVar *corev1.EnvVar, found bool) {
48-
for i := range proxyEnvVar {
49-
if name == proxyEnvVar[i].Name {
50-
// Environment variable names are case sensitive.
51-
found = true
52-
foundEnvVar = &proxyEnvVar[i]
53-
54-
break
55-
}
58+
merged = append(merged, envVar)
5659
}
5760

58-
return
61+
return merged
5962
}
6063

6164
// InjectVolumesIntoDeployment injects the provided Volumes

pkg/controller/operators/olm/overrides/inject/inject_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package inject_test
33
import (
44
"testing"
55

6-
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm/overrides/inject"
76
"github.com/stretchr/testify/assert"
87
corev1 "k8s.io/api/core/v1"
98
"k8s.io/apimachinery/pkg/api/resource"
9+
10+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm/overrides/inject"
1011
)
1112

1213
var (
@@ -386,6 +387,10 @@ func TestInjectEnvIntoDeployment(t *testing.T) {
386387
},
387388
},
388389
envVar: []corev1.EnvVar{
390+
corev1.EnvVar{
391+
Name: "extra",
392+
Value: "extra_value",
393+
},
389394
corev1.EnvVar{
390395
Name: "foo",
391396
Value: "new_foo_value",
@@ -407,6 +412,10 @@ func TestInjectEnvIntoDeployment(t *testing.T) {
407412
Name: "bar",
408413
Value: "new_bar_value",
409414
},
415+
corev1.EnvVar{
416+
Name: "extra",
417+
Value: "extra_value",
418+
},
410419
},
411420
},
412421
},

0 commit comments

Comments
 (0)