Skip to content

Commit

Permalink
Merge pull request #241 from weaveworks/default-region
Browse files Browse the repository at this point in the history
Changed region handling
  • Loading branch information
errordeveloper authored Oct 5, 2018
2 parents 7e3e686 + e96cbaa commit 5b98761
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
15 changes: 10 additions & 5 deletions cmd/eksctl/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func createClusterCmd() *cobra.Command {

fs.StringVarP(&cfg.ClusterName, "name", "n", "", fmt.Sprintf("EKS cluster name (generated if unspecified, e.g. %q)", exampleClusterName))

fs.StringVarP(&cfg.Region, "region", "r", api.DEFAULT_EKS_REGION, "AWS region")
fs.StringVarP(&cfg.Region, "region", "r", "", "AWS region")
fs.StringVarP(&cfg.Profile, "profile", "p", "", "AWS credentials profile to use (overrides the AWS_PROFILE environment variable)")
fs.StringToStringVarP(&cfg.Tags, "tags", "", map[string]string{}, `A list of KV pairs used to tag the AWS resources (e.g. "Owner=John Doe,Team=Some Team")`)

Expand Down Expand Up @@ -102,6 +102,15 @@ func createClusterCmd() *cobra.Command {
func doCreateCluster(cfg *api.ClusterConfig, name string) error {
ctl := eks.New(cfg)

if cfg.Region == "" {
logger.Debug("no region specified in flags or config, setting to %s", api.DEFAULT_EKS_REGION)
cfg.Region = api.DEFAULT_EKS_REGION
}

if cfg.Region != api.EKS_REGION_US_WEST_2 && cfg.Region != api.EKS_REGION_US_EAST_1 && cfg.Region != api.EKS_REGION_EU_WEST_1 {
return fmt.Errorf("%s is not supported only %s, %s and %s are supported", cfg.Region, api.EKS_REGION_US_WEST_2, api.EKS_REGION_US_EAST_1, api.EKS_REGION_EU_WEST_1)
}

if err := ctl.CheckAuth(); err != nil {
return err
}
Expand All @@ -122,10 +131,6 @@ func doCreateCluster(cfg *api.ClusterConfig, name string) error {
return fmt.Errorf("--ssh-public-key must be non-empty string")
}

if cfg.Region != api.EKS_REGION_US_WEST_2 && cfg.Region != api.EKS_REGION_US_EAST_1 && cfg.Region != api.EKS_REGION_EU_WEST_1 {
return fmt.Errorf("--region=%s is not supported only %s, %s and %s are supported", cfg.Region, api.EKS_REGION_US_WEST_2, api.EKS_REGION_US_EAST_1, api.EKS_REGION_EU_WEST_1)
}

if err := ctl.SetAvailabilityZones(availabilityZones); err != nil {
return err
}
Expand Down
11 changes: 8 additions & 3 deletions cmd/eksctl/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package main

import (
"fmt"
"github.com/spf13/cobra"
"os"
"strings"

"github.com/spf13/cobra"

"github.com/kubicorn/kubicorn/pkg/logger"

"github.com/weaveworks/eksctl/pkg/eks"
Expand Down Expand Up @@ -45,7 +46,7 @@ func deleteClusterCmd() *cobra.Command {

fs.StringVarP(&cfg.ClusterName, "name", "n", "", "EKS cluster name (required)")

fs.StringVarP(&cfg.Region, "region", "r", api.DEFAULT_EKS_REGION, "AWS region")
fs.StringVarP(&cfg.Region, "region", "r", "", "AWS region")
fs.StringVarP(&cfg.Profile, "profile", "p", "", "AWS credentials profile to use (overrides the AWS_PROFILE environment variable)")

fs.DurationVar(&cfg.WaitTimeout, "timeout", api.DefaultWaitTimeout, "max wait time in any polling operations")
Expand All @@ -56,6 +57,10 @@ func deleteClusterCmd() *cobra.Command {
func doDeleteCluster(cfg *api.ClusterConfig, name string) error {
ctl := eks.New(cfg)

if cfg.Region == "" {
cfg.Region = api.DEFAULT_EKS_REGION
}

if err := ctl.CheckAuth(); err != nil {
return err
}
Expand Down Expand Up @@ -92,7 +97,7 @@ func doDeleteCluster(cfg *api.ClusterConfig, name string) error {

handleIfError(stackManager.WaitDeleteNodeGroup(), "node group")
if handleIfError(stackManager.DeleteCluster(), "cluster") {
if handleIfError(ctl.DeprecatedDeleteControlPlane(),"control plane") {
if handleIfError(ctl.DeprecatedDeleteControlPlane(), "control plane") {
handleIfError(stackManager.DeprecatedDeleteStackControlPlane(), "stack control plane")
}
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/eksctl/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func getClusterCmd() *cobra.Command {
fs.StringVarP(&cfg.ClusterName, "name", "n", "", "EKS cluster name")
fs.IntVar(&chunkSize, "chunk-size", DEFAULT_CHUNK_SIZE, "Return large lists in chunks rather than all at once. Pass 0 to disable.")

fs.StringVarP(&cfg.Region, "region", "r", api.DEFAULT_EKS_REGION, "AWS region")
fs.StringVarP(&cfg.Region, "region", "r", "", "AWS region")
fs.StringVarP(&cfg.Profile, "profile", "p", "", "AWS credentials profile to use (overrides the AWS_PROFILE environment variable)")

fs.StringVarP(&output, "output", "o", "table", "Specifies the output format. Choose from table,json,yaml. Defaults to table.")
Expand All @@ -64,6 +64,10 @@ func getClusterCmd() *cobra.Command {
func doGetCluster(cfg *api.ClusterConfig, name string) error {
ctl := eks.New(cfg)

if cfg.Region == "" {
cfg.Region = api.DEFAULT_EKS_REGION
}

if err := ctl.CheckAuth(); err != nil {
return err
}
Expand Down
12 changes: 10 additions & 2 deletions cmd/eksctl/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func writeKubeconfigCmd() *cobra.Command {

fs.StringVarP(&cfg.ClusterName, "name", "n", "", "EKS cluster name (required)")

fs.StringVarP(&cfg.Region, "region", "r", api.DEFAULT_EKS_REGION, "AWS region")
fs.StringVarP(&cfg.Region, "region", "r", "", "AWS region")
fs.StringVarP(&cfg.Profile, "profile", "p", "", "AWS credentials profile to use (overrides the AWS_PROFILE environment variable)")

fs.BoolVar(&utilsAutoKubeconfigPath, "auto-kubeconfig", false, fmt.Sprintf("save kubconfig file by cluster name – %q", kubeconfig.AutoPath("<name>")))
Expand All @@ -122,6 +122,10 @@ func writeKubeconfigCmd() *cobra.Command {
func doWriteKubeconfigCmd(cfg *api.ClusterConfig, name string) error {
ctl := eks.New(cfg)

if cfg.Region == "" {
cfg.Region = api.DEFAULT_EKS_REGION
}

if err := ctl.CheckAuth(); err != nil {
return err
}
Expand Down Expand Up @@ -190,7 +194,7 @@ func describeStacksCmd() *cobra.Command {

fs.StringVarP(&cfg.ClusterName, "name", "n", "", "EKS cluster name (required)")

fs.StringVarP(&cfg.Region, "region", "r", api.DEFAULT_EKS_REGION, "AWS region")
fs.StringVarP(&cfg.Region, "region", "r", "", "AWS region")
fs.StringVarP(&cfg.Profile, "profile", "p", "", "AWS credentials profile to use (overrides the AWS_PROFILE environment variable)")

fs.BoolVar(&utilsDescribeStackAll, "all", false, "include deleted stacks")
Expand All @@ -202,6 +206,10 @@ func describeStacksCmd() *cobra.Command {
func doDescribeStacksCmd(cfg *api.ClusterConfig, name string) error {
ctl := eks.New(cfg)

if cfg.Region == "" {
cfg.Region = api.DEFAULT_EKS_REGION
}

if err := ctl.CheckAuth(); err != nil {
return err
}
Expand Down
11 changes: 10 additions & 1 deletion pkg/eks/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ func New(clusterConfig *api.ClusterConfig) *ClusterProvider {
// later re-use if overriding sessions due to custom URL
s := newSession(clusterConfig, "", nil)

// If there was no region supplied and we have a region
// from the config files then update the cluster config
if clusterConfig.Region == "" && *s.Config.Region != "" {
logger.Debug("using region %s from AWS shared configuraion or environment variables", *s.Config.Region)
clusterConfig.Region = *s.Config.Region
}

provider := &ProviderServices{
cfn: cloudformation.New(s),
eks: awseks.New(s),
Expand Down Expand Up @@ -187,7 +194,9 @@ func newSession(clusterConfig *api.ClusterConfig, endpoint string, credentials *
// 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)
if clusterConfig.Region != "" {
config = config.WithRegion(clusterConfig.Region)
}
config = config.WithCredentialsChainVerboseErrors(true)
if logger.Level >= api.AWSDebugLevel {
config = config.WithLogLevel(aws.LogDebug |
Expand Down

0 comments on commit 5b98761

Please sign in to comment.