-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Remove all versions of a file form the S3 bucket #9171
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
831e3f0
Remove all versions of a file form the S3 bucket
56af880
Remove TODO that was not addressed for a long time
1a38a3f
Return os.ErrNotExist when no versions are found
a48ccfa
Return warning instead of error to hide issues during cluster teardown
b565122
Remove delete markers also from S3 bucket
9675692
Implement RemoveAll() for S3 paths
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 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -106,6 +106,71 @@ func (p *S3Path) Remove() error { | |
return nil | ||
} | ||
|
||
func (p *S3Path) RemoveAll() error { | ||
client, err := p.client() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
klog.V(8).Infof("removing file %s", p) | ||
|
||
request := &s3.ListObjectVersionsInput{ | ||
Bucket: aws.String(p.bucket), | ||
Prefix: aws.String(p.key), | ||
} | ||
|
||
response, err := client.ListObjectVersions(request) | ||
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. TBD: will this require new IAM permissions? |
||
if err != nil { | ||
return fmt.Errorf("error listing versions %s: %v", p, err) | ||
hakman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if len(response.Versions) == 0 && len(response.DeleteMarkers) == 0 { | ||
return os.ErrNotExist | ||
} | ||
|
||
// Sometimes S3 will return paginated results if there are too many versions and markers for an object. | ||
// This happens at about entries 1000, so it is unlikely with current use cases. | ||
if aws.BoolValue(response.IsTruncated) { | ||
klog.Warningf("too many versions for %s", p) | ||
} | ||
|
||
objects := []*s3.ObjectIdentifier{} | ||
for _, version := range response.Versions { | ||
klog.V(8).Infof("removing file %s version %q", p, aws.StringValue(version.VersionId)) | ||
file := s3.ObjectIdentifier{ | ||
Key: version.Key, | ||
VersionId: version.VersionId, | ||
} | ||
objects = append(objects, &file) | ||
} | ||
for _, version := range response.DeleteMarkers { | ||
klog.V(8).Infof("removing marker %s version %q", p, aws.StringValue(version.VersionId)) | ||
marker := s3.ObjectIdentifier{ | ||
Key: version.Key, | ||
VersionId: version.VersionId, | ||
} | ||
objects = append(objects, &marker) | ||
} | ||
|
||
if len(objects) > 0 { | ||
klog.V(8).Infof("removing %d file/marker versions\n", len(objects)) | ||
|
||
request := &s3.DeleteObjectsInput{ | ||
Bucket: aws.String(p.bucket), | ||
Delete: &s3.Delete{ | ||
Objects: objects, | ||
}, | ||
} | ||
|
||
_, err = client.DeleteObjects(request) | ||
if err != nil { | ||
return fmt.Errorf("error removing %d file/marker versions: %v", len(objects), err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (p *S3Path) Join(relativePath ...string) Path { | ||
args := []string{p.key} | ||
args = append(args, relativePath...) | ||
|
This file contains 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 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 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 |
---|---|---|
|
@@ -59,6 +59,9 @@ type Path interface { | |
// Remove deletes the file | ||
Remove() error | ||
|
||
// RemoveAll completely deletes the file (with all its versions and markers) | ||
RemoveAll() error | ||
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: RemoveAllVersions might be clearer (vs this being a recursive delete or something akin to MkdirAll) |
||
|
||
// Base returns the base name (last element) | ||
Base() 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.
nit: "removing all versions of %s" or "remove file %s (all versions)" might be clearer