Skip to content

Commit

Permalink
fix: fixed sorting of env vars with secret refs (#8)
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Meier <[email protected]>
  • Loading branch information
astromechza authored May 17, 2024
1 parent 3d618e3 commit 1a2f4ac
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
8 changes: 8 additions & 0 deletions internal/convert/container_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ func convertContainerVariables(variables scoretypes.ContainerVariables, substitu
}
}
slices.SortFunc(out, func(a, b coreV1.EnvVar) int {
// note __ref-'s must always be first!
aRef, bRef := strings.HasPrefix(a.Name, "__ref_"), strings.HasPrefix(b.Name, "__ref_")
if aRef && !bRef {
return -1
} else if bRef && !aRef {
return 1
}
// anything else gets sorted naturally
return strings.Compare(a.Name, b.Name)
})
return out, nil
Expand Down
31 changes: 31 additions & 0 deletions internal/convert/container_variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,34 @@ func Test_convertContainerVariable_2_secret(t *testing.T) {
{Name: "KEY", Value: "$(__ref_0960osB2KjQY08QKfHliCg) $(__ref_mWObImRl7lfuP04NHDPsvA)"},
}, out)
}

func Test_convertContainerVariables_sorting(t *testing.T) {
out, err := convertContainerVariables(map[string]string{
"BUZZ": "FIZZ",
"KEY": "${foo.bar} ${a.b}",
"FIZZ": "BUZZ",
}, func(s string) (string, error) {
return map[string]string{
"foo.bar": internal.EncodeSecretReference("default", "some-key"),
"a.b": internal.EncodeSecretReference("default", "other-key"),
}[s], nil
})
assert.NoError(t, err)
assert.Equal(t, []coreV1.EnvVar{
{Name: "__ref_0960osB2KjQY08QKfHliCg", ValueFrom: &coreV1.EnvVarSource{
SecretKeyRef: &coreV1.SecretKeySelector{
LocalObjectReference: coreV1.LocalObjectReference{Name: "default"},
Key: "some-key",
},
}},
{Name: "__ref_mWObImRl7lfuP04NHDPsvA", ValueFrom: &coreV1.EnvVarSource{
SecretKeyRef: &coreV1.SecretKeySelector{
LocalObjectReference: coreV1.LocalObjectReference{Name: "default"},
Key: "other-key",
},
}},
{Name: "BUZZ", Value: "FIZZ"},
{Name: "FIZZ", Value: "BUZZ"},
{Name: "KEY", Value: "$(__ref_0960osB2KjQY08QKfHliCg) $(__ref_mWObImRl7lfuP04NHDPsvA)"},
}, out)
}

0 comments on commit 1a2f4ac

Please sign in to comment.