Skip to content

Commit

Permalink
Issue-4292: ConfigMaps items are generated with random order
Browse files Browse the repository at this point in the history
- Sort pair alphabetically when read from KvPairSources
  • Loading branch information
adilfulara authored and Adil Fulara committed Aug 21, 2022
1 parent bf17fe1 commit 061a480
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 4 additions & 2 deletions api/internal/generators/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ metadata:
data:
DB_PASSWORD: qwerty
DB_USERNAME: admin
a: bar
z: foo
`,
},
},
Expand Down Expand Up @@ -122,7 +124,7 @@ binaryData:
GeneratorArgs: types.GeneratorArgs{
Name: "literalConfigMap1",
KvPairSources: types.KvPairSources{
LiteralSources: []string{"a=x", "b=y", "c=\"Hello World\"", "d='true'"},
LiteralSources: []string{"b=y", "a=x", "c=\"Hello World\"", "d='true'"},
},
Options: &types.GeneratorOptions{
Labels: map[string]string{
Expand Down Expand Up @@ -192,7 +194,7 @@ immutable: true
fSys := filesys.MakeFsInMemory()
fSys.WriteFile(
filesys.RootedPath("configmap", "app.env"),
[]byte("DB_USERNAME=admin\nDB_PASSWORD=qwerty\n"))
[]byte("z=foo\na=bar\nDB_USERNAME=admin\nDB_PASSWORD=qwerty\n"))
fSys.WriteFile(
filesys.RootedPath("configmap", "app-init.ini"),
[]byte("FOO=bar\nBAR=baz\n"))
Expand Down
5 changes: 4 additions & 1 deletion api/internal/generators/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ package generators

import (
"fmt"

"github.com/go-errors/errors"
"sigs.k8s.io/kustomize/api/ifc"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kyaml/yaml"
"sort"
)

func makeBaseNode(kind, name, namespace string) (*yaml.RNode, error) {
Expand Down Expand Up @@ -40,6 +40,9 @@ func makeValidatedDataMap(
if err != nil {
return nil, errors.WrapPrefix(err, "loading KV pairs", 0)
}
sort.Slice(pairs, func(i, j int) bool {
return pairs[i].Key < pairs[j].Key
})
knownKeys := make(map[string]string)
for _, p := range pairs {
// legal key: alphanumeric characters, '-', '_' or '.'
Expand Down

0 comments on commit 061a480

Please sign in to comment.