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

Add namespace to NotFound error for kubectl logs command #120111

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: 4 additions & 0 deletions staging/src/k8s.io/kubectl/pkg/cmd/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/spf13/cobra"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/genericclioptions"
Expand Down Expand Up @@ -276,6 +277,9 @@ func (o *LogsOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []str
}
infos, err := builder.Do().Infos()
if err != nil {
if apierrors.IsNotFound(err) {
err = fmt.Errorf("error from server (NotFound): %w in namespace %q", err, o.Namespace)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
err = fmt.Errorf("error from server (NotFound): %w in namespace %q", err, o.Namespace)
err = fmt.Errorf("error from server (NotFound): %v in namespace %s", err, o.Namespace)

seems better but output is equivalent anyway.

}
return err
}
if o.Selector == "" && len(infos) != 1 {
Expand Down
44 changes: 44 additions & 0 deletions staging/src/k8s.io/kubectl/pkg/cmd/logs/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ import (
"time"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/genericiooptions"
restclient "k8s.io/client-go/rest"
Expand Down Expand Up @@ -833,6 +835,48 @@ func TestNoResourceFoundMessage(t *testing.T) {
}
}

func TestNoPodInNamespaceFoundMessage(t *testing.T) {
namespace, podName := "test", "bar"

tf := cmdtesting.NewTestFactory().WithNamespace(namespace)
defer tf.Cleanup()

ns := scheme.Codecs.WithoutConversion()
codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
errStatus := apierrors.NewNotFound(schema.GroupResource{Resource: "pods"}, podName).Status()

tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
switch req.URL.Path {
case fmt.Sprintf("/namespaces/%s/pods/%s", namespace, podName):
fallthrough
case fmt.Sprintf("/namespaces/%s/pods", namespace):
fallthrough
case fmt.Sprintf("/api/v1/namespaces/%s", namespace):
return &http.Response{StatusCode: http.StatusNotFound, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, &errStatus)}, nil
default:
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
return nil, nil
}
}),
}

streams, _, _, _ := genericiooptions.NewTestIOStreams()
cmd := NewCmdLogs(tf, streams)
o := NewLogsOptions(streams, false)
err := o.Complete(tf, cmd, []string{podName})

if err == nil {
t.Fatal("Expected NotFound error, got nil")
}

expected := fmt.Sprintf("error from server (NotFound): pods %q not found in namespace %q", podName, namespace)
if e, a := expected, err.Error(); e != a {
t.Errorf("expected to find:\n\t%s\nfound:\n\t%s\n", e, a)
}
}

type responseWrapperMock struct {
data io.Reader
err error
Expand Down