2014-10-06 01:24:19 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2014-10-27 02:21:31 +00:00
|
|
|
"fmt"
|
2014-10-06 01:24:19 +00:00
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2014-12-15 17:32:46 +00:00
|
|
|
|
2015-01-03 23:02:39 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
2015-02-05 00:14:48 +00:00
|
|
|
cmdutil "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/util"
|
2014-12-28 04:49:51 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/resource"
|
2015-01-03 23:02:39 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
2014-10-06 01:24:19 +00:00
|
|
|
)
|
|
|
|
|
2014-10-27 02:21:31 +00:00
|
|
|
func (f *Factory) NewCmdDelete(out io.Writer) *cobra.Command {
|
2015-01-03 23:02:39 +00:00
|
|
|
flags := &struct {
|
|
|
|
Filenames util.StringList
|
|
|
|
}{}
|
2014-10-06 01:24:19 +00:00
|
|
|
cmd := &cobra.Command{
|
2014-12-15 17:32:46 +00:00
|
|
|
Use: "delete ([-f filename] | (<resource> [(<id> | -l <label>)]",
|
2014-10-06 01:24:19 +00:00
|
|
|
Short: "Delete a resource by filename, stdin or resource and id",
|
2014-12-15 17:32:46 +00:00
|
|
|
Long: `Delete a resource by filename, stdin, resource and id or by resources and label selector.
|
2014-10-06 01:24:19 +00:00
|
|
|
|
|
|
|
JSON and YAML formats are accepted.
|
|
|
|
|
|
|
|
If both a filename and command line arguments are passed, the command line
|
|
|
|
arguments are used and the filename is ignored.
|
|
|
|
|
|
|
|
Note that the delete command does NOT do resource version checks, so if someone
|
|
|
|
submits an update to a resource right when you submit a delete, their update
|
|
|
|
will be lost along with the rest of the resource.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
$ kubectl delete -f pod.json
|
|
|
|
<delete a pod using the type and id pod.json>
|
|
|
|
|
|
|
|
$ cat pod.json | kubectl delete -f -
|
|
|
|
<delete a pod based on the type and id in the json passed into stdin>
|
|
|
|
|
2014-12-15 17:32:46 +00:00
|
|
|
$ kubectl delete pods,services -l name=myLabel
|
|
|
|
<delete pods and services with label name=myLabel>
|
|
|
|
|
2014-10-06 01:24:19 +00:00
|
|
|
$ kubectl delete pod 1234-56-7890-234234-456456
|
|
|
|
<delete a pod with ID 1234-56-7890-234234-456456>`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2015-01-02 18:08:37 +00:00
|
|
|
cmdNamespace, err := f.DefaultNamespace(cmd)
|
|
|
|
checkErr(err)
|
|
|
|
|
2014-12-31 23:35:52 +00:00
|
|
|
mapper, typer := f.Object(cmd)
|
2015-02-05 00:14:48 +00:00
|
|
|
r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand(cmd)).
|
2015-01-03 23:02:39 +00:00
|
|
|
ContinueOnError().
|
2015-01-02 18:08:37 +00:00
|
|
|
NamespaceParam(cmdNamespace).DefaultNamespace().
|
2015-01-03 23:02:39 +00:00
|
|
|
FilenameParam(flags.Filenames...).
|
2015-02-05 00:14:48 +00:00
|
|
|
SelectorParam(cmdutil.GetFlagString(cmd, "selector")).
|
2015-01-03 23:02:39 +00:00
|
|
|
ResourceTypeOrNameArgs(args...).
|
|
|
|
Flatten().
|
|
|
|
Do()
|
2015-02-10 14:32:26 +00:00
|
|
|
checkErr(r.Err())
|
2015-01-03 23:02:39 +00:00
|
|
|
|
|
|
|
found := 0
|
2015-01-02 18:08:37 +00:00
|
|
|
err = r.IgnoreErrors(errors.IsNotFound).Visit(func(r *resource.Info) error {
|
2014-12-15 17:32:46 +00:00
|
|
|
found++
|
2014-12-28 04:49:51 +00:00
|
|
|
if err := resource.NewHelper(r.Client, r.Mapping).Delete(r.Namespace, r.Name); err != nil {
|
2014-12-15 17:32:46 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Fprintf(out, "%s\n", r.Name)
|
|
|
|
return nil
|
|
|
|
})
|
2015-01-14 18:52:03 +00:00
|
|
|
checkErr(err)
|
2014-12-15 17:32:46 +00:00
|
|
|
if found == 0 {
|
2015-01-14 18:52:03 +00:00
|
|
|
fmt.Fprintf(cmd.Out(), "No resources found\n")
|
2014-12-15 17:32:46 +00:00
|
|
|
}
|
2014-10-06 01:24:19 +00:00
|
|
|
},
|
|
|
|
}
|
2015-01-03 23:02:39 +00:00
|
|
|
cmd.Flags().VarP(&flags.Filenames, "filename", "f", "Filename, directory, or URL to a file containing the resource to delete")
|
2014-12-15 17:32:46 +00:00
|
|
|
cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on")
|
2014-10-06 01:24:19 +00:00
|
|
|
return cmd
|
|
|
|
}
|