Skip to content

Commit bc24890

Browse files
committed
Ensure that YAML fields are sorted
This change switches to usining gopkg.in/yaml.v2 when Marshalling data to YAML. This ensures that YAML fields are sorted as they are defined in the structs and not alphabetically. Signed-off-by: Evan Lezar <[email protected]>
1 parent 4149a8a commit bc24890

File tree

11 files changed

+48
-49
lines changed

11 files changed

+48
-49
lines changed

cmd/cdi/cmd/format.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"path/filepath"
2323
"strings"
2424

25-
"sigs.k8s.io/yaml"
25+
orderedyaml "gopkg.in/yaml.v2"
2626
)
2727

2828
func chooseFormat(format string, path string) string {
@@ -46,7 +46,7 @@ func marshalObject(level int, obj interface{}, format string) string {
4646
if format == "json" {
4747
raw, err = json.MarshalIndent(obj, "", " ")
4848
} else {
49-
raw, err = yaml.Marshal(obj)
49+
raw, err = orderedyaml.Marshal(obj)
5050
}
5151

5252
if err != nil {

cmd/cdi/go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,20 @@ require (
77
github.com/opencontainers/runtime-spec v1.1.0
88
github.com/opencontainers/runtime-tools v0.9.1-0.20221107090550-2e043c6bd626
99
github.com/spf13/cobra v1.6.0
10+
gopkg.in/yaml.v2 v2.4.0
1011
sigs.k8s.io/yaml v1.3.0
1112
tags.cncf.io/container-device-interface v0.0.0
1213
)
1314

1415
require (
1516
github.com/inconshreveable/mousetrap v1.0.1 // indirect
16-
github.com/opencontainers/selinux v1.10.0 // indirect
1717
github.com/spf13/pflag v1.0.5 // indirect
1818
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 // indirect
1919
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
2020
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
2121
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
2222
golang.org/x/mod v0.19.0 // indirect
2323
golang.org/x/sys v0.19.0 // indirect
24-
gopkg.in/yaml.v2 v2.4.0 // indirect
2524
tags.cncf.io/container-device-interface/specs-go v0.8.0 // indirect
2625
)
2726

cmd/cdi/go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ github.com/opencontainers/runtime-tools v0.9.1-0.20221107090550-2e043c6bd626 h1:
2222
github.com/opencontainers/runtime-tools v0.9.1-0.20221107090550-2e043c6bd626/go.mod h1:BRHJJd0E+cx42OybVYSgUvZmU0B8P9gZuRXlZUP7TKI=
2323
github.com/opencontainers/selinux v1.9.1/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI=
2424
github.com/opencontainers/selinux v1.10.0 h1:rAiKF8hTcgLI3w0DHm6i0ylVVcOrlgR1kK99DRLDhyU=
25-
github.com/opencontainers/selinux v1.10.0/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI=
2625
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2726
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2827
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=

cmd/validate/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ require (
99
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
1010
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
1111
gopkg.in/yaml.v2 v2.4.0 // indirect
12-
sigs.k8s.io/yaml v1.3.0 // indirect
1312
)
1413

1514
replace tags.cncf.io/container-device-interface => ../..

cmd/validate/go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
22
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3-
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
43
github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI=
54
github.com/opencontainers/runtime-spec v1.1.0 h1:HHUyrt9mwHUjtasSbXSMvs4cyFxh+Bll4AjJ9odEGpg=
65
github.com/opencontainers/runtime-tools v0.9.1-0.20221107090550-2e043c6bd626 h1:DmNGcqH3WDbV5k8OJ+esPWbqUOX5rMLR2PMvziDMJi0=
@@ -24,4 +23,3 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
2423
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
2524
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
2625
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
27-
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ require (
99
github.com/stretchr/testify v1.7.0
1010
github.com/xeipuuv/gojsonschema v1.2.0
1111
golang.org/x/sys v0.19.0
12+
gopkg.in/yaml.v2 v2.4.0
1213
sigs.k8s.io/yaml v1.3.0
1314
tags.cncf.io/container-device-interface/specs-go v0.8.0
1415
)
1516

1617
require (
1718
github.com/davecgh/go-spew v1.1.1 // indirect
19+
github.com/opencontainers/selinux v1.10.0 // indirect
1820
github.com/pmezard/go-difflib v1.0.0 // indirect
1921
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 // indirect
2022
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
2123
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
2224
golang.org/x/mod v0.19.0 // indirect
23-
gopkg.in/yaml.v2 v2.4.0 // indirect
2425
gopkg.in/yaml.v3 v3.0.1 // indirect
2526
)
2627

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ github.com/opencontainers/runtime-spec v1.1.0 h1:HHUyrt9mwHUjtasSbXSMvs4cyFxh+Bl
1717
github.com/opencontainers/runtime-spec v1.1.0/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
1818
github.com/opencontainers/runtime-tools v0.9.1-0.20221107090550-2e043c6bd626 h1:DmNGcqH3WDbV5k8OJ+esPWbqUOX5rMLR2PMvziDMJi0=
1919
github.com/opencontainers/runtime-tools v0.9.1-0.20221107090550-2e043c6bd626/go.mod h1:BRHJJd0E+cx42OybVYSgUvZmU0B8P9gZuRXlZUP7TKI=
20-
github.com/opencontainers/selinux v1.9.1 h1:b4VPEF3O5JLZgdTDBmGepaaIbAo0GqoF6EBRq5f/g3Y=
2120
github.com/opencontainers/selinux v1.9.1/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI=
21+
github.com/opencontainers/selinux v1.10.0 h1:rAiKF8hTcgLI3w0DHm6i0ylVVcOrlgR1kK99DRLDhyU=
22+
github.com/opencontainers/selinux v1.10.0/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI=
2223
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2324
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2425
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=

pkg/cdi/cache_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
oci "github.com/opencontainers/runtime-spec/specs-go"
3131
"github.com/stretchr/testify/require"
3232
"sigs.k8s.io/yaml"
33+
3334
"tags.cncf.io/container-device-interface/pkg/cdi/validate"
3435
cdi "tags.cncf.io/container-device-interface/specs-go"
3536
)

pkg/cdi/spec.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"sync"
2626

2727
oci "github.com/opencontainers/runtime-spec/specs-go"
28+
orderedyaml "gopkg.in/yaml.v2"
2829
"sigs.k8s.io/yaml"
2930

3031
"tags.cncf.io/container-device-interface/internal/validation"
@@ -131,7 +132,7 @@ func (s *Spec) write(overwrite bool) error {
131132
}
132133

133134
if filepath.Ext(s.path) == ".yaml" {
134-
data, err = yaml.Marshal(s.Spec)
135+
data, err = orderedyaml.Marshal(s.Spec)
135136
data = append([]byte("---\n"), data...)
136137
} else {
137138
data, err = json.Marshal(s.Spec)

schema/schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"path/filepath"
2929
"strings"
3030

31-
"sigs.k8s.io/yaml"
31+
"gopkg.in/yaml.v2"
3232

3333
schema "github.com/xeipuuv/gojsonschema"
3434
"tags.cncf.io/container-device-interface/internal/validation"

0 commit comments

Comments
 (0)