Skip to content

Commit e1415f9

Browse files
committed
Use ephemeral S3 buckets for E2E tests
Use S3 buckets created during the lifecycle of a test instead of a static one. Signed-off-by: Arnaud Meukam <[email protected]>
1 parent 2a5166f commit e1415f9

File tree

1 file changed

+120
-0
lines changed
  • tests/e2e/kubetest2-kops/aws

1 file changed

+120
-0
lines changed

tests/e2e/kubetest2-kops/aws/s3.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package aws
18+
19+
import (
20+
"context"
21+
"errors"
22+
"fmt"
23+
"regexp"
24+
"strings"
25+
"time"
26+
27+
"github.com/aws/aws-sdk-go-v2/aws"
28+
awsconfig "github.com/aws/aws-sdk-go-v2/config"
29+
"github.com/aws/aws-sdk-go-v2/service/s3"
30+
"github.com/aws/aws-sdk-go-v2/service/s3/types"
31+
"github.com/aws/aws-sdk-go-v2/service/sts"
32+
"k8s.io/klog/v2"
33+
)
34+
35+
// It contains S3Client, an Amazon S3 service client that is used to perform bucket
36+
// and object actions.
37+
type awsClient struct {
38+
S3Client *s3.Client
39+
}
40+
41+
func AWSBucketName(ctx context.Context) (string, error) {
42+
config, err := awsconfig.LoadDefaultConfig(ctx)
43+
if err != nil {
44+
return "", fmt.Errorf("failed to load AWS config: %w", err)
45+
}
46+
47+
stsSvc := sts.NewFromConfig(config)
48+
callerIdentity, err := stsSvc.GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{})
49+
if err != nil {
50+
return "", fmt.Errorf("building AWS STS presigned request: %w", err)
51+
}
52+
53+
// Add timestamp suffix
54+
timestamp := time.Now().Format("01022006")
55+
bucket := fmt.Sprintf("k8s-infra-kops-%s", *callerIdentity.Account)
56+
bucket = fmt.Sprintf("%s-%s", bucket, timestamp)
57+
58+
bucket = strings.ToLower(bucket)
59+
bucket = regexp.MustCompile("[^a-z0-9-]").ReplaceAllString(bucket, "") // Only allow lowercase letters, numbers, and hyphens
60+
61+
if len(bucket) > 63 {
62+
bucket = bucket[:63] // Max length is 63
63+
}
64+
65+
return bucket, nil
66+
}
67+
68+
func (client awsClient) EnsureS3Bucket(ctx context.Context, bucketName string) error {
69+
_, err := client.S3Client.CreateBucket(ctx, &s3.CreateBucketInput{
70+
Bucket: aws.String(bucketName),
71+
CreateBucketConfiguration: &types.CreateBucketConfiguration{
72+
LocationConstraint: types.BucketLocationConstraintUsEast2,
73+
},
74+
})
75+
76+
var exists *types.BucketAlreadyExists
77+
if err != nil {
78+
if errors.As(err, &exists) {
79+
klog.Infof("Bucket %s already exists.\n", bucketName)
80+
err = exists
81+
}
82+
} else {
83+
err := s3.NewBucketExistsWaiter(client.S3Client).Wait(
84+
ctx, &s3.HeadBucketInput{
85+
Bucket: aws.String(bucketName),
86+
},
87+
time.Minute)
88+
if err != nil {
89+
klog.Infof("Failed attempt to wait for bucket %s to exist.", bucketName)
90+
}
91+
}
92+
return err
93+
}
94+
95+
func (client awsClient) DeleteS3Bucket(ctx context.Context, bucketName string) error {
96+
_, err := client.S3Client.DeleteBucket(ctx, &s3.DeleteBucketInput{
97+
Bucket: aws.String(bucketName),
98+
})
99+
if err != nil {
100+
var noBucket *types.NoSuchBucket
101+
if errors.As(err, &noBucket) {
102+
klog.Infof("Bucket %s does not exits", bucketName)
103+
err = noBucket
104+
} else {
105+
klog.Infof("Couldn't delete bucket %s. Reason: %v", bucketName, err)
106+
}
107+
} else {
108+
err = s3.NewBucketNotExistsWaiter(client.S3Client).Wait(
109+
ctx, &s3.HeadBucketInput{
110+
Bucket: aws.String(bucketName),
111+
},
112+
time.Minute)
113+
if err != nil {
114+
klog.Infof("Failed attempt to wait for bucket %s to be deleted", bucketName)
115+
} else {
116+
klog.Infof("Bucket %s deleted", bucketName)
117+
}
118+
}
119+
return err
120+
}

0 commit comments

Comments
 (0)