Merge pull request #80644 from verb/debug-kubectl

Allow kubectl to attach to ephemeral containers
This commit is contained in:
Kubernetes Prow Robot 2019-08-27 15:39:10 -07:00 committed by GitHub
commit 586d200e35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 3 deletions

View File

@ -322,6 +322,11 @@ func (o *AttachOptions) containerToAttachTo(pod *corev1.Pod) (*corev1.Container,
return &pod.Spec.InitContainers[i], nil
}
}
for i := range pod.Spec.EphemeralContainers {
if pod.Spec.EphemeralContainers[i].Name == o.ContainerName {
return (*corev1.Container)(&pod.Spec.EphemeralContainers[i].EphemeralContainerCommon), nil
}
}
return nil, fmt.Errorf("container not found (%s)", o.ContainerName)
}

View File

@ -102,6 +102,14 @@ func TestPodAndContainerAttach(t *testing.T) {
expectedContainerName: "initfoo",
obj: attachPod(),
},
{
name: "ephemeral container in flag",
options: &AttachOptions{StreamOptions: exec.StreamOptions{ContainerName: "debugger"}, GetPodTimeout: 30},
args: []string{"foo"},
expectedPodName: "foo",
expectedContainerName: "debugger",
obj: attachPod(),
},
{
name: "non-existing container",
options: &AttachOptions{StreamOptions: exec.StreamOptions{ContainerName: "wrong"}, GetPodTimeout: 10},
@ -136,7 +144,7 @@ func TestPodAndContainerAttach(t *testing.T) {
test.options.Resources = test.args
if err := test.options.Validate(); err != nil {
if !strings.Contains(err.Error(), test.expectError) {
if test.expectError == "" || !strings.Contains(err.Error(), test.expectError) {
t.Errorf("unexpected error: expected %q, got %q", test.expectError, err)
}
return
@ -153,7 +161,7 @@ func TestPodAndContainerAttach(t *testing.T) {
},
})
if err != nil {
if !strings.Contains(err.Error(), test.expectError) {
if test.expectError == "" || !strings.Contains(err.Error(), test.expectError) {
t.Errorf("unexpected error: expected %q, got %q", err, test.expectError)
}
return
@ -165,7 +173,7 @@ func TestPodAndContainerAttach(t *testing.T) {
container, err := test.options.containerToAttachTo(attachPod())
if err != nil {
if !strings.Contains(err.Error(), test.expectError) {
if test.expectError == "" || !strings.Contains(err.Error(), test.expectError) {
t.Errorf("unexpected error: expected %q, got %q", err, test.expectError)
}
return
@ -414,6 +422,13 @@ func attachPod() *corev1.Pod {
Name: "initfoo",
},
},
EphemeralContainers: []corev1.EphemeralContainer{
{
EphemeralContainerCommon: corev1.EphemeralContainerCommon{
Name: "debugger",
},
},
},
},
Status: corev1.PodStatus{
Phase: corev1.PodRunning,