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

Add --config-file to eksctl drain ng #741

Merged
merged 2 commits into from
Apr 17, 2019
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
2 changes: 0 additions & 2 deletions pkg/ctl/create/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ var (
setContext bool
availabilityZones []string

clusterConfigFile = ""

kopsClusterNameForVPC string
subnets map[api.SubnetTopology]*[]string
addonsStorageClass bool
Expand Down
4 changes: 4 additions & 0 deletions pkg/ctl/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils"
)

var (
clusterConfigFile = ""
)

// Command will create the `create` commands
func Command(g *cmdutils.Grouping) *cobra.Command {
cmd := &cobra.Command{
Expand Down
4 changes: 0 additions & 4 deletions pkg/ctl/delete/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ import (
"github.com/weaveworks/eksctl/pkg/printers"
)

var (
clusterConfigFile = ""
)

func deleteClusterCmd(g *cmdutils.Grouping) *cobra.Command {
p := &api.ProviderConfig{}
cfg := api.NewClusterConfig()
Expand Down
2 changes: 2 additions & 0 deletions pkg/ctl/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
var (
wait = false
plan = true

clusterConfigFile = ""
)

// Command will create the `delete` commands
Expand Down
6 changes: 6 additions & 0 deletions pkg/ctl/drain/drain.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import (
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils"
)

var (
plan = true

clusterConfigFile = ""
)

// Command will create the `drain` commands
func Command(g *cmdutils.Grouping) *cobra.Command {
cmd := &cobra.Command{
Expand Down
70 changes: 47 additions & 23 deletions pkg/ctl/drain/nodegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import (

var (
drainNodeGroupUndo bool

includeNodeGroups []string
excludeNodeGroups []string

drainOnlyMissingNodeGroups bool
)

func drainNodeGroupCmd(g *cmdutils.Grouping) *cobra.Command {
Expand All @@ -28,8 +33,8 @@ func drainNodeGroupCmd(g *cmdutils.Grouping) *cobra.Command {
Use: "nodegroup",
Short: "Cordon and drain a nodegroup",
Aliases: []string{"ng"},
Run: func(_ *cobra.Command, args []string) {
if err := doDrainNodeGroup(p, cfg, ng, cmdutils.GetNameArg(args)); err != nil {
Run: func(cmd *cobra.Command, args []string) {
if err := doDrainNodeGroup(p, cfg, ng, cmdutils.GetNameArg(args), cmd); err != nil {
logger.Critical("%s\n", err.Error())
os.Exit(1)
}
Expand All @@ -41,7 +46,11 @@ func drainNodeGroupCmd(g *cmdutils.Grouping) *cobra.Command {
group.InFlagSet("General", func(fs *pflag.FlagSet) {
fs.StringVar(&cfg.Metadata.Name, "cluster", "", "EKS cluster name")
cmdutils.AddRegionFlag(fs, p)
fs.StringVarP(&ng.Name, "name", "n", "", "Name of the nodegroup to delete (required)")
fs.StringVarP(&ng.Name, "name", "n", "", "Name of the nodegroup to delete")
cmdutils.AddConfigFileFlag(&clusterConfigFile, fs)
cmdutils.AddApproveFlag(&plan, cmd, fs)
cmdutils.AddNodeGroupFilterFlags(&includeNodeGroups, &excludeNodeGroups, fs)
fs.BoolVar(&drainOnlyMissingNodeGroups, "only-missing", false, "Only drain nodegroups that are not defined in the given config file")
fs.BoolVar(&drainNodeGroupUndo, "undo", false, "Uncordone the nodegroup")
})

Expand All @@ -52,33 +61,19 @@ func drainNodeGroupCmd(g *cmdutils.Grouping) *cobra.Command {
return cmd
}

func doDrainNodeGroup(p *api.ProviderConfig, cfg *api.ClusterConfig, ng *api.NodeGroup, nameArg string) error {
ctl := eks.New(p, cfg)
func doDrainNodeGroup(p *api.ProviderConfig, cfg *api.ClusterConfig, ng *api.NodeGroup, nameArg string, cmd *cobra.Command) error {
ngFilter := cmdutils.NewNodeGroupFilter()

if err := api.Register(); err != nil {
if err := cmdutils.NewDeleteNodeGroupLoader(p, cfg, ng, clusterConfigFile, nameArg, cmd, ngFilter, includeNodeGroups, excludeNodeGroups, &plan).Load(); err != nil {
return err
}

ctl := eks.New(p, cfg)

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

if cfg.Metadata.Name == "" {
return cmdutils.ErrMustBeSet("--cluster")
}

if ng.Name != "" && nameArg != "" {
return cmdutils.ErrNameFlagAndArg(ng.Name, nameArg)
}

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

if ng.Name == "" {
return cmdutils.ErrMustBeSet("--name")
}

if err := ctl.GetCredentials(cfg); err != nil {
return errors.Wrapf(err, "getting credentials for cluster %q", cfg.Metadata.Name)
}
Expand All @@ -88,5 +83,34 @@ func doDrainNodeGroup(p *api.ProviderConfig, cfg *api.ClusterConfig, ng *api.Nod
return err
}

return drain.NodeGroup(clientSet, ng, ctl.Provider.WaitTimeout(), drainNodeGroupUndo)
stackManager := ctl.NewStackManager(cfg)

if clusterConfigFile != "" {
logger.Info("comparing %d nodegroups defined in the given config (%q) against remote state", len(cfg.NodeGroups), clusterConfigFile)
if err := ngFilter.SetIncludeOrExcludeMissingFilter(stackManager, drainOnlyMissingNodeGroups, &cfg.NodeGroups); err != nil {
return err
}
}

ngSubset, _ := ngFilter.MatchAll(cfg.NodeGroups)
ngCount := ngSubset.Len()

ngFilter.LogInfo(cfg.NodeGroups)
verb := "drain"
if drainNodeGroupUndo {
verb = "uncordon"
}
cmdutils.LogIntendedAction(plan, "%s %d nodegroups in cluster %q", verb, ngCount, cfg.Metadata.Name)

cmdutils.LogPlanModeWarning(plan && ngCount > 0)

return ngFilter.ForEach(cfg.NodeGroups, func(_ int, ng *api.NodeGroup) error {
if plan {
return nil
}
if err := drain.NodeGroup(clientSet, ng, ctl.Provider.WaitTimeout(), drainNodeGroupUndo); err != nil {
return err
}
return nil
})
}
7 changes: 0 additions & 7 deletions pkg/ctl/update/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ import (
"github.com/weaveworks/eksctl/pkg/printers"
)

var (
plan = true
wait = true

clusterConfigFile string
)

func updateClusterCmd(g *cmdutils.Grouping) *cobra.Command {
p := &api.ProviderConfig{}
cfg := api.NewClusterConfig()
Expand Down
7 changes: 7 additions & 0 deletions pkg/ctl/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import (
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils"
)

var (
plan = true
wait = true

clusterConfigFile string
)

// Command will create the `create` commands
func Command(g *cmdutils.Grouping) *cobra.Command {
cmd := &cobra.Command{
Expand Down