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

AWS Profile Changes #59

Merged
merged 2 commits into from
Jun 14, 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
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))
}