Skip to content

Commit

Permalink
Improve consistency of command function
Browse files Browse the repository at this point in the history
- perform defaulting and validation
- check region is supported
  • Loading branch information
errordeveloper committed Aug 8, 2019
1 parent 4ca14cd commit 3c85303
Show file tree
Hide file tree
Showing 19 changed files with 147 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/ctl/create/iamidentitymapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,19 @@ func doCreateIAMIdentityMapping(rc *cmdutils.ResourceCmd, id *authconfigmap.MapR

cfg := rc.ClusterConfig

api.SetClusterConfigDefaults(cfg)

if err := api.ValidateClusterConfig(cfg); err != nil {
return err
}

ctl := eks.New(rc.ProviderConfig, cfg)

if !ctl.IsSupportedRegion() {
return cmdutils.ErrUnsupportedRegion(rc.ProviderConfig)
}
logger.Info("using region %s", cfg.Metadata.Region)

if err := ctl.CheckAuth(); err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/ctl/delete/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ func doDeleteCluster(rc *cmdutils.ResourceCmd) error {
cfg := rc.ClusterConfig
meta := rc.ClusterConfig.Metadata

api.SetClusterConfigDefaults(cfg)

if err := api.ValidateClusterConfig(cfg); err != nil {
return err
}

printer := printers.NewJSONPrinter()
ctl := eks.New(rc.ProviderConfig, cfg)

Expand Down
11 changes: 11 additions & 0 deletions pkg/ctl/delete/iamidentitymapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,19 @@ func doDeleteIAMIdentityMapping(rc *cmdutils.ResourceCmd, role string, all bool)

cfg := rc.ClusterConfig

api.SetClusterConfigDefaults(cfg)

if err := api.ValidateClusterConfig(cfg); err != nil {
return err
}

ctl := eks.New(rc.ProviderConfig, cfg)

if !ctl.IsSupportedRegion() {
return cmdutils.ErrUnsupportedRegion(rc.ProviderConfig)
}
logger.Info("using region %s", cfg.Metadata.Region)

if err := ctl.CheckAuth(); err != nil {
return err
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/ctl/delete/nodegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,19 @@ func doDeleteNodeGroup(rc *cmdutils.ResourceCmd, ng *api.NodeGroup, updateAuthCo

cfg := rc.ClusterConfig

api.SetClusterConfigDefaults(cfg)

if err := api.ValidateClusterConfig(cfg); err != nil {
return err
}

ctl := eks.New(rc.ProviderConfig, cfg)

if !ctl.IsSupportedRegion() {
return cmdutils.ErrUnsupportedRegion(rc.ProviderConfig)
}
logger.Info("using region %s", cfg.Metadata.Region)

if err := ctl.CheckAuth(); err != nil {
return err
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/ctl/drain/nodegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,19 @@ func doDrainNodeGroup(rc *cmdutils.ResourceCmd, ng *api.NodeGroup, undo, onlyMis

cfg := rc.ClusterConfig

api.SetClusterConfigDefaults(cfg)

if err := api.ValidateClusterConfig(cfg); err != nil {
return err
}

ctl := eks.New(rc.ProviderConfig, cfg)

if !ctl.IsSupportedRegion() {
return cmdutils.ErrUnsupportedRegion(rc.ProviderConfig)
}
logger.Info("using region %s", cfg.Metadata.Region)

if err := ctl.CheckAuth(); err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/ctl/get/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ func doGetCluster(rc *cmdutils.ResourceCmd, params *getCmdParams, listAllRegions
cfg := rc.ClusterConfig
regionGiven := cfg.Metadata.Region != "" // eks.New resets this field, so we need to check if it was set in the fist place

api.SetClusterConfigDefaults(cfg)

if err := api.ValidateClusterConfig(cfg); err != nil {
return err
}

ctl := eks.New(rc.ProviderConfig, cfg)

if !ctl.IsSupportedRegion() {
Expand Down
10 changes: 10 additions & 0 deletions pkg/ctl/get/iamidentitymapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,18 @@ func doGetIAMIdentityMapping(rc *cmdutils.ResourceCmd, params *getCmdParams, rol

cfg := rc.ClusterConfig

api.SetClusterConfigDefaults(cfg)

if err := api.ValidateClusterConfig(cfg); err != nil {
return err
}

ctl := eks.New(rc.ProviderConfig, cfg)

if !ctl.IsSupportedRegion() {
return cmdutils.ErrUnsupportedRegion(rc.ProviderConfig)
}

if err := ctl.CheckAuth(); err != nil {
return err
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/ctl/get/nodegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,19 @@ func getNodeGroupCmd(rc *cmdutils.ResourceCmd) {

func doGetNodeGroup(rc *cmdutils.ResourceCmd, ng *api.NodeGroup, params *getCmdParams) error {
cfg := rc.ClusterConfig

api.SetClusterConfigDefaults(cfg)

if err := api.ValidateClusterConfig(cfg); err != nil {
return err
}

ctl := eks.New(rc.ProviderConfig, cfg)

if !ctl.IsSupportedRegion() {
return cmdutils.ErrUnsupportedRegion(rc.ProviderConfig)
}

if err := ctl.CheckAuth(); err != nil {
return err
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/ctl/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,21 @@ func newFluxInstaller(ctx context.Context, rc *cmdutils.ResourceCmd, opts *insta
if err := cmdutils.NewMetadataLoader(rc).Load(); err != nil {
return nil, err
}

cfg := rc.ClusterConfig

api.SetClusterConfigDefaults(cfg)

if err := api.ValidateClusterConfig(cfg); err != nil {
return nil, err
}

ctl := eks.New(rc.ProviderConfig, cfg)

if !ctl.IsSupportedRegion() {
return nil, cmdutils.ErrUnsupportedRegion(rc.ProviderConfig)
}

if err := ctl.RefreshClusterConfig(cfg); err != nil {
return nil, err
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/ctl/scale/nodegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/pflag"

api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils"
"github.com/weaveworks/eksctl/pkg/eks"
Expand Down Expand Up @@ -44,6 +45,10 @@ func doScaleNodeGroup(rc *cmdutils.ResourceCmd, ng *api.NodeGroup) error {

ctl := eks.New(rc.ProviderConfig, cfg)

if !ctl.IsSupportedRegion() {
return cmdutils.ErrUnsupportedRegion(rc.ProviderConfig)
}

if err := ctl.CheckAuth(); err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/ctl/update/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func doUpdateClusterCmd(rc *cmdutils.ResourceCmd) error {

api.SetClusterConfigDefaults(cfg)

if err := api.ValidateClusterConfig(cfg); err != nil {
return err
}

printer := printers.NewJSONPrinter()
ctl := eks.New(rc.ProviderConfig, cfg)

Expand Down
11 changes: 11 additions & 0 deletions pkg/ctl/utils/describe_stacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,19 @@ func describeStacksCmd(rc *cmdutils.ResourceCmd) {
func doDescribeStacksCmd(rc *cmdutils.ResourceCmd, all, events, trail bool) error {
cfg := rc.ClusterConfig

api.SetClusterConfigDefaults(cfg)

if err := api.ValidateClusterConfig(cfg); err != nil {
return err
}

ctl := eks.New(rc.ProviderConfig, cfg)

if !ctl.IsSupportedRegion() {
return cmdutils.ErrUnsupportedRegion(rc.ProviderConfig)
}
logger.Info("using region %s", cfg.Metadata.Region)

if err := ctl.CheckAuth(); err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/ctl/utils/update_aws_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ func doUpdateAWSNode(rc *cmdutils.ResourceCmd) error {
cfg := rc.ClusterConfig
meta := rc.ClusterConfig.Metadata

api.SetClusterConfigDefaults(cfg)

if err := api.ValidateClusterConfig(cfg); err != nil {
return err
}

ctl := eks.New(rc.ProviderConfig, cfg)

if !ctl.IsSupportedRegion() {
Expand Down
4 changes: 4 additions & 0 deletions pkg/ctl/utils/update_cluster_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ func doEnableLogging(rc *cmdutils.ResourceCmd, logTypesToEnable []string, logTyp

api.SetClusterConfigDefaults(cfg)

if err := api.ValidateClusterConfig(cfg); err != nil {
return err
}

printer := printers.NewJSONPrinter()
ctl := eks.New(rc.ProviderConfig, cfg)

Expand Down
6 changes: 6 additions & 0 deletions pkg/ctl/utils/update_coredns.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ func doUpdateCoreDNS(rc *cmdutils.ResourceCmd) error {
cfg := rc.ClusterConfig
meta := rc.ClusterConfig.Metadata

api.SetClusterConfigDefaults(cfg)

if err := api.ValidateClusterConfig(cfg); err != nil {
return err
}

ctl := eks.New(rc.ProviderConfig, cfg)

if !ctl.IsSupportedRegion() {
Expand Down
6 changes: 6 additions & 0 deletions pkg/ctl/utils/update_kube_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ func doUpdateKubeProxy(rc *cmdutils.ResourceCmd) error {
cfg := rc.ClusterConfig
meta := rc.ClusterConfig.Metadata

api.SetClusterConfigDefaults(cfg)

if err := api.ValidateClusterConfig(cfg); err != nil {
return err
}

ctl := eks.New(rc.ProviderConfig, cfg)

if !ctl.IsSupportedRegion() {
Expand Down
1 change: 1 addition & 0 deletions pkg/ctl/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func Command(flagGrouping *cmdutils.FlagGrouping) *cobra.Command {
cmdutils.AddResourceCmd(flagGrouping, verbCmd, updateAWSNodeCmd)
cmdutils.AddResourceCmd(flagGrouping, verbCmd, updateCoreDNSCmd)
cmdutils.AddResourceCmd(flagGrouping, verbCmd, enableLoggingCmd)
cmdutils.AddResourceCmd(flagGrouping, verbCmd, associateIAMOIDCProviderCmd)

return verbCmd
}
6 changes: 6 additions & 0 deletions pkg/ctl/utils/wait_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ func waitNodesCmd(rc *cmdutils.ResourceCmd) {
func doWaitNodes(rc *cmdutils.ResourceCmd, ng *api.NodeGroup, kubeconfigPath string) error {
cfg := rc.ClusterConfig

api.SetClusterConfigDefaults(cfg)

if err := api.ValidateClusterConfig(cfg); err != nil {
return err
}

ctl := eks.New(rc.ProviderConfig, cfg)

if kubeconfigPath == "" {
Expand Down
11 changes: 11 additions & 0 deletions pkg/ctl/utils/write_kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,19 @@ func writeKubeconfigCmd(rc *cmdutils.ResourceCmd) {
func doWriteKubeconfigCmd(rc *cmdutils.ResourceCmd, outputPath, roleARN string, setContext, autoPath bool) error {
cfg := rc.ClusterConfig

api.SetClusterConfigDefaults(cfg)

if err := api.ValidateClusterConfig(cfg); err != nil {
return err
}

ctl := eks.New(rc.ProviderConfig, cfg)

if !ctl.IsSupportedRegion() {
return cmdutils.ErrUnsupportedRegion(rc.ProviderConfig)
}
logger.Info("using region %s", cfg.Metadata.Region)

if err := ctl.CheckAuth(); err != nil {
return err
}
Expand Down

0 comments on commit 3c85303

Please sign in to comment.