Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling empty result when invoking kubectl get #77681

Merged
merged 1 commit into from Aug 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/kubectl/cmd/get/get.go
Expand Up @@ -750,8 +750,8 @@ func (o *GetOptions) printGeneric(r *resource.Result) error {
}

var obj runtime.Object
if !singleItemImplied || len(infos) > 1 {
// we have more than one item, so coerce all items into a list.
if !singleItemImplied || len(infos) != 1 {
// we have zero or multple items, so coerce all items into a list.
// we don't want an *unstructured.Unstructured list yet, as we
// may be dealing with non-unstructured objects. Compose all items
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to initialize items to Items: []runtime.RawExtension{} to avoid null items if they do -o json?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non initialized array will be an empty array and that's sufficient for our needs. I've added TestEmptyResultJSON as a security net for that case. We only print No resources found when using the default printers, b/c we don't have other way of saying there's nothing there. In case of JSON or other structured output you'll get an empty list.

// into an corev1.List, and then decode using an unstructured scheme.
Expand Down
54 changes: 54 additions & 0 deletions pkg/kubectl/cmd/get/get_test.go
Expand Up @@ -702,6 +702,60 @@ func TestGetObjectIgnoreNotFound(t *testing.T) {
}
}

func TestEmptyResult(t *testing.T) {
cmdtesting.InitTestErrorHandler(t)

tf := cmdtesting.NewTestFactory().WithNamespace("test")
defer tf.Cleanup()
codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)

tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &corev1.PodList{})}, nil
}),
}

streams, _, _, errbuf := genericclioptions.NewTestIOStreams()
cmd := NewCmdGet("kubectl", tf, streams)
// we're assuming that an empty file is being passed from stdin
cmd.Flags().Set("filename", "-")
cmd.Run(cmd, []string{})

if !strings.Contains(errbuf.String(), "No resources found") {
t.Errorf("unexpected output: %q", errbuf.String())
}
}

func TestEmptyResultJSON(t *testing.T) {
cmdtesting.InitTestErrorHandler(t)

tf := cmdtesting.NewTestFactory().WithNamespace("test")
defer tf.Cleanup()
codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)

tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{StatusCode: 200, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &corev1.PodList{})}, nil
}),
}

streams, _, outbuf, errbuf := genericclioptions.NewTestIOStreams()
cmd := NewCmdGet("kubectl", tf, streams)
// we're assuming that an empty file is being passed from stdin
cmd.Flags().Set("filename", "-")
cmd.Flags().Set("output", "json")
cmd.Run(cmd, []string{})

if errbuf.Len() > 0 {
t.Errorf("unexpected error: %q", errbuf.String())
}
if !strings.Contains(outbuf.String(), `"items": []`) {
t.Errorf("unexpected output: %q", outbuf.String())
}
}

func TestGetSortedObjects(t *testing.T) {
pods := &corev1.PodList{
ListMeta: metav1.ListMeta{
Expand Down