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>
662 lines
20 KiB
Go
662 lines
20 KiB
Go
// cluster-map: a live topology view of a Deckhouse/Kubernetes cluster.
|
|
//
|
|
// Builds a graph from the live k8s API on every request to /api/graph:
|
|
// namespaces group workloads (Deployment/StatefulSet/DaemonSet/CronJob),
|
|
// which connect to Services (by label-selector match) and Ingresses (by
|
|
// backend reference). Nothing is cached or watched -- this is meant to
|
|
// be a small, low-traffic internal tool, not a high-QPS dashboard, so a
|
|
// fresh List() per request is simpler and correct-by-construction (no
|
|
// stale-cache class of bugs to worry about).
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"sort"
|
|
"strings"
|
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
|
batchv1 "k8s.io/api/batch/v1"
|
|
corev1 "k8s.io/api/core/v1"
|
|
networkingv1 "k8s.io/api/networking/v1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/client-go/kubernetes"
|
|
"k8s.io/client-go/rest"
|
|
"sigs.k8s.io/yaml"
|
|
)
|
|
|
|
type Node struct {
|
|
ID string `json:"id"`
|
|
Label string `json:"label"`
|
|
Kind string `json:"kind"` // namespace | deployment | statefulset | daemonset | cronjob | service | ingress | node
|
|
Namespace string `json:"namespace"` // empty for namespace/node kind
|
|
Parent string `json:"parent,omitempty"`
|
|
NodeName string `json:"nodeName,omitempty"` // physical node a workload's pods land on, if known
|
|
Facts map[string]string `json:"facts,omitempty"` // short summary shown in the sidebar without a follow-up fetch
|
|
}
|
|
|
|
type Edge struct {
|
|
ID string `json:"id"`
|
|
Source string `json:"source"`
|
|
Target string `json:"target"`
|
|
Kind string `json:"kind"` // selects | routes | schedules-on
|
|
}
|
|
|
|
type Graph struct {
|
|
Nodes []Node `json:"nodes"`
|
|
Edges []Edge `json:"edges"`
|
|
}
|
|
|
|
var clientset *kubernetes.Clientset
|
|
|
|
func main() {
|
|
cfg, err := rest.InClusterConfig()
|
|
if err != nil {
|
|
log.Fatalf("not running in-cluster and no kubeconfig fallback wired up: %v", err)
|
|
}
|
|
clientset, err = kubernetes.NewForConfig(cfg)
|
|
if err != nil {
|
|
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)
|
|
mux.HandleFunc("/api/logs", handleLogs)
|
|
mux.Handle("/", http.FileServer(http.Dir("./static")))
|
|
|
|
addr := ":8080"
|
|
log.Printf("cluster-map listening on %s", addr)
|
|
log.Fatal(http.ListenAndServe(addr, mux))
|
|
}
|
|
|
|
func handleGraph(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
g, err := buildGraph(ctx)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(g)
|
|
}
|
|
|
|
// selectorSubset reports whether every key/value in sel is present in labels --
|
|
// the standard Kubernetes rule for whether a Service's spec.selector matches a
|
|
// pod template's labels.
|
|
func selectorSubset(sel, labels map[string]string) bool {
|
|
if len(sel) == 0 {
|
|
return false // an empty selector matches everything in real k8s, but showing an edge to "everything" is useless noise here
|
|
}
|
|
for k, v := range sel {
|
|
if labels[k] != v {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func buildGraph(ctx context.Context) (*Graph, error) {
|
|
g := &Graph{}
|
|
|
|
namespaces, err := clientset.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, ns := range namespaces.Items {
|
|
system := strings.HasPrefix(ns.Name, "d8-") || ns.Name == "kube-system" || ns.Name == "kube-public" || ns.Name == "kube-node-lease"
|
|
g.Nodes = append(g.Nodes, Node{
|
|
ID: "ns/" + ns.Name,
|
|
Label: ns.Name,
|
|
Kind: "namespace",
|
|
Facts: map[string]string{"system": boolStr(system)},
|
|
})
|
|
}
|
|
|
|
// pod-template-labels indexed by namespace, used to match Services to
|
|
// their owning workload below.
|
|
type workloadRef struct {
|
|
id string
|
|
labels map[string]string
|
|
}
|
|
var workloads []workloadRef
|
|
|
|
deployments, err := clientset.AppsV1().Deployments(corev1.NamespaceAll).List(ctx, metav1.ListOptions{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, d := range deployments.Items {
|
|
id := workloadID(d.Namespace, "deployment", d.Name)
|
|
g.Nodes = append(g.Nodes, workloadNode(id, "deployment", d.Namespace, d.Name, deploymentFacts(d)))
|
|
workloads = append(workloads, workloadRef{id, d.Spec.Template.Labels})
|
|
}
|
|
|
|
statefulsets, err := clientset.AppsV1().StatefulSets(corev1.NamespaceAll).List(ctx, metav1.ListOptions{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, s := range statefulsets.Items {
|
|
id := workloadID(s.Namespace, "statefulset", s.Name)
|
|
g.Nodes = append(g.Nodes, workloadNode(id, "statefulset", s.Namespace, s.Name, statefulsetFacts(s)))
|
|
workloads = append(workloads, workloadRef{id, s.Spec.Template.Labels})
|
|
}
|
|
|
|
daemonsets, err := clientset.AppsV1().DaemonSets(corev1.NamespaceAll).List(ctx, metav1.ListOptions{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, ds := range daemonsets.Items {
|
|
id := workloadID(ds.Namespace, "daemonset", ds.Name)
|
|
g.Nodes = append(g.Nodes, workloadNode(id, "daemonset", ds.Namespace, ds.Name, daemonsetFacts(ds)))
|
|
workloads = append(workloads, workloadRef{id, ds.Spec.Template.Labels})
|
|
}
|
|
|
|
cronjobs, err := clientset.BatchV1().CronJobs(corev1.NamespaceAll).List(ctx, metav1.ListOptions{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, cj := range cronjobs.Items {
|
|
id := workloadID(cj.Namespace, "cronjob", cj.Name)
|
|
g.Nodes = append(g.Nodes, workloadNode(id, "cronjob", cj.Namespace, cj.Name, cronjobFacts(cj)))
|
|
// 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
|
|
}
|
|
for _, svc := range services.Items {
|
|
id := "service/" + svc.Namespace + "/" + svc.Name
|
|
g.Nodes = append(g.Nodes, Node{
|
|
ID: id, Label: svc.Name, Kind: "service", Namespace: svc.Namespace, Parent: "ns/" + svc.Namespace,
|
|
Facts: serviceFacts(svc),
|
|
})
|
|
for _, wl := range workloads {
|
|
if !strings.HasPrefix(wl.id, "wl/"+svc.Namespace+"/") {
|
|
continue
|
|
}
|
|
if selectorSubset(svc.Spec.Selector, wl.labels) {
|
|
g.Edges = append(g.Edges, Edge{ID: wl.id + "->" + id, Source: wl.id, Target: id, Kind: "selects"})
|
|
}
|
|
}
|
|
}
|
|
|
|
ingresses, err := clientset.NetworkingV1().Ingresses(corev1.NamespaceAll).List(ctx, metav1.ListOptions{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, ing := range ingresses.Items {
|
|
id := "ingress/" + ing.Namespace + "/" + ing.Name
|
|
g.Nodes = append(g.Nodes, Node{
|
|
ID: id, Label: ing.Name, Kind: "ingress", Namespace: ing.Namespace, Parent: "ns/" + ing.Namespace,
|
|
Facts: ingressFacts(ing),
|
|
})
|
|
for _, svcName := range ingressBackendServices(ing) {
|
|
target := "service/" + ing.Namespace + "/" + svcName
|
|
g.Edges = append(g.Edges, Edge{ID: id + "->" + target, Source: id, Target: target, Kind: "routes"})
|
|
}
|
|
}
|
|
|
|
// Physical nodes as a separate, non-namespaced layer, linked from each
|
|
// workload to wherever its pods actually run right now -- this is the
|
|
// "what's deployed where" question specifically, not covered by the
|
|
// Service/Ingress graph above.
|
|
nodes, err := clientset.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, n := range nodes.Items {
|
|
g.Nodes = append(g.Nodes, Node{ID: "node/" + n.Name, Label: n.Name, Kind: "node", Facts: nodeFacts(n)})
|
|
}
|
|
|
|
seenPlacement := map[string]bool{}
|
|
for _, p := range pods.Items {
|
|
if p.Spec.NodeName == "" {
|
|
continue
|
|
}
|
|
ownerWl := ownerWorkloadID(p)
|
|
if ownerWl == "" {
|
|
continue
|
|
}
|
|
key := ownerWl + "@" + p.Spec.NodeName
|
|
if seenPlacement[key] {
|
|
continue
|
|
}
|
|
seenPlacement[key] = true
|
|
g.Edges = append(g.Edges, Edge{ID: key, Source: ownerWl, Target: "node/" + p.Spec.NodeName, Kind: "schedules-on"})
|
|
}
|
|
|
|
sort.Slice(g.Nodes, func(i, j int) bool { return g.Nodes[i].ID < g.Nodes[j].ID })
|
|
sort.Slice(g.Edges, func(i, j int) bool { return g.Edges[i].ID < g.Edges[j].ID })
|
|
return g, nil
|
|
}
|
|
|
|
// 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
|
|
}
|
|
}
|
|
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 }
|
|
|
|
func workloadNode(id, kind, ns, name string, facts map[string]string) Node {
|
|
return Node{ID: id, Label: name, Kind: kind, Namespace: ns, Parent: "ns/" + ns, Facts: facts}
|
|
}
|
|
|
|
func boolStr(b bool) string {
|
|
if b {
|
|
return "true"
|
|
}
|
|
return "false"
|
|
}
|
|
|
|
func ingressBackendServices(ing networkingv1.Ingress) []string {
|
|
seen := map[string]bool{}
|
|
var out []string
|
|
add := func(name string) {
|
|
if name != "" && !seen[name] {
|
|
seen[name] = true
|
|
out = append(out, name)
|
|
}
|
|
}
|
|
if ing.Spec.DefaultBackend != nil && ing.Spec.DefaultBackend.Service != nil {
|
|
add(ing.Spec.DefaultBackend.Service.Name)
|
|
}
|
|
for _, rule := range ing.Spec.Rules {
|
|
if rule.HTTP == nil {
|
|
continue
|
|
}
|
|
for _, path := range rule.HTTP.Paths {
|
|
if path.Backend.Service != nil {
|
|
add(path.Backend.Service.Name)
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func firstContainerImage(containers []corev1.Container) string {
|
|
if len(containers) == 0 {
|
|
return ""
|
|
}
|
|
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),
|
|
"readyReplicas": itoa(int(d.Status.ReadyReplicas)),
|
|
"image": firstContainerImage(d.Spec.Template.Spec.Containers),
|
|
"health": health(replicasOf(d.Spec.Replicas), int(d.Status.ReadyReplicas)),
|
|
}
|
|
}
|
|
|
|
func statefulsetFacts(s appsv1.StatefulSet) map[string]string {
|
|
return 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)),
|
|
}
|
|
}
|
|
|
|
func daemonsetFacts(ds appsv1.DaemonSet) map[string]string {
|
|
return 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, "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
|
|
}
|
|
|
|
func serviceFacts(svc corev1.Service) map[string]string {
|
|
var ports []string
|
|
for _, p := range svc.Spec.Ports {
|
|
ports = append(ports, itoa(int(p.Port))+"/"+string(p.Protocol))
|
|
}
|
|
return map[string]string{"type": string(svc.Spec.Type), "ports": strings.Join(ports, ", ")}
|
|
}
|
|
|
|
func ingressFacts(ing networkingv1.Ingress) map[string]string {
|
|
var hosts []string
|
|
for _, r := range ing.Spec.Rules {
|
|
if r.Host != "" {
|
|
hosts = append(hosts, r.Host)
|
|
}
|
|
}
|
|
class := ""
|
|
if ing.Spec.IngressClassName != nil {
|
|
class = *ing.Spec.IngressClassName
|
|
}
|
|
return map[string]string{"hosts": strings.Join(hosts, ", "), "class": class}
|
|
}
|
|
|
|
func nodeFacts(n corev1.Node) map[string]string {
|
|
ready := "False"
|
|
for _, c := range n.Status.Conditions {
|
|
if c.Type == corev1.NodeReady {
|
|
ready = string(c.Status)
|
|
}
|
|
}
|
|
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 {
|
|
if i == 0 {
|
|
return "0"
|
|
}
|
|
neg := i < 0
|
|
if neg {
|
|
i = -i
|
|
}
|
|
var b []byte
|
|
for i > 0 {
|
|
b = append([]byte{byte('0' + i%10)}, b...)
|
|
i /= 10
|
|
}
|
|
if neg {
|
|
b = append([]byte{'-'}, b...)
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
func itoa32(p *int32) string {
|
|
if p == nil {
|
|
return "?"
|
|
}
|
|
return itoa(int(*p))
|
|
}
|
|
|
|
// handleResource returns the raw manifest (as YAML, easiest to read) for a
|
|
// single graph node, fetched fresh -- not cached alongside the graph, since
|
|
// most nodes are never clicked and a full spec dump for every workload on
|
|
// every /api/graph call would be wasteful.
|
|
func handleResource(w http.ResponseWriter, r *http.Request) {
|
|
kind := r.URL.Query().Get("kind")
|
|
ns := r.URL.Query().Get("namespace")
|
|
name := r.URL.Query().Get("name")
|
|
ctx := r.Context()
|
|
|
|
var obj interface{}
|
|
var err error
|
|
switch kind {
|
|
case "namespace":
|
|
obj, err = clientset.CoreV1().Namespaces().Get(ctx, name, metav1.GetOptions{})
|
|
case "deployment":
|
|
obj, err = clientset.AppsV1().Deployments(ns).Get(ctx, name, metav1.GetOptions{})
|
|
case "statefulset":
|
|
obj, err = clientset.AppsV1().StatefulSets(ns).Get(ctx, name, metav1.GetOptions{})
|
|
case "daemonset":
|
|
obj, err = clientset.AppsV1().DaemonSets(ns).Get(ctx, name, metav1.GetOptions{})
|
|
case "cronjob":
|
|
obj, err = clientset.BatchV1().CronJobs(ns).Get(ctx, name, metav1.GetOptions{})
|
|
case "service":
|
|
obj, err = clientset.CoreV1().Services(ns).Get(ctx, name, metav1.GetOptions{})
|
|
case "ingress":
|
|
obj, err = clientset.NetworkingV1().Ingresses(ns).Get(ctx, name, metav1.GetOptions{})
|
|
case "node":
|
|
obj, err = clientset.CoreV1().Nodes().Get(ctx, name, metav1.GetOptions{})
|
|
default:
|
|
http.Error(w, "unknown kind: "+kind, http.StatusBadRequest)
|
|
return
|
|
}
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusNotFound)
|
|
return
|
|
}
|
|
out, err := yaml.Marshal(obj)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
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, target string) ([]corev1.Pod, error) {
|
|
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) {
|
|
id := r.URL.Query().Get("id")
|
|
ns := r.URL.Query().Get("namespace")
|
|
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, id)
|
|
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
|