File tree Expand file tree Collapse file tree 2 files changed +26
-4
lines changed
Expand file tree Collapse file tree 2 files changed +26
-4
lines changed Original file line number Diff line number Diff line change @@ -38,6 +38,9 @@ import (
3838 "sigs.k8s.io/cluster-api-provider-aws/v2/util/system"
3939)
4040
41+ // AWSDefaultRegion is the default AWS region.
42+ const AWSDefaultRegion string = "us-east-1"
43+
4144// Service holds a collection of interfaces.
4245// The interfaces are broken down like this to group functions together.
4346// One alternative is to have a large list of functions from the ec2 client.
@@ -223,11 +226,13 @@ func (s *Service) Delete(m *scope.MachineScope) error {
223226}
224227
225228func (s * Service ) createBucketIfNotExist (bucketName string ) error {
226- input := & s3.CreateBucketInput {
227- Bucket : aws .String (bucketName ),
228- CreateBucketConfiguration : & s3.CreateBucketConfiguration {
229+ input := & s3.CreateBucketInput {Bucket : aws .String (bucketName )}
230+
231+ // See https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateBucket.html#AmazonS3-CreateBucket-request-LocationConstraint.
232+ if s .scope .Region () != AWSDefaultRegion {
233+ input .CreateBucketConfiguration = & s3.CreateBucketConfiguration {
229234 LocationConstraint : aws .String (s .scope .Region ()),
230- },
235+ }
231236 }
232237
233238 _ , err := s .S3Client .CreateBucket (input )
Original file line number Diff line number Diff line change @@ -307,6 +307,23 @@ func TestReconcileBucket(t *testing.T) {
307307 t .Fatalf ("Expected error" )
308308 }
309309 })
310+
311+ t .Run ("creates_bucket_without_location" , func (t * testing.T ) {
312+ t .Parallel ()
313+
314+ svc , s3Mock := testService (t , & infrav1.S3Bucket {})
315+ input := & s3svc.CreateBucketInput {
316+ CreateBucketConfiguration : & s3svc.CreateBucketConfiguration {
317+ LocationConstraint : nil ,
318+ },
319+ }
320+
321+ s3Mock .EXPECT ().CreateBucket (gomock .Eq (input )).Return (nil , nil ).Times (1 )
322+
323+ if err := svc .ReconcileBucket (); err != nil {
324+ t .Fatalf ("Unexpected error: %v" , err )
325+ }
326+ })
310327 })
311328}
312329
You can’t perform that action at this time.
0 commit comments