Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 7 additions & 27 deletions pkg/cdi/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"github.com/fsnotify/fsnotify"
oci "github.com/opencontainers/runtime-spec/specs-go"
"tags.cncf.io/container-device-interface/pkg/cdi/producer"
cdi "tags.cncf.io/container-device-interface/specs-go"
)

Expand Down Expand Up @@ -282,25 +283,13 @@ func (c *Cache) highestPrioritySpecDir() (string, int) {
// priority Spec directory. If name has a "json" or "yaml" extension it
// choses the encoding. Otherwise the default YAML encoding is used.
func (c *Cache) WriteSpec(raw *cdi.Spec, name string) error {
var (
specDir string
path string
prio int
spec *Spec
err error
)

specDir, prio = c.highestPrioritySpecDir()
specDir, prio := c.highestPrioritySpecDir()
if specDir == "" {
return errors.New("no Spec directories to write to")
}

path = filepath.Join(specDir, name)
if ext := filepath.Ext(path); ext != ".json" && ext != ".yaml" {
path += defaultSpecExt
}

spec, err = newSpec(raw, path, prio)
path := producer.EnsureExtension(filepath.Join(specDir, name))
spec, err := newSpec(raw, path, prio)
if err != nil {
return err
}
Expand All @@ -313,23 +302,14 @@ func (c *Cache) WriteSpec(raw *cdi.Spec, name string) error {
// Spec previously written by WriteSpec(). If the file exists and
// its removal fails RemoveSpec returns an error.
func (c *Cache) RemoveSpec(name string) error {
var (
specDir string
path string
err error
)
Comment on lines -316 to -320
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personal preference, but declaring these up front add additional vertical space to a function. Happy to revert if there is a strong opinion.


specDir, _ = c.highestPrioritySpecDir()
specDir, _ := c.highestPrioritySpecDir()
if specDir == "" {
return errors.New("no Spec directories to remove from")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question here: Should this really be an error when removing a spec? If this fails is that not essentially the same as "Not Found" ... we could even return a wrapped NotFound here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think returning a wrapped appropriate error (maybe an fs.ErrNotExist) would probably be the right thing here, giving the caller an easy way to identify and ignore it at will.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #309 since this is separate from the producer API.

}

path = filepath.Join(specDir, name)
if ext := filepath.Ext(path); ext != ".json" && ext != ".yaml" {
path += defaultSpecExt
}
path := producer.EnsureExtension(filepath.Join(specDir, name))

err = os.Remove(path)
err := os.Remove(path)
if err != nil && errors.Is(err, fs.ErrNotExist) {
err = nil
}
Expand Down
30 changes: 16 additions & 14 deletions pkg/cdi/container-edits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,10 @@ func TestApplyContainerEdits(t *testing.T) {
edits: &cdi.ContainerEdits{
DeviceNodes: []*cdi.DeviceNode{
{
Path: "/dev/nil",
Path: "/dev/null",
Type: "c",
Major: 1,
Minor: 3,
Major: nullDeviceMajor,
Minor: nullDeviceMinor,
Permissions: NoPermissions,
},
},
Expand All @@ -450,10 +450,11 @@ func TestApplyContainerEdits(t *testing.T) {
Linux: &oci.Linux{
Devices: []oci.LinuxDevice{
{
Path: "/dev/nil",
Type: "c",
Major: nullDeviceMajor,
Minor: nullDeviceMinor,
Path: "/dev/null",
Type: "c",
Major: nullDeviceMajor,
Minor: nullDeviceMinor,
FileMode: nullDeviceFileMode,
},
},
Resources: &oci.LinuxResources{
Expand All @@ -476,10 +477,10 @@ func TestApplyContainerEdits(t *testing.T) {
edits: &cdi.ContainerEdits{
DeviceNodes: []*cdi.DeviceNode{
{
Path: "/dev/nil",
Path: "/dev/null",
Type: "c",
Major: 1,
Minor: 3,
Major: nullDeviceMajor,
Minor: nullDeviceMinor,
Permissions: "",
},
},
Expand All @@ -488,10 +489,11 @@ func TestApplyContainerEdits(t *testing.T) {
Linux: &oci.Linux{
Devices: []oci.LinuxDevice{
{
Path: "/dev/nil",
Type: "c",
Major: nullDeviceMajor,
Minor: nullDeviceMinor,
Path: "/dev/null",
Type: "c",
Major: nullDeviceMajor,
Minor: nullDeviceMinor,
FileMode: nullDeviceFileMode,
},
},
Resources: &oci.LinuxResources{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

package cdi
package producer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this new package could also be placed under pkg/cdi/producer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was uncertain of where to store this. In the longer term, it may be useful to have this be a separate go module so that producers don't need to import all the unwanted dependencies. (e.g. spec generator).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this back to pkg/cdi/producer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note that I think having it under pkg/cdi/producer shouldn't prevent it from being its own go module... although it might be that the location would be a bit strange if it were.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true. I don't want to go into the module discussion in the first iteration though.


import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
limitations under the License.
*/

package cdi
package producer

import (
"os"
Expand Down
Loading