mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 11:21:47 +00:00
kubectl delete command: adding labelSelector
Add a Visitor pattern on top of ResourcesFromArgs Allows ResourcesFromArgs to return an opaque list of items and have client commands react to them. Change request.go to return apiserver errors for arbitrary status codes to better respond to generic actions that don't make sense (kubectl delete operations foo)
This commit is contained in:
parent
da4967fe38
commit
61ac2f7437
@ -411,11 +411,26 @@ func (r *Request) transformResponse(resp *http.Response, req *http.Request) ([]b
|
||||
switch {
|
||||
case resp.StatusCode < http.StatusOK || resp.StatusCode > http.StatusPartialContent:
|
||||
if !isStatusResponse {
|
||||
return nil, false, &UnexpectedStatusError{
|
||||
var err error = &UnexpectedStatusError{
|
||||
Request: req,
|
||||
Response: resp,
|
||||
Body: string(body),
|
||||
}
|
||||
// TODO: handle other error classes we know about
|
||||
switch resp.StatusCode {
|
||||
case http.StatusConflict:
|
||||
if req.Method == "POST" {
|
||||
// TODO: add Resource() and ResourceName() as Request methods so that we can set these
|
||||
err = errors.NewAlreadyExists("", "")
|
||||
} else {
|
||||
err = errors.NewConflict("", "", err)
|
||||
}
|
||||
case http.StatusNotFound:
|
||||
err = errors.NewNotFound("", "")
|
||||
case http.StatusBadRequest:
|
||||
err = errors.NewBadRequest(err.Error())
|
||||
}
|
||||
return nil, false, err
|
||||
}
|
||||
return nil, false, errors.FromObject(&status)
|
||||
}
|
||||
|
@ -20,15 +20,17 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
|
||||
"github.com/golang/glog"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
|
||||
)
|
||||
|
||||
func (f *Factory) NewCmdDelete(out io.Writer) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "delete ([-f filename] | (<resource> <id>))",
|
||||
Use: "delete ([-f filename] | (<resource> [(<id> | -l <label>)]",
|
||||
Short: "Delete a resource by filename, stdin or resource and id",
|
||||
Long: `Delete a resource by filename, stdin or resource and id.
|
||||
Long: `Delete a resource by filename, stdin, resource and id or by resources and label selector.
|
||||
|
||||
JSON and YAML formats are accepted.
|
||||
|
||||
@ -46,21 +48,31 @@ Examples:
|
||||
$ cat pod.json | kubectl delete -f -
|
||||
<delete a pod based on the type and id in the json passed into stdin>
|
||||
|
||||
$ kubectl delete pods,services -l name=myLabel
|
||||
<delete pods and services with label name=myLabel>
|
||||
|
||||
$ 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) {
|
||||
filename := GetFlagString(cmd, "filename")
|
||||
schema, err := f.Validator(cmd)
|
||||
checkErr(err)
|
||||
mapping, namespace, name := ResourceFromArgsOrFile(cmd, args, filename, f.Typer, f.Mapper, schema)
|
||||
client, err := f.Client(cmd, mapping)
|
||||
checkErr(err)
|
||||
|
||||
err = kubectl.NewRESTHelper(client, mapping).Delete(namespace, name)
|
||||
checkErr(err)
|
||||
fmt.Fprintf(out, "%s\n", name)
|
||||
selector := GetFlagString(cmd, "selector")
|
||||
found := 0
|
||||
ResourcesFromArgsOrFile(cmd, args, filename, selector, f.Typer, f.Mapper, f.Client, schema).Visit(func(r *ResourceInfo) error {
|
||||
found++
|
||||
if err := kubectl.NewRESTHelper(r.Client, r.Mapping).Delete(r.Namespace, r.Name); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(out, "%s\n", r.Name)
|
||||
return nil
|
||||
})
|
||||
if found == 0 {
|
||||
glog.V(2).Infof("No resource(s) found")
|
||||
}
|
||||
},
|
||||
}
|
||||
cmd.Flags().StringP("filename", "f", "", "Filename or URL to file to use to delete the resource")
|
||||
cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on")
|
||||
return cmd
|
||||
}
|
||||
|
@ -18,15 +18,172 @@ package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/validation"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
)
|
||||
|
||||
// ResourceInfo contains temporary info to execute REST call
|
||||
type ResourceInfo struct {
|
||||
Client kubectl.RESTClient
|
||||
Mapping *meta.RESTMapping
|
||||
Namespace string
|
||||
Name string
|
||||
|
||||
// Optional, this is the most recent value returned by the server if available
|
||||
runtime.Object
|
||||
}
|
||||
|
||||
// ResourceVisitor lets clients walk the list of resources
|
||||
type ResourceVisitor interface {
|
||||
Visit(func(*ResourceInfo) error) error
|
||||
}
|
||||
|
||||
type ResourceVisitorList []ResourceVisitor
|
||||
|
||||
// Visit implements ResourceVisitor
|
||||
func (l ResourceVisitorList) Visit(fn func(r *ResourceInfo) error) error {
|
||||
for i := range l {
|
||||
if err := l[i].Visit(fn); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewResourceInfo(client kubectl.RESTClient, mapping *meta.RESTMapping, namespace, name string) *ResourceInfo {
|
||||
return &ResourceInfo{
|
||||
Client: client,
|
||||
Mapping: mapping,
|
||||
Namespace: namespace,
|
||||
Name: name,
|
||||
}
|
||||
}
|
||||
|
||||
// Visit implements ResourceVisitor
|
||||
func (r *ResourceInfo) Visit(fn func(r *ResourceInfo) error) error {
|
||||
return fn(r)
|
||||
}
|
||||
|
||||
// ResourceSelector is a facade for all the resources fetched via label selector
|
||||
type ResourceSelector struct {
|
||||
Client kubectl.RESTClient
|
||||
Mapping *meta.RESTMapping
|
||||
Namespace string
|
||||
Selector labels.Selector
|
||||
}
|
||||
|
||||
// NewResourceSelector creates a resource selector which hides details of getting items by their label selector.
|
||||
func NewResourceSelector(client kubectl.RESTClient, mapping *meta.RESTMapping, namespace string, selector labels.Selector) *ResourceSelector {
|
||||
return &ResourceSelector{
|
||||
Client: client,
|
||||
Mapping: mapping,
|
||||
Namespace: namespace,
|
||||
Selector: selector,
|
||||
}
|
||||
}
|
||||
|
||||
// Visit implements ResourceVisitor
|
||||
func (r *ResourceSelector) Visit(fn func(r *ResourceInfo) error) error {
|
||||
list, err := kubectl.NewRESTHelper(r.Client, r.Mapping).List(r.Namespace, r.Selector)
|
||||
if err != nil {
|
||||
if errors.IsBadRequest(err) || errors.IsNotFound(err) {
|
||||
glog.V(2).Infof("Unable to perform a label selector query on %s with labels %s: %v", r.Mapping.Resource, r.Selector, err)
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
items, err := runtime.ExtractList(list)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
accessor := meta.NewAccessor()
|
||||
for i := range items {
|
||||
name, err := accessor.Name(items[i])
|
||||
if err != nil {
|
||||
// items without names cannot be visited
|
||||
glog.V(2).Infof("Found %s with labels %s, but can't access the item by name.", r.Mapping.Resource, r.Selector)
|
||||
continue
|
||||
}
|
||||
item := &ResourceInfo{
|
||||
Client: r.Client,
|
||||
Mapping: r.Mapping,
|
||||
Namespace: r.Namespace,
|
||||
Name: name,
|
||||
Object: items[i],
|
||||
}
|
||||
if err := fn(item); err != nil {
|
||||
if errors.IsNotFound(err) {
|
||||
glog.V(2).Infof("Found %s named %q, but can't be accessed now: %v", r.Mapping.Resource, name, err)
|
||||
return nil
|
||||
}
|
||||
log.Printf("got error for resource %s: %v", r.Mapping.Resource, err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ResourcesFromArgsOrFile computes a list of Resources by extracting info from filename or args. It will
|
||||
// handle label selectors provided.
|
||||
func ResourcesFromArgsOrFile(
|
||||
cmd *cobra.Command,
|
||||
args []string,
|
||||
filename, selector string,
|
||||
typer runtime.ObjectTyper,
|
||||
mapper meta.RESTMapper,
|
||||
clientBuilder func(cmd *cobra.Command, mapping *meta.RESTMapping) (kubectl.RESTClient, error),
|
||||
schema validation.Schema,
|
||||
) ResourceVisitor {
|
||||
|
||||
// handling filename & resource id
|
||||
if len(selector) == 0 {
|
||||
mapping, namespace, name := ResourceFromArgsOrFile(cmd, args, filename, typer, mapper, schema)
|
||||
client, err := clientBuilder(cmd, mapping)
|
||||
checkErr(err)
|
||||
|
||||
return NewResourceInfo(client, mapping, namespace, name)
|
||||
}
|
||||
|
||||
labelSelector, err := labels.ParseSelector(selector)
|
||||
checkErr(err)
|
||||
|
||||
namespace := GetKubeNamespace(cmd)
|
||||
visitors := ResourceVisitorList{}
|
||||
|
||||
if len(args) != 1 {
|
||||
usageError(cmd, "Must specify the type of resource")
|
||||
}
|
||||
types := SplitResourceArgument(args[0])
|
||||
for _, arg := range types {
|
||||
resource := kubectl.ExpandResourceShortcut(arg)
|
||||
if len(resource) == 0 {
|
||||
usageError(cmd, "Unknown resource %s", resource)
|
||||
}
|
||||
version, kind, err := mapper.VersionAndKindForResource(resource)
|
||||
checkErr(err)
|
||||
|
||||
mapping, err := mapper.RESTMapping(version, kind)
|
||||
checkErr(err)
|
||||
|
||||
client, err := clientBuilder(cmd, mapping)
|
||||
checkErr(err)
|
||||
|
||||
visitors = append(visitors, NewResourceSelector(client, mapping, namespace, labelSelector))
|
||||
}
|
||||
return visitors
|
||||
}
|
||||
|
||||
// ResourceFromArgsOrFile expects two arguments or a valid file with a given type, and extracts
|
||||
// the fields necessary to uniquely locate a resource. Displays a usageError if that contract is
|
||||
// not satisfied, or a generic error if any other problems occur.
|
||||
@ -166,3 +323,9 @@ func CompareNamespaceFromFile(cmd *cobra.Command, namespace string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func SplitResourceArgument(arg string) []string {
|
||||
set := util.NewStringSet()
|
||||
set.Insert(strings.Split(arg, ",")...)
|
||||
return set.List()
|
||||
}
|
||||
|
@ -50,6 +50,10 @@ func (m *RESTHelper) Get(namespace, name string, selector labels.Selector) (runt
|
||||
return m.RESTClient.Get().Path(m.Resource).Namespace(namespace).Path(name).SelectorParam("labels", selector).Do().Get()
|
||||
}
|
||||
|
||||
func (m *RESTHelper) List(namespace string, selector labels.Selector) (runtime.Object, error) {
|
||||
return m.RESTClient.Get().Path(m.Resource).Namespace(namespace).Path("").SelectorParam("labels", selector).Do().Get()
|
||||
}
|
||||
|
||||
func (m *RESTHelper) Watch(namespace, resourceVersion string, labelSelector, fieldSelector labels.Selector) (watch.Interface, error) {
|
||||
return m.RESTClient.Get().
|
||||
Path("watch").
|
||||
|
Loading…
Reference in New Issue
Block a user