Skip to content

Commit

Permalink
Merge pull request #4667 from thefirstofthe300/reduce-log-spam
Browse files Browse the repository at this point in the history
🐛 cleanup: eliminate log spam when using S3 secrets
  • Loading branch information
k8s-ci-robot authored Jan 11, 2024
2 parents 988d136 + 0be00ee commit 2234387
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ func (t Template) ControllersPolicy() *iamv1.PolicyDocument {
Action: iamv1.Actions{
"s3:CreateBucket",
"s3:DeleteBucket",
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject",
"s3:PutBucketPolicy",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ Resources:
- Action:
- s3:CreateBucket
- s3:DeleteBucket
- s3:GetObject
- s3:PutObject
- s3:DeleteObject
- s3:PutBucketPolicy
Expand Down
2 changes: 0 additions & 2 deletions controllers/awsmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -850,8 +850,6 @@ func (r *AWSMachineReconciler) deleteIgnitionBootstrapDataFromS3(machineScope *s
return nil
}

machineScope.Info("Deleting unneeded entry from AWS S3", "secretPrefix", machineScope.GetSecretPrefix())

if err := objectStoreSvc.Delete(machineScope); err != nil {
return errors.Wrap(err, "deleting bootstrap data object")
}
Expand Down
1 change: 0 additions & 1 deletion controllers/awsmachine_controller_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,6 @@ func TestAWSMachineReconciler(t *testing.T) {

_, err := reconciler.reconcileDelete(ms, cs, cs, cs, cs)
g.Expect(err).To(BeNil())
g.Expect(buf.String()).To(ContainSubstring("Deleting unneeded entry from AWS S3"))
})
})

Expand Down
55 changes: 40 additions & 15 deletions pkg/cloud/services/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,27 +173,52 @@ func (s *Service) Delete(m *scope.MachineScope) error {
bucket := s.bucketName()
key := s.bootstrapDataKey(m)

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

_, err := s.S3Client.DeleteObject(&s3.DeleteObjectInput{
_, err := s.S3Client.HeadObject(&s3.HeadObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err == nil {
return nil
}

aerr, ok := err.(awserr.Error)
if !ok {
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case "Forbidden":
// In the case that the IAM policy does not have sufficient
// permissions to get the object, we will attempt to delete it
// 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")
}

s.scope.Debug("Delete object call succeeded despite missing GetObject permission", "bucket", bucket, "key", key)

return nil
case s3.ErrCodeNoSuchKey:
s.scope.Debug("Object already deleted", "bucket", bucket, "key", key)
return nil
case s3.ErrCodeNoSuchBucket:
s.scope.Debug("Bucket does not exist", "bucket", bucket)
return nil
default:
return errors.Wrap(aerr, "deleting S3 object")
}
}
}

s.scope.Info("Deleting S3 object", "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")
}

switch aerr.Code() {
case s3.ErrCodeNoSuchBucket:
default:
return errors.Wrap(aerr, "deleting S3 object")
}

return nil
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/cloud/services/s3/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,8 @@ func TestDeleteObject(t *testing.T) {
},
}

s3Mock.EXPECT().HeadObject(gomock.Any())

s3Mock.EXPECT().DeleteObject(gomock.Any()).Do(func(deleteObjectInput *s3svc.DeleteObjectInput) {
t.Run("use_configured_bucket_name_on_cluster_level", func(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -672,7 +674,7 @@ func TestDeleteObject(t *testing.T) {
},
}

s3Mock.EXPECT().DeleteObject(gomock.Any()).Return(nil, awserr.New(s3svc.ErrCodeNoSuchBucket, "", nil)).Times(1)
s3Mock.EXPECT().HeadObject(gomock.Any()).Return(nil, awserr.New(s3svc.ErrCodeNoSuchBucket, "", nil))

if err := svc.Delete(machineScope); err != nil {
t.Fatalf("Unexpected error, got: %v", err)
Expand All @@ -696,6 +698,7 @@ func TestDeleteObject(t *testing.T) {
},
}

s3Mock.EXPECT().HeadObject(gomock.Any())
s3Mock.EXPECT().DeleteObject(gomock.Any()).Return(nil, errors.New("foo")).Times(1)

if err := svc.Delete(machineScope); err == nil {
Expand Down Expand Up @@ -747,6 +750,7 @@ func TestDeleteObject(t *testing.T) {
},
}

s3Mock.EXPECT().HeadObject(gomock.Any()).Times(2)
s3Mock.EXPECT().DeleteObject(gomock.Any()).Return(nil, nil).Times(2)

if err := svc.Delete(machineScope); err != nil {
Expand Down

0 comments on commit 2234387

Please sign in to comment.