UI rework: inventory tree, compact cards, live Hubble traffic
Frontend rewritten around three panes. Inventory tree on the left (namespaces by kind, or nodes by namespace) with search and keyboard navigation replaces the namespace checkbox list. The map shows only the selected namespace(s) or node: fixed-size cards inside namespace boxes, laid out in two elk passes so a big namespace packs into rows instead of one tall column; zoom is capped so the view no longer jumps. Details panel has Overview / Links / YAML / Logs tabs. Light and dark themes. Libraries are vendored, no CDN. Backend: /api/flows reads recent flows from hubble-relay (TLS verified with the hubble CA, no client cert needed) and aggregates them per source/destination/port. Pods are resolved to workloads with the same rules as the graph, since Hubble leaves Workloads empty on some flows. Pods under non-native controllers (kruise DaemonSet, CNPG clusters, bare Jobs, static pods) are now graph nodes too. Probes move to /healthz: /api/graph was timing out the 1s probe. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
2ac0ddb6ba
commit
cf84e9976f
@@ -63,7 +63,14 @@ func main() {
|
||||
log.Fatalf("building clientset: %v", err)
|
||||
}
|
||||
|
||||
initHubble()
|
||||
if hubbleInitErr != nil {
|
||||
log.Printf("hubble disabled: %v", hubbleInitErr)
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) { w.Write([]byte("ok")) })
|
||||
mux.HandleFunc("/api/flows", handleFlows)
|
||||
mux.HandleFunc("/api/graph", handleGraph)
|
||||
mux.HandleFunc("/api/resource", handleResource)
|
||||
mux.HandleFunc("/api/pods", handlePods)
|
||||
@@ -166,6 +173,64 @@ func buildGraph(ctx context.Context) (*Graph, error) {
|
||||
// CronJob pods don't back a Service, so no labels needed for matching.
|
||||
}
|
||||
|
||||
// Pods whose controller is not a native workload (kruise Advanced
|
||||
// DaemonSet behind ingress-nginx, database operators, bare Jobs) would
|
||||
// otherwise be invisible. They become workload nodes of their own, IDed
|
||||
// the way Hubble names workloads, so traffic attaches to them too.
|
||||
pods, err := clientset.CoreV1().Pods(corev1.NamespaceAll).List(ctx, metav1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
known := map[string]bool{}
|
||||
for _, n := range g.Nodes {
|
||||
known[n.ID] = true
|
||||
}
|
||||
type foreignWl struct {
|
||||
ns, kind, name, controller string
|
||||
pods, ready int
|
||||
labels map[string]string
|
||||
}
|
||||
foreign := map[string]*foreignWl{}
|
||||
for _, p := range pods.Items {
|
||||
kind, name, ctrl := podOwner(p)
|
||||
if kind == "" {
|
||||
continue
|
||||
}
|
||||
id := workloadID(p.Namespace, kind, name)
|
||||
if known[id] {
|
||||
continue
|
||||
}
|
||||
f := foreign[id]
|
||||
if f == nil {
|
||||
f = &foreignWl{ns: p.Namespace, kind: kind, name: name, controller: ctrl, labels: p.Labels}
|
||||
foreign[id] = f
|
||||
}
|
||||
if p.Status.Phase == corev1.PodSucceeded {
|
||||
continue
|
||||
}
|
||||
f.pods++
|
||||
if podReady(p) {
|
||||
f.ready++
|
||||
}
|
||||
}
|
||||
for id, f := range foreign {
|
||||
kind := f.kind
|
||||
switch kind {
|
||||
case "deployment", "statefulset", "daemonset", "cronjob":
|
||||
default:
|
||||
kind = "pod"
|
||||
}
|
||||
n := workloadNode(id, kind, f.ns, f.name, map[string]string{
|
||||
"controller": f.controller,
|
||||
"pods": itoa(f.pods),
|
||||
"ready": itoa(f.ready),
|
||||
"health": health(f.pods, f.ready),
|
||||
"foreign": "true",
|
||||
})
|
||||
g.Nodes = append(g.Nodes, n)
|
||||
workloads = append(workloads, workloadRef{id, f.labels})
|
||||
}
|
||||
|
||||
services, err := clientset.CoreV1().Services(corev1.NamespaceAll).List(ctx, metav1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -214,10 +279,6 @@ func buildGraph(ctx context.Context) (*Graph, error) {
|
||||
g.Nodes = append(g.Nodes, Node{ID: "node/" + n.Name, Label: n.Name, Kind: "node", Facts: nodeFacts(n)})
|
||||
}
|
||||
|
||||
pods, err := clientset.CoreV1().Pods(corev1.NamespaceAll).List(ctx, metav1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
seenPlacement := map[string]bool{}
|
||||
for _, p := range pods.Items {
|
||||
if p.Spec.NodeName == "" {
|
||||
@@ -240,31 +301,69 @@ func buildGraph(ctx context.Context) (*Graph, error) {
|
||||
return g, nil
|
||||
}
|
||||
|
||||
// ownerWorkloadID walks a Pod's ownerReferences to find the workload node ID
|
||||
// this pod belongs to. ReplicaSet-owned pods (Deployments) are attributed to
|
||||
// the Deployment, not the intermediate ReplicaSet, since we don't graph
|
||||
// ReplicaSets as their own nodes.
|
||||
func ownerWorkloadID(p corev1.Pod) string {
|
||||
for _, o := range p.OwnerReferences {
|
||||
switch o.Kind {
|
||||
case "StatefulSet":
|
||||
return workloadID(p.Namespace, "statefulset", o.Name)
|
||||
case "DaemonSet":
|
||||
return workloadID(p.Namespace, "daemonset", o.Name)
|
||||
case "ReplicaSet":
|
||||
// name is "<deployment>-<hash>" by convention; the owning
|
||||
// Deployment isn't in the Pod's own ownerReferences, so this is
|
||||
// the standard heuristic (kubectl itself does the same thing).
|
||||
if i := strings.LastIndex(o.Name, "-"); i > 0 {
|
||||
return workloadID(p.Namespace, "deployment", o.Name[:i])
|
||||
}
|
||||
case "Job":
|
||||
if i := strings.LastIndex(o.Name, "-"); i > 0 {
|
||||
return workloadID(p.Namespace, "cronjob", o.Name[:i])
|
||||
}
|
||||
// podOwner names the workload a pod belongs to the way Hubble does
|
||||
// (cilium's GetWorkloadMetaFromPod): ReplicaSet -> its Deployment, a Job
|
||||
// whose name ends in the scheduler's numeric suffix -> its CronJob, any
|
||||
// other controller kind as-is. controller is "<apiVersion> <Kind>".
|
||||
func podOwner(p corev1.Pod) (kind, name, controller string) {
|
||||
var o *metav1.OwnerReference
|
||||
for i := range p.OwnerReferences {
|
||||
if p.OwnerReferences[i].Controller != nil && *p.OwnerReferences[i].Controller {
|
||||
o = &p.OwnerReferences[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
return ""
|
||||
if o == nil && len(p.OwnerReferences) > 0 {
|
||||
o = &p.OwnerReferences[0]
|
||||
}
|
||||
if o == nil {
|
||||
return "", "", ""
|
||||
}
|
||||
controller = o.APIVersion + " " + o.Kind
|
||||
switch o.Kind {
|
||||
case "Node":
|
||||
// static pod mirrored by the kubelet: "<component>-<node>", grouped
|
||||
// per component across nodes like a DaemonSet would be
|
||||
return "staticpod", strings.TrimSuffix(p.Name, "-"+p.Spec.NodeName), "static pod (kubelet)"
|
||||
case "ReplicaSet":
|
||||
if i := strings.LastIndex(o.Name, "-"); i > 0 {
|
||||
return "deployment", o.Name[:i], controller
|
||||
}
|
||||
case "Job":
|
||||
if i := strings.LastIndex(o.Name, "-"); i > 0 && allDigits(o.Name[i+1:]) {
|
||||
return "cronjob", o.Name[:i], controller
|
||||
}
|
||||
}
|
||||
return strings.ToLower(o.Kind), o.Name, controller
|
||||
}
|
||||
|
||||
func allDigits(s string) bool {
|
||||
if s == "" {
|
||||
return false
|
||||
}
|
||||
for _, c := range s {
|
||||
if c < '0' || c > '9' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func podReady(p corev1.Pod) bool {
|
||||
for _, c := range p.Status.Conditions {
|
||||
if c.Type == corev1.PodReady {
|
||||
return c.Status == corev1.ConditionTrue
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func ownerWorkloadID(p corev1.Pod) string {
|
||||
kind, name, _ := podOwner(p)
|
||||
if kind == "" {
|
||||
return ""
|
||||
}
|
||||
return workloadID(p.Namespace, kind, name)
|
||||
}
|
||||
|
||||
func workloadID(ns, kind, name string) string { return "wl/" + ns + "/" + kind + "/" + name }
|
||||
@@ -312,11 +411,33 @@ func firstContainerImage(containers []corev1.Container) string {
|
||||
return containers[0].Image
|
||||
}
|
||||
|
||||
// health is what the status dot on a card shows: ok, warn (partly ready),
|
||||
// error (nothing ready), idle (scaled to zero or suspended).
|
||||
func health(want, ready int) string {
|
||||
switch {
|
||||
case want == 0:
|
||||
return "idle"
|
||||
case ready >= want:
|
||||
return "ok"
|
||||
case ready == 0:
|
||||
return "error"
|
||||
}
|
||||
return "warn"
|
||||
}
|
||||
|
||||
func replicasOf(p *int32) int {
|
||||
if p == nil {
|
||||
return 1
|
||||
}
|
||||
return int(*p)
|
||||
}
|
||||
|
||||
func deploymentFacts(d appsv1.Deployment) map[string]string {
|
||||
return map[string]string{
|
||||
"replicas": itoa32(d.Spec.Replicas),
|
||||
"replicas": itoa32(d.Spec.Replicas),
|
||||
"readyReplicas": itoa(int(d.Status.ReadyReplicas)),
|
||||
"image": firstContainerImage(d.Spec.Template.Spec.Containers),
|
||||
"image": firstContainerImage(d.Spec.Template.Spec.Containers),
|
||||
"health": health(replicasOf(d.Spec.Replicas), int(d.Status.ReadyReplicas)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -325,6 +446,7 @@ func statefulsetFacts(s appsv1.StatefulSet) map[string]string {
|
||||
"replicas": itoa32(s.Spec.Replicas),
|
||||
"readyReplicas": itoa(int(s.Status.ReadyReplicas)),
|
||||
"image": firstContainerImage(s.Spec.Template.Spec.Containers),
|
||||
"health": health(replicasOf(s.Spec.Replicas), int(s.Status.ReadyReplicas)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -333,13 +455,18 @@ func daemonsetFacts(ds appsv1.DaemonSet) map[string]string {
|
||||
"desired": itoa(int(ds.Status.DesiredNumberScheduled)),
|
||||
"ready": itoa(int(ds.Status.NumberReady)),
|
||||
"image": firstContainerImage(ds.Spec.Template.Spec.Containers),
|
||||
"health": health(int(ds.Status.DesiredNumberScheduled), int(ds.Status.NumberReady)),
|
||||
}
|
||||
}
|
||||
|
||||
func cronjobFacts(cj batchv1.CronJob) map[string]string {
|
||||
f := map[string]string{"schedule": cj.Spec.Schedule}
|
||||
f := map[string]string{"schedule": cj.Spec.Schedule, "health": "ok", "active": itoa(len(cj.Status.Active))}
|
||||
if cj.Spec.Suspend != nil && *cj.Spec.Suspend {
|
||||
f["suspended"] = "true"
|
||||
f["health"] = "idle"
|
||||
}
|
||||
if cj.Status.LastSuccessfulTime != nil {
|
||||
f["lastSuccess"] = cj.Status.LastSuccessfulTime.UTC().Format("2006-01-02 15:04")
|
||||
}
|
||||
return f
|
||||
}
|
||||
@@ -373,7 +500,11 @@ func nodeFacts(n corev1.Node) map[string]string {
|
||||
ready = string(c.Status)
|
||||
}
|
||||
}
|
||||
return map[string]string{"ready": ready, "os": n.Status.NodeInfo.OSImage}
|
||||
h := "ok"
|
||||
if ready != "True" {
|
||||
h = "error"
|
||||
}
|
||||
return map[string]string{"ready": ready, "os": n.Status.NodeInfo.OSImage, "kubelet": n.Status.NodeInfo.KubeletVersion, "health": h}
|
||||
}
|
||||
|
||||
func itoa(i int) string {
|
||||
@@ -452,8 +583,7 @@ func handleResource(w http.ResponseWriter, r *http.Request) {
|
||||
// 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)
|
||||
func podsForWorkload(ctx context.Context, ns, target string) ([]corev1.Pod, error) {
|
||||
pods, err := clientset.CoreV1().Pods(ns).List(ctx, metav1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -480,14 +610,13 @@ type PodInfo struct {
|
||||
// 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")
|
||||
id := r.URL.Query().Get("id")
|
||||
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)
|
||||
if ns == "" || !strings.HasPrefix(id, "wl/"+ns+"/") {
|
||||
http.Error(w, "namespace and a workload id (wl/<ns>/<kind>/<name>) are required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
pods, err := podsForWorkload(r.Context(), ns, kind, name)
|
||||
pods, err := podsForWorkload(r.Context(), ns, id)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user