Skip to content

Commit

Permalink
Merge pull request #94724 from eddiezane/automated-cherry-pick-of-#94…
Browse files Browse the repository at this point in the history
…580-origin-release-1.19

Automated cherry pick of #94580: Fix index out of range panic for kubectl alpha debug
  • Loading branch information
k8s-ci-robot authored Sep 11, 2020
2 parents a1fade3 + 39fd13d commit 19706d9
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 2 deletions.
6 changes: 4 additions & 2 deletions staging/src/k8s.io/kubectl/pkg/cmd/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,8 @@ func (o *DebugOptions) generatePodCopyWithDebugContainer(pod *corev1.Pod) (*core
},
Spec: *pod.Spec.DeepCopy(),
}
// set EphemeralContainers to nil so that the copy of pod can be created
copied.Spec.EphemeralContainers = nil
// change ShareProcessNamespace configuration only when commanded explicitly
if o.shareProcessedChanged {
copied.Spec.ShareProcessNamespace = pointer.BoolPtr(o.ShareProcesses)
Expand Down Expand Up @@ -544,11 +546,11 @@ func containerNameToRef(pod *corev1.Pod) map[string]*corev1.Container {
names[ref.Name] = ref
}
for i := range pod.Spec.InitContainers {
ref := &pod.Spec.Containers[i]
ref := &pod.Spec.InitContainers[i]
names[ref.Name] = ref
}
for i := range pod.Spec.EphemeralContainers {
ref := &pod.Spec.Containers[i]
ref := (*corev1.Container)(&pod.Spec.EphemeralContainers[i].EphemeralContainerCommon)
names[ref.Name] = ref
}
return names
Expand Down
172 changes: 172 additions & 0 deletions staging/src/k8s.io/kubectl/pkg/cmd/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,74 @@ func TestGenerateDebugContainer(t *testing.T) {
},
},
},
{
name: "pod with init containers",
opts: &DebugOptions{
Image: "busybox",
PullPolicy: corev1.PullIfNotPresent,
},
pod: &corev1.Pod{
Spec: corev1.PodSpec{
InitContainers: []corev1.Container{
{
Name: "init-container-1",
},
{
Name: "init-container-2",
},
},
Containers: []corev1.Container{
{
Name: "debugger",
},
},
},
},
expected: &corev1.EphemeralContainer{
EphemeralContainerCommon: corev1.EphemeralContainerCommon{
Name: "debugger-1",
Image: "busybox",
ImagePullPolicy: "IfNotPresent",
TerminationMessagePolicy: "File",
},
},
},
{
name: "pod with ephemeral containers",
opts: &DebugOptions{
Image: "busybox",
PullPolicy: corev1.PullIfNotPresent,
},
pod: &corev1.Pod{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "debugger",
},
},
EphemeralContainers: []corev1.EphemeralContainer{
{
EphemeralContainerCommon: corev1.EphemeralContainerCommon{
Name: "ephemeral-container-1",
},
},
{
EphemeralContainerCommon: corev1.EphemeralContainerCommon{
Name: "ephemeral-container-2",
},
},
},
},
},
expected: &corev1.EphemeralContainer{
EphemeralContainerCommon: corev1.EphemeralContainerCommon{
Name: "debugger-1",
Image: "busybox",
ImagePullPolicy: "IfNotPresent",
TerminationMessagePolicy: "File",
},
},
},
} {
t.Run(tc.name, func(t *testing.T) {
tc.opts.IOStreams = genericclioptions.NewTestIOStreamsDiscard()
Expand Down Expand Up @@ -590,6 +658,110 @@ func TestGeneratePodCopyWithDebugContainer(t *testing.T) {
},
},
},
{
name: "pod with init containers",
opts: &DebugOptions{
CopyTo: "debugger",
Image: "busybox",
PullPolicy: corev1.PullIfNotPresent,
},
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "target",
},
Spec: corev1.PodSpec{
InitContainers: []corev1.Container{
{
Name: "init-container-1",
},
{
Name: "init-container-2",
},
},
Containers: []corev1.Container{
{
Name: "debugger-1",
},
},
},
},
expected: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "debugger",
},
Spec: corev1.PodSpec{
InitContainers: []corev1.Container{
{
Name: "init-container-1",
},
{
Name: "init-container-2",
},
},
Containers: []corev1.Container{
{
Name: "debugger-1",
},
{
Name: "debugger-2",
Image: "busybox",
ImagePullPolicy: corev1.PullIfNotPresent,
TerminationMessagePolicy: corev1.TerminationMessageReadFile,
},
},
},
},
},
{
name: "pod with ephemeral containers",
opts: &DebugOptions{
CopyTo: "debugger",
Image: "busybox",
PullPolicy: corev1.PullIfNotPresent,
},
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "target",
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "debugger-1",
},
},
EphemeralContainers: []corev1.EphemeralContainer{
{
EphemeralContainerCommon: corev1.EphemeralContainerCommon{
Name: "ephemeral-container-1",
},
},
{
EphemeralContainerCommon: corev1.EphemeralContainerCommon{
Name: "ephemeral-container-2",
},
},
},
},
},
expected: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "debugger",
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "debugger-1",
},
{
Name: "debugger-2",
Image: "busybox",
ImagePullPolicy: corev1.PullIfNotPresent,
TerminationMessagePolicy: corev1.TerminationMessageReadFile,
},
},
},
},
},
{
name: "shared process namespace",
opts: &DebugOptions{
Expand Down

0 comments on commit 19706d9

Please sign in to comment.