Merge pull request #28012 from sttts/sttts-kubectl-attach-err-non-existing-ctr

Automatic merge from submit-queue

kubectl attach: error out for non-existing containers

Currently, kubectl attach falls back to the first container which is pretty confusing.

Based on https://github.com/kubernetes/kubernetes/pull/27541.
This commit is contained in:
k8s-merge-robot
2016-06-29 10:11:03 -07:00
committed by GitHub
3 changed files with 70 additions and 38 deletions

View File

@@ -335,8 +335,12 @@ func handleAttachPod(f *cmdutil.Factory, c *client.Client, pod *api.Pod, opts *A
if err != nil {
return err
}
ctrName, err := opts.GetContainerName(pod)
if err != nil {
return err
}
if status == api.PodSucceeded || status == api.PodFailed {
req, err := f.LogsForObject(pod, &api.PodLogOptions{Container: opts.GetContainerName(pod)})
req, err := f.LogsForObject(pod, &api.PodLogOptions{Container: ctrName})
if err != nil {
return err
}
@@ -353,7 +357,7 @@ func handleAttachPod(f *cmdutil.Factory, c *client.Client, pod *api.Pod, opts *A
opts.Namespace = pod.Namespace
if err := opts.Run(); err != nil {
fmt.Fprintf(opts.Out, "Error attaching, falling back to logs: %v\n", err)
req, err := f.LogsForObject(pod, &api.PodLogOptions{Container: opts.GetContainerName(pod)})
req, err := f.LogsForObject(pod, &api.PodLogOptions{Container: ctrName})
if err != nil {
return err
}