Add valid container name info in the kubectl logs/exec output

This commit is contained in:
astraw99 2025-08-08 18:33:35 +08:00
parent 8e6d788887
commit 71c9d3e708
5 changed files with 14 additions and 5 deletions

View file

@ -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)))
}
}

View file

@ -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,
},
{

View file

@ -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 {

View file

@ -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
}

View file

@ -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 {