From 35b1ab365a29ea6b91c4d9a9880601eee8f77bbd Mon Sep 17 00:00:00 2001 From: Corey Daley Date: Mon, 26 Nov 2018 17:27:38 -0500 Subject: [PATCH] Set installConfig.Platform.AWS.UserTags as tags on the created S3 bucket --- pkg/storage/s3/s3.go | 36 ++++++++++++++++++++---------------- test/e2e/aws_test.go | 30 ++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/pkg/storage/s3/s3.go b/pkg/storage/s3/s3.go index c294566a5a..041b4997a2 100644 --- a/pkg/storage/s3/s3.go +++ b/pkg/storage/s3/s3.go @@ -13,6 +13,8 @@ import ( corev1 "k8s.io/api/core/v1" + installer "github.com/openshift/installer/pkg/types" + opapi "github.com/openshift/cluster-image-registry-operator/pkg/apis/imageregistry/v1alpha1" "github.com/openshift/cluster-image-registry-operator/pkg/clusterconfig" "github.com/openshift/cluster-image-registry-operator/pkg/storage/util" @@ -82,7 +84,7 @@ func (d *driver) checkBucketExists(svc *s3.S3) error { } // createBucket attempts to create an s3 bucket with the given name -func (d *driver) createAndTagBucket(svc *s3.S3, clusterID string) error { +func (d *driver) createAndTagBucket(svc *s3.S3, installConfig *installer.InstallConfig) error { createBucketInput := &s3.CreateBucketInput{ Bucket: aws.String(d.Config.Bucket), } @@ -96,21 +98,23 @@ func (d *driver) createAndTagBucket(svc *s3.S3, clusterID string) error { return err } - tagBucketInput := &s3.PutBucketTaggingInput{ - Bucket: aws.String(d.Config.Bucket), - Tagging: &s3.Tagging{ - TagSet: []*s3.Tag{ - { - Key: aws.String("tectonicClusterID"), - Value: aws.String(clusterID), - }, + if installConfig.Platform.AWS != nil { + var tagSet []*s3.Tag + tagSet = append(tagSet, &s3.Tag{Key: aws.String("tectonicClusterID"), Value: aws.String(installConfig.ClusterID)}) + for k, v := range installConfig.Platform.AWS.UserTags { + tagSet = append(tagSet, &s3.Tag{Key: aws.String(k), Value: aws.String(v)}) + } + + tagBucketInput := &s3.PutBucketTaggingInput{ + Bucket: aws.String(d.Config.Bucket), + Tagging: &s3.Tagging{ + TagSet: tagSet, }, - }, - } - if _, err := svc.PutBucketTagging(tagBucketInput); err != nil { - return err + } + if _, err := svc.PutBucketTagging(tagBucketInput); err != nil { + return err + } } - return nil } @@ -151,7 +155,7 @@ func (d *driver) CompleteConfiguration() error { if len(d.Config.Bucket) == 0 { for { d.Config.Bucket = fmt.Sprintf("%s-%s", clusterconfig.STORAGE_PREFIX, string(uuid.NewUUID())) - if err := d.createAndTagBucket(svc, installConfig.ClusterID); err != nil { + if err := d.createAndTagBucket(svc, installConfig); err != nil { if aerr, ok := err.(awserr.Error); ok { switch aerr.Code() { case s3.ErrCodeBucketAlreadyExists: @@ -169,7 +173,7 @@ func (d *driver) CompleteConfiguration() error { if aerr, ok := err.(awserr.Error); ok { switch aerr.Code() { case s3.ErrCodeNoSuchBucket: - if err = d.createAndTagBucket(svc, installConfig.ClusterID); err != nil { + if err = d.createAndTagBucket(svc, installConfig); err != nil { return err } default: diff --git a/test/e2e/aws_test.go b/test/e2e/aws_test.go index c6472d43b8..550aff3458 100644 --- a/test/e2e/aws_test.go +++ b/test/e2e/aws_test.go @@ -90,7 +90,7 @@ func TestAWS(t *testing.T) { t.Errorf("s3 bucket %s does not exist or is inaccessible: %#v", imageRegistryOperatorCustomResource.Spec.Storage.S3.Bucket, err) } - // Check that the S3 bucket has the correct cluster id tag + // Check that the S3 bucket has the correct tags getBucketTaggingResult, err := svc.GetBucketTagging(&s3.GetBucketTaggingInput{ Bucket: aws.String(imageRegistryOperatorCustomResource.Spec.Storage.S3.Bucket), }) @@ -98,17 +98,27 @@ func TestAWS(t *testing.T) { t.Errorf("unable to get tagging information for s3 bucket: %#v", err) } - found := false - for _, v := range getBucketTaggingResult.TagSet { - if *v.Key == "tectonicClusterID" { - found = true - if *v.Value != installConfig.ClusterID { - t.Errorf("s3 bucket has the wrong value for tag \"tectonicClusterID\": wanted %s, got %s", installConfig.ClusterID, *v.Value) + tagShouldExist := map[string]string{ + "tectonicClusterID": installConfig.ClusterID, + } + for k, v := range installConfig.Platform.AWS.UserTags { + tagShouldExist[k] = v + } + + for tk, tv := range tagShouldExist { + found := false + + for _, v := range getBucketTaggingResult.TagSet { + if *v.Key == tk { + found = true + if *v.Value != tv { + t.Errorf("s3 bucket has the wrong value for tag \"%s\": wanted %s, got %s", tk, *v.Value, tv) + } } } - } - if !found { - t.Errorf("s3 bucket does not have the tag \"tectonicClusterID\": got %#v", getBucketTaggingResult.TagSet) + if !found { + t.Errorf("s3 bucket does not have the tag \"%s\": got %#v", tk, getBucketTaggingResult.TagSet) + } } // Check that the S3 configuration environment variables