Skip to content

Commit

Permalink
Merge pull request #59 from richardcase/profile-support
Browse files Browse the repository at this point in the history
AWS Profile Changes
  • Loading branch information
marccarre authored Jun 14, 2018
2 parents da23c85 + 4ffdc79 commit 98f9899
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 11 deletions.
1 change: 1 addition & 0 deletions cmd/eksctl/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func createClusterCmd() *cobra.Command {

fs.StringVarP(&cfg.ClusterName, "cluster-name", "n", "", fmt.Sprintf("EKS cluster name (generated if unspecified, e.g. %q)", exampleClusterName))
fs.StringVarP(&cfg.Region, "region", "r", DEFAULT_EKS_REGION, "AWS region")
fs.StringVarP(&cfg.Profile, "profile", "p", "", "AWS profile to use. If provided, this overrides the AWS_PROFILE environment variable")

fs.StringVarP(&cfg.NodeType, "node-type", "t", DEFAULT_NODE_TYPE, "node instance type")
fs.IntVarP(&cfg.Nodes, "nodes", "N", DEFAULT_NODE_COUNT, "total number of nodes (for a static ASG)")
Expand Down
1 change: 1 addition & 0 deletions cmd/eksctl/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func deleteClusterCmd() *cobra.Command {

fs.StringVarP(&cfg.ClusterName, "cluster-name", "n", "", "EKS cluster name (required)")
fs.StringVarP(&cfg.Region, "region", "r", DEFAULT_EKS_REGION, "AWS region")
fs.StringVarP(&cfg.Profile, "profile", "p", "", "AWS profile to use. If provided, this overrides the AWS_PROFILE environment variable")

return cmd
}
Expand Down
1 change: 1 addition & 0 deletions cmd/eksctl/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func getClusterCmd() *cobra.Command {

fs.StringVarP(&cfg.ClusterName, "cluster-name", "n", "", "EKS cluster name")
fs.StringVarP(&cfg.Region, "region", "r", DEFAULT_EKS_REGION, "AWS region")
fs.StringVarP(&cfg.Profile, "profile", "p", "", "AWS profile to use. If provided, this overrides the AWS_PROFILE environment variable")

return cmd
}
Expand Down
1 change: 1 addition & 0 deletions cmd/eksctl/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func writeKubeconfigCmd() *cobra.Command {

fs.StringVarP(&cfg.ClusterName, "cluster-name", "n", "", fmt.Sprintf("EKS cluster name (generated if unspecified, e.g. %q)", utils.ClusterName()))
fs.StringVarP(&cfg.Region, "region", "r", DEFAULT_EKS_REGION, "AWS region")
fs.StringVarP(&cfg.Profile, "profile", "p", "", "AWS profile to use. If provided, this overrides the AWS_PROFILE environment variable")

fs.StringVar(&utilsKubeconfigOutputPath, "kubeconfig", "", "path to write kubeconfig")

Expand Down
54 changes: 43 additions & 11 deletions pkg/eks/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
"os"
"sync"

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

"github.com/pkg/errors"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/ec2"
Expand Down Expand Up @@ -34,6 +37,7 @@ type providerServices struct {
// simple config, to be replaced with Cluster API
type ClusterConfig struct {
Region string
Profile string
ClusterName string
NodeAMI string
NodeType string
Expand All @@ -57,14 +61,11 @@ type ClusterConfig struct {
}

func New(clusterConfig *ClusterConfig) *ClusterProvider {
// we might want to use bits from kops, although right now it seems like too many thing we
// don't want yet
// https://github.com/kubernetes/kops/blob/master/upup/pkg/fi/cloudup/awsup/aws_cloud.go#L179
config := aws.NewConfig()
config = config.WithRegion(clusterConfig.Region)
config = config.WithCredentialsChainVerboseErrors(true)

s := session.Must(session.NewSession(config))
// Create a new session and save credentials for possible
// later re-use if overriding sessions due to custom URL
s := newSession(clusterConfig, "", nil)
creds := s.Config.Credentials

cfn := &ClusterProvider{
cfg: clusterConfig,
Expand All @@ -78,19 +79,23 @@ func New(clusterConfig *ClusterConfig) *ClusterProvider {

// override sessions if any custom endpoints specified
if endpoint, ok := os.LookupEnv("AWS_CLOUDFORMATION_ENDPOINT"); ok {
s := session.Must(session.NewSession(config.WithEndpoint(endpoint)))
logger.Debug("Setting CloudFormation endpoint to %s", endpoint)
s := newSession(clusterConfig, endpoint, creds)
cfn.svc.cfn = cloudformation.New(s)
}
if endpoint, ok := os.LookupEnv("AWS_EKS_ENDPOINT"); ok {
s := session.Must(session.NewSession(config.WithEndpoint(endpoint)))
logger.Debug("Setting EKS endpoint to %s", endpoint)
s := newSession(clusterConfig, endpoint, creds)
cfn.svc.eks = eks.New(s)
}
if endpoint, ok := os.LookupEnv("AWS_EC2_ENDPOINT"); ok {
s := session.Must(session.NewSession(config.WithEndpoint(endpoint)))
logger.Debug("Setting EC2 endpoint to %s", endpoint)
s := newSession(clusterConfig, endpoint, creds)
cfn.svc.ec2 = ec2.New(s)
}
if endpoint, ok := os.LookupEnv("AWS_STS_ENDPOINT"); ok {
s := session.Must(session.NewSession(config.WithEndpoint(endpoint)))
logger.Debug("Setting STS endpoint to %s", endpoint)
s := newSession(clusterConfig, endpoint, creds)
cfn.svc.sts = sts.New(s)
}

Expand Down Expand Up @@ -153,3 +158,30 @@ func (c *ClusterProvider) CreateCluster(taskErrs chan error) {
}, taskErrs)
close(taskErrs)
}

func newSession(clusterConfig *ClusterConfig, endpoint string, credentials *credentials.Credentials) *session.Session {
// we might want to use bits from kops, although right now it seems like too many thing we
// don't want yet
// https://github.com/kubernetes/kops/blob/master/upup/pkg/fi/cloudup/awsup/aws_cloud.go#L179
config := aws.NewConfig()
config = config.WithRegion(clusterConfig.Region)
config = config.WithCredentialsChainVerboseErrors(true)

// Create the options for the session
opts := session.Options{
Config: *config,
SharedConfigState: session.SharedConfigEnable,
Profile: clusterConfig.Profile,
AssumeRoleTokenProvider: stscreds.StdinTokenProvider,
}

if len(endpoint) > 0 {
opts.Config.Endpoint = &endpoint
}

if credentials != nil {
opts.Config.Credentials = credentials
}

return session.Must(session.NewSessionWithOptions(opts))
}

0 comments on commit 98f9899

Please sign in to comment.