Skip to content

Commit 4b74fc5

Browse files
committed
pkg/cdi: default to YAML spec encoding.
Signed-off-by: Krisztian Litkey <krisztian.litkey@intel.com>
1 parent 53682ee commit 4b74fc5

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

pkg/cdi/cache.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,7 @@ func (c *Cache) highestPrioritySpecDir() (string, int) {
274274

275275
// WriteSpec writes a Spec file with the given content into the highest
276276
// priority Spec directory. If name has a "json" or "yaml" extension it
277-
// choses the encoding. Otherwise JSON encoding is used with a "json"
278-
// extension appended.
277+
// choses the encoding. Otherwise the default YAML encoding is used.
279278
func (c *Cache) WriteSpec(raw *cdi.Spec, name string) error {
280279
var (
281280
specDir string
@@ -292,7 +291,7 @@ func (c *Cache) WriteSpec(raw *cdi.Spec, name string) error {
292291

293292
path = filepath.Join(specDir, name)
294293
if ext := filepath.Ext(path); ext != ".json" && ext != ".yaml" {
295-
path += ".json"
294+
path += defaultSpecExt
296295
}
297296

298297
spec, err = NewSpec(raw, path, prio)
@@ -321,7 +320,7 @@ func (c *Cache) RemoveSpec(name string) error {
321320

322321
path = filepath.Join(specDir, name)
323322
if ext := filepath.Ext(path); ext != ".json" && ext != ".yaml" {
324-
path += ".json"
323+
path += defaultSpecExt
325324
}
326325

327326
err = os.Remove(path)

pkg/cdi/spec.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ import (
2929
cdi "github.com/container-orchestrated-devices/container-device-interface/specs-go"
3030
)
3131

32+
const (
33+
// defaultSpecExt is the file extension for the default encoding.
34+
defaultSpecExt = ".yaml"
35+
)
36+
3237
var (
3338
// Valid CDI Spec versions.
3439
validSpecVersions = map[string]struct{}{
@@ -101,6 +106,10 @@ func NewSpec(raw *cdi.Spec, path string, priority int) (*Spec, error) {
101106
priority: priority,
102107
}
103108

109+
if ext := filepath.Ext(spec.path); ext != ".yaml" && ext != ".json" {
110+
spec.path += defaultSpecExt
111+
}
112+
104113
spec.vendor, spec.class = ParseQualifier(spec.Kind)
105114

106115
if spec.devices, err = spec.validate(); err != nil {

0 commit comments

Comments
 (0)