-
Notifications
You must be signed in to change notification settings - Fork 82
aws cloudstorage bucket refactor #561
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
Merged
dymurray
merged 7 commits into
openshift:master
from
kaovilai:cloudstorage_azure-aws_refactor
Feb 15, 2022
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cabf219
aws cloudstorage bucket refactor
kaovilai 253b89d
remove unused configmap
kaovilai f1c8318
azure wip
kaovilai 65ec035
remove added internal methods from interface, passes more parameters
kaovilai 7a2aa73
indent
kaovilai 31027fc
remove wip azure.go
kaovilai e9ca4f8
address https://github.com/openshift/oadp-operator/pull/561#pullreque…
kaovilai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,11 +19,13 @@ type CloudStorage struct { | |
type CloudStorageProvider string | ||
|
||
const ( | ||
AWSBucketProvider CloudStorageProvider = "aws" | ||
AWSBucketProvider CloudStorageProvider = "aws" | ||
AzureBucketProvider CloudStorageProvider = "azure" | ||
GCPBucketProvider CloudStorageProvider = "gcp" | ||
) | ||
|
||
type CloudStorageSpec struct { | ||
// Name is the name requested for the bucket | ||
// Name is the name requested for the bucket (aws) or container (azure) | ||
Name string `json:"name"` | ||
// CreationSecret is the secret that is needed to be used while creating the bucket. | ||
CreationSecret corev1.SecretKeySelector `json:"creationSecret"` | ||
|
@@ -36,6 +38,14 @@ type CloudStorageSpec struct { | |
Region string `json:"region,omitempty"` | ||
// +kubebuilder:validation:Enum=aws | ||
Provider CloudStorageProvider `json:"provider"` | ||
|
||
// https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/storage/[email protected]#section-readme | ||
// azure blob primary endpoint | ||
// az storage account show -g <resource-group> -n <storage-account> | ||
// need storage account name and key to create azure container | ||
// az storage container create -n <container-name> --account-name <storage-account-name> --account-key <storage-account-key> | ||
// azure account key will use CreationSecret to store key and account name | ||
|
||
} | ||
|
||
type CloudStorageStatus struct { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package bucket | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/s3" | ||
"github.com/aws/aws-sdk-go/service/s3/s3iface" | ||
"github.com/openshift/oadp-operator/api/v1alpha1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type awsBucketClient struct { | ||
bucket v1alpha1.CloudStorage | ||
client client.Client | ||
} | ||
|
||
func (a awsBucketClient) Exists() (bool, error) { | ||
s3Client, err := a.getS3Client() | ||
if err != nil { | ||
return false, err | ||
} | ||
input := &s3.HeadBucketInput{ | ||
Bucket: aws.String(a.bucket.Spec.Name), | ||
} | ||
_, err = s3Client.HeadBucket(input) | ||
if err != nil { | ||
if aerr, ok := err.(awserr.Error); ok { | ||
switch aerr.Code() { | ||
// This is supposed to say "NoSuchBucket", but actually emits "NotFound" | ||
// https://github.com/aws/aws-sdk-go/issues/2593 | ||
case s3.ErrCodeNoSuchBucket, "NotFound": | ||
return false, nil | ||
default: | ||
// Return true, because we are unable to detemine if bucket exists or not | ||
return true, fmt.Errorf("unable to determine bucket %v status: %v", a.bucket.Spec.Name, aerr.Error()) | ||
} | ||
} else { | ||
// Return true, because we are unable to detemine if bucket exists or not | ||
return true, fmt.Errorf("unable to determine bucket %v status: %v", a.bucket.Spec.Name, aerr.Error()) | ||
} | ||
} | ||
|
||
err = a.tagBucket() | ||
if err != nil { | ||
return true, err | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
func (a awsBucketClient) Create() (bool, error) { | ||
s3Client, err := a.getS3Client() | ||
if err != nil { | ||
return false, err | ||
} | ||
createBucketInput := &s3.CreateBucketInput{ | ||
ACL: aws.String(s3.BucketCannedACLPrivate), | ||
Bucket: aws.String(a.bucket.Spec.Name), | ||
} | ||
if a.bucket.Spec.Region != "us-east-1" { | ||
createBucketConfiguration := &s3.CreateBucketConfiguration{ | ||
LocationConstraint: &a.bucket.Spec.Region, | ||
} | ||
createBucketInput.SetCreateBucketConfiguration(createBucketConfiguration) | ||
} | ||
if err := createBucketInput.Validate(); err != nil { | ||
return false, fmt.Errorf("unable to validate %v bucket creation configuration: %v", a.bucket.Spec.Name, err) | ||
} | ||
|
||
_, err = s3Client.CreateBucket(createBucketInput) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
// tag Bucket. | ||
err = a.tagBucket() | ||
if err != nil { | ||
return true, err | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
func (a awsBucketClient) tagBucket() error { | ||
s3Client, err := a.getS3Client() | ||
// Clear bucket tags. | ||
if err != nil { | ||
return err | ||
} | ||
deleteInput := &s3.DeleteBucketTaggingInput{Bucket: aws.String(a.bucket.Spec.Name)} | ||
_, err = s3Client.DeleteBucketTagging(deleteInput) | ||
if err != nil { | ||
return err | ||
} | ||
input := CreateBucketTaggingInput(a.bucket.Spec.Name, a.bucket.Spec.Tags) | ||
|
||
_, err = s3Client.PutBucketTagging(input) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// CreateBucketTaggingInput creates an S3 PutBucketTaggingInput object, | ||
// which is used to associate a list of tags with a bucket. | ||
func CreateBucketTaggingInput(bucketname string, tags map[string]string) *s3.PutBucketTaggingInput { | ||
putInput := &s3.PutBucketTaggingInput{ | ||
Bucket: aws.String(bucketname), | ||
Tagging: &s3.Tagging{ | ||
TagSet: []*s3.Tag{}, | ||
}, | ||
} | ||
for key, value := range tags { | ||
newTag := s3.Tag{ | ||
Key: aws.String(key), | ||
Value: aws.String(value), | ||
} | ||
putInput.Tagging.TagSet = append(putInput.Tagging.TagSet, &newTag) | ||
} | ||
return putInput | ||
} | ||
|
||
func (a awsBucketClient) getS3Client() (s3iface.S3API, error) { | ||
awsConfig := &aws.Config{Region: &a.bucket.Spec.Region} | ||
cred, err := getCredentialFromCloudStorageSecret(a.client, a.bucket) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
opts := session.Options{ | ||
Config: *awsConfig, | ||
SharedConfigFiles: []string{cred}, | ||
} | ||
|
||
if a.bucket.Spec.EnableSharedConfig != nil && *a.bucket.Spec.EnableSharedConfig { | ||
opts.SharedConfigState = session.SharedConfigEnable | ||
} | ||
|
||
s, err := session.NewSessionWithOptions(opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return s3.New(s), nil | ||
} | ||
|
||
func (a awsBucketClient) ForceCredentialRefresh() error { | ||
return fmt.Errorf("force credential refresh is not yet implemented") | ||
} | ||
|
||
func (a awsBucketClient) Delete() (bool, error) { | ||
s3Client, err := a.getS3Client() | ||
if err != nil { | ||
return false, err | ||
} | ||
deleteBucketInput := &s3.DeleteBucketInput{ | ||
Bucket: aws.String(a.bucket.Spec.Name), | ||
} | ||
|
||
if err := deleteBucketInput.Validate(); err != nil { | ||
return false, fmt.Errorf("unable to validate %v bucket deletion configuration: %v", a.bucket.Spec.Name, err) | ||
} | ||
|
||
_, err = s3Client.DeleteBucket(deleteBucketInput) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return true, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.