Skip to content
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 6 commits into from
May 27, 2020
Merged
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
4 changes: 4 additions & 0 deletions upup/models/vfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,7 @@ func (p *AssetPath) String() string {
func (p *AssetPath) Remove() error {
return ReadOnlyError
}

func (p *AssetPath) RemoveAll() error {
return p.Remove()
}
4 changes: 4 additions & 0 deletions util/pkg/vfs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ func (p *FSPath) Remove() error {
return os.Remove(p.location)
}

func (p *FSPath) RemoveAll() error {
return p.Remove()
}

func (p *FSPath) PreferredHash() (*hashing.Hash, error) {
return p.Hash(hashing.HashAlgorithmSHA256)
}
Expand Down
4 changes: 4 additions & 0 deletions util/pkg/vfs/gsfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ func (p *GSPath) Remove() error {
}
}

func (p *GSPath) RemoveAll() error {
return p.Remove()
}

func (p *GSPath) Join(relativePath ...string) Path {
args := []string{p.key}
args = append(args, relativePath...)
Expand Down
4 changes: 4 additions & 0 deletions util/pkg/vfs/k8sfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func (p *KubernetesPath) Remove() error {
return fmt.Errorf("KubernetesPath::Remove not supported")
}

func (p *KubernetesPath) RemoveAll() error {
return p.Remove()
}

func (p *KubernetesPath) Join(relativePath ...string) Path {
args := []string{p.key}
args = append(args, relativePath...)
Expand Down
4 changes: 4 additions & 0 deletions util/pkg/vfs/memfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,7 @@ func (p *MemFSPath) Remove() error {
p.contents = nil
return nil
}

func (p *MemFSPath) RemoveAll() error {
return p.Remove()
}
4 changes: 4 additions & 0 deletions util/pkg/vfs/ossfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ func (p *OSSPath) Remove() error {
}
}

func (p *OSSPath) RemoveAll() error {
return p.Remove()
}

func (p *OSSPath) Base() string {
return path.Base(p.key)
}
Expand Down
65 changes: 65 additions & 0 deletions util/pkg/vfs/s3fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

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


request := &s3.ListObjectVersionsInput{
Bucket: aws.String(p.bucket),
Prefix: aws.String(p.key),
}

response, err := client.ListObjectVersions(request)
Copy link
Member

Choose a reason for hiding this comment

The 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...)
Expand Down
4 changes: 4 additions & 0 deletions util/pkg/vfs/sshfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func (p *SSHPath) Remove() error {
return nil
}

func (p *SSHPath) RemoveAll() error {
return p.Remove()
}

func (p *SSHPath) Join(relativePath ...string) Path {
args := []string{p.path}
args = append(args, relativePath...)
Expand Down
4 changes: 4 additions & 0 deletions util/pkg/vfs/swiftfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ func (p *SwiftPath) Remove() error {
}
}

func (p *SwiftPath) RemoveAll() error {
return p.Remove()
}

func (p *SwiftPath) Join(relativePath ...string) Path {
args := []string{p.key}
args = append(args, relativePath...)
Expand Down
3 changes: 3 additions & 0 deletions util/pkg/vfs/vfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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

Expand Down