mirror of https://github.com/kubernetes/kops.git
Merge pull request #2240 from MrTrustor/delete-f
Implement --filename option for kops delete
This commit is contained in:
commit
bd564cf7fd
|
@ -17,25 +17,48 @@ limitations under the License.
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"bytes"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"github.com/spf13/cobra"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/kops/cmd/kops/util"
|
||||
kopsapi "k8s.io/kops/pkg/apis/kops"
|
||||
"k8s.io/kops/pkg/apis/kops/v1alpha1"
|
||||
"k8s.io/kops/util/pkg/vfs"
|
||||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
||||
"k8s.io/kubernetes/pkg/kubectl/resource"
|
||||
)
|
||||
|
||||
type DeleteOptions struct {
|
||||
resource.FilenameOptions
|
||||
Yes bool
|
||||
}
|
||||
|
||||
func NewCmdDelete(f *util.Factory, out io.Writer) *cobra.Command {
|
||||
//options := &DeleteOptions{}
|
||||
options := &DeleteOptions{}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "delete",
|
||||
Short: "Delete clusters and other resources.",
|
||||
Long: `Delete clusters`,
|
||||
Use: "delete -f FILENAME [--yes]",
|
||||
Short: "Delete clusters and instancegroups",
|
||||
Long: `Delete clusters and instancegroups`,
|
||||
SuggestFor: []string{"rm"},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if cmdutil.IsFilenameEmpty(options.Filenames) {
|
||||
cmd.Help()
|
||||
return
|
||||
}
|
||||
cmdutil.CheckErr(RunDelete(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.MarkFlagRequired("filename")
|
||||
|
||||
// create subcommands
|
||||
cmd.AddCommand(NewCmdDeleteCluster(f, out))
|
||||
cmd.AddCommand(NewCmdDeleteInstanceGroup(f, out))
|
||||
|
@ -43,3 +66,68 @@ func NewCmdDelete(f *util.Factory, out io.Writer) *cobra.Command {
|
|||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func RunDelete(factory *util.Factory, out io.Writer, d *DeleteOptions) error {
|
||||
// Codecs provides access to encoding and decoding for the scheme
|
||||
codecs := kopsapi.Codecs //serializer.NewCodecFactory(scheme)
|
||||
|
||||
codec := codecs.UniversalDecoder(kopsapi.SchemeGroupVersion)
|
||||
|
||||
var sb bytes.Buffer
|
||||
fmt.Fprintf(&sb, "\n")
|
||||
for _, f := range d.Filenames {
|
||||
contents, err := vfs.Context.ReadFile(f)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading file %q: %v", f, err)
|
||||
}
|
||||
|
||||
sections := bytes.Split(contents, []byte("\n---\n"))
|
||||
for _, section := range sections {
|
||||
defaults := &schema.GroupVersionKind{
|
||||
Group: v1alpha1.SchemeGroupVersion.Group,
|
||||
Version: v1alpha1.SchemeGroupVersion.Version,
|
||||
}
|
||||
o, gvk, err := codec.Decode(section, defaults, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error parsing file %q: %v", f, err)
|
||||
}
|
||||
|
||||
switch v := o.(type) {
|
||||
case *kopsapi.Cluster:
|
||||
options := &DeleteClusterOptions{}
|
||||
options.ClusterName = v.ObjectMeta.Name
|
||||
options.Yes = d.Yes
|
||||
err = RunDeleteCluster(factory, out, options)
|
||||
if err != nil {
|
||||
exitWithError(err)
|
||||
}
|
||||
if d.Yes {
|
||||
fmt.Fprintf(&sb, "Deleted cluster/%s\n", v.ObjectMeta.Name)
|
||||
}
|
||||
case *kopsapi.InstanceGroup:
|
||||
options := &DeleteInstanceGroupOptions{}
|
||||
options.GroupName = v.ObjectMeta.Name
|
||||
options.ClusterName = v.ObjectMeta.Labels[kopsapi.LabelClusterName]
|
||||
options.Yes = d.Yes
|
||||
err := RunDeleteInstanceGroup(factory, out, options)
|
||||
if err != nil {
|
||||
exitWithError(err)
|
||||
}
|
||||
if d.Yes {
|
||||
fmt.Fprintf(&sb, "Deleted instancegroup/%s\n", v.ObjectMeta.Name)
|
||||
}
|
||||
default:
|
||||
glog.V(2).Infof("Type of object was %T", v)
|
||||
return fmt.Errorf("Unhandled kind %q in %s", gvk, f)
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
_, err := out.Write(sb.Bytes())
|
||||
if err != nil {
|
||||
return fmt.Errorf("error writing to output: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -1,11 +1,21 @@
|
|||
## kops delete
|
||||
|
||||
delete clusters
|
||||
Deletes a resource by filename or stdin
|
||||
|
||||
### Synopsis
|
||||
|
||||
Delete clusters or instancegroups by filename or stdin
|
||||
|
||||
Delete clusters
|
||||
```
|
||||
kops delete -f FILENAME [--yes]
|
||||
```
|
||||
|
||||
### Options
|
||||
|
||||
```
|
||||
-f, --filename stringSlice Filename to use to delete the resource
|
||||
-y, --yes Specify --yes to delete the resource
|
||||
```
|
||||
|
||||
### Options inherited from parent commands
|
||||
|
||||
|
@ -27,4 +37,3 @@ Delete clusters
|
|||
* [kops delete cluster](kops_delete_cluster.md) - Delete cluster
|
||||
* [kops delete instancegroup](kops_delete_instancegroup.md) - Delete instancegroup
|
||||
* [kops delete secret](kops_delete_secret.md) - Delete secret
|
||||
|
||||
|
|
Loading…
Reference in New Issue