Add pod logs by click: /api/pods + /api/logs endpoints

Deployment/StatefulSet/DaemonSet/CronJob details now show a pod +
container picker with the pod's recent log tail (500 lines, no follow),
sitting alongside the existing raw-manifest view. Pod resolution reuses
buildGraph's ownerReferences-walking logic (ownerWorkloadID) so it stays
consistent with the schedules-on edges already shown on the graph.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Виталий Чефанов
2026-09-23 18:56:50 +03:00
co-authored by Claude Sonnet 5
parent 8f9c650960
commit 2ac0ddb6ba
2 changed files with 146 additions and 0 deletions
+84
View File
@@ -12,6 +12,7 @@ package main
import (
"context"
"encoding/json"
"io"
"log"
"net/http"
"os"
@@ -65,6 +66,8 @@ func main() {
mux := http.NewServeMux()
mux.HandleFunc("/api/graph", handleGraph)
mux.HandleFunc("/api/resource", handleResource)
mux.HandleFunc("/api/pods", handlePods)
mux.HandleFunc("/api/logs", handleLogs)
mux.Handle("/", http.FileServer(http.Dir("./static")))
addr := ":8080"
@@ -445,4 +448,85 @@ func handleResource(w http.ResponseWriter, r *http.Request) {
w.Write(out)
}
// podsForWorkload finds the live pods currently owned by a given workload,
// reusing the same ownerReferences-walking logic buildGraph uses for the
// schedules-on edges -- so "which pods back this node" stays consistent
// with what the graph already shows for placement.
func podsForWorkload(ctx context.Context, ns, kind, name string) ([]corev1.Pod, error) {
target := workloadID(ns, kind, name)
pods, err := clientset.CoreV1().Pods(ns).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
var out []corev1.Pod
for _, p := range pods.Items {
if ownerWorkloadID(p) == target {
out = append(out, p)
}
}
sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name })
return out, nil
}
type PodInfo struct {
Name string `json:"name"`
NodeName string `json:"nodeName"`
Phase string `json:"phase"`
Containers []string `json:"containers"`
}
// handlePods lists the pods backing a workload node, with their containers --
// the frontend uses this to build the pod/container pickers before fetching
// logs, since a Deployment/StatefulSet/DaemonSet/CronJob isn't itself a log
// source, its pods are.
func handlePods(w http.ResponseWriter, r *http.Request) {
kind := r.URL.Query().Get("kind")
ns := r.URL.Query().Get("namespace")
name := r.URL.Query().Get("name")
if kind == "" || ns == "" || name == "" {
http.Error(w, "kind, namespace and name are required", http.StatusBadRequest)
return
}
pods, err := podsForWorkload(r.Context(), ns, kind, name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
out := make([]PodInfo, 0, len(pods))
for _, p := range pods {
var containers []string
for _, c := range p.Spec.Containers {
containers = append(containers, c.Name)
}
out = append(out, PodInfo{Name: p.Name, NodeName: p.Spec.NodeName, Phase: string(p.Status.Phase), Containers: containers})
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(out)
}
// handleLogs streams a single container's recent log tail. Read-only,
// no follow -- this is a click-to-inspect tool, not a live tail viewer.
func handleLogs(w http.ResponseWriter, r *http.Request) {
ns := r.URL.Query().Get("namespace")
pod := r.URL.Query().Get("pod")
container := r.URL.Query().Get("container")
if ns == "" || pod == "" {
http.Error(w, "namespace and pod are required", http.StatusBadRequest)
return
}
var tail int64 = 500
opts := &corev1.PodLogOptions{TailLines: &tail}
if container != "" {
opts.Container = container
}
stream, err := clientset.CoreV1().Pods(ns).GetLogs(pod, opts).Stream(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer stream.Close()
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
io.Copy(w, stream)
}
var _ = os.Stdout // keep the os import if logging grows; avoids an unused-import edit later