Skip to content

Commit 481223c

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 857ddea commit 481223c

File tree

8 files changed

+47
-46
lines changed

8 files changed

+47
-46
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,22 +7,21 @@ 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
tags.cncf.io/container-device-interface/schema v0.0.0
1314
)
1415

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

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=

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ require (
88
github.com/opencontainers/runtime-tools v0.9.1-0.20221107090550-2e043c6bd626
99
github.com/stretchr/testify v1.7.0
1010
golang.org/x/sys v0.19.0
11+
gopkg.in/yaml.v2 v2.4.0
1112
sigs.k8s.io/yaml v1.3.0
1213
tags.cncf.io/container-device-interface/specs-go v0.8.0
1314
)
1415

1516
require (
1617
github.com/davecgh/go-spew v1.1.1 // indirect
18+
github.com/opencontainers/selinux v1.10.0 // indirect
1719
github.com/pmezard/go-difflib v1.0.0 // indirect
1820
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 // indirect
1921
golang.org/x/mod v0.19.0 // indirect
20-
gopkg.in/yaml.v2 v2.4.0 // indirect
2122
gopkg.in/yaml.v3 v3.0.1 // indirect
2223
)
2324

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/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"
@@ -135,7 +136,7 @@ func (s *Spec) write(overwrite bool) error {
135136
}
136137

137138
if filepath.Ext(s.path) == ".yaml" {
138-
data, err = yaml.Marshal(s.Spec)
139+
data, err = orderedyaml.Marshal(s.Spec)
139140
data = append([]byte("---\n"), data...)
140141
} else {
141142
data, err = json.Marshal(s.Spec)

schema/go.sum

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ 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=
2222
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2323
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2424
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=

specs-go/config.go

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,69 +4,69 @@ import "os"
44

55
// Spec is the base configuration for CDI
66
type Spec struct {
7-
Version string `json:"cdiVersion"`
8-
Kind string `json:"kind"`
7+
Version string `json:"cdiVersion" yaml:"cdiVersion"`
8+
Kind string `json:"kind" yaml:"kind"`
99
// Annotations add meta information per CDI spec. Note these are CDI-specific and do not affect container metadata.
1010
// Added in v0.6.0.
11-
Annotations map[string]string `json:"annotations,omitempty"`
12-
Devices []Device `json:"devices"`
13-
ContainerEdits ContainerEdits `json:"containerEdits,omitempty"`
11+
Annotations map[string]string `json:"annotations,omitempty" yaml:"annotations,omitempty"`
12+
Devices []Device `json:"devices" yaml:"devices"`
13+
ContainerEdits ContainerEdits `json:"containerEdits,omitempty" yaml:"containerEdits,omitempty"`
1414
}
1515

1616
// Device is a "Device" a container runtime can add to a container
1717
type Device struct {
18-
Name string `json:"name"`
18+
Name string `json:"name" yaml:"name"`
1919
// Annotations add meta information per device. Note these are CDI-specific and do not affect container metadata.
2020
// Added in v0.6.0.
21-
Annotations map[string]string `json:"annotations,omitempty"`
22-
ContainerEdits ContainerEdits `json:"containerEdits"`
21+
Annotations map[string]string `json:"annotations,omitempty" yaml:"annotations,omitempty"`
22+
ContainerEdits ContainerEdits `json:"containerEdits" yaml:"containerEdits"`
2323
}
2424

2525
// ContainerEdits are edits a container runtime must make to the OCI spec to expose the device.
2626
type ContainerEdits struct {
27-
Env []string `json:"env,omitempty"`
28-
DeviceNodes []*DeviceNode `json:"deviceNodes,omitempty"`
29-
Hooks []*Hook `json:"hooks,omitempty"`
30-
Mounts []*Mount `json:"mounts,omitempty"`
31-
IntelRdt *IntelRdt `json:"intelRdt,omitempty"` // Added in v0.7.0
32-
AdditionalGIDs []uint32 `json:"additionalGids,omitempty"` // Added in v0.7.0
27+
Env []string `json:"env,omitempty" yaml:"env,omitempty"`
28+
DeviceNodes []*DeviceNode `json:"deviceNodes,omitempty" yaml:"deviceNodes,omitempty"`
29+
Hooks []*Hook `json:"hooks,omitempty" yaml:"hooks,omitempty"`
30+
Mounts []*Mount `json:"mounts,omitempty" yaml:"mounts,omitempty"`
31+
IntelRdt *IntelRdt `json:"intelRdt,omitempty" yaml:"intelRdt,omitempty"` // Added in v0.7.0
32+
AdditionalGIDs []uint32 `json:"additionalGids,omitempty" yaml:"additionalGids,omitempty"` // Added in v0.7.0
3333
}
3434

3535
// DeviceNode represents a device node that needs to be added to the OCI spec.
3636
type DeviceNode struct {
37-
Path string `json:"path"`
38-
HostPath string `json:"hostPath,omitempty"` // Added in v0.5.0
39-
Type string `json:"type,omitempty"`
40-
Major int64 `json:"major,omitempty"`
41-
Minor int64 `json:"minor,omitempty"`
42-
FileMode *os.FileMode `json:"fileMode,omitempty"`
43-
Permissions string `json:"permissions,omitempty"`
44-
UID *uint32 `json:"uid,omitempty"`
45-
GID *uint32 `json:"gid,omitempty"`
37+
Path string `json:"path" yaml:"path"`
38+
HostPath string `json:"hostPath,omitempty" yaml:"hostPath,omitempty"` // Added in v0.5.0
39+
Type string `json:"type,omitempty" yaml:"type,omitempty"`
40+
Major int64 `json:"major,omitempty" yaml:"major,omitempty"`
41+
Minor int64 `json:"minor,omitempty" yaml:"minor,omitempty"`
42+
FileMode *os.FileMode `json:"fileMode,omitempty" yaml:"fileMode,omitempty"`
43+
Permissions string `json:"permissions,omitempty" yaml:"permissions,omitempty"`
44+
UID *uint32 `json:"uid,omitempty" yaml:"uid,omitempty"`
45+
GID *uint32 `json:"gid,omitempty" yaml:"gid,omitempty"`
4646
}
4747

4848
// Mount represents a mount that needs to be added to the OCI spec.
4949
type Mount struct {
50-
HostPath string `json:"hostPath"`
51-
ContainerPath string `json:"containerPath"`
52-
Options []string `json:"options,omitempty"`
53-
Type string `json:"type,omitempty"` // Added in v0.4.0
50+
HostPath string `json:"hostPath" yaml:"hostPath"`
51+
ContainerPath string `json:"containerPath" yaml:"containerPath"`
52+
Options []string `json:"options,omitempty" yaml:"options,omitempty"`
53+
Type string `json:"type,omitempty" yaml:"type,omitempty"` // Added in v0.4.0
5454
}
5555

5656
// Hook represents a hook that needs to be added to the OCI spec.
5757
type Hook struct {
58-
HookName string `json:"hookName"`
59-
Path string `json:"path"`
60-
Args []string `json:"args,omitempty"`
61-
Env []string `json:"env,omitempty"`
62-
Timeout *int `json:"timeout,omitempty"`
58+
HookName string `json:"hookName" yaml:"hookName"`
59+
Path string `json:"path" yaml:"path"`
60+
Args []string `json:"args,omitempty" yaml:"args,omitempty"`
61+
Env []string `json:"env,omitempty" yaml:"env,omitempty"`
62+
Timeout *int `json:"timeout,omitempty" yaml:"timeout,omitempty"`
6363
}
6464

6565
// IntelRdt describes the Linux IntelRdt parameters to set in the OCI spec.
6666
type IntelRdt struct {
67-
ClosID string `json:"closID,omitempty"`
68-
L3CacheSchema string `json:"l3CacheSchema,omitempty"`
69-
MemBwSchema string `json:"memBwSchema,omitempty"`
70-
EnableCMT bool `json:"enableCMT,omitempty"`
71-
EnableMBM bool `json:"enableMBM,omitempty"`
67+
ClosID string `json:"closID,omitempty" yaml:"closID,omitempty"`
68+
L3CacheSchema string `json:"l3CacheSchema,omitempty" yaml:"l3CacheSchema,omitempty"`
69+
MemBwSchema string `json:"memBwSchema,omitempty" yaml:"memBwSchema,omitempty"`
70+
EnableCMT bool `json:"enableCMT,omitempty" yaml:"enableCMT,omitempty"`
71+
EnableMBM bool `json:"enableMBM,omitempty" yaml:"enableMBM,omitempty"`
7272
}

0 commit comments

Comments
 (0)