Skip to content

Commit

Permalink
Merge pull request #1547 from justinsb/refactor_delete
Browse files Browse the repository at this point in the history
Refactor delete commands
  • Loading branch information
justinsb authored Jan 21, 2017
2 parents 930acc3 + 701749a commit a0a59f3
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 204 deletions.
61 changes: 17 additions & 44 deletions cmd/kops/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,28 @@ limitations under the License.
package main

import (
"fmt"
"os"
"strings"

"k8s.io/kops/util/pkg/ui"

"github.com/spf13/cobra"
"io"
"k8s.io/kops/cmd/kops/util"
)

var confirmDelete bool

// deleteCmd represents the delete command
var deleteCmd = &cobra.Command{
Use: "delete",
Short: "delete clusters",
Long: `Delete clusters`,
SuggestFor: []string{"rm"},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// cobra doesn't give you the full arg list even though it should.
args = os.Args

// args should be [delete, resource, resource name]
// if there are less args than 3 confirming isnt necessary as the child command will fail
if !confirmDelete && len(args) >= 3 {
message := fmt.Sprintf(
"Do you really want to %s? This action cannot be undone.",
strings.Join(args[1:], " "),
)
type DeleteOptions struct {
}

c := &ui.ConfirmArgs{
Out: os.Stdout,
Message: message,
Default: "no",
Retries: 2,
}
func NewCmdDelete(f *util.Factory, out io.Writer) *cobra.Command {
//options := &DeleteOptions{}

confirmed, err := ui.GetConfirm(c)
if err != nil {
exitWithError(err)
}
if !confirmed {
os.Exit(1)
}
cmd := &cobra.Command{
Use: "delete",
Short: "delete clusters",
Long: `Delete clusters`,
SuggestFor: []string{"rm"},
}

}
},
}
// create subcommands
cmd.AddCommand(NewCmdDeleteCluster(f, out))
cmd.AddCommand(NewCmdDeleteInstanceGroup(f, out))
cmd.AddCommand(NewCmdDeleteSecret(f, out))

func init() {
deleteCmd.PersistentFlags().BoolVarP(&confirmDelete, "yes", "y", false, "Auto confirm deletetion.")
rootCommand.AddCommand(deleteCmd)
return cmd
}
97 changes: 39 additions & 58 deletions cmd/kops/delete_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ package main

import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"io"
"k8s.io/kops/cmd/kops/util"
api "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/apis/kops/registry"
"k8s.io/kops/pkg/client/simple"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
Expand All @@ -33,66 +32,61 @@ import (
"k8s.io/kops/util/pkg/vfs"
)

type DeleteClusterCmd struct {
Yes bool
Region string
External bool
Unregister bool
type DeleteClusterOptions struct {
Yes bool
Region string
External bool
Unregister bool
ClusterName string
}

var deleteCluster DeleteClusterCmd
func NewCmdDeleteCluster(f *util.Factory, out io.Writer) *cobra.Command {
options := &DeleteClusterOptions{}

func init() {
cmd := &cobra.Command{
Use: "cluster CLUSTERNAME [--yes]",
Short: "Delete cluster",
Long: `Deletes a k8s cluster.`,
Run: func(cmd *cobra.Command, args []string) {
err := deleteCluster.Run(args)
err := rootCommand.ProcessArgs(args)
if err != nil {
exitWithError(err)
}
},
}

deleteCmd.AddCommand(cmd)
// Note _not_ ClusterName(); we only want the --name flag
options.ClusterName = rootCommand.clusterName

// had to do this because this init function is running before the flag is set
for _, arg := range os.Args {
arg = strings.ToLower(arg)
if arg == "-y" || arg == "--yes" {
deleteCluster.Yes = true
break
}
err = RunDeleteCluster(f, out, options)
if err != nil {
exitWithError(err)
}
},
}

cmd.Flags().BoolVar(&deleteCluster.Unregister, "unregister", false, "Don't delete cloud resources, just unregister the cluster")
cmd.Flags().BoolVar(&deleteCluster.External, "external", false, "Delete an external cluster")
cmd.Flags().BoolVarP(&options.Yes, "yes", "y", options.Yes, "Specify --yes to delete the cluster")
cmd.Flags().BoolVar(&options.Unregister, "unregister", options.Unregister, "Don't delete cloud resources, just unregister the cluster")
cmd.Flags().BoolVar(&options.External, "external", options.External, "Delete an external cluster")

cmd.Flags().StringVar(&deleteCluster.Region, "region", "", "region")
cmd.Flags().StringVar(&options.Region, "region", options.Region, "region")
return cmd
}

type getter func(o interface{}) interface{}

func (c *DeleteClusterCmd) Run(args []string) error {
func RunDeleteCluster(f *util.Factory, out io.Writer, options *DeleteClusterOptions) error {
var configBase vfs.Path
var clientset simple.Clientset

err := rootCommand.ProcessArgs(args)
if err != nil {
return err
}

clusterName := rootCommand.clusterName
clusterName := options.ClusterName
if clusterName == "" {
return fmt.Errorf("--name is required (for safety)")
}

var cloud fi.Cloud
var cluster *api.Cluster
var err error

if c.External {
region := c.Region
if options.External {
region := options.Region
if region == "" {
return fmt.Errorf("--region is required (when --external)")
}
Expand All @@ -103,24 +97,11 @@ func (c *DeleteClusterCmd) Run(args []string) error {
return fmt.Errorf("error initializing AWS client: %v", err)
}
} else {
clientset, err = rootCommand.Clientset()
cluster, err = GetCluster(f, clusterName)
if err != nil {
return err
}

cluster, err = clientset.Clusters().Get(clusterName)
if err != nil {
return err
}

if cluster == nil {
return fmt.Errorf("cluster %q not found", clusterName)
}

if clusterName != cluster.ObjectMeta.Name {
return fmt.Errorf("sanity check failed: cluster name mismatch")
}

configBase, err = registry.ConfigBase(cluster)
if err != nil {
return err
Expand All @@ -129,7 +110,7 @@ func (c *DeleteClusterCmd) Run(args []string) error {

wouldDeleteCloudResources := false

if !c.Unregister {
if !options.Unregister {
if cloud == nil {
cloud, err = cloudup.BuildCloud(cluster)
if err != nil {
Expand All @@ -147,7 +128,7 @@ func (c *DeleteClusterCmd) Run(args []string) error {
}

if len(resources) == 0 {
fmt.Printf("No cloud resources to delete\n")
fmt.Fprintf(out, "No cloud resources to delete\n")
} else {
wouldDeleteCloudResources = true

Expand All @@ -166,16 +147,16 @@ func (c *DeleteClusterCmd) Run(args []string) error {
l = append(l, v)
}

err := t.Render(l, os.Stdout, "TYPE", "NAME", "ID")
err := t.Render(l, out, "TYPE", "NAME", "ID")
if err != nil {
return err
}

if !c.Yes {
if !options.Yes {
return fmt.Errorf("Must specify --yes to delete")
}

fmt.Fprintf(os.Stdout, "\n")
fmt.Fprintf(out, "\n")

err = d.DeleteResources(resources)
if err != nil {
Expand All @@ -184,12 +165,12 @@ func (c *DeleteClusterCmd) Run(args []string) error {
}
}

if !c.External {
if !c.Yes {
if !options.External {
if !options.Yes {
if wouldDeleteCloudResources {
fmt.Printf("\nMust specify --yes to delete cloud resources & unregister cluster\n")
fmt.Fprintf(out, "\nMust specify --yes to delete cloud resources & unregister cluster\n")
} else {
fmt.Printf("\nMust specify --yes to unregister the cluster\n")
fmt.Fprintf(out, "\nMust specify --yes to unregister the cluster\n")
}
return nil
}
Expand All @@ -203,6 +184,6 @@ func (c *DeleteClusterCmd) Run(args []string) error {
b.Context = cluster.ObjectMeta.Name
b.DeleteKubeConfig()

fmt.Printf("\nCluster deleted\n")
fmt.Fprintf(out, "\nCluster deleted\n")
return nil
}
17 changes: 0 additions & 17 deletions cmd/kops/delete_confirm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,6 @@ import (
"k8s.io/kops/util/pkg/ui"
)

// TestContainsString tests the ContainsString() function
func TestContainsString(t *testing.T) {
testString := "my test string"
answer := ui.ContainsString(strings.Split(testString, " "), "my")
if !answer {
t.Fatal("Failed to find string using ui.ContainsString()")
}
answer = ui.ContainsString(strings.Split(testString, " "), "string")
if !answer {
t.Fatal("Failed to find string using ui.ContainsString()")
}
answer = ui.ContainsString(strings.Split(testString, " "), "random")
if answer {
t.Fatal("Found string that does not exist using ui.ContainsString()")
}
}

// TestConfirmation attempts to test the majority of the ui.GetConfirm function used in the 'kogs delete' commands
func TestConfirmation(t *testing.T) {
var out bytes.Buffer
Expand Down
Loading

0 comments on commit a0a59f3

Please sign in to comment.