Skip to content

Commit

Permalink
Merge pull request kubernetes#2751 from sdminonne/delete_by_labels
Browse files Browse the repository at this point in the history
kubectl delete command: adding  labelSelector
  • Loading branch information
smarterclayton committed Dec 17, 2014
2 parents 7a0892d + 61ac2f7 commit ff30500
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 13 deletions.
17 changes: 16 additions & 1 deletion pkg/client/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
32 changes: 22 additions & 10 deletions pkg/kubectl/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
167 changes: 165 additions & 2 deletions pkg/kubectl/cmd/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
}
4 changes: 4 additions & 0 deletions pkg/kubectl/resthelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand Down

0 comments on commit ff30500

Please sign in to comment.