initial: graph backend (client-go) + cytoscape.js topology frontend
Namespaces as compound parents, Deployment/StatefulSet/DaemonSet/CronJob as workload nodes, Service/Ingress with edges derived from label-selector matching and backend references, plus a physical-node layer via pod placement. Click-through to live YAML manifest per node.
This commit is contained in:
@@ -0,0 +1,2 @@
|
|||||||
|
/cluster-map
|
||||||
|
*.log
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
FROM golang:1.22-alpine AS build
|
||||||
|
WORKDIR /src
|
||||||
|
COPY go.mod go.sum* ./
|
||||||
|
RUN go mod download
|
||||||
|
COPY main.go ./
|
||||||
|
RUN CGO_ENABLED=0 go build -o /cluster-map .
|
||||||
|
|
||||||
|
FROM alpine:3.20
|
||||||
|
RUN adduser -D -u 10001 app
|
||||||
|
COPY --from=build /cluster-map /cluster-map
|
||||||
|
COPY static /static
|
||||||
|
USER app
|
||||||
|
WORKDIR /
|
||||||
|
ENTRYPOINT ["/cluster-map"]
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
# cluster-map
|
||||||
|
|
||||||
|
Interactive topology view of the Project Chimera Deckhouse cluster --
|
||||||
|
namespaces group workloads (Deployment/StatefulSet/DaemonSet/CronJob),
|
||||||
|
connected to Services (by label-selector match) and Ingresses (by backend
|
||||||
|
reference), plus a physical-node layer showing what's actually scheduled
|
||||||
|
where. Click any node for its live manifest.
|
||||||
|
|
||||||
|
Go backend (client-go, builds the graph fresh from the k8s API on every
|
||||||
|
`/api/graph` request -- no caching, no watch loop, correct by construction)
|
||||||
|
+ a single-page Cytoscape.js frontend, no build step.
|
||||||
|
|
||||||
|
Phase 1 (current): plain in-cluster app, deployed like everything else in
|
||||||
|
this project (`03-kubernetes/apps/cluster-map/` in the `project-chimera`
|
||||||
|
docs repo, mirrored to `k8s-manifests` for Argo). No Hubble traffic overlay
|
||||||
|
yet, no packaging as an actual Deckhouse module (ModuleConfig/ModuleSource)
|
||||||
|
-- both are deliberately deferred until the base idea proves useful.
|
||||||
|
|
||||||
|
See STATUS.md / OpenProject (chimera-core) in `project-chimera` for the
|
||||||
|
full design rationale and decision history.
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
module git.project-chimera.ru/fanchji/cluster-map
|
||||||
|
|
||||||
|
go 1.22.0
|
||||||
|
|
||||||
|
require (
|
||||||
|
k8s.io/api v0.30.3
|
||||||
|
k8s.io/apimachinery v0.30.3
|
||||||
|
k8s.io/client-go v0.30.3
|
||||||
|
sigs.k8s.io/yaml v1.4.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
|
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
|
||||||
|
github.com/go-logr/logr v1.4.1 // indirect
|
||||||
|
github.com/go-openapi/jsonpointer v0.19.6 // indirect
|
||||||
|
github.com/go-openapi/jsonreference v0.20.2 // indirect
|
||||||
|
github.com/go-openapi/swag v0.22.3 // indirect
|
||||||
|
github.com/gogo/protobuf v1.3.2 // indirect
|
||||||
|
github.com/golang/protobuf v1.5.4 // indirect
|
||||||
|
github.com/google/gnostic-models v0.6.8 // indirect
|
||||||
|
github.com/google/gofuzz v1.2.0 // indirect
|
||||||
|
github.com/google/uuid v1.3.0 // indirect
|
||||||
|
github.com/josharian/intern v1.0.0 // indirect
|
||||||
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
|
github.com/mailru/easyjson v0.7.7 // indirect
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||||
|
golang.org/x/net v0.23.0 // indirect
|
||||||
|
golang.org/x/oauth2 v0.10.0 // indirect
|
||||||
|
golang.org/x/sys v0.18.0 // indirect
|
||||||
|
golang.org/x/term v0.18.0 // indirect
|
||||||
|
golang.org/x/text v0.14.0 // indirect
|
||||||
|
golang.org/x/time v0.3.0 // indirect
|
||||||
|
google.golang.org/appengine v1.6.7 // indirect
|
||||||
|
google.golang.org/protobuf v1.33.0 // indirect
|
||||||
|
gopkg.in/inf.v0 v0.9.1 // indirect
|
||||||
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
k8s.io/klog/v2 v2.120.1 // indirect
|
||||||
|
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
|
||||||
|
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
|
||||||
|
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
|
||||||
|
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
|
||||||
|
)
|
||||||
@@ -0,0 +1,150 @@
|
|||||||
|
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g=
|
||||||
|
github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
|
||||||
|
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
|
||||||
|
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||||
|
github.com/go-openapi/jsonpointer v0.19.6 h1:eCs3fxoIi3Wh6vtgmLTOjdhSpiqphQ+DaPn38N2ZdrE=
|
||||||
|
github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs=
|
||||||
|
github.com/go-openapi/jsonreference v0.20.2 h1:3sVjiK66+uXK/6oQ8xgcRKcFgQ5KXa2KvnJRumpMGbE=
|
||||||
|
github.com/go-openapi/jsonreference v0.20.2/go.mod h1:Bl1zwGIM8/wsvqjsOQLJ/SH+En5Ap4rVB5KVcIDZG2k=
|
||||||
|
github.com/go-openapi/swag v0.22.3 h1:yMBqmnQ0gyZvEb/+KzuWZOXgllrXT4SADYbvDaXHv/g=
|
||||||
|
github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
|
||||||
|
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
|
||||||
|
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
|
||||||
|
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
|
||||||
|
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
||||||
|
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
|
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
|
||||||
|
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
||||||
|
github.com/google/gnostic-models v0.6.8 h1:yo/ABAfM5IMRsS1VnXjTBvUb61tFIHozhlYvRgGre9I=
|
||||||
|
github.com/google/gnostic-models v0.6.8/go.mod h1:5n7qKqH0f5wFt+aWF8CW6pZLLNOfYuF5OpfBSENuI8U=
|
||||||
|
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
|
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
|
||||||
|
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
|
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 h1:K6RDEckDVWvDI9JAJYCmNdQXq6neHJOYx3V6jnqNEec=
|
||||||
|
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
|
||||||
|
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||||
|
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
|
||||||
|
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
|
||||||
|
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||||
|
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||||
|
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
|
||||||
|
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||||
|
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||||
|
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||||
|
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||||
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
|
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
|
||||||
|
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||||
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
|
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||||
|
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||||
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
|
||||||
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||||
|
github.com/onsi/ginkgo/v2 v2.15.0 h1:79HwNRBAZHOEwrczrgSOPy+eFTTlIGELKy5as+ClttY=
|
||||||
|
github.com/onsi/ginkgo/v2 v2.15.0/go.mod h1:HlxMHtYF57y6Dpf+mc5529KKmSq9h2FpCF+/ZkwUxKM=
|
||||||
|
github.com/onsi/gomega v1.31.0 h1:54UJxxj6cPInHS3a35wm6BK/F9nHYueZ1NVujHDrnXE=
|
||||||
|
github.com/onsi/gomega v1.31.0/go.mod h1:DW9aCi7U6Yi40wNVAvT6kzFnEVEI5n3DloYBiKiT6zk=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
|
||||||
|
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
|
||||||
|
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||||
|
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
|
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||||
|
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||||
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
|
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||||
|
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||||
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
|
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||||
|
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||||
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
|
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
|
||||||
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||||
|
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
|
||||||
|
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
|
||||||
|
golang.org/x/oauth2 v0.10.0 h1:zHCpF2Khkwy4mMB4bv0U37YtJdTGW8jI0glAApi0Kh8=
|
||||||
|
golang.org/x/oauth2 v0.10.0/go.mod h1:kTpgurOux7LqtuxjuyZa4Gj2gdezIt/jQtGnNFfypQI=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
||||||
|
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
|
||||||
|
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||||
|
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
|
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
|
||||||
|
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
|
||||||
|
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||||
|
golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ=
|
||||||
|
golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg=
|
||||||
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
|
||||||
|
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
|
||||||
|
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
|
||||||
|
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||||
|
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
|
||||||
|
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
|
||||||
|
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||||
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
k8s.io/api v0.30.3 h1:ImHwK9DCsPA9uoU3rVh4QHAHHK5dTSv1nxJUapx8hoQ=
|
||||||
|
k8s.io/api v0.30.3/go.mod h1:GPc8jlzoe5JG3pb0KJCSLX5oAFIW3/qNJITlDj8BH04=
|
||||||
|
k8s.io/apimachinery v0.30.3 h1:q1laaWCmrszyQuSQCfNB8cFgCuDAoPszKY4ucAjDwHc=
|
||||||
|
k8s.io/apimachinery v0.30.3/go.mod h1:iexa2somDaxdnj7bha06bhb43Zpa6eWH8N8dbqVjTUc=
|
||||||
|
k8s.io/client-go v0.30.3 h1:bHrJu3xQZNXIi8/MoxYtZBBWQQXwy16zqJwloXXfD3k=
|
||||||
|
k8s.io/client-go v0.30.3/go.mod h1:8d4pf8vYu665/kUbsxWAQ/JDBNWqfFeZnvFiVdmx89U=
|
||||||
|
k8s.io/klog/v2 v2.120.1 h1:QXU6cPEOIslTGvZaXvFWiP9VKyeet3sawzTOvdXb4Vw=
|
||||||
|
k8s.io/klog/v2 v2.120.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE=
|
||||||
|
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 h1:BZqlfIlq5YbRMFko6/PM7FjZpUb45WallggurYhKGag=
|
||||||
|
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340/go.mod h1:yD4MZYeKMBwQKVht279WycxKyM84kkAx2DPrTXaeb98=
|
||||||
|
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=
|
||||||
|
k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
|
||||||
|
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
|
||||||
|
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
|
||||||
|
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 h1:150L+0vs/8DA78h1u02ooW1/fFq/Lwr+sGiqlzvrtq4=
|
||||||
|
sigs.k8s.io/structured-merge-diff/v4 v4.4.1/go.mod h1:N8hJocpFajUSSeSJ9bOZ77VzejKZaXsTtZo4/u7Io08=
|
||||||
|
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
|
||||||
|
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
|
||||||
@@ -0,0 +1,448 @@
|
|||||||
|
// 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"
|
||||||
|
"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.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 "<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])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = os.Stdout // keep the os import if logging grows; avoids an unused-import edit later
|
||||||
@@ -0,0 +1,183 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="ru">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>cluster-map</title>
|
||||||
|
<script src="https://unpkg.com/cytoscape@3.30.2/dist/cytoscape.min.js"></script>
|
||||||
|
<script src="https://unpkg.com/dagre@0.8.5/dist/dagre.min.js"></script>
|
||||||
|
<script src="https://unpkg.com/cytoscape-dagre@2.5.0/cytoscape-dagre.js"></script>
|
||||||
|
<style>
|
||||||
|
:root { color-scheme: dark; }
|
||||||
|
html, body { margin: 0; height: 100%; background: #0f1115; color: #e6e6e6; font: 13px/1.4 -apple-system, Segoe UI, sans-serif; }
|
||||||
|
#app { display: flex; height: 100%; }
|
||||||
|
#sidebar { width: 260px; flex: none; padding: 12px; border-right: 1px solid #262a33; overflow-y: auto; }
|
||||||
|
#graph { flex: 1; position: relative; }
|
||||||
|
#cy { position: absolute; inset: 0; }
|
||||||
|
#details { width: 420px; flex: none; padding: 12px; border-left: 1px solid #262a33; overflow-y: auto; display: none; }
|
||||||
|
#details.open { display: block; }
|
||||||
|
h1 { font-size: 15px; margin: 0 0 12px; }
|
||||||
|
h2 { font-size: 13px; margin: 0 0 6px; color: #8fb8ff; }
|
||||||
|
label { display: block; margin: 4px 0; cursor: pointer; }
|
||||||
|
input[type=checkbox] { margin-right: 6px; }
|
||||||
|
input[type=text] { width: 100%; box-sizing: border-box; padding: 6px; background: #1a1d24; border: 1px solid #333; color: #eee; border-radius: 4px; margin-bottom: 8px; }
|
||||||
|
pre { white-space: pre-wrap; word-break: break-word; font-size: 11px; background: #14161b; padding: 8px; border-radius: 4px; }
|
||||||
|
.fact { margin: 2px 0; }
|
||||||
|
.fact b { color: #9aa4b2; }
|
||||||
|
.legend-swatch { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: middle; }
|
||||||
|
#status { position: absolute; top: 8px; left: 8px; font-size: 11px; color: #666; }
|
||||||
|
button { background: #1a1d24; color: #eee; border: 1px solid #333; border-radius: 4px; padding: 4px 8px; cursor: pointer; margin-bottom: 8px; }
|
||||||
|
button:hover { background: #23262e; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="app">
|
||||||
|
<div id="sidebar">
|
||||||
|
<h1>cluster-map</h1>
|
||||||
|
<button id="reload">Обновить граф</button>
|
||||||
|
<h2>Namespaces</h2>
|
||||||
|
<label><input type="checkbox" id="show-system"> показать системные (d8-*, kube-*)</label>
|
||||||
|
<input type="text" id="ns-filter" placeholder="фильтр по имени namespace...">
|
||||||
|
<div id="ns-list"></div>
|
||||||
|
<h2 style="margin-top:16px">Легенда</h2>
|
||||||
|
<div class="fact"><span class="legend-swatch" style="background:#4c8bf5"></span>deployment</div>
|
||||||
|
<div class="fact"><span class="legend-swatch" style="background:#7a5cf0"></span>statefulset</div>
|
||||||
|
<div class="fact"><span class="legend-swatch" style="background:#c0567a"></span>daemonset</div>
|
||||||
|
<div class="fact"><span class="legend-swatch" style="background:#9a9a9a"></span>cronjob</div>
|
||||||
|
<div class="fact"><span class="legend-swatch" style="background:#37b37e"></span>service</div>
|
||||||
|
<div class="fact"><span class="legend-swatch" style="background:#e0a800"></span>ingress</div>
|
||||||
|
<div class="fact"><span class="legend-swatch" style="background:#2a2d36;border:1px solid #555"></span>node (физический)</div>
|
||||||
|
</div>
|
||||||
|
<div id="graph">
|
||||||
|
<div id="cy"></div>
|
||||||
|
<div id="status">загрузка...</div>
|
||||||
|
</div>
|
||||||
|
<div id="details">
|
||||||
|
<button id="close-details">закрыть</button>
|
||||||
|
<h2 id="details-title"></h2>
|
||||||
|
<div id="details-facts"></div>
|
||||||
|
<pre id="details-yaml">загрузка...</pre>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<script>
|
||||||
|
const KIND_COLOR = {
|
||||||
|
deployment: '#4c8bf5', statefulset: '#7a5cf0', daemonset: '#c0567a', cronjob: '#9a9a9a',
|
||||||
|
service: '#37b37e', ingress: '#e0a800', node: '#2a2d36',
|
||||||
|
};
|
||||||
|
const KIND_SHAPE = {
|
||||||
|
deployment: 'round-rectangle', statefulset: 'round-rectangle', daemonset: 'round-rectangle', cronjob: 'round-rectangle',
|
||||||
|
service: 'ellipse', ingress: 'diamond', node: 'hexagon',
|
||||||
|
};
|
||||||
|
|
||||||
|
let fullGraph = { nodes: [], edges: [] };
|
||||||
|
let cy;
|
||||||
|
|
||||||
|
async function loadGraph() {
|
||||||
|
document.getElementById('status').textContent = 'загрузка...';
|
||||||
|
const resp = await fetch('/api/graph');
|
||||||
|
fullGraph = await resp.json();
|
||||||
|
document.getElementById('status').textContent = `${fullGraph.nodes.length} узлов, ${fullGraph.edges.length} рёбер`;
|
||||||
|
buildNsList();
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildNsList() {
|
||||||
|
const el = document.getElementById('ns-list');
|
||||||
|
const showSystem = document.getElementById('show-system').checked;
|
||||||
|
const filterText = document.getElementById('ns-filter').value.toLowerCase();
|
||||||
|
const namespaces = fullGraph.nodes.filter(n => n.kind === 'namespace');
|
||||||
|
el.innerHTML = '';
|
||||||
|
namespaces
|
||||||
|
.filter(n => (showSystem || n.facts.system !== 'true') && n.label.toLowerCase().includes(filterText))
|
||||||
|
.sort((a, b) => a.label.localeCompare(b.label))
|
||||||
|
.forEach(n => {
|
||||||
|
const id = 'ns-cb-' + n.label;
|
||||||
|
const lbl = document.createElement('label');
|
||||||
|
lbl.innerHTML = `<input type="checkbox" class="ns-cb" data-ns="${n.label}" id="${id}" checked> ${n.label}`;
|
||||||
|
el.appendChild(lbl);
|
||||||
|
});
|
||||||
|
el.querySelectorAll('.ns-cb').forEach(cb => cb.addEventListener('change', render));
|
||||||
|
}
|
||||||
|
|
||||||
|
function selectedNamespaces() {
|
||||||
|
return new Set([...document.querySelectorAll('.ns-cb:checked')].map(cb => cb.dataset.ns));
|
||||||
|
}
|
||||||
|
|
||||||
|
function render() {
|
||||||
|
const selected = selectedNamespaces();
|
||||||
|
const visibleNodeIds = new Set();
|
||||||
|
const elements = [];
|
||||||
|
|
||||||
|
fullGraph.nodes.forEach(n => {
|
||||||
|
if (n.kind === 'namespace') {
|
||||||
|
if (!selected.has(n.label)) return;
|
||||||
|
} else if (n.kind === 'node') {
|
||||||
|
// physical nodes are always shown -- they're the "where" layer, not namespaced
|
||||||
|
} else {
|
||||||
|
if (n.namespace && !selected.has(n.namespace)) return;
|
||||||
|
}
|
||||||
|
visibleNodeIds.add(n.id);
|
||||||
|
elements.push({
|
||||||
|
group: 'nodes',
|
||||||
|
data: {
|
||||||
|
id: n.id, label: n.label, kind: n.kind, parent: n.kind === 'node' ? undefined : n.parent,
|
||||||
|
color: KIND_COLOR[n.kind] || '#666', shape: KIND_SHAPE[n.kind] || 'ellipse',
|
||||||
|
namespace: n.namespace, facts: n.facts,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
fullGraph.edges.forEach(e => {
|
||||||
|
if (!visibleNodeIds.has(e.source) || !visibleNodeIds.has(e.target)) return;
|
||||||
|
elements.push({ group: 'edges', data: { id: e.id, source: e.source, target: e.target, kind: e.kind } });
|
||||||
|
});
|
||||||
|
|
||||||
|
if (cy) cy.destroy();
|
||||||
|
cy = cytoscape({
|
||||||
|
container: document.getElementById('cy'),
|
||||||
|
elements,
|
||||||
|
style: [
|
||||||
|
{ selector: 'node[kind = "namespace"]', style: {
|
||||||
|
'background-color': '#1a1d24', 'background-opacity': 0.5, 'border-color': '#333', 'border-width': 1,
|
||||||
|
label: 'data(label)', 'font-size': 11, color: '#8fb8ff', 'text-valign': 'top', 'text-halign': 'left',
|
||||||
|
'padding': '14px', shape: 'round-rectangle',
|
||||||
|
}},
|
||||||
|
{ selector: 'node[kind != "namespace"]', style: {
|
||||||
|
'background-color': 'data(color)', shape: 'data(shape)', label: 'data(label)',
|
||||||
|
'font-size': 10, color: '#fff', 'text-outline-width': 1, 'text-outline-color': '#0009',
|
||||||
|
width: 'label', height: 24, padding: '6px', 'text-valign': 'center',
|
||||||
|
}},
|
||||||
|
{ selector: 'edge[kind = "selects"]', style: { 'line-color': '#37b37e', width: 1.5, 'target-arrow-color': '#37b37e', 'target-arrow-shape': 'triangle', 'curve-style': 'bezier' } },
|
||||||
|
{ selector: 'edge[kind = "routes"]', style: { 'line-color': '#e0a800', width: 1.5, 'target-arrow-color': '#e0a800', 'target-arrow-shape': 'triangle', 'curve-style': 'bezier' } },
|
||||||
|
{ selector: 'edge[kind = "schedules-on"]', style: { 'line-color': '#3a3f4b', width: 1, 'line-style': 'dashed', 'curve-style': 'bezier', 'target-arrow-shape': 'none' } },
|
||||||
|
],
|
||||||
|
layout: { name: 'dagre', rankDir: 'LR', nodeSep: 20, rankSep: 60, animate: false },
|
||||||
|
});
|
||||||
|
|
||||||
|
cy.on('tap', 'node', evt => {
|
||||||
|
const n = evt.target.data();
|
||||||
|
if (n.kind === 'namespace') return;
|
||||||
|
showDetails(n);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function showDetails(n) {
|
||||||
|
document.getElementById('details').classList.add('open');
|
||||||
|
document.getElementById('details-title').textContent = `${n.kind}: ${n.label}`;
|
||||||
|
const factsEl = document.getElementById('details-facts');
|
||||||
|
factsEl.innerHTML = Object.entries(n.facts || {}).map(([k, v]) => `<div class="fact"><b>${k}:</b> ${v || '—'}</div>`).join('');
|
||||||
|
const yamlEl = document.getElementById('details-yaml');
|
||||||
|
yamlEl.textContent = 'загрузка манифеста...';
|
||||||
|
const params = new URLSearchParams({ kind: n.kind, namespace: n.namespace || '', name: n.label });
|
||||||
|
const resp = await fetch('/api/resource?' + params);
|
||||||
|
yamlEl.textContent = resp.ok ? await resp.text() : 'не удалось получить манифест: ' + resp.status;
|
||||||
|
}
|
||||||
|
|
||||||
|
document.getElementById('reload').addEventListener('click', loadGraph);
|
||||||
|
document.getElementById('close-details').addEventListener('click', () => document.getElementById('details').classList.remove('open'));
|
||||||
|
document.getElementById('show-system').addEventListener('change', () => { buildNsList(); render(); });
|
||||||
|
document.getElementById('ns-filter').addEventListener('input', buildNsList);
|
||||||
|
|
||||||
|
loadGraph();
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user