// 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) } 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" 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. } 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)}) } 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 == "" { 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 } // 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 "-" 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]) } } } return "" } 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 } 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), } } 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), } } 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), } } func cronjobFacts(cj batchv1.CronJob) map[string]string { f := map[string]string{"schedule": cj.Spec.Schedule} if cj.Spec.Suspend != nil && *cj.Spec.Suspend { f["suspended"] = "true" } 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) } } return map[string]string{"ready": ready, "os": n.Status.NodeInfo.OSImage} } 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, 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