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
|
|
|
)
|
|
|
|
|
2015-02-20 21:28:43 +00:00
|
|
|
const (
|
|
|
|
delete_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
|
2015-02-20 21:28:43 +00:00
|
|
|
will be lost along with the rest of the resource.`
|
|
|
|
delete_example = `// Delete a pod using the type and ID specified in pod.json.
|
|
|
|
$ kubectl delete -f pod.json
|
2014-10-06 01:24:19 +00:00
|
|
|
|
2015-02-20 21:28:43 +00:00
|
|
|
// Delete a pod based on the type and ID in the JSON passed into stdin.
|
|
|
|
$ cat pod.json | kubectl delete -f -
|
2014-12-15 17:32:46 +00:00
|
|
|
|
2015-02-20 21:28:43 +00:00
|
|
|
// Delete pods and services with label name=myLabel.
|
|
|
|
$ kubectl delete pods,services -l name=myLabel
|
2015-02-03 17:59:21 +00:00
|
|
|
|
2015-02-20 21:28:43 +00:00
|
|
|
// Delete a pod with ID 1234-56-7890-234234-456456.
|
|
|
|
$ kubectl delete pod 1234-56-7890-234234-456456
|
2015-02-12 14:59:58 +00:00
|
|
|
|
2015-02-20 21:28:43 +00:00
|
|
|
// Delete all pods
|
|
|
|
$ kubectl delete pods --all`
|
|
|
|
)
|
2015-02-12 14:59:58 +00:00
|
|
|
|
2015-02-20 21:28:43 +00:00
|
|
|
func (f *Factory) NewCmdDelete(out io.Writer) *cobra.Command {
|
|
|
|
flags := &struct {
|
|
|
|
Filenames util.StringList
|
|
|
|
}{}
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "delete ([-f filename] | (<resource> [(<id> | -l <label> | --all)]",
|
|
|
|
Short: "Delete a resource by filename, stdin, or resource and ID.",
|
|
|
|
Long: delete_long,
|
|
|
|
Example: delete_example,
|
2014-10-06 01:24:19 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2015-01-02 18:08:37 +00:00
|
|
|
cmdNamespace, err := f.DefaultNamespace(cmd)
|
2015-03-04 00:24:29 +00:00
|
|
|
cmdutil.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-02-12 14:59:58 +00:00
|
|
|
SelectAllParam(cmdutil.GetFlagBool(cmd, "all")).
|
|
|
|
ResourceTypeOrNameArgs(false, args...).
|
2015-01-03 23:02:39 +00:00
|
|
|
Flatten().
|
|
|
|
Do()
|
2015-03-04 00:24:29 +00:00
|
|
|
cmdutil.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-03-04 00:24:29 +00:00
|
|
|
cmdutil.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")
|
2015-02-12 14:59:58 +00:00
|
|
|
cmd.Flags().Bool("all", false, "[-all] to select all the specified resources")
|
2014-10-06 01:24:19 +00:00
|
|
|
return cmd
|
|
|
|
}
|