Skip to content

Commit

Permalink
Refactor out config part
Browse files Browse the repository at this point in the history
  • Loading branch information
Hakan Memisoglu committed Aug 20, 2019
1 parent e992040 commit bf088c5
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 49 deletions.
34 changes: 2 additions & 32 deletions pkg/blockstorage/awsebs/awsebs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
Expand All @@ -21,6 +20,7 @@ import (
"github.com/kanisterio/kanister/pkg/blockstorage"
ktags "github.com/kanisterio/kanister/pkg/blockstorage/tags"
"github.com/kanisterio/kanister/pkg/blockstorage/zone"
awsconfig "github.com/kanisterio/kanister/pkg/config/aws"
)

var _ blockstorage.Provider = (*ebsStorage)(nil)
Expand All @@ -38,17 +38,6 @@ type EC2 struct {

const (
maxRetries = 10
// ConfigRegion represents region key required in the map "config"
ConfigRegion = "region"
// ConfigRole represents the key for the ARN of the role which can be assumed.
// It is optional.
ConfigRole = "role"
// AccessKeyID represents AWS Access key ID
AccessKeyID = "AWS_ACCESS_KEY_ID"
// SecretAccessKey represents AWS Secret Access Key
SecretAccessKey = "AWS_SECRET_ACCESS_KEY"
// SessionToken represents AWS Session Key
SessionToken = "AWS_SESSION_TOKEN"
)

func (s *ebsStorage) Type() blockstorage.Type {
Expand All @@ -57,7 +46,7 @@ func (s *ebsStorage) Type() blockstorage.Type {

// NewProvider returns a provider for the EBS storage type in the specified region
func NewProvider(config map[string]string) (blockstorage.Provider, error) {
awsConfig, region, _, err := GetConfig(config)
awsConfig, region, _, err := awsconfig.GetConfig(config)
if err != nil {
return nil, err
}
Expand All @@ -68,25 +57,6 @@ func NewProvider(config map[string]string) (blockstorage.Provider, error) {
return &ebsStorage{ec2Cli: ec2Cli}, nil
}

// GetConfig returns a configuration to establish AWS connection, connected region name and the role to assume if it exists.
func GetConfig(config map[string]string) (awsConfig *aws.Config, region string, role string, err error) {
region, ok := config[ConfigRegion]
if !ok {
return nil, "", "", errors.New("region required for storage type EBS")
}
accessKey, ok := config[AccessKeyID]
if !ok {
return nil, "", "", errors.New("AWS_ACCESS_KEY_ID required for storage type EBS")
}
secretAccessKey, ok := config[SecretAccessKey]
if !ok {
return nil, "", "", errors.New("AWS_SECRET_ACCESS_KEY required for storage type EBS")
}
sessionToken := config[SessionToken]
role = config[ConfigRole]
return &aws.Config{Credentials: credentials.NewStaticCredentials(accessKey, secretAccessKey, sessionToken)}, region, role, nil
}

// newEC2Client returns ec2 client struct.
func newEC2Client(awsRegion string, config *aws.Config) (*EC2, error) {
httpClient := &http.Client{Transport: http.DefaultTransport}
Expand Down
4 changes: 2 additions & 2 deletions pkg/blockstorage/awsefs/awsefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"k8s.io/apimachinery/pkg/util/rand"

"github.com/kanisterio/kanister/pkg/blockstorage"
"github.com/kanisterio/kanister/pkg/blockstorage/awsebs"
kantags "github.com/kanisterio/kanister/pkg/blockstorage/tags"
awsconfig "github.com/kanisterio/kanister/pkg/config/aws"
)

type efs struct {
Expand Down Expand Up @@ -45,7 +45,7 @@ const (

// NewEFSProvider retuns a blockstorage provider for AWS EFS.
func NewEFSProvider(config map[string]string) (blockstorage.Provider, error) {
awsConfig, region, role, err := awsebs.GetConfig(config)
awsConfig, region, role, err := awsconfig.GetConfig(config)
if err != nil {
return nil, errors.Wrap(err, "Failed to get configuration for EFS")
}
Expand Down
41 changes: 41 additions & 0 deletions pkg/config/aws/aws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package aws

import (
"errors"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
)

const (
// ConfigRegion represents region key required in the map "config"
ConfigRegion = "region"
// ConfigRole represents the key for the ARN of the role which can be assumed.
// It is optional.
ConfigRole = "role"
// AccessKeyID represents AWS Access key ID
AccessKeyID = "AWS_ACCESS_KEY_ID"
// SecretAccessKey represents AWS Secret Access Key
SecretAccessKey = "AWS_SECRET_ACCESS_KEY"
// SessionToken represents AWS Session Key
SessionToken = "AWS_SESSION_TOKEN"
)

// GetConfig returns a configuration to establish AWS connection, connected region name and the role to assume if it exists.
func GetConfig(config map[string]string) (awsConfig *aws.Config, region string, role string, err error) {
region, ok := config[ConfigRegion]
if !ok {
return nil, "", "", errors.New("region required for storage type EBS")
}
accessKey, ok := config[AccessKeyID]
if !ok {
return nil, "", "", errors.New("AWS_ACCESS_KEY_ID required for storage type EBS")
}
secretAccessKey, ok := config[SecretAccessKey]
if !ok {
return nil, "", "", errors.New("AWS_SECRET_ACCESS_KEY required for storage type EBS")
}
sessionToken := config[SessionToken]
role = config[ConfigRole]
return &aws.Config{Credentials: credentials.NewStaticCredentials(accessKey, secretAccessKey, sessionToken)}, region, role, nil
}
8 changes: 4 additions & 4 deletions pkg/function/create_volume_from_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

kanister "github.com/kanisterio/kanister/pkg"
"github.com/kanisterio/kanister/pkg/blockstorage"
"github.com/kanisterio/kanister/pkg/blockstorage/awsebs"
awsconfig "github.com/kanisterio/kanister/pkg/config/aws"
"github.com/kanisterio/kanister/pkg/blockstorage/getter"
"github.com/kanisterio/kanister/pkg/kube"
kubevolume "github.com/kanisterio/kanister/pkg/kube/volume"
Expand Down Expand Up @@ -60,9 +60,9 @@ func createVolumeFromSnapshot(ctx context.Context, cli kubernetes.Interface, nam
}
switch pvcInfo.Type {
case blockstorage.TypeEBS:
config[awsebs.ConfigRegion] = pvcInfo.Region
config[awsebs.AccessKeyID] = profile.Credential.KeyPair.ID
config[awsebs.SecretAccessKey] = profile.Credential.KeyPair.Secret
config[awsconfig.ConfigRegion] = pvcInfo.Region
config[awsconfig.AccessKeyID] = profile.Credential.KeyPair.ID
config[awsconfig.SecretAccessKey] = profile.Credential.KeyPair.Secret
case blockstorage.TypeGPD:
config[blockstorage.GoogleProjectID] = profile.Credential.KeyPair.ID
config[blockstorage.GoogleServiceKey] = profile.Credential.KeyPair.Secret
Expand Down
7 changes: 4 additions & 3 deletions pkg/function/create_volume_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/kanisterio/kanister/pkg/blockstorage"
"github.com/kanisterio/kanister/pkg/blockstorage/awsebs"
"github.com/kanisterio/kanister/pkg/blockstorage/getter"
awsconfig "github.com/kanisterio/kanister/pkg/config/aws"
"github.com/kanisterio/kanister/pkg/kube"
kubevolume "github.com/kanisterio/kanister/pkg/kube/volume"
"github.com/kanisterio/kanister/pkg/param"
Expand Down Expand Up @@ -203,9 +204,9 @@ func getPVCInfo(ctx context.Context, kubeCli kubernetes.Interface, namespace str
}
}
if pvZone, ok := pvLabels[kubevolume.PVZoneLabelName]; ok {
config[awsebs.ConfigRegion] = region
config[awsebs.AccessKeyID] = tp.Profile.Credential.KeyPair.ID
config[awsebs.SecretAccessKey] = tp.Profile.Credential.KeyPair.Secret
config[awsconfig.ConfigRegion] = region
config[awsconfig.AccessKeyID] = tp.Profile.Credential.KeyPair.ID
config[awsconfig.SecretAccessKey] = tp.Profile.Credential.KeyPair.Secret
provider, err = getter.Get(blockstorage.TypeEBS, config)
if err != nil {
return nil, errors.Wrap(err, "Could not get storage provider")
Expand Down
8 changes: 4 additions & 4 deletions pkg/function/delete_volume_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

kanister "github.com/kanisterio/kanister/pkg"
"github.com/kanisterio/kanister/pkg/blockstorage"
"github.com/kanisterio/kanister/pkg/blockstorage/awsebs"
"github.com/kanisterio/kanister/pkg/blockstorage/getter"
awsconfig "github.com/kanisterio/kanister/pkg/config/aws"
"github.com/kanisterio/kanister/pkg/kube"
"github.com/kanisterio/kanister/pkg/param"
)
Expand Down Expand Up @@ -52,9 +52,9 @@ func deleteVolumeSnapshot(ctx context.Context, cli kubernetes.Interface, namespa
}
switch pvcInfo.Type {
case blockstorage.TypeEBS:
config[awsebs.ConfigRegion] = pvcInfo.Region
config[awsebs.AccessKeyID] = profile.Credential.KeyPair.ID
config[awsebs.SecretAccessKey] = profile.Credential.KeyPair.Secret
config[awsconfig.ConfigRegion] = pvcInfo.Region
config[awsconfig.AccessKeyID] = profile.Credential.KeyPair.ID
config[awsconfig.SecretAccessKey] = profile.Credential.KeyPair.Secret
case blockstorage.TypeGPD:
config[blockstorage.GoogleProjectID] = profile.Credential.KeyPair.ID
config[blockstorage.GoogleServiceKey] = profile.Credential.KeyPair.Secret
Expand Down
8 changes: 4 additions & 4 deletions pkg/function/wait_for_snapshot_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

kanister "github.com/kanisterio/kanister/pkg"
"github.com/kanisterio/kanister/pkg/blockstorage"
"github.com/kanisterio/kanister/pkg/blockstorage/awsebs"
"github.com/kanisterio/kanister/pkg/blockstorage/getter"
awsconfig "github.com/kanisterio/kanister/pkg/config/aws"
"github.com/kanisterio/kanister/pkg/param"
)

Expand Down Expand Up @@ -56,9 +56,9 @@ func waitForSnapshotsCompletion(ctx context.Context, snapshotinfo string, profil
}
switch pvcInfo.Type {
case blockstorage.TypeEBS:
config[awsebs.ConfigRegion] = pvcInfo.Region
config[awsebs.AccessKeyID] = profile.Credential.KeyPair.ID
config[awsebs.SecretAccessKey] = profile.Credential.KeyPair.Secret
config[awsconfig.ConfigRegion] = pvcInfo.Region
config[awsconfig.AccessKeyID] = profile.Credential.KeyPair.ID
config[awsconfig.SecretAccessKey] = profile.Credential.KeyPair.Secret
case blockstorage.TypeGPD:
config[blockstorage.GoogleProjectID] = profile.Credential.KeyPair.ID
config[blockstorage.GoogleServiceKey] = profile.Credential.KeyPair.Secret
Expand Down

0 comments on commit bf088c5

Please sign in to comment.