Skip to content

Commit

Permalink
Add namespace to NotFound error for kubectl logs command (kubernetes#…
Browse files Browse the repository at this point in the history
…120111)

Signed-off-by: Craig Newton <newtondev@gmail.com>
  • Loading branch information
newtondev authored and ah8ad3 committed Apr 6, 2024
1 parent be549ec commit 76e55d4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
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)
}
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

0 comments on commit 76e55d4

Please sign in to comment.