Skip to content

Commit

Permalink
Reworked region logic
Browse files Browse the repository at this point in the history
Changed the the region logic so that if we create a AWS session
and there is no region from either the flags, aws shared config,
and aws environment variables then we re-create the session again
but specifying a default region. This is less than ideal but the
shared config isn't loaded until we create new session and the code
to read the shared config (ini files) is private.

A new issue will be opened to change this in the future to be cleaner
but we need a change to the AWS SDK first.

Signed-off-by: Richard Case <richard.case@outlook.com>
  • Loading branch information
richardcase committed Oct 11, 2018
1 parent e402f11 commit cd1d1cb
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions pkg/eks/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ func New(clusterConfig *api.ClusterConfig) *ClusterProvider {
// later re-use if overriding sessions due to custom URL
s := newSession(clusterConfig, "", nil)

if *s.Config.Region == "" {
logger.Debug("no region specified in flags or config, setting to %s", api.DefaultEKSRegion)
clusterConfig.Region = api.DefaultEKSRegion
s = newSession(clusterConfig, "", nil)
}
logger.Info("using region %s", *s.Config.Region)

provider := &ProviderServices{
cfn: cloudformation.New(s),
eks: awseks.New(s),
Expand Down Expand Up @@ -201,20 +208,10 @@ func newSession(clusterConfig *api.ClusterConfig, endpoint string, credentials *
// https://github.com/kubernetes/kops/blob/master/upup/pkg/fi/cloudup/awsup/aws_cloud.go#L179
config := aws.NewConfig()

if clusterConfig.Region == "" {
if config.Region != nil {
// If there was no region supplied and we have a region
// from the config files then update the cluster config
logger.Debug("using region %s from AWS shared configuraion or environment variables", *config.Region)
clusterConfig.Region = *config.Region
} else {
// Otherwise set our own default
logger.Debug("no region specified in flags or config, setting to %s", api.DefaultEKSRegion)
clusterConfig.Region = api.DefaultEKSRegion
}
if clusterConfig.Region != "" {
config = config.WithRegion(clusterConfig.Region)
}

config = config.WithRegion(clusterConfig.Region)
config = config.WithCredentialsChainVerboseErrors(true)
if logger.Level >= api.AWSDebugLevel {
config = config.WithLogLevel(aws.LogDebug |
Expand Down Expand Up @@ -245,7 +242,13 @@ func newSession(clusterConfig *api.ClusterConfig, endpoint string, credentials *
opts.Config.Credentials = credentials
}

return session.Must(session.NewSessionWithOptions(opts))
s := session.Must(session.NewSessionWithOptions(opts))

if clusterConfig.Region == "" {
clusterConfig.Region = *s.Config.Region
}

return s
}

// NewStackManager returns a new stack manager
Expand Down

0 comments on commit cd1d1cb

Please sign in to comment.