Fix attach command for InitContainers

pull/6/head
Sylwester Brzeczkowski 2016-06-24 14:35:02 +02:00
parent eedc438da9
commit a558cadedd
2 changed files with 22 additions and 2 deletions

View File

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

View File

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