mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 15:25:57 +00:00
Merge pull request #27541 from gitfred/attach-init-con
Automatic merge from submit-queue Fix attach command for InitContainers Added InitContainers to the things that GetContainer in attach.go has to look for to find a container to attach. Also test case added. fixes #27540
This commit is contained in:
commit
7fbf4ac625
@ -169,9 +169,11 @@ func (p *AttachOptions) Run() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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
|
p.Pod = pod
|
||||||
// TODO: convert this to a clean "wait" behavior
|
// TODO: convert this to a clean "wait" behavior
|
||||||
}
|
}
|
||||||
@ -235,6 +237,11 @@ func (p *AttachOptions) GetContainer(pod *api.Pod) api.Container {
|
|||||||
return 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)
|
glog.V(4).Infof("defaulting container name to %s", pod.Spec.Containers[0].Name)
|
||||||
|
@ -79,7 +79,15 @@ func TestPodAndContainerAttach(t *testing.T) {
|
|||||||
expectedContainer: "bar",
|
expectedContainer: "bar",
|
||||||
name: "container in flag",
|
name: "container in flag",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
p: &AttachOptions{ContainerName: "initfoo"},
|
||||||
|
args: []string{"foo"},
|
||||||
|
expectedPod: "foo",
|
||||||
|
expectedContainer: "initfoo",
|
||||||
|
name: "init container in flag",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
f, tf, codec := NewAPIFactory()
|
f, tf, codec := NewAPIFactory()
|
||||||
tf.Client = &fake.RESTClient{
|
tf.Client = &fake.RESTClient{
|
||||||
@ -271,6 +279,11 @@ func attachPod() *api.Pod {
|
|||||||
Name: "bar",
|
Name: "bar",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
InitContainers: []api.Container{
|
||||||
|
{
|
||||||
|
Name: "initfoo",
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Status: api.PodStatus{
|
Status: api.PodStatus{
|
||||||
Phase: api.PodRunning,
|
Phase: api.PodRunning,
|
||||||
|
Loading…
Reference in New Issue
Block a user