Skip to content

Commit

Permalink
Implement completion for "kops delete instancegroup"
Browse files Browse the repository at this point in the history
  • Loading branch information
johngmyers committed Jul 10, 2021
1 parent 56b57b5 commit ea8cd3b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 42 deletions.
89 changes: 55 additions & 34 deletions cmd/kops/delete_instancegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ package main
import (
"context"
"fmt"
"strings"

"io"
"os"

"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kops/cmd/kops/util"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/instancegroups"
"k8s.io/kops/upup/pkg/fi/cloudup"
"k8s.io/kops/util/pkg/ui"
Expand All @@ -34,20 +36,20 @@ import (
)

var (
deleteIgLong = templates.LongDesc(i18n.T(`
Delete an instancegroup configuration. kOps has the concept of "instance groups",
deleteInstanceGroupLong = templates.LongDesc(i18n.T(`
Delete an instance group configuration. kOps has the concept of "instance groups",
which are a group of similar virtual machines. On AWS, they map to an
AutoScalingGroup. An ig work either as a Kubernetes master or a node.`))
AutoScalingGroup.`))

deleteIgExample = templates.Examples(i18n.T(`
deleteInstanceGroupExample = templates.Examples(i18n.T(`
# Delete an instancegroup for the k8s-cluster.example.com cluster.
# The --yes option runs the command immediately.
# Note that the cloud resources will be deleted immediately, without running "kops update cluster"
kops delete ig --name=k8s-cluster.example.com node-example --yes
`))

deleteIgShort = i18n.T(`Delete instancegroup`)
deleteInstanceGroupShort = i18n.T(`Delete instance group.`)
)

type DeleteInstanceGroupOptions struct {
Expand All @@ -60,28 +62,36 @@ func NewCmdDeleteInstanceGroup(f *util.Factory, out io.Writer) *cobra.Command {
options := &DeleteInstanceGroupOptions{}

cmd := &cobra.Command{
Use: "instancegroup",
Use: "instancegroup INSTANCE_GROUP",
Aliases: []string{"instancegroups", "ig"},
Short: deleteIgShort,
Long: deleteIgLong,
Example: deleteIgExample,
Run: func(cmd *cobra.Command, args []string) {
ctx := context.TODO()
Short: deleteInstanceGroupShort,
Long: deleteInstanceGroupLong,
Example: deleteInstanceGroupExample,
Args: func(cmd *cobra.Command, args []string) error {
options.ClusterName = rootCommand.ClusterName(true)

if options.ClusterName == "" {
return fmt.Errorf("--name is required")
}

if len(args) == 0 {
exitWithError(fmt.Errorf("Specify name of instance group to delete"))
return fmt.Errorf("must specify the name of instance group to delete")
}

options.GroupName = args[0]

if len(args) != 1 {
exitWithError(fmt.Errorf("Can only edit one instance group at a time!"))
return fmt.Errorf("can only edit one instance group at a time")
}

groupName := args[0]
options.GroupName = groupName

options.ClusterName = rootCommand.ClusterName(true)
return nil
},
ValidArgsFunction: completeInstanceGroup(nil, &[]string{strings.ToLower(string(kops.InstanceGroupRoleMaster))}),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.TODO()

if !options.Yes {
message := fmt.Sprintf("Do you really want to delete instance group %q? This action cannot be undone.", groupName)
message := fmt.Sprintf("Do you really want to delete instance group %q? This action cannot be undone.", options.GroupName)

c := &ui.ConfirmArgs{
Out: out,
Expand All @@ -92,7 +102,7 @@ func NewCmdDeleteInstanceGroup(f *util.Factory, out io.Writer) *cobra.Command {

confirmed, err := ui.GetConfirm(c)
if err != nil {
exitWithError(err)
return err
}
if !confirmed {
os.Exit(1)
Expand All @@ -101,10 +111,7 @@ func NewCmdDeleteInstanceGroup(f *util.Factory, out io.Writer) *cobra.Command {
}
}

err := RunDeleteInstanceGroup(ctx, f, out, options)
if err != nil {
exitWithError(err)
}
return RunDeleteInstanceGroup(ctx, f, out, options)
},
}

Expand All @@ -123,12 +130,7 @@ func RunDeleteInstanceGroup(ctx context.Context, f *util.Factory, out io.Writer,
return fmt.Errorf("GroupName is required")
}

clusterName := options.ClusterName
if clusterName == "" {
return fmt.Errorf("ClusterName is required")
}

cluster, err := GetCluster(ctx, f, clusterName)
cluster, err := GetCluster(ctx, f, options.ClusterName)
if err != nil {
return err
}
Expand All @@ -146,18 +148,37 @@ func RunDeleteInstanceGroup(ctx context.Context, f *util.Factory, out io.Writer,
return fmt.Errorf("InstanceGroup %q not found", groupName)
}

cloud, err := cloudup.BuildCloud(cluster)
if err != nil {
return err
}

fmt.Fprintf(out, "InstanceGroup %q found for deletion\n", groupName)

if group.Spec.Role == kops.InstanceGroupRoleMaster {
groups, err := clientset.InstanceGroupsFor(cluster).List(ctx, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("listing InstanceGroups: %v", err)
}

onlyMaster := true
for _, ig := range groups.Items {
if ig.Name != groupName && ig.Spec.Role == kops.InstanceGroupRoleMaster {
onlyMaster = false
break
}
}

if onlyMaster {
return fmt.Errorf("cannot delete the only control plane instance group")
}
}

if !options.Yes {
fmt.Fprintf(out, "\nMust specify --yes to delete instancegroup\n")
return nil
}

cloud, err := cloudup.BuildCloud(cluster)
if err != nil {
return err
}

d := &instancegroups.DeleteInstanceGroup{}
d.Cluster = cluster
d.Cloud = cloud
Expand Down
14 changes: 10 additions & 4 deletions cmd/kops/rollingupdate_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func NewCmdRollingUpdateCluster(f *util.Factory, out io.Writer) *cobra.Command {
cmd.Flags().DurationVar(&options.PostDrainDelay, "post-drain-delay", options.PostDrainDelay, "Time to wait after draining each node")
cmd.Flags().BoolVarP(&options.Interactive, "interactive", "i", options.Interactive, "Prompt to continue after each instance is updated")
cmd.Flags().StringSliceVar(&options.InstanceGroups, "instance-group", options.InstanceGroups, "Instance groups to update (defaults to all if not specified)")
cmd.RegisterFlagCompletionFunc("instance-group", completeInstanceGroup(&options))
cmd.RegisterFlagCompletionFunc("instance-group", completeInstanceGroup(&options.InstanceGroups, &options.InstanceGroupRoles))
cmd.Flags().StringSliceVar(&options.InstanceGroupRoles, "instance-group-roles", options.InstanceGroupRoles, "Instance group roles to update ("+strings.Join(allRoles, ",")+")")
cmd.RegisterFlagCompletionFunc("instance-group-roles", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return sets.NewString(allRoles...).Delete(options.InstanceGroupRoles...).List(), cobra.ShellCompDirectiveNoFileComp
Expand Down Expand Up @@ -429,7 +429,7 @@ func RunRollingUpdateCluster(ctx context.Context, f *util.Factory, out io.Writer
return d.RollingUpdate(groups, list)
}

func completeInstanceGroup(options *RollingUpdateOptions) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
func completeInstanceGroup(selectedInstanceGroups *[]string, selectedInstanceGroupRoles *[]string) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
commandutils.ConfigureKlogForCompletion()
ctx := context.TODO()
Expand All @@ -444,8 +444,14 @@ func completeInstanceGroup(options *RollingUpdateOptions) func(cmd *cobra.Comman
return commandutils.CompletionError("listing instance groups", err)
}

alreadySelected := sets.NewString(options.InstanceGroups...)
alreadySelectedRoles := sets.NewString(options.InstanceGroupRoles...)
alreadySelected := sets.NewString()
if selectedInstanceGroups != nil {
alreadySelected = alreadySelected.Insert(*selectedInstanceGroups...)
}
alreadySelectedRoles := sets.NewString()
if selectedInstanceGroupRoles != nil {
alreadySelectedRoles = alreadySelectedRoles.Insert(*selectedInstanceGroupRoles...)
}
var igs []string
for _, ig := range list.Items {
if !alreadySelected.Has(ig.Name) && !alreadySelectedRoles.Has(strings.ToLower(string(ig.Spec.Role))) {
Expand Down
2 changes: 1 addition & 1 deletion docs/cli/kops_delete.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions docs/cli/kops_delete_instancegroup.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ea8cd3b

Please sign in to comment.