-
Notifications
You must be signed in to change notification settings - Fork 772
Volume QA, "volume remove" (fixes and tests) #3125
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,9 +19,11 @@ package volume | |
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/containerd/containerd" | ||
| "github.com/containerd/log" | ||
| "github.com/containerd/nerdctl/v2/pkg/api/types" | ||
| "github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat" | ||
| "github.com/containerd/nerdctl/v2/pkg/labels" | ||
|
|
@@ -44,23 +46,28 @@ func Remove(ctx context.Context, client *containerd.Client, volumes []string, op | |
|
|
||
| var volumenames []string // nolint: prealloc | ||
| for _, name := range volumes { | ||
| volume, err := volStore.Get(name, false) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if _, ok := usedVolumes[volume.Name]; ok { | ||
| if _, ok := usedVolumes[name]; ok { | ||
| return fmt.Errorf("volume %q is in use", name) | ||
| } | ||
| volumenames = append(volumenames, name) | ||
| } | ||
| removedNames, err := volStore.Remove(volumenames) | ||
| // if err is set, this is a hard filesystem error | ||
| removedNames, warns, err := volStore.Remove(volumenames) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| // Otherwise, output on stdout whatever was successful | ||
|
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. This and following is to match docker behavior on multi remove:
|
||
| for _, name := range removedNames { | ||
| fmt.Fprintln(options.Stdout, name) | ||
| } | ||
| return err | ||
| // Log the rest | ||
| for _, volErr := range warns { | ||
| log.G(ctx).Warn(volErr) | ||
| } | ||
| if len(warns) > 0 { | ||
| return errors.New("some volumes could not be removed") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func usedVolumes(ctx context.Context, containers []containerd.Container) (map[string]struct{}, error) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ package volumestore | |
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
|
|
@@ -65,7 +66,7 @@ type VolumeStore interface { | |
| // Get may return ErrNotFound | ||
| Get(name string, size bool) (*native.Volume, error) | ||
| List(size bool) (map[string]native.Volume, error) | ||
| Remove(names []string) (removedNames []string, err error) | ||
| Remove(names []string) (removed []string, warns []error, err error) | ||
| } | ||
|
|
||
| type volumeStore struct { | ||
|
|
@@ -80,7 +81,7 @@ func (vs *volumeStore) Dir() string { | |
|
|
||
| func (vs *volumeStore) Create(name string, labels []string) (*native.Volume, error) { | ||
| if err := identifiers.Validate(name); err != nil { | ||
| return nil, fmt.Errorf("malformed name %s: %w", name, err) | ||
| return nil, fmt.Errorf("malformed volume name: %w", err) | ||
| } | ||
| volPath := filepath.Join(vs.dir, name) | ||
| volDataPath := filepath.Join(volPath, DataDirName) | ||
|
|
@@ -131,12 +132,11 @@ func (vs *volumeStore) Create(name string, labels []string) (*native.Volume, err | |
| return os.WriteFile(volFilePath, labelsJSON, 0644) | ||
| } | ||
|
|
||
| if err := lockutil.WithDirLock(vs.dir, fn); err != nil { | ||
| if err := lockutil.WithDirLock(vs.dir, fn); err != nil && !errors.Is(err, os.ErrExist) { | ||
|
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. This to match docker behavior. |
||
| return nil, err | ||
| } | ||
|
|
||
| // If other new actions that might fail are added below, we should move the cleanup function out of fn. | ||
|
|
||
| vol := &native.Volume{ | ||
| Name: name, | ||
| Mountpoint: volDataPath, | ||
|
|
@@ -196,23 +196,31 @@ func (vs *volumeStore) List(size bool) (map[string]native.Volume, error) { | |
| return res, nil | ||
| } | ||
|
|
||
| func (vs *volumeStore) Remove(names []string) ([]string, error) { | ||
| var removed []string | ||
| func (vs *volumeStore) Remove(names []string) (removed []string, warns []error, err error) { | ||
| fn := func() error { | ||
| for _, name := range names { | ||
| if err := identifiers.Validate(name); err != nil { | ||
| return fmt.Errorf("malformed name %s: %w", name, err) | ||
| warns = append(warns, fmt.Errorf("malformed volume name: %w", err)) | ||
| continue | ||
| } | ||
| dir := filepath.Join(vs.dir, name) | ||
| if _, err := os.Stat(dir); os.IsNotExist(err) { | ||
| warns = append(warns, fmt.Errorf("no such volume: %s", name)) | ||
| continue | ||
| } | ||
| // This is a hard filesystem error. Exit on this. | ||
| if err := os.RemoveAll(dir); err != nil { | ||
| return err | ||
| } | ||
| removed = append(removed, name) | ||
| } | ||
| return nil | ||
| } | ||
| err := lockutil.WithDirLock(vs.dir, fn) | ||
| return removed, err | ||
|
|
||
| if err := lockutil.WithDirLock(vs.dir, fn); err != nil { | ||
| return nil, nil, err | ||
| } | ||
| return removed, warns, nil | ||
| } | ||
|
|
||
| func Labels(b []byte) *map[string]string { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This section serves no purpose, and does make things racy.