Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set user tags on s3 bucket #84

Merged
merged 1 commit into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions pkg/storage/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
30 changes: 20 additions & 10 deletions test/e2e/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,25 +90,35 @@ 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),
})
if err != nil {
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
Expand Down