From a558cadedda1e7cd572e5da07a0675b8814faa84 Mon Sep 17 00:00:00 2001 From: Sylwester Brzeczkowski Date: Fri, 24 Jun 2016 14:35:02 +0200 Subject: [PATCH] Fix attach command for InitContainers --- pkg/kubectl/cmd/attach.go | 11 +++++++++-- pkg/kubectl/cmd/attach_test.go | 13 +++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/pkg/kubectl/cmd/attach.go b/pkg/kubectl/cmd/attach.go index 3150bccaee5..56df25e757e 100644 --- a/pkg/kubectl/cmd/attach.go +++ b/pkg/kubectl/cmd/attach.go @@ -167,9 +167,11 @@ func (p *AttachOptions) Run() error { if err != nil { return err } - if pod.Status.Phase != api.PodRunning { - return fmt.Errorf("pod %s is not running and cannot be attached to; current phase is %s", p.PodName, pod.Status.Phase) + + if pod.Status.Phase == api.PodSucceeded || pod.Status.Phase == api.PodFailed { + return fmt.Errorf("cannot attach a container in a completed pod; current phase is %s", pod.Status.Phase) } + p.Pod = pod // TODO: convert this to a clean "wait" behavior } @@ -233,6 +235,11 @@ func (p *AttachOptions) GetContainer(pod *api.Pod) api.Container { return container } } + for _, container := range pod.Spec.InitContainers { + if container.Name == p.ContainerName { + return container + } + } } glog.V(4).Infof("defaulting container name to %s", pod.Spec.Containers[0].Name) diff --git a/pkg/kubectl/cmd/attach_test.go b/pkg/kubectl/cmd/attach_test.go index 29128ae5188..e4af36582c7 100644 --- a/pkg/kubectl/cmd/attach_test.go +++ b/pkg/kubectl/cmd/attach_test.go @@ -79,7 +79,15 @@ func TestPodAndContainerAttach(t *testing.T) { expectedContainer: "bar", name: "container in flag", }, + { + p: &AttachOptions{ContainerName: "initfoo"}, + args: []string{"foo"}, + expectedPod: "foo", + expectedContainer: "initfoo", + name: "init container in flag", + }, } + for _, test := range tests { f, tf, codec := NewAPIFactory() tf.Client = &fake.RESTClient{ @@ -271,6 +279,11 @@ func attachPod() *api.Pod { Name: "bar", }, }, + InitContainers: []api.Container{ + { + Name: "initfoo", + }, + }, }, Status: api.PodStatus{ Phase: api.PodRunning,