mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 05:03:09 +00:00
Merge pull request #3468 from smarterclayton/better_log_on_empty_delete
Write to STDERR when Delete contains no resources
This commit is contained in:
commit
6459b1a1f3
@ -20,7 +20,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
||||||
@ -70,7 +69,7 @@ Examples:
|
|||||||
Do()
|
Do()
|
||||||
|
|
||||||
found := 0
|
found := 0
|
||||||
r.IgnoreErrors(errors.IsNotFound).Visit(func(r *resource.Info) error {
|
err := r.IgnoreErrors(errors.IsNotFound).Visit(func(r *resource.Info) error {
|
||||||
found++
|
found++
|
||||||
if err := resource.NewHelper(r.Client, r.Mapping).Delete(r.Namespace, r.Name); err != nil {
|
if err := resource.NewHelper(r.Client, r.Mapping).Delete(r.Namespace, r.Name); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -78,8 +77,9 @@ Examples:
|
|||||||
fmt.Fprintf(out, "%s\n", r.Name)
|
fmt.Fprintf(out, "%s\n", r.Name)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
checkErr(err)
|
||||||
if found == 0 {
|
if found == 0 {
|
||||||
glog.V(2).Infof("No resource(s) found")
|
fmt.Fprintf(cmd.Out(), "No resources found\n")
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -82,6 +83,37 @@ func TestDeleteObjectIgnoreNotFound(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDeleteNoObjects(t *testing.T) {
|
||||||
|
f, tf, codec := NewAPIFactory()
|
||||||
|
tf.Printer = &testPrinter{}
|
||||||
|
tf.Client = &client.FakeRESTClient{
|
||||||
|
Codec: codec,
|
||||||
|
Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) {
|
||||||
|
switch p, m := req.URL.Path, req.Method; {
|
||||||
|
case p == "/ns/test/pods" && m == "GET":
|
||||||
|
return &http.Response{StatusCode: 200, Body: objBody(codec, &api.PodList{})}, nil
|
||||||
|
default:
|
||||||
|
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
stderr := bytes.NewBuffer([]byte{})
|
||||||
|
|
||||||
|
cmd := f.NewCmdDelete(buf)
|
||||||
|
cmd.SetOutput(stderr)
|
||||||
|
cmd.Flags().String("namespace", "test", "")
|
||||||
|
cmd.Run(cmd, []string{"pods"})
|
||||||
|
|
||||||
|
if buf.String() != "" {
|
||||||
|
t.Errorf("unexpected output: %s", buf.String())
|
||||||
|
}
|
||||||
|
if stderr.String() != "No resources found\n" {
|
||||||
|
t.Errorf("unexpected output: %s", stderr.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestDeleteMultipleObject(t *testing.T) {
|
func TestDeleteMultipleObject(t *testing.T) {
|
||||||
pods, svc := testData()
|
pods, svc := testData()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user