From 71c9d3e708d6a035b2e047ec3fc6bc513013a4ef Mon Sep 17 00:00:00 2001 From: astraw99 Date: Fri, 8 Aug 2025 18:33:35 +0800 Subject: [PATCH] Add valid container name info in the kubectl logs/exec output --- pkg/registry/core/pod/strategy.go | 11 ++++++++++- pkg/registry/core/pod/strategy_test.go | 2 +- .../src/k8s.io/kubectl/pkg/cmd/attach/attach_test.go | 2 +- .../src/k8s.io/kubectl/pkg/cmd/util/podcmd/podcmd.go | 2 +- .../kubectl/pkg/polymorphichelpers/logsforobject.go | 2 +- 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/pkg/registry/core/pod/strategy.go b/pkg/registry/core/pod/strategy.go index ccdf35e76f4..06e6efa08e4 100644 --- a/pkg/registry/core/pod/strategy.go +++ b/pkg/registry/core/pod/strategy.go @@ -47,10 +47,13 @@ import ( utilfeature "k8s.io/apiserver/pkg/util/feature" "k8s.io/apiserver/pkg/warning" "k8s.io/client-go/tools/cache" + "k8s.io/klog/v2" + "k8s.io/kubectl/pkg/cmd/util/podcmd" "k8s.io/kubernetes/pkg/api/legacyscheme" podutil "k8s.io/kubernetes/pkg/api/pod" api "k8s.io/kubernetes/pkg/apis/core" "k8s.io/kubernetes/pkg/apis/core/helper/qos" + apiscorev1 "k8s.io/kubernetes/pkg/apis/core/v1" corevalidation "k8s.io/kubernetes/pkg/apis/core/validation" "k8s.io/kubernetes/pkg/features" "k8s.io/kubernetes/pkg/kubelet/client" @@ -836,7 +839,13 @@ func validateContainer(container string, pod *api.Pod) (string, error) { } } else { if !podHasContainerWithName(pod, container) { - return "", errors.NewBadRequest(fmt.Sprintf("container %s is not valid for pod %s", container, pod.Name)) + coreV1Pod := &apiv1.Pod{} + if err := apiscorev1.Convert_core_Pod_To_v1_Pod(pod, coreV1Pod, nil); err != nil { + // This should never happen, but if it does, we want to log an error. + klog.ErrorS(err, "Pod failed to convert to v1", "pod", klog.KObj(pod)) + return "", errors.NewBadRequest(fmt.Sprintf("container %s is not valid for pod %s", container, pod.Name)) + } + return "", errors.NewBadRequest(fmt.Sprintf("container %s is not valid for pod %s out of: %s", container, pod.Name, podcmd.AllContainerNames(coreV1Pod))) } } diff --git a/pkg/registry/core/pod/strategy_test.go b/pkg/registry/core/pod/strategy_test.go index 65fe5dbbf11..6c850d9081d 100644 --- a/pkg/registry/core/pod/strategy_test.go +++ b/pkg/registry/core/pod/strategy_test.go @@ -582,7 +582,7 @@ func TestCheckLogLocation(t *testing.T) { opts: &api.PodLogOptions{ Container: "unknown", }, - expectedErr: errors.NewBadRequest("container unknown is not valid for pod test"), + expectedErr: errors.NewBadRequest("container unknown is not valid for pod test out of: container1, container2"), expectedTransport: nil, }, { diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/attach/attach_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/attach/attach_test.go index 6a599656e45..7796c7acb93 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/attach/attach_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/attach/attach_test.go @@ -276,7 +276,7 @@ func TestAttach(t *testing.T) { attachPath: "/api/" + version + "/namespaces/test/pods/foo/attach", pod: attachPod(), container: "foo", - expectedErr: "cannot attach to the container: container foo not found in pod foo", + expectedErr: "cannot attach to the container: container foo not found in pod foo out of: bar, debugger (ephem), initfoo (init)", }, } for _, test := range tests { diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/util/podcmd/podcmd.go b/staging/src/k8s.io/kubectl/pkg/cmd/util/podcmd/podcmd.go index bf760645d4a..d36c5481fd4 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/util/podcmd/podcmd.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/util/podcmd/podcmd.go @@ -59,7 +59,7 @@ func FindOrDefaultContainerByName(pod *v1.Pod, name string, quiet bool, warn io. if len(name) > 0 { container, _ = FindContainerByName(pod, name) if container == nil { - return nil, fmt.Errorf("container %s not found in pod %s", name, pod.Name) + return nil, fmt.Errorf("container %s not found in pod %s out of: %s", name, pod.Name, AllContainerNames(pod)) } return container, nil } diff --git a/staging/src/k8s.io/kubectl/pkg/polymorphichelpers/logsforobject.go b/staging/src/k8s.io/kubectl/pkg/polymorphichelpers/logsforobject.go index 87968b789d0..064ea910f99 100644 --- a/staging/src/k8s.io/kubectl/pkg/polymorphichelpers/logsforobject.go +++ b/staging/src/k8s.io/kubectl/pkg/polymorphichelpers/logsforobject.go @@ -116,7 +116,7 @@ func logsForObjectWithClient(clientset corev1client.CoreV1Interface, object, opt container, fieldPath := podcmd.FindContainerByName(t, currOpts.Container) if container == nil { - return nil, fmt.Errorf("container %s is not valid for pod %s", currOpts.Container, t.Name) + return nil, fmt.Errorf("container %s is not valid for pod %s out of: %s", currOpts.Container, t.Name, podcmd.AllContainerNames(t)) } ref, err := reference.GetPartialReference(scheme.Scheme, t, fieldPath) if err != nil {