mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Merge pull request #80644 from verb/debug-kubectl
Allow kubectl to attach to ephemeral containers
This commit is contained in:
commit
586d200e35
@ -322,6 +322,11 @@ func (o *AttachOptions) containerToAttachTo(pod *corev1.Pod) (*corev1.Container,
|
|||||||
return &pod.Spec.InitContainers[i], nil
|
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)
|
return nil, fmt.Errorf("container not found (%s)", o.ContainerName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,6 +102,14 @@ func TestPodAndContainerAttach(t *testing.T) {
|
|||||||
expectedContainerName: "initfoo",
|
expectedContainerName: "initfoo",
|
||||||
obj: attachPod(),
|
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",
|
name: "non-existing container",
|
||||||
options: &AttachOptions{StreamOptions: exec.StreamOptions{ContainerName: "wrong"}, GetPodTimeout: 10},
|
options: &AttachOptions{StreamOptions: exec.StreamOptions{ContainerName: "wrong"}, GetPodTimeout: 10},
|
||||||
@ -136,7 +144,7 @@ func TestPodAndContainerAttach(t *testing.T) {
|
|||||||
test.options.Resources = test.args
|
test.options.Resources = test.args
|
||||||
|
|
||||||
if err := test.options.Validate(); err != nil {
|
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)
|
t.Errorf("unexpected error: expected %q, got %q", test.expectError, err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -153,7 +161,7 @@ func TestPodAndContainerAttach(t *testing.T) {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
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)
|
t.Errorf("unexpected error: expected %q, got %q", err, test.expectError)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -165,7 +173,7 @@ func TestPodAndContainerAttach(t *testing.T) {
|
|||||||
|
|
||||||
container, err := test.options.containerToAttachTo(attachPod())
|
container, err := test.options.containerToAttachTo(attachPod())
|
||||||
if err != nil {
|
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)
|
t.Errorf("unexpected error: expected %q, got %q", err, test.expectError)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -414,6 +422,13 @@ func attachPod() *corev1.Pod {
|
|||||||
Name: "initfoo",
|
Name: "initfoo",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
EphemeralContainers: []corev1.EphemeralContainer{
|
||||||
|
{
|
||||||
|
EphemeralContainerCommon: corev1.EphemeralContainerCommon{
|
||||||
|
Name: "debugger",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Status: corev1.PodStatus{
|
Status: corev1.PodStatus{
|
||||||
Phase: corev1.PodRunning,
|
Phase: corev1.PodRunning,
|
||||||
|
Loading…
Reference in New Issue
Block a user