-
Notifications
You must be signed in to change notification settings - Fork 47
Add basic producer package #308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
| ) | ||
|
|
||
|
|
@@ -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 | ||
| } | ||
|
|
@@ -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 | ||
| ) | ||
|
|
||
| specDir, _ = c.highestPrioritySpecDir() | ||
| specDir, _ := c.highestPrioritySpecDir() | ||
| if specDir == "" { | ||
| return errors.New("no Spec directories to remove from") | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think returning a wrapped appropriate error (maybe an
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ | |
| limitations under the License. | ||
| */ | ||
|
|
||
| package cdi | ||
| package producer | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: this new package could also be placed under
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved this back to
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,7 @@ | |
| limitations under the License. | ||
| */ | ||
|
|
||
| package cdi | ||
| package producer | ||
|
|
||
| import ( | ||
| "os" | ||
|
|
||
There was a problem hiding this comment.
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.