Skip to content

Commit c2cbc1f

Browse files
committed
New Feature, s3 bucket tagging for velero backups.
1 parent 9bc8ce2 commit c2cbc1f

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed

deploy/credential_request.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ spec:
1919
- s3:PutBucketPublicAccessBlock
2020
- s3:PutEncryptionConfiguration
2121
- s3:PutLifecycleConfiguration
22+
- s3:PutBucketTagging
23+
- s3:DeleteObjectTagging
24+
- s3:GetBucketTagging
2225
resource: "*"

pkg/controller/velero/s3.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ func (r *ReconcileVelero) provisionS3(reqLogger logr.Logger, s3Client *awss3.S3,
6767
return reconcile.Result{}, fmt.Errorf("error occurred when creating bucket %v: %v", instance.Status.S3Bucket.Name, err.Error())
6868
}
6969
}
70-
70+
err = s3.TagBucket(s3Client, instance.Status.S3Bucket.Name, deafultBackupStorageLocation)
71+
if err != nil {
72+
return reconcile.Result{}, fmt.Errorf("error occurred when tagging bucket %v: %v", instance.Status.S3Bucket.Name, err.Error())
73+
}
7174
}
7275

7376
// Verify S3 bucket exists
@@ -115,6 +118,13 @@ func (r *ReconcileVelero) provisionS3(reqLogger logr.Logger, s3Client *awss3.S3,
115118
return reconcile.Result{}, fmt.Errorf("error occurred when configuring lifecycle rules on bucket %v: %v", instance.Status.S3Bucket.Name, err.Error())
116119
}
117120

121+
// Make sure that tags are applied to buckets
122+
bucketLog.Info("Enforcing S3 Bucket tags on S3 Bucket")
123+
err = s3.TagBucket(s3Client, instance.Status.S3Bucket.Name, deafultBackupStorageLocation)
124+
if err != nil {
125+
return reconcile.Result{}, fmt.Errorf("error occurred when tagging bucket %v: %v", instance.Status.S3Bucket.Name, err.Error())
126+
}
127+
118128
instance.Status.S3Bucket.Provisioned = true
119129
instance.Status.S3Bucket.LastSyncTimestamp = &metav1.Time{
120130
Time: time.Now(),

pkg/controller/velero/velero.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ import (
2525
)
2626

2727
const (
28-
awsCredsSecretIDKey = "aws_access_key_id" // #nosec G101
29-
awsCredsSecretAccessKey = "aws_secret_access_key" // #nosec G101
30-
credentialsRequestName = "velero-iam-credentials"
31-
veleroImage = "gcr.io/heptio-images/velero:v1.0.0"
28+
awsCredsSecretIDKey = "aws_access_key_id" // #nosec G101
29+
awsCredsSecretAccessKey = "aws_secret_access_key" // #nosec G101
30+
credentialsRequestName = "velero-iam-credentials"
31+
veleroImage = "gcr.io/heptio-images/velero:v1.0.0"
32+
deafultBackupStorageLocation = "default"
3233
)
3334

3435
func (r *ReconcileVelero) provisionVelero(reqLogger logr.Logger, namespace string, platformStatus *configv1.PlatformStatus, instance *veleroCR.Velero) (reconcile.Result, error) {
@@ -40,7 +41,7 @@ func (r *ReconcileVelero) provisionVelero(reqLogger logr.Logger, namespace strin
4041
// Install BackupStorageLocation
4142
foundBsl := &velerov1.BackupStorageLocation{}
4243
bsl := veleroInstall.BackupStorageLocation(namespace, strings.ToLower(string(platformStatus.Type)), instance.Status.S3Bucket.Name, "", locationConfig)
43-
if err = r.client.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: "default"}, foundBsl); err != nil {
44+
if err = r.client.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: deafultBackupStorageLocation}, foundBsl); err != nil {
4445
if errors.IsNotFound(err) {
4546
// Didn't find BackupStorageLocation
4647
reqLogger.Info("Creating BackupStorageLocation")

pkg/s3/bucket.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import (
88
"github.com/aws/aws-sdk-go/service/s3"
99
)
1010

11+
const (
12+
bucketTagKey = "velero.io/backup-location"
13+
)
14+
1115
func CreateBucket(s3Client *s3.S3, bucketName string) error {
1216
createBucketInput := &s3.CreateBucketInput{
1317
ACL: aws.String(s3.BucketCannedACLPrivate),
@@ -124,3 +128,38 @@ func SetBucketLifecycle(s3Client *s3.S3, bucketName string) error {
124128

125129
return err
126130
}
131+
func CreateBucketTaggingInput(bucketname string, backUpLocation string) *s3.PutBucketTaggingInput {
132+
putInput := &s3.PutBucketTaggingInput{
133+
Bucket: aws.String(bucketname),
134+
Tagging: &s3.Tagging{
135+
TagSet: []*s3.Tag{
136+
{
137+
Key: aws.String(bucketTagKey),
138+
Value: aws.String(backUpLocation),
139+
},
140+
},
141+
},
142+
}
143+
return putInput
144+
}
145+
146+
func ClearBucketTags(s3Client *s3.S3, bucketName string) (err error) {
147+
deleteInput := &s3.DeleteBucketTaggingInput{Bucket: aws.String(bucketName)}
148+
result, err := s3Client.DeleteBucketTagging(deleteInput)
149+
fmt.Println(result)
150+
return err
151+
}
152+
153+
func TagBucket(s3Client *s3.S3, bucketName string, backUpLocation string) error {
154+
err := ClearBucketTags(s3Client, bucketName)
155+
if err != nil {
156+
return fmt.Errorf("unable to clear %v bucket tags: %v", bucketName, err)
157+
}
158+
input := CreateBucketTaggingInput(bucketName, backUpLocation)
159+
_, err = s3Client.PutBucketTagging(input)
160+
if err != nil {
161+
fmt.Println(err.Error())
162+
return err
163+
}
164+
return nil
165+
}

0 commit comments

Comments
 (0)