Clean up "kops delete -f"

This commit is contained in:
John Gardiner Myers 2021-07-08 06:57:15 -07:00
parent 5095ae93fd
commit c864dc02ca
7 changed files with 22 additions and 56 deletions

View File

@ -30,7 +30,6 @@ import (
"k8s.io/kops/pkg/sshcredentials"
"k8s.io/kops/util/pkg/text"
"k8s.io/kops/util/pkg/vfs"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)
@ -41,59 +40,40 @@ type DeleteOptions struct {
}
var (
deleteLong = templates.LongDesc(i18n.T(`
Delete Kubernetes clusters, instancegroups, instances, and secrets, or a combination of the before mentioned.
`))
deleteExample = templates.Examples(i18n.T(`
# Delete an instance
kops delete instance i-0a5ed581b862d3425
# Delete a cluster using a manifest file
kops delete -f my-cluster.yaml
# Delete a cluster using a pasted manifest file from stdin.
pbpaste | kops delete -f -
# Delete a cluster in AWS.
kops delete cluster --name=k8s.example.com --state=s3://my-state-store
# Delete an instancegroup for the k8s-cluster.example.com cluster.
# The --yes option runs the command immediately.
kops delete ig --name=k8s-cluster.example.com node-example --yes
`))
deleteShort = i18n.T("Delete clusters, instancegroups, instances, or secrets.")
deleteShort = i18n.T("Delete clusters, instancegroups, instances, and secrets.")
)
func NewCmdDelete(f *util.Factory, out io.Writer) *cobra.Command {
options := &DeleteOptions{}
cmd := &cobra.Command{
Use: "delete -f FILENAME [--yes]",
Use: "delete {-f FILENAME}...",
Short: deleteShort,
Long: deleteLong,
Example: deleteExample,
SuggestFor: []string{"rm"},
Run: func(cmd *cobra.Command, args []string) {
ctx := context.TODO()
if len(options.Filenames) == 0 {
cmd.Help()
return
}
cmdutil.CheckErr(RunDelete(ctx, f, out, options))
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return RunDelete(context.TODO(), f, out, options)
},
}
cmd.Flags().StringSliceVarP(&options.Filenames, "filename", "f", options.Filenames, "Filename to use to delete the resource")
cmd.Flags().BoolVarP(&options.Yes, "yes", "y", options.Yes, "Specify --yes to delete the resource")
cmd.Flags().BoolVarP(&options.Yes, "yes", "y", options.Yes, "Specify --yes to immediately delete the resource")
cmd.MarkFlagRequired("filename")
// create subcommands
cmd.AddCommand(NewCmdDeleteCluster(f, out))
cmd.AddCommand(NewCmdDeleteInstance(f, out))
cmd.AddCommand(NewCmdDeleteInstanceGroup(f, out))
cmd.AddCommand(NewCmdDeleteSecret(f, out))
cmd.AddCommand(NewCmdDeleteInstance(f, out))
return cmd
}
@ -108,12 +88,12 @@ func RunDelete(ctx context.Context, factory *util.Factory, out io.Writer, d *Del
if f == "-" {
contents, err = ConsumeStdin()
if err != nil {
return fmt.Errorf("error reading from stdin: %v", err)
return fmt.Errorf("reading from stdin: %v", err)
}
} else {
contents, err = vfs.Context.ReadFile(f)
if err != nil {
return fmt.Errorf("error reading file %q: %v", f, err)
return fmt.Errorf("reading file %q: %v", f, err)
}
}
@ -121,7 +101,7 @@ func RunDelete(ctx context.Context, factory *util.Factory, out io.Writer, d *Del
for _, section := range sections {
o, gvk, err := kopscodecs.Decode(section, nil)
if err != nil {
return fmt.Errorf("error parsing file %q: %v", f, err)
return fmt.Errorf("parsing file %q: %v", f, err)
}
switch v := o.(type) {
@ -132,7 +112,7 @@ func RunDelete(ctx context.Context, factory *util.Factory, out io.Writer, d *Del
}
err = RunDeleteCluster(ctx, factory, out, options)
if err != nil {
exitWithError(err)
return err
}
deletedClusters.Insert(v.ObjectMeta.Name)
case *kopsapi.InstanceGroup:
@ -150,7 +130,7 @@ func RunDelete(ctx context.Context, factory *util.Factory, out io.Writer, d *Del
err := RunDeleteInstanceGroup(ctx, factory, out, options)
if err != nil {
exitWithError(err)
return err
}
case *kopsapi.SSHCredential:
fingerprint, err := sshcredentials.Fingerprint(v.Spec.PublicKey)
@ -167,11 +147,11 @@ func RunDelete(ctx context.Context, factory *util.Factory, out io.Writer, d *Del
err = RunDeleteSecret(ctx, factory, out, options)
if err != nil {
exitWithError(err)
return err
}
default:
klog.V(2).Infof("Type of object was %T", v)
return fmt.Errorf("Unhandled kind %q in %s", gvk, f)
return fmt.Errorf("unhandled kind %q in %s", gvk, f)
}
}
}

2
docs/cli/kops.md generated
View File

@ -39,7 +39,7 @@ kOps is Kubernetes Operations.
* [kops completion](kops_completion.md) - generate the autocompletion script for the specified shell
* [kops create](kops_create.md) - Create a resource by command line, filename or stdin.
* [kops delete](kops_delete.md) - Delete clusters, instancegroups, instances, or secrets.
* [kops delete](kops_delete.md) - Delete clusters, instancegroups, instances, and secrets.
* [kops describe](kops_describe.md) - Describe a resource.
* [kops distrust](kops_distrust.md) - Distrust keypairs.
* [kops edit](kops_edit.md) - Edit clusters and other resources.

View File

@ -3,34 +3,20 @@
## kops delete
Delete clusters, instancegroups, instances, or secrets.
### Synopsis
Delete Kubernetes clusters, instancegroups, instances, and secrets, or a combination of the before mentioned.
Delete clusters, instancegroups, instances, and secrets.
```
kops delete -f FILENAME [--yes] [flags]
kops delete {-f FILENAME}... [flags]
```
### Examples
```
# Delete an instance
kops delete instance i-0a5ed581b862d3425
# Delete a cluster using a manifest file
kops delete -f my-cluster.yaml
# Delete a cluster using a pasted manifest file from stdin.
pbpaste | kops delete -f -
# Delete a cluster in AWS.
kops delete cluster --name=k8s.example.com --state=s3://my-state-store
# Delete an instancegroup for the k8s-cluster.example.com cluster.
# The --yes option runs the command immediately.
kops delete ig --name=k8s-cluster.example.com node-example --yes
```
### Options
@ -38,7 +24,7 @@ kops delete -f FILENAME [--yes] [flags]
```
-f, --filename strings Filename to use to delete the resource
-h, --help help for delete
-y, --yes Specify --yes to delete the resource
-y, --yes Specify --yes to immediately delete the resource
```
### Options inherited from parent commands

View File

@ -54,5 +54,5 @@ kops delete cluster CLUSTERNAME [--yes] [flags]
### SEE ALSO
* [kops delete](kops_delete.md) - Delete clusters, instancegroups, instances, or secrets.
* [kops delete](kops_delete.md) - Delete clusters, instancegroups, instances, and secrets.

View File

@ -64,5 +64,5 @@ kops delete instance [flags]
### SEE ALSO
* [kops delete](kops_delete.md) - Delete clusters, instancegroups, instances, or secrets.
* [kops delete](kops_delete.md) - Delete clusters, instancegroups, instances, and secrets.

View File

@ -52,5 +52,5 @@ kops delete instancegroup [flags]
### SEE ALSO
* [kops delete](kops_delete.md) - Delete clusters, instancegroups, instances, or secrets.
* [kops delete](kops_delete.md) - Delete clusters, instancegroups, instances, and secrets.

View File

@ -50,5 +50,5 @@ kops delete secret [flags]
### SEE ALSO
* [kops delete](kops_delete.md) - Delete clusters, instancegroups, instances, or secrets.
* [kops delete](kops_delete.md) - Delete clusters, instancegroups, instances, and secrets.