Skip to content

Commit 7c8505c

Browse files
committed
pkg/cdi: default to YAML spec encoding.
Signed-off-by: Krisztian Litkey <[email protected]>
1 parent 2fde79d commit 7c8505c

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
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
@@ -31,6 +31,11 @@ import (
3131
cdi "github.com/container-orchestrated-devices/container-device-interface/specs-go"
3232
)
3333

34+
const (
35+
// defaultSpecExt is the file extension for the default encoding.
36+
defaultSpecExt = ".yaml"
37+
)
38+
3439
var (
3540
// Valid CDI Spec versions.
3641
validSpecVersions = map[string]struct{}{
@@ -105,6 +110,10 @@ func NewSpec(raw *cdi.Spec, path string, priority int) (*Spec, error) {
105110
priority: priority,
106111
}
107112

113+
if ext := filepath.Ext(spec.path); ext != ".yaml" && ext != ".json" {
114+
spec.path += defaultSpecExt
115+
}
116+
108117
spec.vendor, spec.class = ParseQualifier(spec.Kind)
109118

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

pkg/cdi/spec_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"io/ioutil"
2121
"os"
2222
"path/filepath"
23+
"strings"
2324
"testing"
2425

2526
"github.com/pkg/errors"
@@ -77,7 +78,7 @@ devices:
7778
},
7879
} {
7980
t.Run(tc.name, func(t *testing.T) {
80-
file, err := mkTestFile(t, []byte(tc.data))
81+
file, err := mkTestSpec(t, []byte(tc.data))
8182
if err != nil {
8283
t.Errorf("failed to create CDI Spec test file: %v", err)
8384
return
@@ -461,7 +462,7 @@ devices:
461462
},
462463
} {
463464
t.Run(tc.name, func(t *testing.T) {
464-
file, err := mkTestFile(t, []byte(tc.data))
465+
file, err := mkTestSpec(t, []byte(tc.data))
465466
if err != nil {
466467
t.Errorf("failed to create CDI Spec test file: %v", err)
467468
return
@@ -493,8 +494,8 @@ devices:
493494
}
494495

495496
// Create an automatically cleaned up temporary file for a test.
496-
func mkTestFile(t *testing.T, data []byte) (string, error) {
497-
tmp, err := ioutil.TempFile("", ".cdi-test.*")
497+
func mkTestSpec(t *testing.T, data []byte) (string, error) {
498+
tmp, err := ioutil.TempFile("", ".cdi-test.*."+specType(data))
498499
if err != nil {
499500
return "", errors.Wrapf(err, "failed to create test file")
500501
}
@@ -515,6 +516,14 @@ func mkTestFile(t *testing.T, data []byte) (string, error) {
515516
return file, nil
516517
}
517518

519+
func specType(content []byte) string {
520+
spec := strings.TrimSpace(string(content))
521+
if spec != "" && spec[0] == '{' {
522+
return "json"
523+
}
524+
return "yaml"
525+
}
526+
518527
func TestCurrentVersionIsValid(t *testing.T) {
519528
require.NoError(t, validateVersion(cdi.CurrentVersion))
520529
}

0 commit comments

Comments
 (0)