Skip to content

Commit 5bd8a04

Browse files
committed
Allow kubectl to attach to an ephemeral container
1 parent c68eeb2 commit 5bd8a04

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/attach/attach.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,11 @@ func (o *AttachOptions) containerToAttachTo(pod *corev1.Pod) (*corev1.Container,
322322
return &pod.Spec.InitContainers[i], nil
323323
}
324324
}
325+
for i := range pod.Spec.EphemeralContainers {
326+
if pod.Spec.EphemeralContainers[i].Name == o.ContainerName {
327+
return (*corev1.Container)(&pod.Spec.EphemeralContainers[i].EphemeralContainerCommon), nil
328+
}
329+
}
325330
return nil, fmt.Errorf("container not found (%s)", o.ContainerName)
326331
}
327332

staging/src/k8s.io/kubectl/pkg/cmd/attach/attach_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ func TestPodAndContainerAttach(t *testing.T) {
102102
expectedContainerName: "initfoo",
103103
obj: attachPod(),
104104
},
105+
{
106+
name: "ephemeral container in flag",
107+
options: &AttachOptions{StreamOptions: exec.StreamOptions{ContainerName: "debugger"}, GetPodTimeout: 30},
108+
args: []string{"foo"},
109+
expectedPodName: "foo",
110+
expectedContainerName: "debugger",
111+
obj: attachPod(),
112+
},
105113
{
106114
name: "non-existing container",
107115
options: &AttachOptions{StreamOptions: exec.StreamOptions{ContainerName: "wrong"}, GetPodTimeout: 10},
@@ -136,7 +144,7 @@ func TestPodAndContainerAttach(t *testing.T) {
136144
test.options.Resources = test.args
137145

138146
if err := test.options.Validate(); err != nil {
139-
if !strings.Contains(err.Error(), test.expectError) {
147+
if test.expectError == "" || !strings.Contains(err.Error(), test.expectError) {
140148
t.Errorf("unexpected error: expected %q, got %q", test.expectError, err)
141149
}
142150
return
@@ -153,7 +161,7 @@ func TestPodAndContainerAttach(t *testing.T) {
153161
},
154162
})
155163
if err != nil {
156-
if !strings.Contains(err.Error(), test.expectError) {
164+
if test.expectError == "" || !strings.Contains(err.Error(), test.expectError) {
157165
t.Errorf("unexpected error: expected %q, got %q", err, test.expectError)
158166
}
159167
return
@@ -165,7 +173,7 @@ func TestPodAndContainerAttach(t *testing.T) {
165173

166174
container, err := test.options.containerToAttachTo(attachPod())
167175
if err != nil {
168-
if !strings.Contains(err.Error(), test.expectError) {
176+
if test.expectError == "" || !strings.Contains(err.Error(), test.expectError) {
169177
t.Errorf("unexpected error: expected %q, got %q", err, test.expectError)
170178
}
171179
return
@@ -414,6 +422,13 @@ func attachPod() *corev1.Pod {
414422
Name: "initfoo",
415423
},
416424
},
425+
EphemeralContainers: []corev1.EphemeralContainer{
426+
{
427+
EphemeralContainerCommon: corev1.EphemeralContainerCommon{
428+
Name: "debugger",
429+
},
430+
},
431+
},
417432
},
418433
Status: corev1.PodStatus{
419434
Phase: corev1.PodRunning,

0 commit comments

Comments
 (0)