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

✨ s3: allow best effort delete for objects #4904

Merged
merged 3 commits into from
Apr 24, 2024
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
1 change: 1 addition & 0 deletions api/v1beta1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions api/v1beta2/awscluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,10 @@ type S3Bucket struct {
// +kubebuilder:validation:MaxLength:=63
// +kubebuilder:validation:Pattern=`^[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]$`
Name string `json:"name"`

// BestEffortDeleteObjects defines whether access/permission errors during object deletion should be ignored.
// +optional
BestEffortDeleteObjects *bool `json:"bestEffortDeleteObjects,omitempty"`
}

// +kubebuilder:object:root=true
Expand Down
5 changes: 5 additions & 0 deletions api/v1beta2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -1514,6 +1514,10 @@ spec:
(https://coreos.github.io/ignition/) for bootstrapping (requires
BootstrapFormatIgnition feature flag to be enabled).
properties:
bestEffortDeleteObjects:
description: BestEffortDeleteObjects defines whether access/permission
errors during object deletion should be ignored.
type: boolean
controlPlaneIAMInstanceProfile:
description: ControlPlaneIAMInstanceProfile is a name of the IAMInstanceProfile,
which will be allowed to read control-plane node bootstrap data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,10 @@ spec:
Ignition (https://coreos.github.io/ignition/) for bootstrapping
(requires BootstrapFormatIgnition feature flag to be enabled).
properties:
bestEffortDeleteObjects:
description: BestEffortDeleteObjects defines whether access/permission
errors during object deletion should be ignored.
type: boolean
controlPlaneIAMInstanceProfile:
description: ControlPlaneIAMInstanceProfile is a name
of the IAMInstanceProfile, which will be allowed to
Expand Down
27 changes: 18 additions & 9 deletions pkg/cloud/services/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/aws/aws-sdk-go/service/sts"
"github.com/aws/aws-sdk-go/service/sts/stsiface"
"github.com/pkg/errors"
"k8s.io/utils/ptr"

infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
iam "sigs.k8s.io/cluster-api-provider-aws/v2/iam/api/v1beta1"
Expand Down Expand Up @@ -194,12 +195,8 @@ func (s *Service) Delete(m *scope.MachineScope) error {
// anyway for backwards compatibility reasons.
s.scope.Debug("Received 403 forbidden from S3 HeadObject call. If GetObject permission has been granted to the controller but not ListBucket, object is already deleted. Attempting deletion anyway in case GetObject permission hasn't been granted to the controller but DeleteObject has.", "bucket", bucket, "key", key)

_, err = s.S3Client.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
return errors.Wrap(err, "deleting S3 object")
if err := s.deleteObject(bucket, key); err != nil {
return err
}

s.scope.Debug("Delete object call succeeded despite missing GetObject permission", "bucket", bucket, "key", key)
Expand All @@ -221,11 +218,23 @@ func (s *Service) Delete(m *scope.MachineScope) error {

s.scope.Info("Deleting S3 object", "bucket", bucket, "key", key)

_, err = s.S3Client.DeleteObject(&s3.DeleteObjectInput{
return s.deleteObject(bucket, key)
}

func (s *Service) deleteObject(bucket, key string) error {
if _, err := s.S3Client.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
}); err != nil {
if ptr.Deref(s.scope.Bucket().BestEffortDeleteObjects, false) {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case "Forbidden", "AccessDenied":
s.scope.Debug("Ignoring deletion error", "bucket", bucket, "key", key, "error", aerr.Message())
return nil
}
}
}
return errors.Wrap(err, "deleting S3 object")
}

Expand Down
33 changes: 33 additions & 0 deletions pkg/cloud/services/s3/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,18 @@ func TestDeleteObject(t *testing.T) {
t.Fatalf("Unexpected error, got: %v", err)
}
})

t.Run("object_access_denied_and_BestEffortDeleteObjects_is_on", func(t *testing.T) {
t.Parallel()

svc, s3Mock := testService(t, &testServiceInput{Bucket: &infrav1.S3Bucket{BestEffortDeleteObjects: aws.Bool(true)}})
s3Mock.EXPECT().HeadObject(gomock.Any()).Return(nil, nil)
s3Mock.EXPECT().DeleteObject(gomock.Any()).Return(nil, awserr.New("AccessDenied", "Access Denied", nil))

if err := svc.Delete(machineScope); err != nil {
t.Fatalf("Unexpected error, got: %v", err)
}
})
})

t.Run("returns_error_when", func(t *testing.T) {
Expand Down Expand Up @@ -793,6 +805,27 @@ func TestDeleteObject(t *testing.T) {
t.Fatalf("Expected error")
}
})

t.Run("object_access_denied_and_BestEffortDeleteObjects_is_off", func(t *testing.T) {
t.Parallel()

svc, s3Mock := testService(t, &testServiceInput{Bucket: &infrav1.S3Bucket{}})
s3Mock.EXPECT().HeadObject(gomock.Any()).Return(nil, nil)
s3Mock.EXPECT().DeleteObject(gomock.Any()).Return(nil, awserr.New("AccessDenied", "Access Denied", nil))

machineScope := &scope.MachineScope{
Machine: &clusterv1.Machine{},
AWSMachine: &infrav1.AWSMachine{
ObjectMeta: metav1.ObjectMeta{
Name: nodeName,
},
},
}

if err := svc.Delete(machineScope); err == nil {
t.Fatalf("Expected error")
}
})
})

t.Run("is_idempotent", func(t *testing.T) {
Expand Down
Loading