-
-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
List all k8s debuggable containers #84
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,6 +96,23 @@ func HandleKubernetesRuntime( | |
return | ||
} | ||
|
||
if commandParams.ActionListDebuggableContainers { | ||
xc.Out.State("action.list_debuggable_containers", | ||
ovars{"namespace": nsName}) | ||
|
||
result, err := listK8sDebuggableContainers(ctx, api, nsName, "") | ||
if err != nil { | ||
logger.WithError(err).Error("listK8sDebuggableContainers") | ||
xc.FailOn(err) | ||
} | ||
|
||
for cname, iname := range result { | ||
xc.Out.Info("debuggable.container", ovars{"name": cname, "image": iname}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @eharris128 if the intent is to show all debuggable containers across all pods then we need the pod name for each record we are printing here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. Definitely a necessary extension. |
||
} | ||
|
||
return | ||
} | ||
|
||
pod, podName, err := ensurePod(ctx, api, nsName, commandParams.TargetPod) | ||
if apierrors.IsNotFound(err) { | ||
logger.WithError(err). | ||
|
@@ -136,22 +153,6 @@ func HandleKubernetesRuntime( | |
"ec.count": len(pod.Spec.EphemeralContainers), | ||
}).Debug("target pod info") | ||
|
||
if commandParams.ActionListDebuggableContainers { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @eharris128 it might still be good to have an option to list all debuggable containers in a given pod, but if we have both options (to list them for all pods and for a specific pod) then we'll need a separate flag for this, so you can explicitly select what option you want. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah supporting both paths - per pod and across pods seems reasonable to me. I do not remember how the Feels like adding a new flag to differentiate by one pod versus all pods would be the clearest UX. |
||
xc.Out.State("action.list_debuggable_containers", | ||
ovars{"namespace": nsName, "pod": podName}) | ||
result, err := listK8sDebuggableContainers(ctx, api, nsName, podName) | ||
if err != nil { | ||
logger.WithError(err).Error("listK8sDebuggableContainers") | ||
xc.FailOn(err) | ||
} | ||
|
||
for cname, iname := range result { | ||
xc.Out.Info("debuggable.container", ovars{"name": cname, "image": iname}) | ||
} | ||
|
||
return | ||
} | ||
|
||
//todo: need to check that if targetRef is not empty it is valid | ||
|
||
if commandParams.ActionListSessions { | ||
|
@@ -1033,6 +1034,37 @@ func listK8sDebuggableContainers( | |
api *kubernetes.Clientset, | ||
nsName string, | ||
podName string) (map[string]string, error) { | ||
activeContainers := map[string]string{} | ||
debuggableContainers := map[string]string{} | ||
|
||
// List all pods in the namespace | ||
if podName == "" { | ||
pods, err := api.CoreV1().Pods(nsName).List(ctx, metav1.ListOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, pod := range pods.Items { | ||
if pod.Status.Phase != corev1.PodRunning { | ||
continue | ||
} | ||
|
||
activeNames := getActiveContainerNames(pod.Status.ContainerStatuses) | ||
for _, name := range activeNames { | ||
activeContainers[name] = "" | ||
} | ||
|
||
for _, c := range pod.Spec.Containers { | ||
_, found := activeContainers[c.Name] | ||
if found { | ||
containerKey := fmt.Sprintf("%s/%s", pod.Name, c.Name) | ||
debuggableContainers[containerKey] = c.Image | ||
} | ||
} | ||
} | ||
|
||
return debuggableContainers, nil | ||
} | ||
|
||
pod, err := api.CoreV1().Pods(nsName).Get(ctx, podName, metav1.GetOptions{}) | ||
if err != nil { | ||
|
@@ -1044,19 +1076,18 @@ func listK8sDebuggableContainers( | |
} | ||
|
||
activeNames := getActiveContainerNames(pod.Status.ContainerStatuses) | ||
activeContainers := map[string]string{} | ||
for _, name := range activeNames { | ||
activeContainers[name] = "" | ||
debuggableContainers[name] = "" | ||
} | ||
|
||
for _, c := range pod.Spec.Containers { | ||
_, found := activeContainers[c.Name] | ||
_, found := debuggableContainers[c.Name] | ||
if found { | ||
activeContainers[c.Name] = c.Image | ||
debuggableContainers[c.Name] = c.Image | ||
} | ||
} | ||
|
||
return activeContainers, nil | ||
return debuggableContainers, nil | ||
} | ||
|
||
func listDebuggableK8sContainersWithConfig( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eharris128 what was the reason to move commandParams.ActionListDebuggableContainers here? Was the intent to support listing debuggable containers across all pods in the (selected) namespace?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes exactly. Although what you point out below was neglected with this approach.