Skip to content

Commit 0ad272c

Browse files
committed
Support getting debuggable containers across more than one pod
Signed-off-by: Evan Harris <[email protected]>
1 parent 43644c4 commit 0ad272c

File tree

1 file changed

+52
-21
lines changed

1 file changed

+52
-21
lines changed

pkg/app/master/command/debug/handle_kubernetes_runtime.go

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,23 @@ func HandleKubernetesRuntime(
9696
return
9797
}
9898

99+
if commandParams.ActionListDebuggableContainers {
100+
xc.Out.State("action.list_debuggable_containers",
101+
ovars{"namespace": nsName})
102+
103+
result, err := listK8sDebuggableContainers(ctx, api, nsName, "")
104+
if err != nil {
105+
logger.WithError(err).Error("listK8sDebuggableContainers")
106+
xc.FailOn(err)
107+
}
108+
109+
for cname, iname := range result {
110+
xc.Out.Info("debuggable.container", ovars{"name": cname, "image": iname})
111+
}
112+
113+
return
114+
}
115+
99116
pod, podName, err := ensurePod(ctx, api, nsName, commandParams.TargetPod)
100117
if apierrors.IsNotFound(err) {
101118
logger.WithError(err).
@@ -136,22 +153,6 @@ func HandleKubernetesRuntime(
136153
"ec.count": len(pod.Spec.EphemeralContainers),
137154
}).Debug("target pod info")
138155

139-
if commandParams.ActionListDebuggableContainers {
140-
xc.Out.State("action.list_debuggable_containers",
141-
ovars{"namespace": nsName, "pod": podName})
142-
result, err := listK8sDebuggableContainers(ctx, api, nsName, podName)
143-
if err != nil {
144-
logger.WithError(err).Error("listK8sDebuggableContainers")
145-
xc.FailOn(err)
146-
}
147-
148-
for cname, iname := range result {
149-
xc.Out.Info("debuggable.container", ovars{"name": cname, "image": iname})
150-
}
151-
152-
return
153-
}
154-
155156
//todo: need to check that if targetRef is not empty it is valid
156157

157158
if commandParams.ActionListSessions {
@@ -1033,6 +1034,37 @@ func listK8sDebuggableContainers(
10331034
api *kubernetes.Clientset,
10341035
nsName string,
10351036
podName string) (map[string]string, error) {
1037+
activeContainers := map[string]string{}
1038+
debuggableContainers := map[string]string{}
1039+
1040+
// List all pods in the namespace
1041+
if podName == "" {
1042+
pods, err := api.CoreV1().Pods(nsName).List(ctx, metav1.ListOptions{})
1043+
if err != nil {
1044+
return nil, err
1045+
}
1046+
1047+
for _, pod := range pods.Items {
1048+
if pod.Status.Phase != corev1.PodRunning {
1049+
continue
1050+
}
1051+
1052+
activeNames := getActiveContainerNames(pod.Status.ContainerStatuses)
1053+
for _, name := range activeNames {
1054+
activeContainers[name] = ""
1055+
}
1056+
1057+
for _, c := range pod.Spec.Containers {
1058+
_, found := activeContainers[c.Name]
1059+
if found {
1060+
containerKey := fmt.Sprintf("%s/%s", pod.Name, c.Name)
1061+
debuggableContainers[containerKey] = c.Image
1062+
}
1063+
}
1064+
}
1065+
1066+
return debuggableContainers, nil
1067+
}
10361068

10371069
pod, err := api.CoreV1().Pods(nsName).Get(ctx, podName, metav1.GetOptions{})
10381070
if err != nil {
@@ -1044,19 +1076,18 @@ func listK8sDebuggableContainers(
10441076
}
10451077

10461078
activeNames := getActiveContainerNames(pod.Status.ContainerStatuses)
1047-
activeContainers := map[string]string{}
10481079
for _, name := range activeNames {
1049-
activeContainers[name] = ""
1080+
debuggableContainers[name] = ""
10501081
}
10511082

10521083
for _, c := range pod.Spec.Containers {
1053-
_, found := activeContainers[c.Name]
1084+
_, found := debuggableContainers[c.Name]
10541085
if found {
1055-
activeContainers[c.Name] = c.Image
1086+
debuggableContainers[c.Name] = c.Image
10561087
}
10571088
}
10581089

1059-
return activeContainers, nil
1090+
return debuggableContainers, nil
10601091
}
10611092

10621093
func listDebuggableK8sContainersWithConfig(

0 commit comments

Comments
 (0)