mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-06 10:43:56 +00:00
Add namespace to NotFound error for kubectl logs command (#120111)
Signed-off-by: Craig Newton <newtondev@gmail.com>
This commit is contained in:
parent
6cc77a577e
commit
20b28312ea
@ -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"
|
||||
@ -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 {
|
||||
|
@ -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"
|
||||
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user