Skip to content

Commit

Permalink
Add cmdutils.LoadMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
errordeveloper committed Mar 5, 2019
1 parent 926ea1a commit 30e685f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 44 deletions.
71 changes: 71 additions & 0 deletions pkg/ctl/cmdutils/configfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cmdutils

import (
"fmt"

"github.com/spf13/cobra"

api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha4"
"github.com/weaveworks/eksctl/pkg/eks"
)

// LoadMetadata handles loading of clusterConfigFile vs using flags for all commands that require only
// metadata fileds, e.g. `eksctl delete cluster` or `eksctl utils update-kube-proxy` and other similar
// commands that do simple operations against existing clusters
func LoadMetadata(p *api.ProviderConfig, cfg *api.ClusterConfig, clusterConfigFile, nameArg string, cmd *cobra.Command) error {
meta := cfg.Metadata

if clusterConfigFile != "" {
if err := eks.LoadConfigFromFile(clusterConfigFile, cfg); err != nil {
return err
}
meta = cfg.Metadata

incompatibleFlags := []string{
"name",
"region",
"version",
}

for _, f := range incompatibleFlags {
if flag := cmd.Flag(f); flag != nil && flag.Changed {
return fmt.Errorf("cannot use --%s when --config-file/-f is set", f)
}
}

if nameArg != "" {
return fmt.Errorf("cannot use name argument %q when --config-file/-f is set", nameArg)
}

if meta.Name == "" {
return fmt.Errorf("metadata.name must be set")
}

// region is always required when config file is used
if meta.Region == "" {
return fmt.Errorf("metadata.region must be set")
}

p.Region = meta.Region

// version has different default values in some command
// we don't check it here
} else {
if meta.Name != "" && nameArg != "" {
return ErrNameFlagAndArg(meta.Name, nameArg)
}

if nameArg != "" {
meta.Name = nameArg
}

if meta.Name == "" {
return fmt.Errorf("--name must be set")
}

// default region will get picked by eks.New, and
// version validation gets handled separately
}

return nil
}
52 changes: 8 additions & 44 deletions pkg/ctl/delete/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,59 +53,23 @@ func deleteClusterCmd(g *cmdutils.Grouping) *cobra.Command {
}

func doDeleteCluster(p *api.ProviderConfig, cfg *api.ClusterConfig, nameArg string, cmd *cobra.Command) error {
meta := cfg.Metadata

printer := printers.NewJSONPrinter()

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

if clusterConfigFile != "" {
if err := eks.LoadConfigFromFile(clusterConfigFile, cfg); err != nil {
return err
}
meta = cfg.Metadata

incompatibleFlags := []string{
"name",
"region",
}

for _, f := range incompatibleFlags {
if cmd.Flag(f).Changed {
return fmt.Errorf("cannot use --%s when --config-file/-f is set", f)
}
}

if nameArg != "" {
return fmt.Errorf("cannot use name argument %q when --config-file/-f is set", nameArg)
}

if meta.Name == "" {
return fmt.Errorf("metadata.name must be set")
}

if meta.Region == "" {
return fmt.Errorf("metadata.region must be set")
}

p.Region = meta.Region
} else {
if meta.Name != "" && nameArg != "" {
return cmdutils.ErrNameFlagAndArg(meta.Name, nameArg)
}

if nameArg != "" {
meta.Name = nameArg
}

if meta.Name == "" {
return fmt.Errorf("--name must be set")
}
if err := cmdutils.LoadMetadata(p, cfg, clusterConfigFile, nameArg, cmd); err != nil {
return err
}

ctl := eks.New(p, cfg)
meta := cfg.Metadata

if !ctl.IsSupportedRegion() {
return cmdutils.ErrUnsupportedRegion(p)
}
logger.Info("using region %s", meta.Region)

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

0 comments on commit 30e685f

Please sign in to comment.