From 8408ed1e0b307e691866c0fc0e5dcd903c648c19 Mon Sep 17 00:00:00 2001 From: Lee Verberne Date: Fri, 13 Apr 2018 16:55:15 +0200 Subject: [PATCH] Allow kubectl to attach to an ephemeral container Kubernetes-commit: 5bd8a045d651005cd8cb1d34d835fdb487651325 --- pkg/cmd/attach/attach.go | 5 +++++ pkg/cmd/attach/attach_test.go | 21 ++++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/attach/attach.go b/pkg/cmd/attach/attach.go index c39e5ce50..dbbd03d89 100644 --- a/pkg/cmd/attach/attach.go +++ b/pkg/cmd/attach/attach.go @@ -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) } diff --git a/pkg/cmd/attach/attach_test.go b/pkg/cmd/attach/attach_test.go index 8c6f01929..f6886fa3a 100644 --- a/pkg/cmd/attach/attach_test.go +++ b/pkg/cmd/attach/attach_test.go @@ -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,