Compare commits
16
Commits
@@ -16,3 +16,5 @@ oss.yaml
|
|||||||
module.yaml
|
module.yaml
|
||||||
changelog.yaml
|
changelog.yaml
|
||||||
LICENSE
|
LICENSE
|
||||||
|
version.json
|
||||||
|
images_digests.json
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ git:
|
|||||||
- docs/internal
|
- docs/internal
|
||||||
includePaths:
|
includePaths:
|
||||||
- .helmignore
|
- .helmignore
|
||||||
|
- changelog.yaml
|
||||||
- docs
|
- docs
|
||||||
- charts
|
- charts
|
||||||
- crds
|
- crds
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
name: cluster-map
|
name: cluster-map
|
||||||
version: 0.7.0
|
version: 0.10.0
|
||||||
dependencies:
|
dependencies:
|
||||||
- name: deckhouse_lib_helm
|
- name: deckhouse_lib_helm
|
||||||
version: 1.72.24
|
version: 1.72.24
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ WERF ?= werf
|
|||||||
BUILD_REGISTRY ?= localhost:5555
|
BUILD_REGISTRY ?= localhost:5555
|
||||||
BUILD_REPO ?= $(BUILD_REGISTRY)/$(MODULE)
|
BUILD_REPO ?= $(BUILD_REGISTRY)/$(MODULE)
|
||||||
DMT ?= dmt
|
DMT ?= dmt
|
||||||
API_VERSIONS ?= --api-versions autoscaling.k8s.io/v1/VerticalPodAutoscaler --api-versions monitoring.coreos.com/v1/PodMonitor
|
API_VERSIONS ?= --api-versions autoscaling.k8s.io/v1/VerticalPodAutoscaler --api-versions monitoring.coreos.com/v1/PodMonitor --api-versions monitoring.coreos.com/v1/PrometheusRule
|
||||||
|
|
||||||
.PHONY: test lint render base-images build-registry build publish release
|
.PHONY: test lint render base-images build-registry build publish release
|
||||||
|
|
||||||
@@ -57,10 +57,13 @@ base_images.yml:
|
|||||||
|
|
||||||
base-images: base_images.yml
|
base-images: base_images.yml
|
||||||
|
|
||||||
|
# The container survives a reboot stopped, so it is started, not only created.
|
||||||
build-registry:
|
build-registry:
|
||||||
@docker inspect cluster-map-build-registry >/dev/null 2>&1 || \
|
@docker inspect cluster-map-build-registry >/dev/null 2>&1 || \
|
||||||
docker run -d --name cluster-map-build-registry -p 127.0.0.1:5555:5000 -v cluster-map-build-registry:/var/lib/registry registry:2 >/dev/null
|
docker run -d --name cluster-map-build-registry -p 127.0.0.1:5555:5000 -v cluster-map-build-registry:/var/lib/registry registry:2 >/dev/null
|
||||||
@until curl -sf http://$(BUILD_REGISTRY)/v2/ >/dev/null; do sleep 1; done
|
@docker start cluster-map-build-registry >/dev/null
|
||||||
|
@for i in $$(seq 30); do curl -sf http://$(BUILD_REGISTRY)/v2/ >/dev/null && exit 0; sleep 1; done; \
|
||||||
|
echo "build registry on $(BUILD_REGISTRY) did not come up" >&2; exit 1
|
||||||
|
|
||||||
build: base_images.yml build-registry
|
build: base_images.yml build-registry
|
||||||
@test -n "$(VERSION)" || (echo "VERSION is required, e.g. VERSION=v0.7.0" && exit 1)
|
@test -n "$(VERSION)" || (echo "VERSION is required, e.g. VERSION=v0.7.0" && exit 1)
|
||||||
|
|||||||
@@ -25,13 +25,19 @@ The module follows [deckhouse/modules-template](https://github.com/deckhouse/mod
|
|||||||
| `cmd/cluster-map` | wiring, signals, graceful shutdown |
|
| `cmd/cluster-map` | wiring, signals, graceful shutdown |
|
||||||
| `internal/config` | settings from the environment (the Deployment template is the only writer) |
|
| `internal/config` | settings from the environment (the Deployment template is the only writer) |
|
||||||
| `internal/kube` | informer caches, managedFields stripped |
|
| `internal/kube` | informer caches, managedFields stripped |
|
||||||
| `internal/topology` | the graph: workloads, Services, Ingresses, nodes, and the pod-owner rules shared with traffic |
|
| `internal/topology` | the graph: workloads, Services, Ingresses, nodes, the pod-owner rules shared with traffic, and the problems attached to cards |
|
||||||
| `internal/traffic` | Hubble follow stream, per-minute buckets of distinct connections |
|
| `internal/traffic` | Hubble follow stream, per-minute buckets of distinct connections |
|
||||||
|
| `internal/actions` | restart, scale, run, suspend, delete pod, cordon — as the viewer |
|
||||||
|
| `internal/access` | what each viewer may see: SelfSubjectAccessReviews as the viewer, cached per token |
|
||||||
| `internal/server` | HTTP API, CSP headers, request metrics |
|
| `internal/server` | HTTP API, CSP headers, request metrics |
|
||||||
| `internal/metrics` | Prometheus metrics |
|
| `internal/metrics` | Prometheus metrics |
|
||||||
| `web/` | the frontend, embedded into the binary: plain JS, Cytoscape.js, elkjs, English and Russian |
|
| `web/` | the frontend, embedded into the binary: plain JS, Cytoscape.js, elkjs, English and Russian |
|
||||||
|
|
||||||
API: `/api/graph`, `/api/flows?namespaces=a,b&window=<seconds>`, `/api/resource?kind=&namespace=&name=`, `/api/pods?id=&namespace=`, `/api/logs?namespace=&pod=&container=`, `/healthz`, `/readyz`; metrics on `:9090/metrics`.
|
API: `/api/graph` (with problems), `/api/flows?namespaces=a,b&window=<seconds>`, `/api/resource?kind=&namespace=&name=`, `/api/pods?id=&namespace=`, `/api/logs?namespace=&pod=&container=`, `/api/events?id=`, `/api/usage?id=`, `/api/me`, `/api/permissions?kind=&namespace=&name=`, `POST /api/action`, `/healthz`, `/readyz`; metrics on `:9090/metrics`.
|
||||||
|
|
||||||
|
`/api/me`, `/api/permissions` and `/api/action` use the viewer's bearer token from the `Authorization` header (the DexAuthenticator adds it when `actions.enabled` or `auth.visibility: ViewerRBAC`), never the module's service account. With `ViewerRBAC` every read endpoint also filters by the viewer's scope and answers 401 without a token.
|
||||||
|
|
||||||
|
To run the binary on a workstation, point `KUBECONFIG` at a kubeconfig; without it the in-cluster service account is used.
|
||||||
|
|
||||||
## Development
|
## Development
|
||||||
|
|
||||||
@@ -49,8 +55,8 @@ Base images come from the `base_images.yml` Deckhouse publishes for its own modu
|
|||||||
|
|
||||||
```shell
|
```shell
|
||||||
docker login git.project-chimera.ru
|
docker login git.project-chimera.ru
|
||||||
make publish VERSION=v0.7.0 # build, push bundle and release, register, channel alpha
|
make publish VERSION=v0.10.0 # build, push bundle and release, register, channel alpha
|
||||||
make release VERSION=v0.7.0 CHANNEL=stable
|
make release VERSION=v0.10.0 CHANNEL=stable
|
||||||
```
|
```
|
||||||
|
|
||||||
The registry layout matches [deckhouse/modules-actions](https://github.com/deckhouse/modules-actions):
|
The registry layout matches [deckhouse/modules-actions](https://github.com/deckhouse/modules-actions):
|
||||||
|
|||||||
@@ -1,3 +1,59 @@
|
|||||||
|
v0.11.4:
|
||||||
|
fixes:
|
||||||
|
- "Failed probes (Unhealthy events) of a pod that is ready now are listed for information: the pod's state already reports NotReady and restarts. Deckhouse answers /readyz with 500 while it works through its queue after a module change and tolerates ten minutes of it; that badged its card after every module release."
|
||||||
|
v0.11.3:
|
||||||
|
fixes:
|
||||||
|
- "A single FailedMount of a pod that is running and ready (kubelet failing to refresh a volume, such as a ConfigMap prometheus-operator deletes and recreates) is listed for information; it badges the card after five repeats in the window, like other events."
|
||||||
|
v0.11.2:
|
||||||
|
fixes:
|
||||||
|
- "Warning events of a pod that has since finished, and FailedScheduling of a pod that has since been scheduled, are listed for information and no longer badge the card: a backup CronJob that waited all night for its node and then ran stayed yellow for half an hour."
|
||||||
|
v0.11.1:
|
||||||
|
fixes:
|
||||||
|
- "A container restarted three times or more stays marked for 24 hours after its last restart or OOM kill, not 30 minutes: a pod OOM-killed five times in a day was no longer shown 40 minutes after the last kill. A single restart still clears after 30 minutes."
|
||||||
|
v0.11.0:
|
||||||
|
features:
|
||||||
|
- "HTTP details where Cilium parses L7 (a CiliumNetworkPolicy with an http rule on the port): requests, responses by class, p50 and p95 latency and the ten busiest paths on the link, IDs in paths grouped as :id. Links with 5xx responses are drawn in the warning colour. Headers are never requested from Hubble. The docs have an example policy that turns this on for one service without restricting it."
|
||||||
|
fixes:
|
||||||
|
- "Traffic through the L7 proxy is counted once: Envoy's own connection to the pod no longer shows as a link from world, and the pod's answers to Envoy no longer add one link to the node per request. A pod that reached itself through its own Service is not shown as a link from world either."
|
||||||
|
v0.10.0:
|
||||||
|
features:
|
||||||
|
- "Per-user visibility (auth.visibility: ViewerRBAC, the default): each viewer sees the namespaces where their own RBAC lets them list pods, or the whole cluster if they can list pods everywhere. Logs also need get pods/log, node cards list nodes; traffic peers in hidden namespaces are shown as hidden. Checked with SelfSubjectAccessReviews as the viewer, cached for a minute. auth.visibility: Everything keeps the previous behaviour."
|
||||||
|
v0.9.0:
|
||||||
|
features:
|
||||||
|
- "Vulnerabilities from operator-trivy: a red shield with the number of CRITICAL CVEs on each card, and in the details every CRITICAL and HIGH CVE with its packages, installed and fixed versions and a link. A CVE counts once per card."
|
||||||
|
fixes:
|
||||||
|
- "Event counts are scaled to the 30-minute window: a cumulative count spread over days (40 probe failures in two days) no longer badges a card as a burst."
|
||||||
|
- upmeter's smoke-mini services and pods, which it moves between nodes on purpose, are not reported as problems.
|
||||||
|
v0.8.3:
|
||||||
|
fixes:
|
||||||
|
- UnexpectedJob warnings (the CronJob controller's note on every manual run) are listed for information and no longer badge the CronJob.
|
||||||
|
v0.8.2:
|
||||||
|
fixes:
|
||||||
|
- An Ingress that answers with a redirect (redirect annotations, or rewrite ... permanent / return 30x in a snippet) is not reported for a backend Service that does not exist; Deckhouse's prometheus Ingress does exactly that.
|
||||||
|
- Warning events about a pod that no longer exists are listed for information and no longer badge the card.
|
||||||
|
v0.8.1:
|
||||||
|
fixes:
|
||||||
|
- "Actions: the DexAuthenticator is annotated allow-access-to-kubernetes, so the user's token carries the kubernetes audience and the API server accepts it. In v0.8.0 the token lacked that audience, the API server rejected it and actions were unavailable."
|
||||||
|
v0.8.0:
|
||||||
|
features:
|
||||||
|
- "Problem badges: a yellow or red \"!\" on cards, namespace and node boxes and in the tree, from object state (crash loops, pending pods, readiness, failed CronJob runs, Services without endpoints, node pressure), Warning events and Deckhouse ClusterAlerts."
|
||||||
|
- A Problems panel with every current problem, most severe first, filterable to applications or system namespaces.
|
||||||
|
- "Actions from the details panel: restart, scale, run a CronJob now, suspend and resume, delete a pod, cordon and uncordon a node. They run with the viewer's own token and RBAC; the module's service account stays read-only."
|
||||||
|
- "Load section: CPU and memory of a workload or node against requests, limits and allocatable, with a per-pod table."
|
||||||
|
- An Events tab with the events of the object, its pods and ReplicaSets.
|
||||||
|
- Cards are locked in place until the padlock is opened; the map can be saved as a PNG picture.
|
||||||
|
- The open page refreshes cards, badges and the tree every minute without moving the layout.
|
||||||
|
fixes:
|
||||||
|
- "Layout of a large namespace or node view is 3-4 times faster: levels without edges are packed without a call into ELK."
|
||||||
|
- "About 20% fewer flows through hubble-relay: the to-overlay copy of pod-to-pod traffic is filtered at the agents; traffic counts are unchanged."
|
||||||
|
- Static files are revalidated with ETags instead of downloaded again on every visit (about 2 MB).
|
||||||
|
v0.7.2:
|
||||||
|
fixes:
|
||||||
|
- A namespace without traffic in the selected window is drawn again; the empty window was sent as null and the map kept the previous namespace on screen.
|
||||||
|
- A map that fails to draw shows an error instead of the previous view.
|
||||||
|
v0.7.1:
|
||||||
|
fixes:
|
||||||
|
- Alert rules are rendered on DKP versions without the operator-prometheus-crd module.
|
||||||
v0.7.0:
|
v0.7.0:
|
||||||
features:
|
features:
|
||||||
- First release as a Deckhouse module (ModuleSource, release channels, lib-helm templates).
|
- First release as a Deckhouse module (ModuleSource, release channels, lib-helm templates).
|
||||||
|
|||||||
@@ -40,3 +40,43 @@ spec:
|
|||||||
```
|
```
|
||||||
|
|
||||||
Memory grows with retention; watch `cluster_map_traffic_tracked_tuples` and the `ClusterMapTrackedTuplesHigh` alert.
|
Memory grows with retention; watch `cluster_map_traffic_tracked_tuples` and the `ClusterMapTrackedTuplesHigh` alert.
|
||||||
|
|
||||||
|
## A read-only map
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
spec:
|
||||||
|
settings:
|
||||||
|
actions:
|
||||||
|
enabled: false
|
||||||
|
```
|
||||||
|
|
||||||
|
No action buttons. With `auth.visibility: Everything` as well, the user's token is no longer passed to the module.
|
||||||
|
|
||||||
|
## HTTP details for one service
|
||||||
|
|
||||||
|
This policy selects the pods of the `web` Deployment and sends port 8080 through Envoy without restricting anything: `enableDefaultDeny: false` keeps all other traffic allowed, and the empty `http` rule matches every request.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
apiVersion: cilium.io/v2
|
||||||
|
kind: CiliumNetworkPolicy
|
||||||
|
metadata:
|
||||||
|
name: http-visibility
|
||||||
|
namespace: shop
|
||||||
|
spec:
|
||||||
|
endpointSelector:
|
||||||
|
matchLabels:
|
||||||
|
app: web
|
||||||
|
enableDefaultDeny:
|
||||||
|
ingress: false
|
||||||
|
egress: false
|
||||||
|
ingress:
|
||||||
|
- toPorts:
|
||||||
|
- ports:
|
||||||
|
- port: "8080"
|
||||||
|
protocol: TCP
|
||||||
|
rules:
|
||||||
|
http:
|
||||||
|
- {}
|
||||||
|
```
|
||||||
|
|
||||||
|
Delete the policy to go back to L3/L4. Before applying it to a service, read the notes in [HTTP details](README.md#http-details).
|
||||||
|
|||||||
@@ -40,3 +40,43 @@ spec:
|
|||||||
```
|
```
|
||||||
|
|
||||||
Память растёт вместе с глубиной хранения — следите за `cluster_map_traffic_tracked_tuples` и алертом `ClusterMapTrackedTuplesHigh`.
|
Память растёт вместе с глубиной хранения — следите за `cluster_map_traffic_tracked_tuples` и алертом `ClusterMapTrackedTuplesHigh`.
|
||||||
|
|
||||||
|
## Карта только для просмотра
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
spec:
|
||||||
|
settings:
|
||||||
|
actions:
|
||||||
|
enabled: false
|
||||||
|
```
|
||||||
|
|
||||||
|
Кнопок действий нет. Если к тому же задано `auth.visibility: Everything`, токен пользователя модулю больше не передаётся.
|
||||||
|
|
||||||
|
## Подробности HTTP для одного сервиса
|
||||||
|
|
||||||
|
Политика выбирает поды Deployment `web` и пускает порт 8080 через Envoy, ничего не ограничивая: `enableDefaultDeny: false` оставляет весь остальной трафик разрешённым, а пустое правило `http` подходит под любой запрос.
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
apiVersion: cilium.io/v2
|
||||||
|
kind: CiliumNetworkPolicy
|
||||||
|
metadata:
|
||||||
|
name: http-visibility
|
||||||
|
namespace: shop
|
||||||
|
spec:
|
||||||
|
endpointSelector:
|
||||||
|
matchLabels:
|
||||||
|
app: web
|
||||||
|
enableDefaultDeny:
|
||||||
|
ingress: false
|
||||||
|
egress: false
|
||||||
|
ingress:
|
||||||
|
- toPorts:
|
||||||
|
- ports:
|
||||||
|
- port: "8080"
|
||||||
|
protocol: TCP
|
||||||
|
rules:
|
||||||
|
http:
|
||||||
|
- {}
|
||||||
|
```
|
||||||
|
|
||||||
|
Чтобы вернуться к L3/L4, удалите политику. Прежде чем включать её для сервиса, прочитайте замечания в разделе [Подробности HTTP](README_RU.md#подробности-http).
|
||||||
|
|||||||
+59
-5
@@ -14,10 +14,45 @@ The `cluster-map` module answers "where does this service live and what does it
|
|||||||
- Solid arrows are configuration links: Ingress backends and Service selectors.
|
- Solid arrows are configuration links: Ingress backends and Service selectors.
|
||||||
- Dashed animated arrows are real connections from Hubble over the chosen window (5 minutes, 15 minutes, 1 hour); red when Cilium dropped some of them. Peers from other namespaces appear as dashed cards in their own boxes.
|
- Dashed animated arrows are real connections from Hubble over the chosen window (5 minutes, 15 minutes, 1 hour); red when Cilium dropped some of them. Peers from other namespaces appear as dashed cards in their own boxes.
|
||||||
- With more than 30 traffic directions on screen, lines stay faint until a card is hovered or selected.
|
- With more than 30 traffic directions on screen, lines stay faint until a card is hovered or selected.
|
||||||
- **Details** (right). Overview, Links (configuration and traffic, clickable), live YAML, and container logs.
|
- **Details** (right). Overview (a one-line verdict, the object's problems, its facts, CPU and memory against requests and limits, its pods), Links (configuration and traffic, clickable), live YAML, Events, container logs, and the actions the viewer is allowed to run.
|
||||||
|
- **Problems** (the button in the top bar). Everything that is wrong right now, most severe first, filterable to applications or system namespaces; a click opens the object.
|
||||||
|
|
||||||
|
Cards cannot be dragged until the padlock under the zoom buttons is opened, so a stray click does not rearrange the map. The last button saves the current map as a PNG picture.
|
||||||
|
|
||||||
Workloads managed by non-native controllers are included: Advanced DaemonSets of OpenKruise, database operator clusters, static pods, standalone Jobs. They are named the way Hubble names workloads, so their traffic attaches to the same cards.
|
Workloads managed by non-native controllers are included: Advanced DaemonSets of OpenKruise, database operator clusters, static pods, standalone Jobs. They are named the way Hubble names workloads, so their traffic attaches to the same cards.
|
||||||
|
|
||||||
|
## Problems and alerts
|
||||||
|
|
||||||
|
A card with a problem carries a badge: yellow `!` for a warning, red `!` for an error. Namespace boxes and cards, node boxes and the tree take the colour of the worst problem inside. Sources:
|
||||||
|
|
||||||
|
- **Object state**: containers in `CrashLoopBackOff`, `ImagePullBackOff` and similar, restarts and OOM kills of the last 30 minutes (of the last 24 hours for a container restarted three times or more), pods pending or unschedulable, workloads with no or not all replicas ready for more than 2 minutes, a stuck rollout, a CronJob whose last run failed, a Service with no ready pod, an Ingress routing to a Service that does not exist, a node that is not ready or is under memory, disk or PID pressure.
|
||||||
|
- **Warning events** of the last 30 minutes, folded per object and reason. Scheduling, volume and back-off failures badge the card at once; any other reason needs five repeats inside the window and is listed without a badge before that, so a single failed probe during a restart does not colour the map. An event's count is cumulative since its first occurrence, so a long-lived event is scaled to the window: 40 probe failures over two days is one an hour, not a burst.
|
||||||
|
- **Prometheus alerts** that Deckhouse mirrors into `ClusterAlert` objects. An alert is attached by its labels (`deployment`, `statefulset`, `daemonset`, `pod`, `service`, `ingress`, `node`, `namespace`); one without an object or namespace goes to the cluster list. `severity_level` 1–3 is an error, 4–6 a warning, 7–9 information.
|
||||||
|
|
||||||
|
Information-level entries (a cordoned node, a single ordinary warning) are listed and never badge a card. Problems are recomputed on every map request and the open page refreshes them every minute.
|
||||||
|
|
||||||
|
## Vulnerabilities
|
||||||
|
|
||||||
|
With operator-trivy installed, a card whose images have CRITICAL CVEs shows a red shield with their number in the bottom-right corner. The details panel lists every CRITICAL and HIGH CVE of the workload's images: the packages it hits, the installed version and the version that fixes it, with a link to the advisory. A CVE counts once per card, however many packages and containers it is found in. Vulnerabilities are shown apart from problems: nearly every image has some, and a badge on each card would hide what is actually broken.
|
||||||
|
|
||||||
|
The data comes from the `VulnerabilityReport` objects operator-trivy writes, so only namespaces it scans appear (labelled `security-scanning.deckhouse.io/enabled`), and only the severities its `severities` setting keeps.
|
||||||
|
|
||||||
|
## Actions
|
||||||
|
|
||||||
|
With `actions.enabled` (the default), the details panel offers:
|
||||||
|
|
||||||
|
| Object | Actions |
|
||||||
|
|---|---|
|
||||||
|
| Deployment, StatefulSet | restart (as `kubectl rollout restart`), scale to 0–100 replicas |
|
||||||
|
| DaemonSet | restart |
|
||||||
|
| CronJob | run now (as `kubectl create job --from=cronjob/…`), suspend, resume |
|
||||||
|
| Pod (in the Load table) | delete, so its controller starts a new one |
|
||||||
|
| Node (click the node box) | cordon, uncordon |
|
||||||
|
|
||||||
|
Every action asks for confirmation and runs **as the viewer**: the DexAuthenticator passes the user's own token to the module, the module sends the request to the API server with that token, and the API server checks the user's RBAC. A button is shown only when a `SelfSubjectAccessReview` says the user may do it. The module's service account never gets write permissions, and the API server's audit log records the user; the module also logs each action with the user's name.
|
||||||
|
|
||||||
|
Write requests from another site are refused: the module checks `Sec-Fetch-Site`, `Origin` and the JSON content type.
|
||||||
|
|
||||||
## How traffic is collected
|
## How traffic is collected
|
||||||
|
|
||||||
Every Cilium agent keeps only a ring buffer of flows (4095 by default); at a few hundred flows per second this is about ten seconds of history. A one-off query "for the last 15 minutes" would silently return the last few seconds. The module therefore keeps a follow stream from Hubble Relay and aggregates it into per-minute buckets for the configured retention.
|
Every Cilium agent keeps only a ring buffer of flows (4095 by default); at a few hundred flows per second this is about ten seconds of history. A one-off query "for the last 15 minutes" would silently return the last few seconds. The module therefore keeps a follow stream from Hubble Relay and aggregates it into per-minute buckets for the configured retention.
|
||||||
@@ -26,6 +61,8 @@ Counts are **distinct connections** (source and destination address and port), n
|
|||||||
|
|
||||||
Kubelet probes and other traffic originating from node hosts, node-to-node traffic, Cilium health checks and DNS are filtered at the agents and never reach the module. On a typical cluster this is about three quarters of all flows and none of it maps to a workload. Set `traffic.collectInfraTraffic` to collect it anyway.
|
Kubelet probes and other traffic originating from node hosts, node-to-node traffic, Cilium health checks and DNS are filtered at the agents and never reach the module. On a typical cluster this is about three quarters of all flows and none of it maps to a workload. Set `traffic.collectInfraTraffic` to collect it anyway.
|
||||||
|
|
||||||
|
Pod-to-pod traffic between nodes is traced twice, as it leaves the sender's node (`to-overlay`) and as it reaches the receiving pod (`to-endpoint`). The first copy is also filtered at the agents when the receiver is a pod; this removes about a fifth of the remaining flows without changing any count.
|
||||||
|
|
||||||
The collector runs in one of three modes, see `traffic.mode`:
|
The collector runs in one of three modes, see `traffic.mode`:
|
||||||
|
|
||||||
| Mode | Cost | Traffic windows |
|
| Mode | Cost | Traffic windows |
|
||||||
@@ -36,6 +73,16 @@ The collector runs in one of three modes, see `traffic.mode`:
|
|||||||
|
|
||||||
History is kept in memory: after a pod restart the windows fill again. The status line says how much of the window has actually been seen.
|
History is kept in memory: after a pod restart the windows fill again. The status line says how much of the window has actually been seen.
|
||||||
|
|
||||||
|
## HTTP details
|
||||||
|
|
||||||
|
Where Cilium parses HTTP on a port, the map adds to that link the number of requests, responses by class (2xx, 3xx, 4xx, 5xx), p50 and p95 latency and the ten busiest paths. A link with 5xx responses is drawn in the warning colour. Paths are grouped: the query string is dropped and segments that look like IDs (numbers, UUIDs, long hex strings) become `:id`, so `/api/users/42?x=1` and `/api/users/43` are one row `/api/users/:id`.
|
||||||
|
|
||||||
|
Cilium parses HTTP only on ports named in an L7 rule of a `CiliumNetworkPolicy`; the module does not create such policies. To enable it for one service without restricting its traffic, see [the example](EXAMPLES.md#http-details-for-one-service). Keep in mind:
|
||||||
|
|
||||||
|
- the connections to that port go through the Envoy proxy on the node: a fraction of a millisecond per request and CPU of the Cilium agent in proportion to the request rate;
|
||||||
|
- the port must speak plain HTTP/1.x or HTTP/2 (h2c): Envoy cannot see inside TLS, so put the rule on the backend port behind the Ingress, not on 443. gRPC is parsed, but its status is in the trailers and every call shows as 200; a gRPC stream or a WebSocket is one request, not a series of messages;
|
||||||
|
- request and response headers reach Hubble, including `Authorization` and cookies. The module asks Hubble for the method, path, status and latency only; the headers are not read, stored or shown. Other Hubble clients (the Hubble UI, `hubble observe`) do see them.
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
- Deckhouse Kubernetes Platform 1.72 or newer.
|
- Deckhouse Kubernetes Platform 1.72 or newer.
|
||||||
@@ -44,9 +91,15 @@ History is kept in memory: after a pod restart the windows fill again. The statu
|
|||||||
|
|
||||||
## Security
|
## Security
|
||||||
|
|
||||||
The web interface reads the cluster through the module's own service account (`d8:cluster-map` ClusterRole): `get`, `list`, `watch` on namespaces, pods, services, nodes, deployments, statefulsets, daemonsets, cronjobs and ingresses, and `get` on `pods/log`. It never reads Secrets or ConfigMaps and has no write verbs.
|
The web interface reads the cluster through the module's own service account (`d8:cluster-map` ClusterRole): `get`, `list`, `watch` on namespaces, pods, services, nodes, events, deployments, statefulsets, daemonsets, cronjobs, ingresses, Deckhouse `clusteralerts` and operator-trivy `vulnerabilityreports`, `get` and `list` on `metrics.k8s.io` pods and nodes, and `get` on `pods/log`. It never reads Secrets or ConfigMaps and has no write verbs. Changes go through [actions](#actions), which use the viewer's own identity.
|
||||||
|
|
||||||
This means **anyone admitted to the web interface sees every namespace and can read container logs**, whatever their own RBAC permissions are. Restrict access with `auth.allowedUserGroups` (and optionally `auth.allowedUserEmails` and `auth.whitelistSourceRanges`).
|
**What a viewer sees follows the viewer's own RBAC** (`auth.visibility: ViewerRBAC`, the default). With every request the DexAuthenticator passes the user's token; the module asks the API server with that token (`SelfSubjectAccessReview`) and caches the answer for a minute:
|
||||||
|
|
||||||
|
- `list pods` in all namespaces — the whole cluster;
|
||||||
|
- otherwise only the namespaces where the user can `list pods`; cards, YAML, events, load and vulnerabilities of other namespaces are not returned, and a traffic peer there is shown as a hidden peer, with its connections but without its name;
|
||||||
|
- container logs additionally need `get pods/log` in that namespace, node cards and the node view `list nodes`; cluster-wide alerts are shown to cluster-wide viewers only.
|
||||||
|
|
||||||
|
With `auth.visibility: Everything`, anyone admitted to the web interface sees every namespace and can read container logs, whatever their own RBAC is. In both modes restrict who is admitted with `auth.allowedUserGroups` (and optionally `auth.allowedUserEmails` and `auth.whitelistSourceRanges`); what a user can change always follows their own RBAC.
|
||||||
|
|
||||||
The frontend is served with a strict Content Security Policy: only its own scripts, styles and API, no inline code, no framing.
|
The frontend is served with a strict Content Security Policy: only its own scripts, styles and API, no inline code, no framing.
|
||||||
|
|
||||||
@@ -62,5 +115,6 @@ The pod exposes Prometheus metrics on port `9090` (`cluster_map_*`: Hubble strea
|
|||||||
## Limitations
|
## Limitations
|
||||||
|
|
||||||
- One replica: traffic history lives in the process. A second replica would keep its own history and double the load on hubble-relay.
|
- One replica: traffic history lives in the process. A second replica would keep its own history and double the load on hubble-relay.
|
||||||
- Access is all-or-nothing, as described in [Security](#security). Showing each user only what their RBAC allows is planned.
|
- Visibility is by namespace: a user who can list pods in a namespace sees all its cards, including the manifests of its Deployments and Services, even if their role would not let them read those objects one by one.
|
||||||
- Traffic is L3/L4. HTTP-level details (paths, status codes) need Cilium L7 visibility and are not shown yet.
|
- Alerts need the `prometheus` module, which creates `ClusterAlert` objects. Without it the map shows problems from object state and events only.
|
||||||
|
- HTTP details only where a CiliumNetworkPolicy turns on L7 parsing, see [HTTP details](#http-details). Other protocols are shown at L3/L4.
|
||||||
|
|||||||
+65
-5
@@ -14,10 +14,45 @@ webIfaces:
|
|||||||
- Сплошные стрелки — связи по конфигурации: backend Ingress и селектор Service.
|
- Сплошные стрелки — связи по конфигурации: backend Ingress и селектор Service.
|
||||||
- Пунктир с анимацией — реальные соединения из Hubble за выбранное окно (5 минут, 15 минут, час); красный, если Cilium часть из них отбросил. Пиры из других namespace — пунктирные карточки в своих рамках.
|
- Пунктир с анимацией — реальные соединения из Hubble за выбранное окно (5 минут, 15 минут, час); красный, если Cilium часть из них отбросил. Пиры из других namespace — пунктирные карточки в своих рамках.
|
||||||
- Если на экране больше 30 направлений трафика, линии бледные, пока не наведёшь курсор на карточку или не выберешь её.
|
- Если на экране больше 30 направлений трафика, линии бледные, пока не наведёшь курсор на карточку или не выберешь её.
|
||||||
- **Детали** (справа). Обзор, Связи (по конфигурации и по трафику, с переходом), живой YAML и логи контейнеров.
|
- **Детали** (справа). Обзор (одна строка — работает или нет, проблемы объекта, его параметры, CPU и память относительно запросов и лимитов, его поды), Связи (по конфигурации и по трафику, с переходом), живой YAML, События, логи контейнеров и действия, которые разрешены пользователю.
|
||||||
|
- **Проблемы** (кнопка в верхней панели). Всё, что сломано прямо сейчас, сначала самое серьёзное, с фильтром по приложениям и системным namespace; клик открывает объект.
|
||||||
|
|
||||||
|
Карточки нельзя перетащить, пока не открыт замок под кнопками масштаба, — случайный клик не перестроит карту. Последняя кнопка сохраняет текущую карту картинкой PNG.
|
||||||
|
|
||||||
Workload'ы под нестандартными контроллерами тоже на карте: Advanced DaemonSet из OpenKruise, кластеры операторов баз данных, статические поды, одиночные Job. Они названы так же, как их называет Hubble, поэтому трафик привязывается к тем же карточкам.
|
Workload'ы под нестандартными контроллерами тоже на карте: Advanced DaemonSet из OpenKruise, кластеры операторов баз данных, статические поды, одиночные Job. Они названы так же, как их называет Hubble, поэтому трафик привязывается к тем же карточкам.
|
||||||
|
|
||||||
|
## Проблемы и алерты
|
||||||
|
|
||||||
|
Карточка с проблемой несёт значок: жёлтый `!` — предупреждение, красный `!` — ошибка. Рамки и карточки namespace, рамки нод и дерево окрашиваются по самой серьёзной проблеме внутри. Источники:
|
||||||
|
|
||||||
|
- **Состояние объектов**: контейнеры в `CrashLoopBackOff`, `ImagePullBackOff` и подобном, перезапуски и убийства за нехватку памяти за последние 30 минут (за сутки, если контейнер перезапускался три раза и больше), поды в ожидании или без места для запуска, workload'ы, у которых больше 2 минут не готовы все или часть реплик, застрявшая выкатка, CronJob с неудачным последним запуском, Service без готовых подов, Ingress на несуществующий Service, нода не готова или ей не хватает памяти, диска или PID.
|
||||||
|
- **Warning-события** за последние 30 минут, сгруппированные по объекту и причине. Сбои планирования, томов и back-off дают значок сразу; любой другой причине нужно пять повторов в пределах окна, до этого она в списке без значка — одна проваленная проба во время рестарта не красит карту. Счётчик события накапливается с первого появления, поэтому давнее событие пересчитывается на окно: 40 проваленных проб за двое суток — это раз в час, а не всплеск.
|
||||||
|
- **Алерты Prometheus**, которые Deckhouse зеркалирует в объекты `ClusterAlert`. Алерт привязывается по меткам (`deployment`, `statefulset`, `daemonset`, `pod`, `service`, `ingress`, `node`, `namespace`); алерт без объекта и namespace попадает в список кластера. `severity_level` 1–3 — ошибка, 4–6 — предупреждение, 7–9 — информация.
|
||||||
|
|
||||||
|
Записи уровня «для сведения» (нода закрыта для новых подов, одиночное обычное предупреждение) есть в списке, но значка не дают. Проблемы пересчитываются при каждом запросе карты, открытая страница обновляет их раз в минуту.
|
||||||
|
|
||||||
|
## Уязвимости
|
||||||
|
|
||||||
|
Если установлен operator-trivy, у карточки, в образах которой есть CVE уровня CRITICAL, в правом нижнем углу красный щит с их числом. В панели деталей — все CVE уровней CRITICAL и HIGH образов workload'а: какие пакеты затронуты, стоящая версия и версия с исправлением, ссылка на описание. Одна CVE считается на карточке один раз, в скольких бы пакетах и контейнерах она ни нашлась. Уязвимости показываются отдельно от проблем: они есть почти в каждом образе, и значок на каждой карточке скрыл бы то, что действительно сломано.
|
||||||
|
|
||||||
|
Данные берутся из объектов `VulnerabilityReport`, которые пишет operator-trivy, поэтому видны только namespace, которые он сканирует (с меткой `security-scanning.deckhouse.io/enabled`), и только уровни, оставленные его настройкой `severities`.
|
||||||
|
|
||||||
|
## Действия
|
||||||
|
|
||||||
|
При `actions.enabled` (по умолчанию включено) в панели деталей есть:
|
||||||
|
|
||||||
|
| Объект | Действия |
|
||||||
|
|---|---|
|
||||||
|
| Deployment, StatefulSet | перезапуск (как `kubectl rollout restart`), масштабирование на 0–100 реплик |
|
||||||
|
| DaemonSet | перезапуск |
|
||||||
|
| CronJob | запустить сейчас (как `kubectl create job --from=cronjob/…`), приостановить, возобновить |
|
||||||
|
| Под (в таблице «Нагрузка») | удалить, чтобы контроллер запустил новый |
|
||||||
|
| Нода (клик по рамке ноды) | закрыть для новых подов (cordon) и открыть обратно |
|
||||||
|
|
||||||
|
Каждое действие требует подтверждения и выполняется **от имени пользователя**: DexAuthenticator передаёт модулю собственный токен пользователя, модуль отправляет запрос в API-сервер с этим токеном, и API-сервер проверяет RBAC пользователя. Кнопка показывается, только если `SelfSubjectAccessReview` подтверждает право. Сервисный аккаунт модуля не получает прав на запись, в аудит-логе API-сервера записан пользователь; модуль тоже пишет каждое действие в лог с именем пользователя.
|
||||||
|
|
||||||
|
Запросы на запись с других сайтов отклоняются: модуль проверяет `Sec-Fetch-Site`, `Origin` и тип содержимого JSON.
|
||||||
|
|
||||||
## Как собирается трафик
|
## Как собирается трафик
|
||||||
|
|
||||||
Каждый агент Cilium хранит только кольцевой буфер потоков (по умолчанию 4095); при нескольких сотнях потоков в секунду это около десяти секунд истории. Разовый запрос «за последние 15 минут» молча вернул бы последние секунды. Поэтому модуль держит постоянный поток из Hubble Relay и раскладывает его по минутным корзинам на заданную глубину.
|
Каждый агент Cilium хранит только кольцевой буфер потоков (по умолчанию 4095); при нескольких сотнях потоков в секунду это около десяти секунд истории. Разовый запрос «за последние 15 минут» молча вернул бы последние секунды. Поэтому модуль держит постоянный поток из Hubble Relay и раскладывает его по минутным корзинам на заданную глубину.
|
||||||
@@ -26,6 +61,8 @@ Workload'ы под нестандартными контроллерами то
|
|||||||
|
|
||||||
Пробы kubelet и прочий трафик с хостов нод, трафик между нодами, проверки Cilium health и DNS отсекаются на агентах и до модуля не доходят. На обычном кластере это около трёх четвертей всех потоков, и ни один из них не привязан к workload'у. Чтобы всё же собирать их, включите `traffic.collectInfraTraffic`.
|
Пробы kubelet и прочий трафик с хостов нод, трафик между нодами, проверки Cilium health и DNS отсекаются на агентах и до модуля не доходят. На обычном кластере это около трёх четвертей всех потоков, и ни один из них не привязан к workload'у. Чтобы всё же собирать их, включите `traffic.collectInfraTraffic`.
|
||||||
|
|
||||||
|
Трафик между подами на разных нодах трассируется дважды: при выходе с ноды отправителя (`to-overlay`) и при доставке в под получателя (`to-endpoint`). Первая копия тоже отсекается на агентах, если получатель — под; это убирает около пятой части оставшихся потоков, ни одно число при этом не меняется.
|
||||||
|
|
||||||
Режимы сбора — параметр `traffic.mode`:
|
Режимы сбора — параметр `traffic.mode`:
|
||||||
|
|
||||||
| Режим | Цена | Окна трафика |
|
| Режим | Цена | Окна трафика |
|
||||||
@@ -36,6 +73,16 @@ Workload'ы под нестандартными контроллерами то
|
|||||||
|
|
||||||
История хранится в памяти: после рестарта пода окна заполняются заново. Строка статуса пишет, сколько из окна реально видно.
|
История хранится в памяти: после рестарта пода окна заполняются заново. Строка статуса пишет, сколько из окна реально видно.
|
||||||
|
|
||||||
|
## Подробности HTTP
|
||||||
|
|
||||||
|
Там, где Cilium разбирает HTTP на порту, карта добавляет к связи число запросов, ответы по классам (2xx, 3xx, 4xx, 5xx), задержку p50 и p95 и десять самых нагруженных путей. Связь, на которой были ответы 5xx, рисуется цветом предупреждения. Пути группируются: строка запроса отбрасывается, а сегменты, похожие на идентификаторы (числа, UUID, длинные hex-строки), заменяются на `:id` — `/api/users/42?x=1` и `/api/users/43` попадают в одну строку `/api/users/:id`.
|
||||||
|
|
||||||
|
Cilium разбирает HTTP только на портах, упомянутых в L7-правиле `CiliumNetworkPolicy`; сам модуль таких политик не создаёт. Как включить это для одного сервиса, не ограничивая его трафик, — в [примере](EXAMPLES_RU.md#подробности-http-для-одного-сервиса). Учтите:
|
||||||
|
|
||||||
|
- соединения на этот порт идут через прокси Envoy на ноде: доли миллисекунды на запрос и CPU агента Cilium пропорционально числу запросов;
|
||||||
|
- порт должен говорить на обычном HTTP/1.x или HTTP/2 (h2c): внутрь TLS Envoy не заглядывает, поэтому правило ставится на порт бэкенда за Ingress, а не на 443. gRPC разбирается, но его статус лежит в трейлерах, и каждый вызов виден как 200; поток gRPC или WebSocket — это один запрос, а не последовательность сообщений;
|
||||||
|
- заголовки запроса и ответа попадают в Hubble, включая `Authorization` и cookie. Модуль запрашивает у Hubble только метод, путь, код и задержку; заголовки он не читает, не хранит и не показывает. Другие клиенты Hubble (Hubble UI, `hubble observe`) их видят.
|
||||||
|
|
||||||
## Требования
|
## Требования
|
||||||
|
|
||||||
- Deckhouse Kubernetes Platform 1.72 или новее.
|
- Deckhouse Kubernetes Platform 1.72 или новее.
|
||||||
@@ -44,9 +91,15 @@ Workload'ы под нестандартными контроллерами то
|
|||||||
|
|
||||||
## Безопасность
|
## Безопасность
|
||||||
|
|
||||||
Веб-интерфейс читает кластер через собственную учётку модуля (ClusterRole `d8:cluster-map`): `get`, `list`, `watch` на namespaces, pods, services, nodes, deployments, statefulsets, daemonsets, cronjobs и ingresses и `get` на `pods/log`. Secret и ConfigMap он не читает, прав на запись нет.
|
Веб-интерфейс читает кластер через собственную учётку модуля (ClusterRole `d8:cluster-map`): `get`, `list`, `watch` на namespaces, pods, services, nodes, events, deployments, statefulsets, daemonsets, cronjobs, ingresses, `clusteralerts` Deckhouse и `vulnerabilityreports` operator-trivy, `get` и `list` на pods и nodes из `metrics.k8s.io` и `get` на `pods/log`. Secret и ConfigMap он не читает, прав на запись нет. Изменения идут через [действия](#действия), которые выполняются от имени пользователя.
|
||||||
|
|
||||||
Отсюда следует: **любой, кого пустили в веб-интерфейс, видит все namespace и может читать логи контейнеров** — независимо от собственных прав RBAC. Ограничивайте доступ через `auth.allowedUserGroups` (а также `auth.allowedUserEmails` и `auth.whitelistSourceRanges`).
|
**Что видит пользователь, определяет его собственный RBAC** (`auth.visibility: ViewerRBAC`, по умолчанию). С каждым запросом DexAuthenticator передаёт токен пользователя; модуль спрашивает API-сервер от его имени (`SelfSubjectAccessReview`) и кеширует ответ на минуту:
|
||||||
|
|
||||||
|
- `list pods` во всех namespace — виден весь кластер;
|
||||||
|
- иначе — только namespace, где пользователь может `list pods`; карточки, YAML, события, нагрузка и уязвимости остальных namespace не отдаются, а сосед по трафику оттуда показывается скрытым — с соединениями, но без имени;
|
||||||
|
- для логов контейнеров нужно ещё `get pods/log` в этом namespace, для карточек нод и вида по нодам — `list nodes`; общекластерные алерты видны только пользователям с доступом ко всему кластеру.
|
||||||
|
|
||||||
|
При `auth.visibility: Everything` любой, кого пустили в веб-интерфейс, видит все namespace и может читать логи контейнеров независимо от своего RBAC. В обоих режимах ограничивайте круг допущенных через `auth.allowedUserGroups` (а также `auth.allowedUserEmails` и `auth.whitelistSourceRanges`); изменить пользователь всегда может только то, что разрешает его RBAC.
|
||||||
|
|
||||||
Фронтенд отдаётся со строгой Content Security Policy: только собственные скрипты, стили и API, без inline-кода, без встраивания во фреймы.
|
Фронтенд отдаётся со строгой Content Security Policy: только собственные скрипты, стили и API, без inline-кода, без встраивания во фреймы.
|
||||||
|
|
||||||
@@ -62,5 +115,12 @@ Workload'ы под нестандартными контроллерами то
|
|||||||
## Ограничения
|
## Ограничения
|
||||||
|
|
||||||
- Одна реплика: история трафика живёт в процессе. Вторая реплика держала бы свою историю и удвоила бы нагрузку на hubble-relay.
|
- Одна реплика: история трафика живёт в процессе. Вторая реплика держала бы свою историю и удвоила бы нагрузку на hubble-relay.
|
||||||
- Доступ по принципу «всё или ничего», см. раздел [Безопасность](#безопасность). Показ каждому пользователю только того, что разрешает его RBAC, — в планах.
|
- Видимость — по namespace: пользователь, который может смотреть поды в namespace, видит все его карточки, включая манифесты Deployment и Service, даже если его роль не дала бы прочитать эти объекты по отдельности.
|
||||||
- Трафик на уровне L3/L4. Подробности HTTP (пути, коды ответа) требуют L7-видимости Cilium и пока не показываются.
|
- Для алертов нужен модуль `prometheus`, он создаёт объекты `ClusterAlert`. Без него карта показывает проблемы только по состоянию объектов и событиям.
|
||||||
|
- Подробности HTTP — только там, где CiliumNetworkPolicy включает разбор L7, см. [Подробности HTTP](#подробности-http). Остальные протоколы показываются на уровне L3/L4.
|
||||||
|
|
||||||
|
## Уточнение диагностики после перезапуска
|
||||||
|
|
||||||
|
Обычный перезапуск контейнера отмечается в течение 30 минут. Накопленный счётчик перезапусков за всю жизнь pod’а не доказывает повторные сбои за сутки; расширенное окно остаётся для последних завершений OOMKilled. Текущий CrashLoopBackOff и неготовность проверяются независимо.
|
||||||
|
|
||||||
|
Сервис без endpoints остаётся предупреждением, кроме подтверждённого случая, когда все подходящие Deployment/StatefulSet/DaemonSet уже обработали конфигурацию и ожидают ноль pod’ов. Тогда запись информационная. Неизвестное состояние, устаревший статус контроллера, подходящий CronJob или незавершённый pod сохраняют предупреждение.
|
||||||
|
|||||||
@@ -35,9 +35,12 @@ import (
|
|||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/credentials"
|
"google.golang.org/grpc/credentials"
|
||||||
|
"k8s.io/client-go/dynamic"
|
||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
"k8s.io/client-go/rest"
|
"k8s.io/client-go/rest"
|
||||||
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
|
|
||||||
|
"git.project-chimera.ru/fanchji/cluster-map/internal/actions"
|
||||||
"git.project-chimera.ru/fanchji/cluster-map/internal/config"
|
"git.project-chimera.ru/fanchji/cluster-map/internal/config"
|
||||||
"git.project-chimera.ru/fanchji/cluster-map/internal/kube"
|
"git.project-chimera.ru/fanchji/cluster-map/internal/kube"
|
||||||
"git.project-chimera.ru/fanchji/cluster-map/internal/metrics"
|
"git.project-chimera.ru/fanchji/cluster-map/internal/metrics"
|
||||||
@@ -63,7 +66,15 @@ func run() error {
|
|||||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
|
||||||
defer stop()
|
defer stop()
|
||||||
|
|
||||||
srv := server.New(cfg, web.Static())
|
restCfg, err := restConfig()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var asViewer actions.ClientFactory
|
||||||
|
if cfg.ActionsEnabled || cfg.ViewerVisibility {
|
||||||
|
asViewer = actions.NewClientFactory(restCfg)
|
||||||
|
}
|
||||||
|
srv := server.New(cfg, web.Static(), asViewer)
|
||||||
api := &http.Server{Addr: cfg.ListenAddr, Handler: srv.Handler(), ReadHeaderTimeout: 10 * time.Second}
|
api := &http.Server{Addr: cfg.ListenAddr, Handler: srv.Handler(), ReadHeaderTimeout: 10 * time.Second}
|
||||||
metricsSrv := &http.Server{Addr: cfg.MetricsAddr, Handler: promhttp.HandlerFor(metrics.Registry, promhttp.HandlerOpts{}), ReadHeaderTimeout: 10 * time.Second}
|
metricsSrv := &http.Server{Addr: cfg.MetricsAddr, Handler: promhttp.HandlerFor(metrics.Registry, promhttp.HandlerOpts{}), ReadHeaderTimeout: 10 * time.Second}
|
||||||
errc := make(chan error, 2)
|
errc := make(chan error, 2)
|
||||||
@@ -74,17 +85,17 @@ func run() error {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
slog.Info("listening", "api", cfg.ListenAddr, "metrics", cfg.MetricsAddr, "traffic", cfg.TrafficMode)
|
slog.Info("listening", "api", cfg.ListenAddr, "metrics", cfg.MetricsAddr, "traffic", cfg.TrafficMode, "actions", cfg.ActionsEnabled, "viewerVisibility", cfg.ViewerVisibility)
|
||||||
|
|
||||||
restCfg, err := rest.InClusterConfig()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("in-cluster config: %w", err)
|
|
||||||
}
|
|
||||||
client, err := kubernetes.NewForConfig(restCfg)
|
client, err := kubernetes.NewForConfig(restCfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
cache, err := kube.Start(ctx, client, 2*time.Minute)
|
dyn, err := dynamic.NewForConfig(restCfg)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
cache, err := kube.Start(ctx, client, dyn, 2*time.Minute)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -110,6 +121,19 @@ func run() error {
|
|||||||
return errors.Join(api.Shutdown(shutdownCtx), metricsSrv.Shutdown(shutdownCtx))
|
return errors.Join(api.Shutdown(shutdownCtx), metricsSrv.Shutdown(shutdownCtx))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// restConfig is the pod's service account; KUBECONFIG, when set, is for
|
||||||
|
// running the binary on a workstation during development.
|
||||||
|
func restConfig() (*rest.Config, error) {
|
||||||
|
if path := os.Getenv("KUBECONFIG"); path != "" {
|
||||||
|
return clientcmd.BuildConfigFromFlags("", path)
|
||||||
|
}
|
||||||
|
cfg, err := rest.InClusterConfig()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("in-cluster config: %w", err)
|
||||||
|
}
|
||||||
|
return cfg, nil
|
||||||
|
}
|
||||||
|
|
||||||
// hubbleClient connects to Hubble Relay over TLS. Relay presents a
|
// hubbleClient connects to Hubble Relay over TLS. Relay presents a
|
||||||
// *.hubble-relay.cilium.io certificate signed by the cluster's hubble CA
|
// *.hubble-relay.cilium.io certificate signed by the cluster's hubble CA
|
||||||
// and does not ask for a client certificate, so only the CA is needed.
|
// and does not ask for a client certificate, so only the CA is needed.
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ require (
|
|||||||
github.com/prometheus/client_model v0.6.2 // indirect
|
github.com/prometheus/client_model v0.6.2 // indirect
|
||||||
github.com/prometheus/common v0.70.1 // indirect
|
github.com/prometheus/common v0.70.1 // indirect
|
||||||
github.com/prometheus/procfs v0.21.1 // indirect
|
github.com/prometheus/procfs v0.21.1 // indirect
|
||||||
|
github.com/spf13/pflag v1.0.6-0.20210604193023-d5e0c0615ace // indirect
|
||||||
github.com/x448/float16 v0.8.4 // indirect
|
github.com/x448/float16 v0.8.4 // indirect
|
||||||
golang.org/x/net v0.57.0 // indirect
|
golang.org/x/net v0.57.0 // indirect
|
||||||
golang.org/x/oauth2 v0.36.0 // indirect
|
golang.org/x/oauth2 v0.36.0 // indirect
|
||||||
|
|||||||
@@ -0,0 +1,184 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2026 The cluster-map Authors
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Package access decides what a viewer may see on the map, from the
|
||||||
|
// viewer's own RBAC.
|
||||||
|
//
|
||||||
|
// The map reads the cluster with the module's service account, which can
|
||||||
|
// read everything it shows. Without this package anyone admitted to the web
|
||||||
|
// interface would see every namespace and every log. With it, each request
|
||||||
|
// carries the viewer's token (the DexAuthenticator's Authorization header),
|
||||||
|
// and the API server answers SelfSubjectAccessReviews for that viewer:
|
||||||
|
// "list pods in all namespaces" means the whole cluster, otherwise the
|
||||||
|
// namespaces where the viewer can list pods. The answer is cached per token
|
||||||
|
// for a minute, so a page load costs one round of reviews, not one per call.
|
||||||
|
package access
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
|
"errors"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
authzv1 "k8s.io/api/authorization/v1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/client-go/kubernetes"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Scope is what one viewer may see.
|
||||||
|
type Scope struct {
|
||||||
|
// All: the viewer can list pods in every namespace; nothing is hidden.
|
||||||
|
All bool
|
||||||
|
// Namespaces the viewer can list pods in, when not All.
|
||||||
|
Namespaces map[string]bool
|
||||||
|
// Nodes: the viewer can list nodes (node cards, node view, node usage).
|
||||||
|
Nodes bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Everything is the scope when filtering is off.
|
||||||
|
var Everything = &Scope{All: true, Nodes: true}
|
||||||
|
|
||||||
|
// Namespace reports whether objects in ns are visible.
|
||||||
|
func (s *Scope) Namespace(ns string) bool {
|
||||||
|
return s.All || s.Namespaces[ns]
|
||||||
|
}
|
||||||
|
|
||||||
|
// ClientFactory returns a clientset authenticated as the viewer.
|
||||||
|
type ClientFactory func(token string) (kubernetes.Interface, error)
|
||||||
|
|
||||||
|
// ErrNoToken is returned when the request has no viewer token.
|
||||||
|
var ErrNoToken = errors.New("no user token in the request: per-user visibility needs the DexAuthenticator to send the Authorization header")
|
||||||
|
|
||||||
|
// Checker computes and caches scopes.
|
||||||
|
type Checker struct {
|
||||||
|
asViewer ClientFactory
|
||||||
|
namespaces func() []string // namespaces to check when the viewer is not cluster-wide
|
||||||
|
ttl time.Duration
|
||||||
|
now func() time.Time
|
||||||
|
|
||||||
|
mu sync.Mutex
|
||||||
|
cache map[[32]byte]entry
|
||||||
|
}
|
||||||
|
|
||||||
|
type entry struct {
|
||||||
|
scope *Scope
|
||||||
|
at time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
const maxEntries = 512
|
||||||
|
|
||||||
|
// NewChecker returns a checker; namespaces lists the cluster's namespaces (from the cache).
|
||||||
|
func NewChecker(asViewer ClientFactory, namespaces func() []string) *Checker {
|
||||||
|
return &Checker{asViewer: asViewer, namespaces: namespaces, ttl: time.Minute, now: time.Now, cache: map[[32]byte]entry{}}
|
||||||
|
}
|
||||||
|
|
||||||
|
// For returns the viewer's scope.
|
||||||
|
func (c *Checker) For(ctx context.Context, token string) (*Scope, error) {
|
||||||
|
if token == "" {
|
||||||
|
return nil, ErrNoToken
|
||||||
|
}
|
||||||
|
key := sha256.Sum256([]byte(token))
|
||||||
|
c.mu.Lock()
|
||||||
|
if e, ok := c.cache[key]; ok && c.now().Sub(e.at) < c.ttl {
|
||||||
|
c.mu.Unlock()
|
||||||
|
return e.scope, nil
|
||||||
|
}
|
||||||
|
c.mu.Unlock()
|
||||||
|
|
||||||
|
client, err := c.asViewer(token)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
scope, err := c.compute(ctx, client)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
c.mu.Lock()
|
||||||
|
if len(c.cache) >= maxEntries {
|
||||||
|
for k, e := range c.cache {
|
||||||
|
if c.now().Sub(e.at) >= c.ttl {
|
||||||
|
delete(c.cache, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(c.cache) >= maxEntries {
|
||||||
|
c.cache = map[[32]byte]entry{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.cache[key] = entry{scope, c.now()}
|
||||||
|
c.mu.Unlock()
|
||||||
|
return scope, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Checker) compute(ctx context.Context, client kubernetes.Interface) (*Scope, error) {
|
||||||
|
all, err := Can(ctx, client, "", "pods", "", "list", "")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
nodes, err := Can(ctx, client, "", "nodes", "", "list", "")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
s := &Scope{All: all, Nodes: nodes}
|
||||||
|
if all {
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
s.Namespaces = map[string]bool{}
|
||||||
|
type result struct {
|
||||||
|
ns string
|
||||||
|
ok bool
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
names := c.namespaces()
|
||||||
|
results := make(chan result, len(names))
|
||||||
|
sem := make(chan struct{}, 8)
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
for _, ns := range names {
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
sem <- struct{}{}
|
||||||
|
defer func() { <-sem }()
|
||||||
|
ok, err := Can(ctx, client, "", "pods", "", "list", ns)
|
||||||
|
results <- result{ns, ok, err}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
close(results)
|
||||||
|
for r := range results {
|
||||||
|
if r.err != nil {
|
||||||
|
return nil, r.err
|
||||||
|
}
|
||||||
|
if r.ok {
|
||||||
|
s.Namespaces[r.ns] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Can asks the API server whether the viewer behind client may perform the
|
||||||
|
// verb; namespace "" means cluster-wide (or all namespaces).
|
||||||
|
func Can(ctx context.Context, client kubernetes.Interface, group, resource, subresource, verb, namespace string) (bool, error) {
|
||||||
|
r, err := client.AuthorizationV1().SelfSubjectAccessReviews().Create(ctx, &authzv1.SelfSubjectAccessReview{
|
||||||
|
Spec: authzv1.SelfSubjectAccessReviewSpec{ResourceAttributes: &authzv1.ResourceAttributes{
|
||||||
|
Group: group, Resource: resource, Subresource: subresource, Verb: verb, Namespace: namespace,
|
||||||
|
}},
|
||||||
|
}, metav1.CreateOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return r.Status.Allowed, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2026 The cluster-map Authors
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package access
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"sync/atomic"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
authzv1 "k8s.io/api/authorization/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
|
"k8s.io/client-go/kubernetes"
|
||||||
|
"k8s.io/client-go/kubernetes/fake"
|
||||||
|
k8stesting "k8s.io/client-go/testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// viewer may list pods in "shop" only, and not nodes.
|
||||||
|
func factory(calls *atomic.Int32, allowAll bool) ClientFactory {
|
||||||
|
return func(string) (kubernetes.Interface, error) {
|
||||||
|
c := fake.NewSimpleClientset()
|
||||||
|
c.PrependReactor("create", "selfsubjectaccessreviews", func(a k8stesting.Action) (bool, runtime.Object, error) {
|
||||||
|
calls.Add(1)
|
||||||
|
r := a.(k8stesting.CreateAction).GetObject().(*authzv1.SelfSubjectAccessReview)
|
||||||
|
at := r.Spec.ResourceAttributes
|
||||||
|
r.Status.Allowed = allowAll || (at.Resource == "pods" && at.Namespace == "shop")
|
||||||
|
return true, r, nil
|
||||||
|
})
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScopeAndCache(t *testing.T) {
|
||||||
|
var calls atomic.Int32
|
||||||
|
c := NewChecker(factory(&calls, false), func() []string { return []string{"shop", "bank", "d8-system"} })
|
||||||
|
s, err := c.For(context.Background(), "tok")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if s.All || s.Nodes || !s.Namespace("shop") || s.Namespace("bank") {
|
||||||
|
t.Errorf("scope: %+v", s)
|
||||||
|
}
|
||||||
|
first := calls.Load()
|
||||||
|
if _, err := c.For(context.Background(), "tok"); err != nil || calls.Load() != first {
|
||||||
|
t.Errorf("second request within the TTL asked the API server again (%d -> %d)", first, calls.Load())
|
||||||
|
}
|
||||||
|
if _, err := c.For(context.Background(), ""); !errors.Is(err, ErrNoToken) {
|
||||||
|
t.Errorf("no token: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClusterWideViewerSkipsNamespaceChecks(t *testing.T) {
|
||||||
|
var calls atomic.Int32
|
||||||
|
c := NewChecker(factory(&calls, true), func() []string { return []string{"a", "b", "c"} })
|
||||||
|
s, err := c.For(context.Background(), "admin")
|
||||||
|
if err != nil || !s.All || !s.Nodes {
|
||||||
|
t.Fatalf("scope %+v, %v", s, err)
|
||||||
|
}
|
||||||
|
if calls.Load() != 2 { // pods cluster-wide and nodes
|
||||||
|
t.Errorf("cluster-wide viewer needed %d reviews, want 2", calls.Load())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,263 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2026 The cluster-map Authors
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Package actions runs the few changes the map offers (restart, scale,
|
||||||
|
// delete a pod, run a CronJob, cordon a node) as the viewer.
|
||||||
|
//
|
||||||
|
// Every call goes to the API server with the viewer's own bearer token,
|
||||||
|
// the one the DexAuthenticator puts into the Authorization header. The
|
||||||
|
// module's service account stays read-only, so the map can never do more
|
||||||
|
// than its viewer could do with kubectl, and the API server's audit log
|
||||||
|
// names the person, not cluster-map.
|
||||||
|
package actions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
authnv1 "k8s.io/api/authentication/v1"
|
||||||
|
authzv1 "k8s.io/api/authorization/v1"
|
||||||
|
batchv1 "k8s.io/api/batch/v1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/types"
|
||||||
|
"k8s.io/client-go/kubernetes"
|
||||||
|
"k8s.io/client-go/rest"
|
||||||
|
"k8s.io/client-go/util/retry"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Action names, as the UI sends them.
|
||||||
|
const (
|
||||||
|
Restart = "restart"
|
||||||
|
Scale = "scale"
|
||||||
|
DeletePod = "delete-pod"
|
||||||
|
Run = "run"
|
||||||
|
Suspend = "suspend"
|
||||||
|
Resume = "resume"
|
||||||
|
Cordon = "cordon"
|
||||||
|
Uncordon = "uncordon"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MaxReplicas bounds Scale; a typo in the dialog should not start 5000 pods.
|
||||||
|
const MaxReplicas = 100
|
||||||
|
|
||||||
|
// Request is the body of POST /api/action.
|
||||||
|
type Request struct {
|
||||||
|
Action string `json:"action"`
|
||||||
|
Kind string `json:"kind"` // deployment | statefulset | daemonset | cronjob | pod | node
|
||||||
|
Namespace string `json:"namespace"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Replicas *int32 `json:"replicas,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Request) String() string {
|
||||||
|
if r.Namespace == "" {
|
||||||
|
return r.Kind + "/" + r.Name
|
||||||
|
}
|
||||||
|
return r.Kind + " " + r.Namespace + "/" + r.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
// check is the RBAC question an action asks the API server first.
|
||||||
|
type check struct{ group, resource, subresource, verb string }
|
||||||
|
|
||||||
|
var byKind = map[string]map[string]check{
|
||||||
|
"deployment": {
|
||||||
|
Restart: {"apps", "deployments", "", "patch"},
|
||||||
|
Scale: {"apps", "deployments", "scale", "update"},
|
||||||
|
},
|
||||||
|
"statefulset": {
|
||||||
|
Restart: {"apps", "statefulsets", "", "patch"},
|
||||||
|
Scale: {"apps", "statefulsets", "scale", "update"},
|
||||||
|
},
|
||||||
|
"daemonset": {
|
||||||
|
Restart: {"apps", "daemonsets", "", "patch"},
|
||||||
|
},
|
||||||
|
"cronjob": {
|
||||||
|
Run: {"batch", "jobs", "", "create"},
|
||||||
|
Suspend: {"batch", "cronjobs", "", "patch"},
|
||||||
|
Resume: {"batch", "cronjobs", "", "patch"},
|
||||||
|
},
|
||||||
|
"pod": {
|
||||||
|
DeletePod: {"", "pods", "", "delete"},
|
||||||
|
},
|
||||||
|
"node": {
|
||||||
|
Cordon: {"", "nodes", "", "patch"},
|
||||||
|
Uncordon: {"", "nodes", "", "patch"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// ErrUnsupported is returned for an action the kind does not offer.
|
||||||
|
var ErrUnsupported = errors.New("this action is not offered for this kind of object")
|
||||||
|
|
||||||
|
// ClientFactory returns a clientset that authenticates as the viewer.
|
||||||
|
type ClientFactory func(token string) (kubernetes.Interface, error)
|
||||||
|
|
||||||
|
// NewClientFactory keeps base's API server address and CA and drops its
|
||||||
|
// credentials, so a request carries the viewer's token and nothing else.
|
||||||
|
func NewClientFactory(base *rest.Config) ClientFactory {
|
||||||
|
return func(token string) (kubernetes.Interface, error) {
|
||||||
|
cfg := rest.AnonymousClientConfig(base)
|
||||||
|
cfg.BearerToken = token
|
||||||
|
cfg.UserAgent = "cluster-map (on behalf of the viewer)"
|
||||||
|
cfg.Timeout = 15 * time.Second
|
||||||
|
return kubernetes.NewForConfig(cfg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WhoAmI returns the viewer's username as the API server sees it.
|
||||||
|
func WhoAmI(ctx context.Context, c kubernetes.Interface) (string, error) {
|
||||||
|
r, err := c.AuthenticationV1().SelfSubjectReviews().Create(ctx, &authnv1.SelfSubjectReview{}, metav1.CreateOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return r.Status.UserInfo.Username, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allowed asks the API server which of the kind's actions the viewer may
|
||||||
|
// run on the object.
|
||||||
|
func Allowed(ctx context.Context, c kubernetes.Interface, kind, namespace, name string) (map[string]bool, error) {
|
||||||
|
out := map[string]bool{}
|
||||||
|
for action, ch := range byKind[kind] {
|
||||||
|
ok, err := can(ctx, c, ch, namespace, name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out[action] = ok
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func can(ctx context.Context, c kubernetes.Interface, ch check, namespace, name string) (bool, error) {
|
||||||
|
attrs := &authzv1.ResourceAttributes{Group: ch.group, Resource: ch.resource, Subresource: ch.subresource, Verb: ch.verb, Namespace: namespace, Name: name}
|
||||||
|
if ch.verb == "create" {
|
||||||
|
attrs.Name = "" // a Job that does not exist yet
|
||||||
|
}
|
||||||
|
r, err := c.AuthorizationV1().SelfSubjectAccessReviews().Create(ctx, &authzv1.SelfSubjectAccessReview{
|
||||||
|
Spec: authzv1.SelfSubjectAccessReviewSpec{ResourceAttributes: attrs},
|
||||||
|
}, metav1.CreateOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return r.Status.Allowed, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do runs the action and returns a short description of what changed.
|
||||||
|
// Authorization is the API server's: a viewer without the right gets its
|
||||||
|
// Forbidden error back unchanged.
|
||||||
|
func Do(ctx context.Context, c kubernetes.Interface, r Request, now time.Time) (string, error) {
|
||||||
|
if _, ok := byKind[r.Kind][r.Action]; !ok {
|
||||||
|
return "", ErrUnsupported
|
||||||
|
}
|
||||||
|
if r.Name == "" || (r.Kind != "node" && r.Namespace == "") {
|
||||||
|
return "", errors.New("namespace and name are required")
|
||||||
|
}
|
||||||
|
switch r.Action {
|
||||||
|
case Restart:
|
||||||
|
// what kubectl rollout restart does
|
||||||
|
patch := fmt.Sprintf(`{"spec":{"template":{"metadata":{"annotations":{"kubectl.kubernetes.io/restartedAt":%q}}}}}`, now.UTC().Format(time.RFC3339))
|
||||||
|
var err error
|
||||||
|
switch r.Kind {
|
||||||
|
case "deployment":
|
||||||
|
_, err = c.AppsV1().Deployments(r.Namespace).Patch(ctx, r.Name, types.StrategicMergePatchType, []byte(patch), metav1.PatchOptions{})
|
||||||
|
case "statefulset":
|
||||||
|
_, err = c.AppsV1().StatefulSets(r.Namespace).Patch(ctx, r.Name, types.StrategicMergePatchType, []byte(patch), metav1.PatchOptions{})
|
||||||
|
case "daemonset":
|
||||||
|
_, err = c.AppsV1().DaemonSets(r.Namespace).Patch(ctx, r.Name, types.StrategicMergePatchType, []byte(patch), metav1.PatchOptions{})
|
||||||
|
}
|
||||||
|
return "rollout restarted", err
|
||||||
|
case Scale:
|
||||||
|
return scale(ctx, c, r)
|
||||||
|
case DeletePod:
|
||||||
|
return "pod deleted", c.CoreV1().Pods(r.Namespace).Delete(ctx, r.Name, metav1.DeleteOptions{})
|
||||||
|
case Run:
|
||||||
|
return runCronJob(ctx, c, r, now)
|
||||||
|
case Suspend, Resume:
|
||||||
|
patch := fmt.Sprintf(`{"spec":{"suspend":%t}}`, r.Action == Suspend)
|
||||||
|
_, err := c.BatchV1().CronJobs(r.Namespace).Patch(ctx, r.Name, types.MergePatchType, []byte(patch), metav1.PatchOptions{})
|
||||||
|
if r.Action == Suspend {
|
||||||
|
return "suspended", err
|
||||||
|
}
|
||||||
|
return "resumed", err
|
||||||
|
case Cordon, Uncordon:
|
||||||
|
patch := fmt.Sprintf(`{"spec":{"unschedulable":%t}}`, r.Action == Cordon)
|
||||||
|
_, err := c.CoreV1().Nodes().Patch(ctx, r.Name, types.MergePatchType, []byte(patch), metav1.PatchOptions{})
|
||||||
|
return r.Action + "ed", err
|
||||||
|
}
|
||||||
|
return "", ErrUnsupported
|
||||||
|
}
|
||||||
|
|
||||||
|
// scale reads and writes the scale subresource, retrying on a conflict:
|
||||||
|
// right after a restart the controller keeps updating the object, and a
|
||||||
|
// single Get+Update then failed with 409.
|
||||||
|
func scale(ctx context.Context, c kubernetes.Interface, r Request) (string, error) {
|
||||||
|
if r.Replicas == nil || *r.Replicas < 0 || *r.Replicas > MaxReplicas {
|
||||||
|
return "", fmt.Errorf("replicas must be between 0 and %d", MaxReplicas)
|
||||||
|
}
|
||||||
|
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
||||||
|
if r.Kind == "deployment" {
|
||||||
|
s, err := c.AppsV1().Deployments(r.Namespace).GetScale(ctx, r.Name, metav1.GetOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
s.Spec.Replicas = *r.Replicas
|
||||||
|
_, err = c.AppsV1().Deployments(r.Namespace).UpdateScale(ctx, r.Name, s, metav1.UpdateOptions{})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
s, err := c.AppsV1().StatefulSets(r.Namespace).GetScale(ctx, r.Name, metav1.GetOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
s.Spec.Replicas = *r.Replicas
|
||||||
|
_, err = c.AppsV1().StatefulSets(r.Namespace).UpdateScale(ctx, r.Name, s, metav1.UpdateOptions{})
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return "scaled to " + strconv.Itoa(int(*r.Replicas)), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// runCronJob creates a Job from the CronJob's template, as kubectl create
|
||||||
|
// job --from=cronjob/<name> does. The name ends in digits, so the map and
|
||||||
|
// Hubble both attribute its pods to the CronJob.
|
||||||
|
func runCronJob(ctx context.Context, c kubernetes.Interface, r Request, now time.Time) (string, error) {
|
||||||
|
cj, err := c.BatchV1().CronJobs(r.Namespace).Get(ctx, r.Name, metav1.GetOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
base := cj.Name
|
||||||
|
if len(base) > 52 { // Job names end up in a 63-character label
|
||||||
|
base = base[:52]
|
||||||
|
}
|
||||||
|
annotations := map[string]string{"cronjob.kubernetes.io/instantiate": "manual"}
|
||||||
|
for k, v := range cj.Spec.JobTemplate.Annotations {
|
||||||
|
annotations[k] = v
|
||||||
|
}
|
||||||
|
job := &batchv1.Job{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: base + "-" + strconv.FormatInt(now.Unix(), 10),
|
||||||
|
Namespace: cj.Namespace,
|
||||||
|
Labels: cj.Spec.JobTemplate.Labels,
|
||||||
|
Annotations: annotations,
|
||||||
|
OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(cj, batchv1.SchemeGroupVersion.WithKind("CronJob"))},
|
||||||
|
},
|
||||||
|
Spec: cj.Spec.JobTemplate.Spec,
|
||||||
|
}
|
||||||
|
created, err := c.BatchV1().Jobs(r.Namespace).Create(ctx, job, metav1.CreateOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return "job " + created.Name + " created", nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2026 The cluster-map Authors
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package actions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
appsv1 "k8s.io/api/apps/v1"
|
||||||
|
autoscalingv1 "k8s.io/api/autoscaling/v1"
|
||||||
|
batchv1 "k8s.io/api/batch/v1"
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
|
"k8s.io/client-go/kubernetes/fake"
|
||||||
|
k8stesting "k8s.io/client-go/testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
var t0 = time.Date(2026, 9, 24, 12, 0, 0, 0, time.UTC)
|
||||||
|
|
||||||
|
func TestRestartSuspendCordon(t *testing.T) {
|
||||||
|
c := fake.NewSimpleClientset(
|
||||||
|
&appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "web", Namespace: "shop"}},
|
||||||
|
&batchv1.CronJob{ObjectMeta: metav1.ObjectMeta{Name: "backup", Namespace: "shop"}},
|
||||||
|
&corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: "node-a"}},
|
||||||
|
)
|
||||||
|
ctx := context.Background()
|
||||||
|
if _, err := Do(ctx, c, Request{Action: Restart, Kind: "deployment", Namespace: "shop", Name: "web"}, t0); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
d, _ := c.AppsV1().Deployments("shop").Get(ctx, "web", metav1.GetOptions{})
|
||||||
|
if d.Spec.Template.Annotations["kubectl.kubernetes.io/restartedAt"] != "2026-09-24T12:00:00Z" {
|
||||||
|
t.Errorf("restart annotation: %v", d.Spec.Template.Annotations)
|
||||||
|
}
|
||||||
|
if msg, err := Do(ctx, c, Request{Action: Suspend, Kind: "cronjob", Namespace: "shop", Name: "backup"}, t0); err != nil || msg != "suspended" {
|
||||||
|
t.Fatal(msg, err)
|
||||||
|
}
|
||||||
|
cj, _ := c.BatchV1().CronJobs("shop").Get(ctx, "backup", metav1.GetOptions{})
|
||||||
|
if cj.Spec.Suspend == nil || !*cj.Spec.Suspend {
|
||||||
|
t.Error("cronjob not suspended")
|
||||||
|
}
|
||||||
|
if _, err := Do(ctx, c, Request{Action: Cordon, Kind: "node", Name: "node-a"}, t0); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
n, _ := c.CoreV1().Nodes().Get(ctx, "node-a", metav1.GetOptions{})
|
||||||
|
if !n.Spec.Unschedulable {
|
||||||
|
t.Error("node not cordoned")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRunCronJob(t *testing.T) {
|
||||||
|
c := fake.NewSimpleClientset(&batchv1.CronJob{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{Name: "backup", Namespace: "shop", UID: "u1"},
|
||||||
|
Spec: batchv1.CronJobSpec{JobTemplate: batchv1.JobTemplateSpec{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": "backup"}},
|
||||||
|
Spec: batchv1.JobSpec{Template: corev1.PodTemplateSpec{Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "dump"}}}}},
|
||||||
|
}},
|
||||||
|
})
|
||||||
|
ctx := context.Background()
|
||||||
|
msg, err := Do(ctx, c, Request{Action: Run, Kind: "cronjob", Namespace: "shop", Name: "backup"}, t0)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
jobs, _ := c.BatchV1().Jobs("shop").List(ctx, metav1.ListOptions{})
|
||||||
|
if len(jobs.Items) != 1 || !strings.Contains(msg, jobs.Items[0].Name) {
|
||||||
|
t.Fatalf("jobs: %+v, msg %q", jobs.Items, msg)
|
||||||
|
}
|
||||||
|
j := jobs.Items[0]
|
||||||
|
if j.Name != "backup-1790251200" || j.OwnerReferences[0].Kind != "CronJob" || j.OwnerReferences[0].UID != "u1" ||
|
||||||
|
j.Annotations["cronjob.kubernetes.io/instantiate"] != "manual" || j.Labels["app"] != "backup" {
|
||||||
|
t.Errorf("job: %+v", j.ObjectMeta)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScaleRetriesConflict(t *testing.T) {
|
||||||
|
c := fake.NewSimpleClientset()
|
||||||
|
var updated int32 = -1
|
||||||
|
attempts := 0
|
||||||
|
c.PrependReactor("get", "deployments", func(k8stesting.Action) (bool, runtime.Object, error) {
|
||||||
|
return true, &autoscalingv1.Scale{ObjectMeta: metav1.ObjectMeta{Name: "web", Namespace: "shop"}, Spec: autoscalingv1.ScaleSpec{Replicas: 1}}, nil
|
||||||
|
})
|
||||||
|
c.PrependReactor("update", "deployments", func(a k8stesting.Action) (bool, runtime.Object, error) {
|
||||||
|
attempts++
|
||||||
|
if attempts == 1 { // the controller got there first
|
||||||
|
return true, nil, apierrors.NewConflict(schema.GroupResource{Group: "apps", Resource: "deployments"}, "web", nil)
|
||||||
|
}
|
||||||
|
s := a.(k8stesting.UpdateAction).GetObject().(*autoscalingv1.Scale)
|
||||||
|
updated = s.Spec.Replicas
|
||||||
|
return true, s, nil
|
||||||
|
})
|
||||||
|
three := int32(3)
|
||||||
|
if _, err := Do(context.Background(), c, Request{Action: Scale, Kind: "deployment", Namespace: "shop", Name: "web", Replicas: &three}, t0); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if updated != 3 || attempts != 2 {
|
||||||
|
t.Errorf("scale sent %d after %d attempts", updated, attempts)
|
||||||
|
}
|
||||||
|
tooMany := int32(MaxReplicas + 1)
|
||||||
|
if _, err := Do(context.Background(), c, Request{Action: Scale, Kind: "deployment", Namespace: "shop", Name: "web", Replicas: &tooMany}, t0); err == nil {
|
||||||
|
t.Error("replicas above the limit accepted")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnsupported(t *testing.T) {
|
||||||
|
c := fake.NewSimpleClientset()
|
||||||
|
for _, r := range []Request{
|
||||||
|
{Action: Scale, Kind: "daemonset", Namespace: "a", Name: "b"},
|
||||||
|
{Action: "exec", Kind: "pod", Namespace: "a", Name: "b"},
|
||||||
|
{Action: DeletePod, Kind: "pod", Name: "b"},
|
||||||
|
} {
|
||||||
|
if _, err := Do(context.Background(), c, r, t0); err == nil {
|
||||||
|
t.Errorf("%+v accepted", r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -57,6 +57,16 @@ type Config struct {
|
|||||||
// Namespaces matching these prefixes/names are grouped as "system".
|
// Namespaces matching these prefixes/names are grouped as "system".
|
||||||
SystemNamespacePrefixes []string
|
SystemNamespacePrefixes []string
|
||||||
LogTailLines int64
|
LogTailLines int64
|
||||||
|
|
||||||
|
// ActionsEnabled turns on restart/scale/delete and the rest. They run
|
||||||
|
// with the viewer's own token (the Authorization header the
|
||||||
|
// DexAuthenticator adds), never with the module's service account.
|
||||||
|
ActionsEnabled bool
|
||||||
|
|
||||||
|
// ViewerVisibility limits what each viewer sees to what the viewer's
|
||||||
|
// own RBAC can read (namespaces where they can list pods). Off, anyone
|
||||||
|
// admitted to the web interface sees the whole cluster.
|
||||||
|
ViewerVisibility bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load reads and validates the configuration from the environment.
|
// Load reads and validates the configuration from the environment.
|
||||||
@@ -83,6 +93,16 @@ func Load() (Config, error) {
|
|||||||
if c.LogTailLines, err = strconv.ParseInt(env("LOG_TAIL_LINES", "500"), 10, 64); err != nil {
|
if c.LogTailLines, err = strconv.ParseInt(env("LOG_TAIL_LINES", "500"), 10, 64); err != nil {
|
||||||
return c, fmt.Errorf("LOG_TAIL_LINES: %w", err)
|
return c, fmt.Errorf("LOG_TAIL_LINES: %w", err)
|
||||||
}
|
}
|
||||||
|
if c.ActionsEnabled, err = strconv.ParseBool(env("ACTIONS_ENABLED", "false")); err != nil {
|
||||||
|
return c, fmt.Errorf("ACTIONS_ENABLED: %w", err)
|
||||||
|
}
|
||||||
|
switch v := env("VISIBILITY", "ViewerRBAC"); v {
|
||||||
|
case "ViewerRBAC":
|
||||||
|
c.ViewerVisibility = true
|
||||||
|
case "Everything":
|
||||||
|
default:
|
||||||
|
return c, fmt.Errorf("VISIBILITY: %q is not one of ViewerRBAC, Everything", v)
|
||||||
|
}
|
||||||
switch c.TrafficMode {
|
switch c.TrafficMode {
|
||||||
case TrafficContinuous, TrafficOnDemand, TrafficDisabled:
|
case TrafficContinuous, TrafficOnDemand, TrafficDisabled:
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -21,22 +21,47 @@ package kube
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/api/meta"
|
"k8s.io/apimachinery/pkg/api/meta"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
|
"k8s.io/apimachinery/pkg/fields"
|
||||||
"k8s.io/apimachinery/pkg/labels"
|
"k8s.io/apimachinery/pkg/labels"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
|
"k8s.io/client-go/dynamic"
|
||||||
|
"k8s.io/client-go/dynamic/dynamicinformer"
|
||||||
"k8s.io/client-go/informers"
|
"k8s.io/client-go/informers"
|
||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
appslisters "k8s.io/client-go/listers/apps/v1"
|
appslisters "k8s.io/client-go/listers/apps/v1"
|
||||||
batchlisters "k8s.io/client-go/listers/batch/v1"
|
batchlisters "k8s.io/client-go/listers/batch/v1"
|
||||||
corelisters "k8s.io/client-go/listers/core/v1"
|
corelisters "k8s.io/client-go/listers/core/v1"
|
||||||
netlisters "k8s.io/client-go/listers/networking/v1"
|
netlisters "k8s.io/client-go/listers/networking/v1"
|
||||||
|
"k8s.io/client-go/tools/cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ClusterAlerts is the resource Deckhouse's prometheus module mirrors
|
||||||
|
// firing Alertmanager alerts into.
|
||||||
|
var ClusterAlerts = schema.GroupVersionResource{Group: "deckhouse.io", Version: "v1alpha1", Resource: "clusteralerts"}
|
||||||
|
|
||||||
|
// VulnerabilityReports are what operator-trivy writes per scanned
|
||||||
|
// container (namespaced, owned by the workload's ReplicaSet/StatefulSet/…).
|
||||||
|
var VulnerabilityReports = schema.GroupVersionResource{Group: "aquasecurity.github.io", Version: "v1alpha1", Resource: "vulnerabilityreports"}
|
||||||
|
|
||||||
// Cache holds synced listers plus a client for reads that must be live
|
// Cache holds synced listers plus a client for reads that must be live
|
||||||
// (YAML view, logs).
|
// (YAML view, logs).
|
||||||
type Cache struct {
|
type Cache struct {
|
||||||
Client kubernetes.Interface
|
Client kubernetes.Interface
|
||||||
|
// Events holds Warning events only; Normal ones are most of the event
|
||||||
|
// volume and none of them is a problem.
|
||||||
|
Events corelisters.EventLister
|
||||||
|
// Alerts is nil when the ClusterAlert resource is not served (the
|
||||||
|
// prometheus module is off).
|
||||||
|
Alerts cache.GenericLister
|
||||||
|
// VulnReports is nil when operator-trivy is not installed.
|
||||||
|
VulnReports cache.GenericLister
|
||||||
Namespaces corelisters.NamespaceLister
|
Namespaces corelisters.NamespaceLister
|
||||||
Pods corelisters.PodLister
|
Pods corelisters.PodLister
|
||||||
Services corelisters.ServiceLister
|
Services corelisters.ServiceLister
|
||||||
@@ -48,16 +73,24 @@ type Cache struct {
|
|||||||
Ingresses netlisters.IngressLister
|
Ingresses netlisters.IngressLister
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start runs the informers and blocks until every cache has synced or ctx
|
// stripManagedFields: managedFields is most of a cached object's size and
|
||||||
// is done.
|
// nothing here reads it.
|
||||||
func Start(ctx context.Context, client kubernetes.Interface, syncTimeout time.Duration) (*Cache, error) {
|
func stripManagedFields(obj any) (any, error) {
|
||||||
f := informers.NewSharedInformerFactoryWithOptions(client, 0,
|
|
||||||
// managedFields is most of a cached object's size and nothing here reads it
|
|
||||||
informers.WithTransform(func(obj any) (any, error) {
|
|
||||||
if a, err := meta.Accessor(obj); err == nil {
|
if a, err := meta.Accessor(obj); err == nil {
|
||||||
a.SetManagedFields(nil)
|
a.SetManagedFields(nil)
|
||||||
}
|
}
|
||||||
return obj, nil
|
return obj, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start runs the informers and blocks until every cache has synced or ctx
|
||||||
|
// is done.
|
||||||
|
func Start(ctx context.Context, client kubernetes.Interface, dyn dynamic.Interface, syncTimeout time.Duration) (*Cache, error) {
|
||||||
|
f := informers.NewSharedInformerFactoryWithOptions(client, 0, informers.WithTransform(stripManagedFields))
|
||||||
|
// A separate factory: a list option tweak applies to every informer of
|
||||||
|
// the factory it is set on.
|
||||||
|
ef := informers.NewSharedInformerFactoryWithOptions(client, 0, informers.WithTransform(stripManagedFields),
|
||||||
|
informers.WithTweakListOptions(func(o *metav1.ListOptions) {
|
||||||
|
o.FieldSelector = fields.OneTermEqualSelector("type", corev1.EventTypeWarning).String()
|
||||||
}))
|
}))
|
||||||
c := &Cache{
|
c := &Cache{
|
||||||
Client: client,
|
Client: client,
|
||||||
@@ -70,8 +103,31 @@ func Start(ctx context.Context, client kubernetes.Interface, syncTimeout time.Du
|
|||||||
DaemonSets: f.Apps().V1().DaemonSets().Lister(),
|
DaemonSets: f.Apps().V1().DaemonSets().Lister(),
|
||||||
CronJobs: f.Batch().V1().CronJobs().Lister(),
|
CronJobs: f.Batch().V1().CronJobs().Lister(),
|
||||||
Ingresses: f.Networking().V1().Ingresses().Lister(),
|
Ingresses: f.Networking().V1().Ingresses().Lister(),
|
||||||
|
Events: ef.Core().V1().Events().Lister(),
|
||||||
|
}
|
||||||
|
var df dynamicinformer.DynamicSharedInformerFactory
|
||||||
|
if dyn != nil {
|
||||||
|
df = dynamicinformer.NewDynamicSharedInformerFactory(dyn, 0)
|
||||||
|
}
|
||||||
|
if df != nil && served(client, ClusterAlerts) {
|
||||||
|
c.Alerts = df.ForResource(ClusterAlerts).Lister()
|
||||||
|
} else {
|
||||||
|
slog.Info("ClusterAlert resource is not served; alerts are not shown")
|
||||||
|
}
|
||||||
|
if df != nil && served(client, VulnerabilityReports) {
|
||||||
|
inf := df.ForResource(VulnerabilityReports)
|
||||||
|
if err := inf.Informer().SetTransform(trimVulnReport); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
c.VulnReports = inf.Lister()
|
||||||
|
} else {
|
||||||
|
slog.Info("VulnerabilityReport resource is not served; vulnerabilities are not shown")
|
||||||
}
|
}
|
||||||
f.Start(ctx.Done())
|
f.Start(ctx.Done())
|
||||||
|
ef.Start(ctx.Done())
|
||||||
|
if df != nil {
|
||||||
|
df.Start(ctx.Done())
|
||||||
|
}
|
||||||
|
|
||||||
syncCtx, cancel := context.WithTimeout(ctx, syncTimeout)
|
syncCtx, cancel := context.WithTimeout(ctx, syncTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
@@ -80,9 +136,102 @@ func Start(ctx context.Context, client kubernetes.Interface, syncTimeout time.Du
|
|||||||
return nil, fmt.Errorf("cache for %v did not sync within %s", t, syncTimeout)
|
return nil, fmt.Errorf("cache for %v did not sync within %s", t, syncTimeout)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for t, ok := range ef.WaitForCacheSync(syncCtx.Done()) {
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("cache for %v did not sync within %s", t, syncTimeout)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if df != nil {
|
||||||
|
for r, ok := range df.WaitForCacheSync(syncCtx.Done()) {
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("cache for %v did not sync within %s", r, syncTimeout)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// served reports whether the API server serves the resource, so an
|
||||||
|
// informer is not started for a CRD that is not installed.
|
||||||
|
func served(client kubernetes.Interface, gvr schema.GroupVersionResource) bool {
|
||||||
|
list, err := client.Discovery().ServerResourcesForGroupVersion(gvr.GroupVersion().String())
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, r := range list.APIResources {
|
||||||
|
if r.Name == gvr.Resource {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// vulnFields are the parts of a vulnerability entry the map shows; the
|
||||||
|
// rest (links, PURLs, dates) is dropped before caching.
|
||||||
|
var vulnFields = []string{"vulnerabilityID", "severity", "resource", "installedVersion", "fixedVersion", "title", "primaryLink", "score"}
|
||||||
|
|
||||||
|
func trimVulnReport(obj any) (any, error) {
|
||||||
|
u, ok := obj.(*unstructured.Unstructured)
|
||||||
|
if !ok {
|
||||||
|
return obj, nil
|
||||||
|
}
|
||||||
|
u.SetManagedFields(nil)
|
||||||
|
vulns, found, _ := unstructured.NestedSlice(u.Object, "report", "vulnerabilities")
|
||||||
|
if !found {
|
||||||
|
return u, nil
|
||||||
|
}
|
||||||
|
for i, v := range vulns {
|
||||||
|
m, ok := v.(map[string]any)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
kept := make(map[string]any, len(vulnFields))
|
||||||
|
for _, f := range vulnFields {
|
||||||
|
if x, ok := m[f]; ok {
|
||||||
|
kept[f] = x
|
||||||
|
}
|
||||||
|
}
|
||||||
|
vulns[i] = kept
|
||||||
|
}
|
||||||
|
return u, unstructured.SetNestedSlice(u.Object, vulns, "report", "vulnerabilities")
|
||||||
|
}
|
||||||
|
|
||||||
|
// VulnReportList returns the cached VulnerabilityReports, nil when off.
|
||||||
|
func (c *Cache) VulnReportList() []*unstructured.Unstructured {
|
||||||
|
if c.VulnReports == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
objs, err := c.VulnReports.List(labels.Everything())
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
out := make([]*unstructured.Unstructured, 0, len(objs))
|
||||||
|
for _, o := range objs {
|
||||||
|
if u, ok := o.(*unstructured.Unstructured); ok {
|
||||||
|
out = append(out, u)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// AlertList returns the cached ClusterAlerts, or nil when they are off.
|
||||||
|
func (c *Cache) AlertList() []*unstructured.Unstructured {
|
||||||
|
if c.Alerts == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
objs, err := c.Alerts.List(labels.Everything())
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
out := make([]*unstructured.Unstructured, 0, len(objs))
|
||||||
|
for _, o := range objs {
|
||||||
|
if u, ok := o.(*unstructured.Unstructured); ok {
|
||||||
|
out = append(out, u)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
// List reads a lister as values. Callers must not mutate cached objects,
|
// List reads a lister as values. Callers must not mutate cached objects,
|
||||||
// and copying the top-level struct is cheap.
|
// and copying the top-level struct is cheap.
|
||||||
func List[T any](l interface {
|
func List[T any](l interface {
|
||||||
|
|||||||
@@ -37,6 +37,9 @@ var (
|
|||||||
HTTPRequests = prometheus.NewCounterVec(prometheus.CounterOpts{
|
HTTPRequests = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||||
Name: "cluster_map_http_requests_total", Help: "HTTP API requests by handler and status code.",
|
Name: "cluster_map_http_requests_total", Help: "HTTP API requests by handler and status code.",
|
||||||
}, []string{"handler", "code"})
|
}, []string{"handler", "code"})
|
||||||
|
Actions = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||||
|
Name: "cluster_map_actions_total", Help: "Actions run from the UI, by action and result (ok, denied, error).",
|
||||||
|
}, []string{"action", "result"})
|
||||||
HTTPDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
HTTPDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
||||||
Name: "cluster_map_http_request_duration_seconds", Help: "HTTP API latency by handler.",
|
Name: "cluster_map_http_request_duration_seconds", Help: "HTTP API latency by handler.",
|
||||||
Buckets: []float64{.001, .005, .01, .05, .1, .5, 1, 5},
|
Buckets: []float64{.001, .005, .01, .05, .1, .5, 1, 5},
|
||||||
@@ -48,7 +51,7 @@ func init() {
|
|||||||
collectors.NewGoCollector(),
|
collectors.NewGoCollector(),
|
||||||
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
|
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
|
||||||
HubbleStreamActive, HubbleConnected, HubbleReconnects, FlowsProcessed, TrackedTuples, CacheSynced,
|
HubbleStreamActive, HubbleConnected, HubbleReconnects, FlowsProcessed, TrackedTuples, CacheSynced,
|
||||||
HTTPRequests, HTTPDuration,
|
HTTPRequests, HTTPDuration, Actions,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,187 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2026 The cluster-map Authors
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"log/slog"
|
||||||
|
"mime"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
|
"k8s.io/client-go/kubernetes"
|
||||||
|
|
||||||
|
"git.project-chimera.ru/fanchji/cluster-map/internal/actions"
|
||||||
|
"git.project-chimera.ru/fanchji/cluster-map/internal/metrics"
|
||||||
|
)
|
||||||
|
|
||||||
|
// viewerToken is the bearer token the DexAuthenticator forwarded, or "".
|
||||||
|
func viewerToken(r *http.Request) string {
|
||||||
|
h := r.Header.Get("Authorization")
|
||||||
|
if len(h) > 7 && strings.EqualFold(h[:7], "Bearer ") {
|
||||||
|
return strings.TrimSpace(h[7:])
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// viewerClient returns a client acting as the viewer, or writes the reason
|
||||||
|
// there is none.
|
||||||
|
func (s *Server) viewerClient(w http.ResponseWriter, r *http.Request) (kubernetes.Interface, bool) {
|
||||||
|
if !s.cfg.ActionsEnabled || s.asViewer == nil {
|
||||||
|
http.Error(w, "actions are disabled in the module settings", http.StatusNotFound)
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
token := viewerToken(r)
|
||||||
|
if token == "" {
|
||||||
|
http.Error(w, "no user token in the request: actions need the DexAuthenticator to send the Authorization header", http.StatusUnauthorized)
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
c, err := s.asViewer(token)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
return c, true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Me is the /api/me response.
|
||||||
|
type Me struct {
|
||||||
|
ActionsEnabled bool `json:"actionsEnabled"`
|
||||||
|
User string `json:"user,omitempty"`
|
||||||
|
Reason string `json:"reason,omitempty"` // why actions are unavailable: disabled | noToken | rejected
|
||||||
|
// Visibility: "all" (the whole cluster), "namespaces" (Namespaces of
|
||||||
|
// them, by the viewer's RBAC) or "" when it could not be determined.
|
||||||
|
Visibility string `json:"visibility,omitempty"`
|
||||||
|
Namespaces int `json:"namespaces,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleMe(w http.ResponseWriter, r *http.Request) {
|
||||||
|
me := Me{ActionsEnabled: s.cfg.ActionsEnabled && s.asViewer != nil}
|
||||||
|
token := viewerToken(r)
|
||||||
|
switch {
|
||||||
|
case s.access == nil:
|
||||||
|
me.Visibility = "all"
|
||||||
|
case token != "":
|
||||||
|
if sc, err := s.access.For(r.Context(), token); err == nil {
|
||||||
|
me.Visibility, me.Namespaces = "namespaces", len(sc.Namespaces)
|
||||||
|
if sc.All {
|
||||||
|
me.Visibility, me.Namespaces = "all", 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
switch {
|
||||||
|
case !me.ActionsEnabled:
|
||||||
|
me.Reason = "disabled"
|
||||||
|
case token == "":
|
||||||
|
me.Reason = "noToken"
|
||||||
|
default:
|
||||||
|
c, err := s.asViewer(token)
|
||||||
|
if err == nil {
|
||||||
|
me.User, err = actions.WhoAmI(r.Context(), c)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
slog.Info("viewer token rejected", "err", err)
|
||||||
|
me.Reason = "rejected"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writeJSON(w, me)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handlePermissions(w http.ResponseWriter, r *http.Request) {
|
||||||
|
c, ok := s.viewerClient(w, r)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
q := r.URL.Query()
|
||||||
|
allowed, err := actions.Allowed(r.Context(), c, q.Get("kind"), q.Get("namespace"), q.Get("name"))
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), apiStatus(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeJSON(w, map[string]any{"allowed": allowed})
|
||||||
|
}
|
||||||
|
|
||||||
|
// sameOrigin rejects cross-site writes: the Fetch metadata header that
|
||||||
|
// every current browser sends, then Origin for older ones. A JSON content
|
||||||
|
// type also keeps a plain HTML form from reaching the handler.
|
||||||
|
func sameOrigin(r *http.Request) error {
|
||||||
|
if ct, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")); ct != "application/json" {
|
||||||
|
return errors.New("content type must be application/json")
|
||||||
|
}
|
||||||
|
switch r.Header.Get("Sec-Fetch-Site") {
|
||||||
|
case "", "same-origin":
|
||||||
|
default:
|
||||||
|
return errors.New("cross-site request")
|
||||||
|
}
|
||||||
|
if o := r.Header.Get("Origin"); o != "" {
|
||||||
|
u, err := url.Parse(o)
|
||||||
|
if err != nil || u.Host != r.Host {
|
||||||
|
return errors.New("origin does not match the host")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleAction(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if err := sameOrigin(r); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusForbidden)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c, ok := s.viewerClient(w, r)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var req actions.Request
|
||||||
|
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4096)).Decode(&req); err != nil {
|
||||||
|
http.Error(w, "bad request body: "+err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
user, err := actions.WhoAmI(r.Context(), c)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "the API server did not accept the user token: "+err.Error(), http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
msg, err := actions.Do(r.Context(), c, req, time.Now())
|
||||||
|
// The API server's audit log has the same record under the user's name;
|
||||||
|
// this one ties it to the map.
|
||||||
|
log := slog.With("user", user, "action", req.Action, "target", req.String())
|
||||||
|
switch {
|
||||||
|
case err == nil:
|
||||||
|
log.Info("action done", "result", msg)
|
||||||
|
metrics.Actions.WithLabelValues(req.Action, "ok").Inc()
|
||||||
|
writeJSON(w, map[string]string{"result": msg})
|
||||||
|
case errors.Is(err, actions.ErrUnsupported):
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
case apierrors.IsForbidden(err):
|
||||||
|
log.Info("action denied", "err", err)
|
||||||
|
metrics.Actions.WithLabelValues(req.Action, "denied").Inc()
|
||||||
|
http.Error(w, err.Error(), http.StatusForbidden)
|
||||||
|
default:
|
||||||
|
log.Warn("action failed", "err", err)
|
||||||
|
metrics.Actions.WithLabelValues(req.Action, "error").Inc()
|
||||||
|
code := http.StatusBadRequest // Do's own validation errors
|
||||||
|
var st apierrors.APIStatus
|
||||||
|
if errors.As(err, &st) && st.Status().Code != 0 {
|
||||||
|
code = int(st.Status().Code)
|
||||||
|
}
|
||||||
|
http.Error(w, err.Error(), code)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,152 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2026 The cluster-map Authors
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"testing/fstest"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
appsv1 "k8s.io/api/apps/v1"
|
||||||
|
authnv1 "k8s.io/api/authentication/v1"
|
||||||
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
|
"k8s.io/client-go/kubernetes"
|
||||||
|
"k8s.io/client-go/kubernetes/fake"
|
||||||
|
k8stesting "k8s.io/client-go/testing"
|
||||||
|
|
||||||
|
"git.project-chimera.ru/fanchji/cluster-map/internal/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
// actionServer: tokens are checked by the fake "API server": "good" is
|
||||||
|
// alice, anything else is rejected; alice may patch deployments only.
|
||||||
|
func actionServer(t *testing.T, enabled bool) (http.Handler, *fake.Clientset) {
|
||||||
|
t.Helper()
|
||||||
|
c := fake.NewSimpleClientset(&appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "web", Namespace: "shop"}})
|
||||||
|
c.PrependReactor("create", "selfsubjectreviews", func(k8stesting.Action) (bool, runtime.Object, error) {
|
||||||
|
return true, &authnv1.SelfSubjectReview{Status: authnv1.SelfSubjectReviewStatus{UserInfo: authnv1.UserInfo{Username: "alice@example.com"}}}, nil
|
||||||
|
})
|
||||||
|
c.PrependReactor("patch", "nodes", func(k8stesting.Action) (bool, runtime.Object, error) {
|
||||||
|
return true, nil, apierrors.NewForbidden(schema.GroupResource{Resource: "nodes"}, "node-a", nil)
|
||||||
|
})
|
||||||
|
factory := func(token string) (kubernetes.Interface, error) {
|
||||||
|
if token != "good" {
|
||||||
|
return fake.NewSimpleClientset(), nil
|
||||||
|
}
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
cfg := config.Config{TrafficMode: config.TrafficDisabled, Retention: time.Hour, ActionsEnabled: enabled}
|
||||||
|
s := New(cfg, fstest.MapFS{"index.html": {Data: []byte("x")}}, factory)
|
||||||
|
s.ready.Store(true)
|
||||||
|
return s.Handler(), c
|
||||||
|
}
|
||||||
|
|
||||||
|
func post(h http.Handler, body string, headers map[string]string) *httptest.ResponseRecorder {
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "http://cluster-map.example.com/api/action", strings.NewReader(body))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
for k, v := range headers {
|
||||||
|
req.Header.Set(k, v)
|
||||||
|
}
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
h.ServeHTTP(rec, req)
|
||||||
|
return rec
|
||||||
|
}
|
||||||
|
|
||||||
|
const restartWeb = `{"action":"restart","kind":"deployment","namespace":"shop","name":"web"}`
|
||||||
|
|
||||||
|
func TestActionRunsAsViewer(t *testing.T) {
|
||||||
|
h, c := actionServer(t, true)
|
||||||
|
rec := post(h, restartWeb, map[string]string{"Authorization": "Bearer good", "Sec-Fetch-Site": "same-origin", "Origin": "http://cluster-map.example.com"})
|
||||||
|
if rec.Code != http.StatusOK {
|
||||||
|
t.Fatalf("%d %s", rec.Code, rec.Body)
|
||||||
|
}
|
||||||
|
var patched bool
|
||||||
|
for _, a := range c.Actions() {
|
||||||
|
if a.GetVerb() == "patch" && a.GetResource().Resource == "deployments" {
|
||||||
|
patched = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !patched {
|
||||||
|
t.Error("the viewer's client did not patch the deployment")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestActionGuards(t *testing.T) {
|
||||||
|
h, _ := actionServer(t, true)
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
body string
|
||||||
|
headers map[string]string
|
||||||
|
want int
|
||||||
|
}{
|
||||||
|
{"no token", restartWeb, map[string]string{"Sec-Fetch-Site": "same-origin"}, http.StatusUnauthorized},
|
||||||
|
{"cross-site", restartWeb, map[string]string{"Authorization": "Bearer good", "Sec-Fetch-Site": "cross-site"}, http.StatusForbidden},
|
||||||
|
{"foreign origin", restartWeb, map[string]string{"Authorization": "Bearer good", "Origin": "https://evil.example.org"}, http.StatusForbidden},
|
||||||
|
{"form post", restartWeb, map[string]string{"Authorization": "Bearer good", "Content-Type": "application/x-www-form-urlencoded"}, http.StatusForbidden},
|
||||||
|
{"api forbids", `{"action":"cordon","kind":"node","name":"node-a"}`, map[string]string{"Authorization": "Bearer good"}, http.StatusForbidden},
|
||||||
|
{"unsupported", `{"action":"scale","kind":"daemonset","namespace":"shop","name":"x","replicas":1}`, map[string]string{"Authorization": "Bearer good"}, http.StatusBadRequest},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
t.Run(c.name, func(t *testing.T) {
|
||||||
|
if rec := post(h, c.body, c.headers); rec.Code != c.want {
|
||||||
|
t.Errorf("got %d (%s), want %d", rec.Code, strings.TrimSpace(rec.Body.String()), c.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
off, _ := actionServer(t, false)
|
||||||
|
if rec := post(off, restartWeb, map[string]string{"Authorization": "Bearer good"}); rec.Code != http.StatusNotFound {
|
||||||
|
t.Errorf("actions disabled: %d", rec.Code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMe(t *testing.T) {
|
||||||
|
h, _ := actionServer(t, true)
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "/api/me", nil)
|
||||||
|
req.Header.Set("Authorization", "Bearer good")
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
h.ServeHTTP(rec, req)
|
||||||
|
if !strings.Contains(rec.Body.String(), `"user":"alice@example.com"`) {
|
||||||
|
t.Errorf("me: %s", rec.Body)
|
||||||
|
}
|
||||||
|
if body := get(h, "/api/me").Body.String(); !strings.Contains(body, `"reason":"noToken"`) {
|
||||||
|
t.Errorf("me without a token: %s", body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStaticRevalidation(t *testing.T) {
|
||||||
|
_, h := newServer(t, config.TrafficDisabled)
|
||||||
|
first := get(h, "/")
|
||||||
|
tag := first.Header().Get("ETag")
|
||||||
|
if tag == "" || first.Header().Get("Cache-Control") != "no-cache" {
|
||||||
|
t.Fatalf("static headers: %v", first.Header())
|
||||||
|
}
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||||
|
req.Header.Set("If-None-Match", tag)
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
h.ServeHTTP(rec, req)
|
||||||
|
if rec.Code != http.StatusNotModified {
|
||||||
|
t.Errorf("revalidation: %d", rec.Code)
|
||||||
|
}
|
||||||
|
if get(h, "/api/graph").Header().Get("Cache-Control") != "no-store" {
|
||||||
|
t.Error("API responses must stay no-store")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,357 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2026 The cluster-map Authors
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/fields"
|
||||||
|
|
||||||
|
"git.project-chimera.ru/fanchji/cluster-map/internal/kube"
|
||||||
|
"git.project-chimera.ru/fanchji/cluster-map/internal/topology"
|
||||||
|
)
|
||||||
|
|
||||||
|
// EventInfo is one row of the Events tab.
|
||||||
|
type EventInfo struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Reason string `json:"reason"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Object string `json:"object"` // Kind/name of what the event is about
|
||||||
|
Count int32 `json:"count"`
|
||||||
|
Last string `json:"last"`
|
||||||
|
}
|
||||||
|
|
||||||
|
const maxEvents = 100
|
||||||
|
|
||||||
|
// handleEvents lists recent events of a card: for a workload also those of
|
||||||
|
// its pods and ReplicaSets, which is where Kubernetes reports most of what
|
||||||
|
// goes wrong with it. Read live, all types, not from the Warning cache.
|
||||||
|
func (s *Server) handleEvents(w http.ResponseWriter, r *http.Request) {
|
||||||
|
id := r.URL.Query().Get("id")
|
||||||
|
parts := strings.Split(id, "/")
|
||||||
|
ctx := r.Context()
|
||||||
|
if !s.cardVisible(w, r, parts) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var list *corev1.EventList
|
||||||
|
var err error
|
||||||
|
match := func(corev1.ObjectReference) bool { return true }
|
||||||
|
switch {
|
||||||
|
case len(parts) == 2 && parts[0] == "node":
|
||||||
|
list, err = s.cache.Client.CoreV1().Events("").List(ctx, metav1.ListOptions{FieldSelector: fields.Set{
|
||||||
|
"involvedObject.kind": "Node", "involvedObject.name": parts[1]}.String()})
|
||||||
|
case len(parts) == 2 && parts[0] == "ns":
|
||||||
|
list, err = s.cache.Client.CoreV1().Events(parts[1]).List(ctx, metav1.ListOptions{Limit: 500})
|
||||||
|
case len(parts) == 3 && (parts[0] == "service" || parts[0] == "ingress"):
|
||||||
|
kind := map[string]string{"service": "Service", "ingress": "Ingress"}[parts[0]]
|
||||||
|
list, err = s.cache.Client.CoreV1().Events(parts[1]).List(ctx, metav1.ListOptions{FieldSelector: fields.Set{
|
||||||
|
"involvedObject.kind": kind, "involvedObject.name": parts[2]}.String()})
|
||||||
|
case len(parts) == 4 && parts[0] == "wl":
|
||||||
|
ns, name := parts[1], parts[3]
|
||||||
|
owners := s.podOwners(ns)
|
||||||
|
match = func(ref corev1.ObjectReference) bool {
|
||||||
|
if topology.EventTargetID(ref, owners) == id {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// a pod already gone is no longer in the cache; its name still tells
|
||||||
|
return ref.Kind == "Pod" && owners[ns+"/"+ref.Name] == "" && strings.HasPrefix(ref.Name, name+"-")
|
||||||
|
}
|
||||||
|
list, err = s.cache.Client.CoreV1().Events(ns).List(ctx, metav1.ListOptions{Limit: 500})
|
||||||
|
default:
|
||||||
|
http.Error(w, "id must be a card id (wl/…, service/…, ingress/…, node/…, ns/…)", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), apiStatus(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
out := []EventInfo{}
|
||||||
|
for _, e := range list.Items {
|
||||||
|
if !match(e.InvolvedObject) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
count := e.Count
|
||||||
|
if e.Series != nil {
|
||||||
|
count = e.Series.Count
|
||||||
|
}
|
||||||
|
out = append(out, EventInfo{
|
||||||
|
Type: e.Type, Reason: e.Reason, Message: e.Message, Count: max(count, 1),
|
||||||
|
Object: e.InvolvedObject.Kind + "/" + e.InvolvedObject.Name, Last: lastSeen(e).UTC().Format(time.RFC3339),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
sort.Slice(out, func(i, j int) bool { return out[i].Last > out[j].Last })
|
||||||
|
if len(out) > maxEvents {
|
||||||
|
out = out[:maxEvents]
|
||||||
|
}
|
||||||
|
writeJSON(w, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func lastSeen(e corev1.Event) time.Time {
|
||||||
|
switch {
|
||||||
|
case e.Series != nil && !e.Series.LastObservedTime.IsZero():
|
||||||
|
return e.Series.LastObservedTime.Time
|
||||||
|
case !e.LastTimestamp.IsZero():
|
||||||
|
return e.LastTimestamp.Time
|
||||||
|
case !e.EventTime.IsZero():
|
||||||
|
return e.EventTime.Time
|
||||||
|
}
|
||||||
|
return e.CreationTimestamp.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) podOwners(ns string) map[string]string {
|
||||||
|
pods, _ := kube.List[corev1.Pod](s.cache.Pods.Pods(ns))
|
||||||
|
out := make(map[string]string, len(pods))
|
||||||
|
for _, p := range pods {
|
||||||
|
out[ns+"/"+p.Name] = topology.OwnerWorkloadID(p)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// Amount is a CPU (millicores) or memory (bytes) figure; nil when unknown.
|
||||||
|
type Amount struct {
|
||||||
|
Usage *int64 `json:"usage"`
|
||||||
|
Request int64 `json:"request"`
|
||||||
|
Limit int64 `json:"limit"` // 0 when any container has no limit
|
||||||
|
Capacity int64 `json:"capacity"` // allocatable, nodes only
|
||||||
|
}
|
||||||
|
|
||||||
|
// PodUsage is one pod in the usage table.
|
||||||
|
type PodUsage struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Node string `json:"node"`
|
||||||
|
Ready bool `json:"ready"`
|
||||||
|
Restarts int32 `json:"restarts"`
|
||||||
|
CPU *int64 `json:"cpu"`
|
||||||
|
Memory *int64 `json:"memory"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Usage is the /api/usage response.
|
||||||
|
type Usage struct {
|
||||||
|
Metrics bool `json:"metrics"` // false when metrics.k8s.io did not answer
|
||||||
|
CPU Amount `json:"cpu"`
|
||||||
|
Memory Amount `json:"memory"`
|
||||||
|
Pods []PodUsage `json:"pods"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// metricsList is the part of a metrics.k8s.io PodMetricsList read here.
|
||||||
|
type metricsList struct {
|
||||||
|
Items []struct {
|
||||||
|
Metadata metav1.ObjectMeta `json:"metadata"`
|
||||||
|
Containers []struct {
|
||||||
|
Usage corev1.ResourceList `json:"usage"`
|
||||||
|
} `json:"containers"`
|
||||||
|
} `json:"items"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type nodeMetrics struct {
|
||||||
|
Usage corev1.ResourceList `json:"usage"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// metricsGet reads the resource metrics API through the discovery REST
|
||||||
|
// client, which spares a dependency on k8s.io/metrics for two GETs.
|
||||||
|
func (s *Server) metricsGet(ctx context.Context, into any, path ...string) bool {
|
||||||
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
raw, err := s.cache.Client.Discovery().RESTClient().Get().AbsPath(append([]string{"/apis/metrics.k8s.io/v1beta1"}, path...)...).DoRaw(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return json.Unmarshal(raw, into) == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ptr(v int64) *int64 { return &v }
|
||||||
|
|
||||||
|
// handleUsage: CPU and memory of a workload (sum over its pods) or a node,
|
||||||
|
// against requests, limits and, for a node, allocatable.
|
||||||
|
func (s *Server) handleUsage(w http.ResponseWriter, r *http.Request) {
|
||||||
|
id := r.URL.Query().Get("id")
|
||||||
|
parts := strings.Split(id, "/")
|
||||||
|
if !s.cardVisible(w, r, parts) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var pods []corev1.Pod
|
||||||
|
u := Usage{Pods: []PodUsage{}}
|
||||||
|
switch {
|
||||||
|
case len(parts) == 4 && parts[0] == "wl":
|
||||||
|
all, err := kube.List[corev1.Pod](s.cache.Pods.Pods(parts[1]))
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, p := range all {
|
||||||
|
if topology.OwnerWorkloadID(p) == id {
|
||||||
|
pods = append(pods, p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case len(parts) == 2 && parts[0] == "node":
|
||||||
|
n, err := s.cache.Nodes.Get(parts[1])
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
u.CPU.Capacity = n.Status.Allocatable.Cpu().MilliValue()
|
||||||
|
u.Memory.Capacity = n.Status.Allocatable.Memory().Value()
|
||||||
|
all, _ := kube.List[corev1.Pod](s.cache.Pods)
|
||||||
|
for _, p := range all {
|
||||||
|
if p.Spec.NodeName == parts[1] {
|
||||||
|
pods = append(pods, p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var nm nodeMetrics
|
||||||
|
if s.metricsGet(r.Context(), &nm, "nodes", parts[1]) {
|
||||||
|
u.Metrics = true
|
||||||
|
u.CPU.Usage, u.Memory.Usage = ptr(nm.Usage.Cpu().MilliValue()), ptr(nm.Usage.Memory().Value())
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
http.Error(w, "id must be a workload (wl/…) or a node (node/…)", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
live := pods[:0]
|
||||||
|
for _, p := range pods {
|
||||||
|
if p.Status.Phase != corev1.PodSucceeded && p.Status.Phase != corev1.PodFailed {
|
||||||
|
live = append(live, p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cpuReq, cpuLim, memReq, memLim, noCPULimit, noMemLimit := requests(live)
|
||||||
|
u.CPU.Request, u.Memory.Request = cpuReq, memReq
|
||||||
|
if !noCPULimit {
|
||||||
|
u.CPU.Limit = cpuLim
|
||||||
|
}
|
||||||
|
if !noMemLimit {
|
||||||
|
u.Memory.Limit = memLim
|
||||||
|
}
|
||||||
|
|
||||||
|
workload := parts[0] == "wl"
|
||||||
|
var ml metricsList
|
||||||
|
byPod := map[string][2]int64{}
|
||||||
|
if workload && s.metricsGet(r.Context(), &ml, "namespaces", parts[1], "pods") {
|
||||||
|
u.Metrics = true
|
||||||
|
for _, it := range ml.Items {
|
||||||
|
var cpu, mem int64
|
||||||
|
for _, c := range it.Containers {
|
||||||
|
cpu += c.Usage.Cpu().MilliValue()
|
||||||
|
mem += c.Usage.Memory().Value()
|
||||||
|
}
|
||||||
|
byPod[it.Metadata.Name] = [2]int64{cpu, mem}
|
||||||
|
}
|
||||||
|
var cpu, mem int64
|
||||||
|
for _, p := range live {
|
||||||
|
cpu += byPod[p.Name][0]
|
||||||
|
mem += byPod[p.Name][1]
|
||||||
|
}
|
||||||
|
u.CPU.Usage, u.Memory.Usage = ptr(cpu), ptr(mem)
|
||||||
|
}
|
||||||
|
if workload {
|
||||||
|
for _, p := range live {
|
||||||
|
row := PodUsage{Name: p.Name, Node: p.Spec.NodeName, Ready: isReady(p)}
|
||||||
|
for _, cs := range p.Status.ContainerStatuses {
|
||||||
|
row.Restarts += cs.RestartCount
|
||||||
|
}
|
||||||
|
if v, ok := byPod[p.Name]; ok {
|
||||||
|
row.CPU, row.Memory = ptr(v[0]), ptr(v[1])
|
||||||
|
}
|
||||||
|
u.Pods = append(u.Pods, row)
|
||||||
|
}
|
||||||
|
sort.Slice(u.Pods, func(i, j int) bool { return u.Pods[i].Name < u.Pods[j].Name })
|
||||||
|
}
|
||||||
|
writeJSON(w, u)
|
||||||
|
}
|
||||||
|
|
||||||
|
// requests sums the regular containers' requests and limits. A limit sum
|
||||||
|
// only means something when every container has one.
|
||||||
|
func requests(pods []corev1.Pod) (cpuReq, cpuLim, memReq, memLim int64, noCPULimit, noMemLimit bool) {
|
||||||
|
for _, p := range pods {
|
||||||
|
for _, c := range p.Spec.Containers {
|
||||||
|
cpuReq += qty(c.Resources.Requests, corev1.ResourceCPU, true)
|
||||||
|
memReq += qty(c.Resources.Requests, corev1.ResourceMemory, false)
|
||||||
|
if _, ok := c.Resources.Limits[corev1.ResourceCPU]; !ok {
|
||||||
|
noCPULimit = true
|
||||||
|
}
|
||||||
|
if _, ok := c.Resources.Limits[corev1.ResourceMemory]; !ok {
|
||||||
|
noMemLimit = true
|
||||||
|
}
|
||||||
|
cpuLim += qty(c.Resources.Limits, corev1.ResourceCPU, true)
|
||||||
|
memLim += qty(c.Resources.Limits, corev1.ResourceMemory, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func qty(l corev1.ResourceList, name corev1.ResourceName, milli bool) int64 {
|
||||||
|
q, ok := l[name]
|
||||||
|
if !ok {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
if milli {
|
||||||
|
return q.MilliValue()
|
||||||
|
}
|
||||||
|
return q.Value()
|
||||||
|
}
|
||||||
|
|
||||||
|
func isReady(p corev1.Pod) bool {
|
||||||
|
for _, c := range p.Status.Conditions {
|
||||||
|
if c.Type == corev1.PodReady {
|
||||||
|
return c.Status == corev1.ConditionTrue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// maxVulns bounds the details list; an old base image can carry hundreds.
|
||||||
|
const maxVulns = 300
|
||||||
|
|
||||||
|
// handleVulns lists the CRITICAL/HIGH findings of a card's images from the
|
||||||
|
// cached VulnerabilityReports.
|
||||||
|
func (s *Server) handleVulns(w http.ResponseWriter, r *http.Request) {
|
||||||
|
id := r.URL.Query().Get("id")
|
||||||
|
if !strings.HasPrefix(id, "wl/") {
|
||||||
|
http.Error(w, "id must be a workload (wl/...)", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
parts := strings.Split(id, "/")
|
||||||
|
if !s.cardVisible(w, r, parts) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ns := parts[1]
|
||||||
|
pods, _ := kube.List[corev1.Pod](s.cache.Pods.Pods(ns))
|
||||||
|
writeJSON(w, map[string]any{
|
||||||
|
"available": s.cache.VulnReports != nil,
|
||||||
|
"vulns": topology.VulnsFor(s.cache.VulnReportList(), pods, id, maxVulns),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// cardVisible checks a card ID (split on "/") against the viewer's scope:
|
||||||
|
// node/<name> needs node access, anything else its namespace.
|
||||||
|
func (s *Server) cardVisible(w http.ResponseWriter, r *http.Request, parts []string) bool {
|
||||||
|
sc, ok := s.scope(w, r)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
visible := len(parts) >= 2 && ((parts[0] == "node" && sc.Nodes) || (parts[0] != "node" && sc.Namespace(parts[1])))
|
||||||
|
if !visible {
|
||||||
|
hidden(w)
|
||||||
|
}
|
||||||
|
return visible
|
||||||
|
}
|
||||||
@@ -18,6 +18,8 @@ limitations under the License.
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
@@ -35,6 +37,8 @@ import (
|
|||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
|
|
||||||
|
"git.project-chimera.ru/fanchji/cluster-map/internal/access"
|
||||||
|
"git.project-chimera.ru/fanchji/cluster-map/internal/actions"
|
||||||
"git.project-chimera.ru/fanchji/cluster-map/internal/config"
|
"git.project-chimera.ru/fanchji/cluster-map/internal/config"
|
||||||
"git.project-chimera.ru/fanchji/cluster-map/internal/kube"
|
"git.project-chimera.ru/fanchji/cluster-map/internal/kube"
|
||||||
"git.project-chimera.ru/fanchji/cluster-map/internal/metrics"
|
"git.project-chimera.ru/fanchji/cluster-map/internal/metrics"
|
||||||
@@ -48,12 +52,90 @@ type Server struct {
|
|||||||
cache *kube.Cache
|
cache *kube.Cache
|
||||||
traffic *traffic.Collector
|
traffic *traffic.Collector
|
||||||
static fs.FS
|
static fs.FS
|
||||||
|
etags map[string]string
|
||||||
|
asViewer actions.ClientFactory // nil when neither actions nor viewer visibility is on
|
||||||
|
access *access.Checker // nil when everyone sees everything
|
||||||
ready atomic.Bool
|
ready atomic.Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a server that answers 503 on the API until SetReady.
|
// New returns a server that answers 503 on the API until SetReady.
|
||||||
func New(cfg config.Config, static fs.FS) *Server {
|
// asViewer builds clients from the viewer's token; nil disables actions
|
||||||
return &Server{cfg: cfg, static: static}
|
// and per-viewer visibility.
|
||||||
|
func New(cfg config.Config, static fs.FS, asViewer actions.ClientFactory) *Server {
|
||||||
|
s := &Server{cfg: cfg, static: static, etags: etags(static), asViewer: asViewer}
|
||||||
|
if cfg.ViewerVisibility && asViewer != nil {
|
||||||
|
s.access = access.NewChecker(access.ClientFactory(asViewer), s.namespaceNames)
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) namespaceNames() []string {
|
||||||
|
nss, _ := kube.List[corev1.Namespace](s.cache.Namespaces)
|
||||||
|
out := make([]string, len(nss))
|
||||||
|
for i, n := range nss {
|
||||||
|
out[i] = n.Name
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// scope is what the viewer of this request may see, or it writes why
|
||||||
|
// nothing can be shown.
|
||||||
|
func (s *Server) scope(w http.ResponseWriter, r *http.Request) (*access.Scope, bool) {
|
||||||
|
if s.access == nil {
|
||||||
|
return access.Everything, true
|
||||||
|
}
|
||||||
|
sc, err := s.access.For(r.Context(), viewerToken(r))
|
||||||
|
switch {
|
||||||
|
case err == nil:
|
||||||
|
return sc, true
|
||||||
|
case errors.Is(err, access.ErrNoToken), apierrors.IsUnauthorized(err):
|
||||||
|
http.Error(w, err.Error(), http.StatusUnauthorized)
|
||||||
|
default:
|
||||||
|
http.Error(w, "checking the viewer's permissions: "+err.Error(), http.StatusBadGateway)
|
||||||
|
}
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// hidden answers 403 for an object outside the viewer's scope.
|
||||||
|
func hidden(w http.ResponseWriter) {
|
||||||
|
http.Error(w, "not visible with your permissions", http.StatusForbidden)
|
||||||
|
}
|
||||||
|
|
||||||
|
// etags hashes the embedded files once: they cannot change while the
|
||||||
|
// process runs, so a browser revalidates with If-None-Match and gets a 304
|
||||||
|
// instead of the 2 MB of vendored scripts on every visit.
|
||||||
|
func etags(static fs.FS) map[string]string {
|
||||||
|
out := map[string]string{}
|
||||||
|
_ = fs.WalkDir(static, ".", func(path string, d fs.DirEntry, err error) error {
|
||||||
|
if err != nil || d.IsDir() {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
b, err := fs.ReadFile(static, path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
sum := sha256.Sum256(b)
|
||||||
|
out["/"+path] = `"` + hex.EncodeToString(sum[:12]) + `"`
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if v, ok := out["/index.html"]; ok {
|
||||||
|
out["/"] = v
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// staticHandler serves the embedded frontend with validators. Everything
|
||||||
|
// is revalidated on each load (no-cache), so a new release shows up on the
|
||||||
|
// next reload without versioned file names.
|
||||||
|
func (s *Server) staticHandler() http.Handler {
|
||||||
|
files := http.FileServerFS(s.static)
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if tag, ok := s.etags[r.URL.Path]; ok {
|
||||||
|
w.Header().Set("ETag", tag)
|
||||||
|
w.Header().Set("Cache-Control", "no-cache")
|
||||||
|
}
|
||||||
|
files.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetReady is called once the caches have synced; until then API calls
|
// SetReady is called once the caches have synced; until then API calls
|
||||||
@@ -79,7 +161,13 @@ func (s *Server) Handler() http.Handler {
|
|||||||
mux.Handle("GET /api/resource", s.api("resource", s.handleResource))
|
mux.Handle("GET /api/resource", s.api("resource", s.handleResource))
|
||||||
mux.Handle("GET /api/pods", s.api("pods", s.handlePods))
|
mux.Handle("GET /api/pods", s.api("pods", s.handlePods))
|
||||||
mux.Handle("GET /api/logs", s.api("logs", s.handleLogs))
|
mux.Handle("GET /api/logs", s.api("logs", s.handleLogs))
|
||||||
mux.Handle("GET /", http.FileServerFS(s.static))
|
mux.Handle("GET /api/events", s.api("events", s.handleEvents))
|
||||||
|
mux.Handle("GET /api/usage", s.api("usage", s.handleUsage))
|
||||||
|
mux.Handle("GET /api/vulns", s.api("vulns", s.handleVulns))
|
||||||
|
mux.Handle("GET /api/me", s.api("me", s.handleMe))
|
||||||
|
mux.Handle("GET /api/permissions", s.api("permissions", s.handlePermissions))
|
||||||
|
mux.Handle("POST /api/action", s.api("action", s.handleAction))
|
||||||
|
mux.Handle("GET /", s.staticHandler())
|
||||||
return securityHeaders(mux)
|
return securityHeaders(mux)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,16 +214,24 @@ func writeJSON(w http.ResponseWriter, v any) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) handleGraph(w http.ResponseWriter, _ *http.Request) {
|
func (s *Server) handleGraph(w http.ResponseWriter, r *http.Request) {
|
||||||
|
sc, ok := s.scope(w, r)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
g, err := topology.Build(s.cache, s.cfg.IsSystemNamespace)
|
g, err := topology.Build(s.cache, s.cfg.IsSystemNamespace)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
writeJSON(w, g)
|
writeJSON(w, topology.Filter(g, sc.All, sc.Nodes, sc.Namespace))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) handleFlows(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) handleFlows(w http.ResponseWriter, r *http.Request) {
|
||||||
|
sc, ok := s.scope(w, r)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
if s.cfg.TrafficMode == config.TrafficDisabled {
|
if s.cfg.TrafficMode == config.TrafficDisabled {
|
||||||
writeJSON(w, traffic.Result{Mode: config.TrafficDisabled})
|
writeJSON(w, traffic.Result{Mode: config.TrafficDisabled})
|
||||||
return
|
return
|
||||||
@@ -154,7 +250,16 @@ func (s *Server) handleFlows(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err != nil || window <= 0 {
|
if err != nil || window <= 0 {
|
||||||
window = 5 * time.Minute
|
window = 5 * time.Minute
|
||||||
}
|
}
|
||||||
writeJSON(w, s.traffic.Query(namespaces, window))
|
for ns := range namespaces {
|
||||||
|
if !sc.Namespace(ns) {
|
||||||
|
delete(namespaces, ns)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res := s.traffic.Query(namespaces, window)
|
||||||
|
if !sc.All {
|
||||||
|
res = traffic.Restrict(res, sc.Namespace)
|
||||||
|
}
|
||||||
|
writeJSON(w, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleResource returns the live object as YAML. It reads through the API
|
// handleResource returns the live object as YAML. It reads through the API
|
||||||
@@ -162,6 +267,14 @@ func (s *Server) handleFlows(w http.ResponseWriter, r *http.Request) {
|
|||||||
func (s *Server) handleResource(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) handleResource(w http.ResponseWriter, r *http.Request) {
|
||||||
q := r.URL.Query()
|
q := r.URL.Query()
|
||||||
kind, ns, name := q.Get("kind"), q.Get("namespace"), q.Get("name")
|
kind, ns, name := q.Get("kind"), q.Get("namespace"), q.Get("name")
|
||||||
|
sc, ok := s.scope(w, r)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (kind == "node" && !sc.Nodes) || (kind == "namespace" && !sc.Namespace(name)) || (kind != "node" && kind != "namespace" && !sc.Namespace(ns)) {
|
||||||
|
hidden(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
c := s.cache.Client
|
c := s.cache.Client
|
||||||
var obj metav1.Object
|
var obj metav1.Object
|
||||||
@@ -227,6 +340,12 @@ func (s *Server) handlePods(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, "namespace and a workload id (wl/<ns>/<kind>/<name>) are required", http.StatusBadRequest)
|
http.Error(w, "namespace and a workload id (wl/<ns>/<kind>/<name>) are required", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if sc, ok := s.scope(w, r); !ok {
|
||||||
|
return
|
||||||
|
} else if !sc.Namespace(ns) {
|
||||||
|
hidden(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
pods, err := kube.List[corev1.Pod](s.cache.Pods.Pods(ns))
|
pods, err := kube.List[corev1.Pod](s.cache.Pods.Pods(ns))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
@@ -256,6 +375,9 @@ func (s *Server) handleLogs(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, "namespace and pod are required", http.StatusBadRequest)
|
http.Error(w, "namespace and pod are required", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if !s.canReadLogs(w, r, ns) {
|
||||||
|
return
|
||||||
|
}
|
||||||
tail := s.cfg.LogTailLines
|
tail := s.cfg.LogTailLines
|
||||||
opts := &corev1.PodLogOptions{TailLines: &tail, Container: container}
|
opts := &corev1.PodLogOptions{TailLines: &tail, Container: container}
|
||||||
stream, err := s.cache.Client.CoreV1().Pods(ns).GetLogs(pod, opts).Stream(r.Context())
|
stream, err := s.cache.Client.CoreV1().Pods(ns).GetLogs(pod, opts).Stream(r.Context())
|
||||||
@@ -269,3 +391,31 @@ func (s *Server) handleLogs(w http.ResponseWriter, r *http.Request) {
|
|||||||
slog.Debug("streaming logs", "err", err)
|
slog.Debug("streaming logs", "err", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// canReadLogs: logs are the most sensitive thing the map shows, so beyond
|
||||||
|
// the namespace scope the viewer must be allowed pods/log there.
|
||||||
|
func (s *Server) canReadLogs(w http.ResponseWriter, r *http.Request, ns string) bool {
|
||||||
|
sc, ok := s.scope(w, r)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !sc.Namespace(ns) {
|
||||||
|
hidden(w)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if s.access == nil || sc.All {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
c, err := s.asViewer(viewerToken(r))
|
||||||
|
if err == nil {
|
||||||
|
ok, err = access.Can(r.Context(), c, "", "pods", "log", "get", ns)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "checking the viewer's permissions: "+err.Error(), http.StatusBadGateway)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
hidden(w)
|
||||||
|
}
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ import (
|
|||||||
func newServer(t *testing.T, mode config.TrafficMode) (*Server, http.Handler) {
|
func newServer(t *testing.T, mode config.TrafficMode) (*Server, http.Handler) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
cfg := config.Config{TrafficMode: mode, Retention: time.Hour, LogTailLines: 10, SystemNamespacePrefixes: []string{"d8-"}}
|
cfg := config.Config{TrafficMode: mode, Retention: time.Hour, LogTailLines: 10, SystemNamespacePrefixes: []string{"d8-"}}
|
||||||
s := New(cfg, fstest.MapFS{"index.html": {Data: []byte("<!doctype html>")}})
|
s := New(cfg, fstest.MapFS{"index.html": {Data: []byte("<!doctype html>")}}, nil)
|
||||||
return s, s.Handler()
|
return s, s.Handler()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +84,7 @@ func TestPodsAndFlows(t *testing.T) {
|
|||||||
)
|
)
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
cache, err := kube.Start(ctx, client, 10*time.Second)
|
cache, err := kube.Start(ctx, client, nil, 10*time.Second)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,104 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2026 The cluster-map Authors
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
"testing/fstest"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
authzv1 "k8s.io/api/authorization/v1"
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
|
"k8s.io/client-go/kubernetes"
|
||||||
|
"k8s.io/client-go/kubernetes/fake"
|
||||||
|
k8stesting "k8s.io/client-go/testing"
|
||||||
|
|
||||||
|
"git.project-chimera.ru/fanchji/cluster-map/internal/config"
|
||||||
|
"git.project-chimera.ru/fanchji/cluster-map/internal/kube"
|
||||||
|
"git.project-chimera.ru/fanchji/cluster-map/internal/topology"
|
||||||
|
"git.project-chimera.ru/fanchji/cluster-map/internal/traffic"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The viewer "shop-dev" may list pods in shop and nothing else.
|
||||||
|
func visibilityServer(t *testing.T) http.Handler {
|
||||||
|
t.Helper()
|
||||||
|
cluster := fake.NewSimpleClientset(
|
||||||
|
&corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "shop"}},
|
||||||
|
&corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "bank"}},
|
||||||
|
&corev1.Service{ObjectMeta: metav1.ObjectMeta{Name: "web", Namespace: "shop"}},
|
||||||
|
&corev1.Service{ObjectMeta: metav1.ObjectMeta{Name: "core", Namespace: "bank"}},
|
||||||
|
)
|
||||||
|
ctx := t.Context()
|
||||||
|
cache, err := kube.Start(ctx, cluster, nil, 10*time.Second)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
factory := func(token string) (kubernetes.Interface, error) {
|
||||||
|
c := fake.NewSimpleClientset()
|
||||||
|
c.PrependReactor("create", "selfsubjectaccessreviews", func(a k8stesting.Action) (bool, runtime.Object, error) {
|
||||||
|
r := a.(k8stesting.CreateAction).GetObject().(*authzv1.SelfSubjectAccessReview)
|
||||||
|
r.Status.Allowed = token == "shop-dev" && r.Spec.ResourceAttributes.Namespace == "shop"
|
||||||
|
return true, r, nil
|
||||||
|
})
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
cfg := config.Config{TrafficMode: config.TrafficDisabled, Retention: time.Hour, ViewerVisibility: true}
|
||||||
|
s := New(cfg, fstest.MapFS{"index.html": {Data: []byte("x")}}, factory)
|
||||||
|
s.SetReady(cache, traffic.NewCollector(cfg, nil, nil))
|
||||||
|
return s.Handler()
|
||||||
|
}
|
||||||
|
|
||||||
|
func getAs(h http.Handler, path, token string) *httptest.ResponseRecorder {
|
||||||
|
req := httptest.NewRequest(http.MethodGet, path, nil)
|
||||||
|
if token != "" {
|
||||||
|
req.Header.Set("Authorization", "Bearer "+token)
|
||||||
|
}
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
h.ServeHTTP(rec, req)
|
||||||
|
return rec
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGraphFollowsViewerRBAC(t *testing.T) {
|
||||||
|
h := visibilityServer(t)
|
||||||
|
if code := getAs(h, "/api/graph", "").Code; code != http.StatusUnauthorized {
|
||||||
|
t.Errorf("no token: %d", code)
|
||||||
|
}
|
||||||
|
rec := getAs(h, "/api/graph", "shop-dev")
|
||||||
|
var g topology.Graph
|
||||||
|
if err := json.Unmarshal(rec.Body.Bytes(), &g); err != nil {
|
||||||
|
t.Fatalf("%d %s", rec.Code, rec.Body)
|
||||||
|
}
|
||||||
|
for _, n := range g.Nodes {
|
||||||
|
if n.Namespace == "bank" || n.ID == "ns/bank" {
|
||||||
|
t.Errorf("bank visible to shop-dev: %s", n.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if code := getAs(h, "/api/resource?kind=service&namespace=bank&name=core", "shop-dev").Code; code != http.StatusForbidden {
|
||||||
|
t.Errorf("YAML from a hidden namespace: %d", code)
|
||||||
|
}
|
||||||
|
if code := getAs(h, "/api/logs?namespace=bank&pod=core-0", "shop-dev").Code; code != http.StatusForbidden {
|
||||||
|
t.Errorf("logs from a hidden namespace: %d", code)
|
||||||
|
}
|
||||||
|
if code := getAs(h, "/api/events?id=node/a", "shop-dev").Code; code != http.StatusForbidden {
|
||||||
|
t.Errorf("node events without node access: %d", code)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2026 The cluster-map Authors
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package topology
|
||||||
|
|
||||||
|
// Filter returns the part of the graph a viewer may see: namespaced cards
|
||||||
|
// in visible namespaces, node cards only with node access, edges whose both
|
||||||
|
// ends stay. Cluster-wide problems (alerts about the cluster itself) are
|
||||||
|
// for cluster-wide viewers only. all short-circuits to the graph as is.
|
||||||
|
func Filter(g *Graph, all, nodes bool, namespace func(string) bool) *Graph {
|
||||||
|
if all {
|
||||||
|
return g
|
||||||
|
}
|
||||||
|
out := &Graph{Nodes: []Node{}, Edges: []Edge{}, ClusterProblems: []Problem{}, AlertsAvailable: g.AlertsAvailable, VulnsAvailable: g.VulnsAvailable}
|
||||||
|
kept := make(map[string]bool, len(g.Nodes))
|
||||||
|
for _, n := range g.Nodes {
|
||||||
|
var ok bool
|
||||||
|
switch n.Kind {
|
||||||
|
case "node":
|
||||||
|
ok = nodes
|
||||||
|
case "namespace":
|
||||||
|
ok = namespace(n.Label)
|
||||||
|
default:
|
||||||
|
ok = namespace(n.Namespace)
|
||||||
|
}
|
||||||
|
if ok {
|
||||||
|
out.Nodes = append(out.Nodes, n)
|
||||||
|
kept[n.ID] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, e := range g.Edges {
|
||||||
|
if kept[e.Source] && kept[e.Target] {
|
||||||
|
out.Edges = append(out.Edges, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2026 The cluster-map Authors
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package topology
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestFilter(t *testing.T) {
|
||||||
|
g := &Graph{
|
||||||
|
Nodes: []Node{
|
||||||
|
{ID: "ns/shop", Kind: "namespace", Label: "shop"}, {ID: "ns/bank", Kind: "namespace", Label: "bank"},
|
||||||
|
{ID: "wl/shop/deployment/web", Kind: "deployment", Namespace: "shop"}, {ID: "wl/bank/deployment/core", Kind: "deployment", Namespace: "bank"},
|
||||||
|
{ID: "node/a", Kind: "node", Label: "a"},
|
||||||
|
},
|
||||||
|
Edges: []Edge{
|
||||||
|
{ID: "e1", Source: "wl/shop/deployment/web", Target: "node/a"},
|
||||||
|
{ID: "e2", Source: "wl/bank/deployment/core", Target: "node/a"},
|
||||||
|
},
|
||||||
|
ClusterProblems: []Problem{{Reason: "ClusterWide"}},
|
||||||
|
}
|
||||||
|
out := Filter(g, false, true, func(ns string) bool { return ns == "shop" })
|
||||||
|
ids := map[string]bool{}
|
||||||
|
for _, n := range out.Nodes {
|
||||||
|
ids[n.ID] = true
|
||||||
|
}
|
||||||
|
if len(ids) != 3 || !ids["ns/shop"] || !ids["wl/shop/deployment/web"] || !ids["node/a"] {
|
||||||
|
t.Errorf("nodes: %v", ids)
|
||||||
|
}
|
||||||
|
if len(out.Edges) != 1 || out.Edges[0].ID != "e1" {
|
||||||
|
t.Errorf("edges: %+v", out.Edges)
|
||||||
|
}
|
||||||
|
if len(out.ClusterProblems) != 0 {
|
||||||
|
t.Error("cluster-wide problems shown to a namespaced viewer")
|
||||||
|
}
|
||||||
|
if noNodes := Filter(g, false, false, func(ns string) bool { return ns == "shop" }); len(noNodes.Edges) != 0 {
|
||||||
|
t.Error("placement edge to a hidden node kept")
|
||||||
|
}
|
||||||
|
if Filter(g, true, true, nil) != g {
|
||||||
|
t.Error("cluster-wide viewer must get the graph as is")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -41,6 +41,8 @@ type Node struct {
|
|||||||
Namespace string `json:"namespace"` // empty for namespace and node
|
Namespace string `json:"namespace"` // empty for namespace and node
|
||||||
Parent string `json:"parent,omitempty"`
|
Parent string `json:"parent,omitempty"`
|
||||||
Facts map[string]string `json:"facts,omitempty"` // short summary shown without a follow-up fetch
|
Facts map[string]string `json:"facts,omitempty"` // short summary shown without a follow-up fetch
|
||||||
|
Problems []Problem `json:"problems,omitempty"`
|
||||||
|
Vulns *VulnSummary `json:"vulns,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Edge is a configuration or placement link between two nodes.
|
// Edge is a configuration or placement link between two nodes.
|
||||||
@@ -55,6 +57,14 @@ type Edge struct {
|
|||||||
type Graph struct {
|
type Graph struct {
|
||||||
Nodes []Node `json:"nodes"`
|
Nodes []Node `json:"nodes"`
|
||||||
Edges []Edge `json:"edges"`
|
Edges []Edge `json:"edges"`
|
||||||
|
// ClusterProblems are problems no card owns: cluster-wide alerts and
|
||||||
|
// alerts about namespaces that no longer exist.
|
||||||
|
ClusterProblems []Problem `json:"clusterProblems"`
|
||||||
|
// AlertsAvailable is false when ClusterAlerts are not served, so the UI
|
||||||
|
// can say that alerts are not part of the picture.
|
||||||
|
AlertsAvailable bool `json:"alertsAvailable"`
|
||||||
|
// VulnsAvailable is false when operator-trivy is not installed.
|
||||||
|
VulnsAvailable bool `json:"vulnsAvailable"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// WorkloadID is the graph ID of a workload. Traffic endpoints use the same
|
// WorkloadID is the graph ID of a workload. Traffic endpoints use the same
|
||||||
@@ -70,7 +80,7 @@ type workloadRef struct {
|
|||||||
|
|
||||||
// Build assembles the graph from the cache. isSystem classifies namespaces.
|
// Build assembles the graph from the cache. isSystem classifies namespaces.
|
||||||
func Build(c *kube.Cache, isSystem func(string) bool) (*Graph, error) {
|
func Build(c *kube.Cache, isSystem func(string) bool) (*Graph, error) {
|
||||||
g := &Graph{}
|
g := &Graph{Nodes: []Node{}, Edges: []Edge{}}
|
||||||
namespaces, err := kube.List[corev1.Namespace](c.Namespaces)
|
namespaces, err := kube.List[corev1.Namespace](c.Namespaces)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -163,6 +173,18 @@ func Build(c *kube.Cache, isSystem func(string) bool) (*Graph, error) {
|
|||||||
}
|
}
|
||||||
g.Edges = append(g.Edges, placementEdges(pods)...)
|
g.Edges = append(g.Edges, placementEdges(pods)...)
|
||||||
|
|
||||||
|
events, err := kube.List[corev1.Event](c.Events)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
attachProblems(g, problemInputs{
|
||||||
|
pods: pods, deployments: deployments, daemonsets: daemonsets, statefulsets: statefulsets, cronjobs: cronjobs,
|
||||||
|
services: services, ingresses: ingresses, nodes: hostNodes, events: events, alerts: c.AlertList(),
|
||||||
|
})
|
||||||
|
g.AlertsAvailable = c.Alerts != nil
|
||||||
|
attachVulns(g, c.VulnReportList(), pods)
|
||||||
|
g.VulnsAvailable = c.VulnReports != nil
|
||||||
|
|
||||||
sort.Slice(g.Nodes, func(i, j int) bool { return g.Nodes[i].ID < g.Nodes[j].ID })
|
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 })
|
sort.Slice(g.Edges, func(i, j int) bool { return g.Edges[i].ID < g.Edges[j].ID })
|
||||||
return g, nil
|
return g, nil
|
||||||
@@ -462,5 +484,9 @@ func nodeFacts(n corev1.Node) map[string]string {
|
|||||||
if ready != "True" {
|
if ready != "True" {
|
||||||
h = "error"
|
h = "error"
|
||||||
}
|
}
|
||||||
return map[string]string{"ready": ready, "os": n.Status.NodeInfo.OSImage, "kubelet": n.Status.NodeInfo.KubeletVersion, "health": h}
|
f := map[string]string{"ready": ready, "os": n.Status.NodeInfo.OSImage, "kubelet": n.Status.NodeInfo.KubeletVersion, "health": h}
|
||||||
|
if n.Spec.Unschedulable {
|
||||||
|
f["unschedulable"] = "true"
|
||||||
|
}
|
||||||
|
return f
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ func TestBuildGraph(t *testing.T) {
|
|||||||
)
|
)
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
cache, err := kube.Start(ctx, client, 10*time.Second)
|
cache, err := kube.Start(ctx, client, nil, 10*time.Second)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,688 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2026 The cluster-map Authors
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package topology
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
appsv1 "k8s.io/api/apps/v1"
|
||||||
|
batchv1 "k8s.io/api/batch/v1"
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
networkingv1 "k8s.io/api/networking/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Severity of a problem, which is the colour of the badge on its card.
|
||||||
|
const (
|
||||||
|
Critical = "critical"
|
||||||
|
Warning = "warning"
|
||||||
|
Info = "info"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Problem is one reason a card carries a badge. Reason is a stable code the
|
||||||
|
// UI translates (CrashLoopBackOff, NoEndpoints, an alert name); Message is
|
||||||
|
// the text Kubernetes or Alertmanager gave, shown as is.
|
||||||
|
type Problem struct {
|
||||||
|
Severity string `json:"severity"`
|
||||||
|
Source string `json:"source"` // alert | state | event
|
||||||
|
Reason string `json:"reason"`
|
||||||
|
Message string `json:"message,omitempty"`
|
||||||
|
Object string `json:"object,omitempty"` // the pod or container it was seen on, when not the card itself
|
||||||
|
Count int `json:"count,omitempty"` // events folded into this problem
|
||||||
|
Since string `json:"since,omitempty"` // RFC 3339
|
||||||
|
}
|
||||||
|
|
||||||
|
// Time windows: a restart or a warning event older than this no longer
|
||||||
|
// marks the card, so a problem fixed an hour ago does not stay red.
|
||||||
|
var (
|
||||||
|
now = time.Now
|
||||||
|
restartWindow = 30 * time.Minute
|
||||||
|
// A container that keeps restarting stays marked much longer: one
|
||||||
|
// restart an hour ago is history, the fifth OOM kill in a day is a
|
||||||
|
// memory limit that is too low. Found when a panel was OOM-killed five
|
||||||
|
// times in 23 hours and the map showed nothing 40 minutes after the
|
||||||
|
// last one.
|
||||||
|
repeatedRestarts int32 = 3
|
||||||
|
repeatedWindow = 24 * time.Hour
|
||||||
|
eventWindow = 30 * time.Minute
|
||||||
|
pendingAfter = 5 * time.Minute
|
||||||
|
notReadyAfter = 2 * time.Minute // a pod starting or restarting is not yet a problem
|
||||||
|
maxPerCard = 20
|
||||||
|
// Warning events that mark a card even when seen once. Any other reason
|
||||||
|
// (a failed readiness probe during a restart, say) needs eventRepeats
|
||||||
|
// occurrences in the window; below that it is listed, not badged.
|
||||||
|
eventNotable = map[string]bool{
|
||||||
|
"FailedScheduling": true, "FailedMount": true, "FailedAttachVolume": true, "FailedCreate": true,
|
||||||
|
"FailedCreatePodSandBox": true, "BackOff": true, "Evicted": true, "OOMKilling": true, "FailedKillPod": true,
|
||||||
|
"ProvisioningFailed": true, "FailedBinding": true, "NetworkNotReady": true, "SystemOOM": true,
|
||||||
|
}
|
||||||
|
eventRepeats = 5
|
||||||
|
// Warnings Kubernetes emits for normal operation. UnexpectedJob comes
|
||||||
|
// from the CronJob controller for every Job of the CronJob it did not
|
||||||
|
// schedule itself, i.e. every manual run (kubectl create job --from).
|
||||||
|
eventBenign = map[string]bool{"UnexpectedJob": true}
|
||||||
|
waitingCritical = map[string]bool{
|
||||||
|
"CrashLoopBackOff": true, "ImagePullBackOff": true, "ErrImagePull": true, "InvalidImageName": true,
|
||||||
|
"CreateContainerConfigError": true, "CreateContainerError": true, "RunContainerError": true,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
var severityRank = map[string]int{Critical: 3, Warning: 2, Info: 1}
|
||||||
|
|
||||||
|
// WorstSeverity is the severity of the most serious problem, empty for none.
|
||||||
|
func WorstSeverity(ps []Problem) string {
|
||||||
|
worst := ""
|
||||||
|
for _, p := range ps {
|
||||||
|
if severityRank[p.Severity] > severityRank[worst] {
|
||||||
|
worst = p.Severity
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return worst
|
||||||
|
}
|
||||||
|
|
||||||
|
type problemSet struct {
|
||||||
|
byID map[string][]Problem
|
||||||
|
cluster []Problem
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *problemSet) add(id string, p Problem) {
|
||||||
|
if id == "" {
|
||||||
|
s.cluster = append(s.cluster, p)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.byID[id] = append(s.byID[id], p)
|
||||||
|
}
|
||||||
|
|
||||||
|
type problemInputs struct {
|
||||||
|
pods []corev1.Pod
|
||||||
|
deployments []appsv1.Deployment
|
||||||
|
daemonsets []appsv1.DaemonSet
|
||||||
|
statefulsets []appsv1.StatefulSet
|
||||||
|
cronjobs []batchv1.CronJob
|
||||||
|
services []corev1.Service
|
||||||
|
ingresses []networkingv1.Ingress
|
||||||
|
nodes []corev1.Node
|
||||||
|
events []corev1.Event
|
||||||
|
alerts []*unstructured.Unstructured
|
||||||
|
}
|
||||||
|
|
||||||
|
// attachProblems fills Node.Problems and Graph.ClusterProblems. Problems
|
||||||
|
// for an ID the graph has no card for (a bare pod, a deleted object) go to
|
||||||
|
// the namespace card, and those without a namespace to the cluster list.
|
||||||
|
func attachProblems(g *Graph, in problemInputs) {
|
||||||
|
s := &problemSet{byID: map[string][]Problem{}}
|
||||||
|
podsByNs := map[string][]corev1.Pod{}
|
||||||
|
podOwner := map[string]string{} // ns/name -> workload ID
|
||||||
|
notReadySince := map[string]time.Time{} // workload ID -> the longest a pod of it has been not ready
|
||||||
|
for _, p := range in.pods {
|
||||||
|
podsByNs[p.Namespace] = append(podsByNs[p.Namespace], p)
|
||||||
|
id := OwnerWorkloadID(p)
|
||||||
|
podOwner[p.Namespace+"/"+p.Name] = id
|
||||||
|
if syntheticProbe(p.Namespace, p.Name) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
podProblems(s, p)
|
||||||
|
if since, ok := notReadyFrom(p); ok && id != "" {
|
||||||
|
if old, seen := notReadySince[id]; !seen || since.Before(old) {
|
||||||
|
notReadySince[id] = since
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, d := range in.deployments {
|
||||||
|
for _, c := range d.Status.Conditions {
|
||||||
|
if c.Type == appsv1.DeploymentProgressing && c.Status == corev1.ConditionFalse && c.Reason == "ProgressDeadlineExceeded" {
|
||||||
|
s.add(WorkloadID(d.Namespace, "deployment", d.Name), Problem{Severity: Critical, Source: "state", Reason: "ProgressDeadlineExceeded", Message: c.Message, Since: stamp(c.LastTransitionTime.Time)})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, cj := range in.cronjobs {
|
||||||
|
cronJobProblems(s, cj)
|
||||||
|
}
|
||||||
|
for _, svc := range in.services {
|
||||||
|
if !syntheticProbe(svc.Namespace, svc.Name) {
|
||||||
|
serviceProblems(s, svc, podsByNs[svc.Namespace], in)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
services := map[string]bool{}
|
||||||
|
for _, svc := range in.services {
|
||||||
|
services[svc.Namespace+"/"+svc.Name] = true
|
||||||
|
}
|
||||||
|
for _, ing := range in.ingresses {
|
||||||
|
if redirects(ing) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, name := range IngressBackendServices(ing) {
|
||||||
|
// Deckhouse's ingress-nginx points its config-reload Ingresses at a
|
||||||
|
// service that does not exist on purpose
|
||||||
|
if !services[ing.Namespace+"/"+name] && name != "fakeservice" {
|
||||||
|
s.add("ingress/"+ing.Namespace+"/"+ing.Name, Problem{Severity: Warning, Source: "state", Reason: "BackendMissing", Object: name})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, n := range in.nodes {
|
||||||
|
nodeProblems(s, n)
|
||||||
|
}
|
||||||
|
eventProblems(s, in.events, podOwner, podsLive(in.pods), podsScheduled(in.pods), podsReady(in.pods))
|
||||||
|
for _, n := range g.Nodes {
|
||||||
|
if !syntheticProbe(n.Namespace, n.Label) {
|
||||||
|
readinessProblem(s, n, notReadySince)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, a := range in.alerts {
|
||||||
|
alertProblem(s, a, podOwner)
|
||||||
|
}
|
||||||
|
|
||||||
|
known := make(map[string]int, len(g.Nodes))
|
||||||
|
for i, n := range g.Nodes {
|
||||||
|
known[n.ID] = i
|
||||||
|
}
|
||||||
|
for id, ps := range s.byID {
|
||||||
|
i, ok := known[id]
|
||||||
|
if !ok {
|
||||||
|
ns := namespaceOfID(id)
|
||||||
|
if i, ok = known["ns/"+ns]; !ok {
|
||||||
|
s.cluster = append(s.cluster, ps...)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
g.Nodes[i].Problems = append(g.Nodes[i].Problems, ps...)
|
||||||
|
}
|
||||||
|
for i := range g.Nodes {
|
||||||
|
g.Nodes[i].Problems = sortProblems(g.Nodes[i].Problems)
|
||||||
|
if len(g.Nodes[i].Problems) > maxPerCard {
|
||||||
|
g.Nodes[i].Problems = g.Nodes[i].Problems[:maxPerCard]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
g.ClusterProblems = sortProblems(s.cluster)
|
||||||
|
if g.ClusterProblems == nil {
|
||||||
|
g.ClusterProblems = []Problem{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func sortProblems(ps []Problem) []Problem {
|
||||||
|
sort.SliceStable(ps, func(i, j int) bool {
|
||||||
|
if severityRank[ps[i].Severity] != severityRank[ps[j].Severity] {
|
||||||
|
return severityRank[ps[i].Severity] > severityRank[ps[j].Severity]
|
||||||
|
}
|
||||||
|
return ps[i].Since > ps[j].Since
|
||||||
|
})
|
||||||
|
return ps
|
||||||
|
}
|
||||||
|
|
||||||
|
// namespaceOfID reads the namespace out of "wl/<ns>/…", "service/<ns>/…",
|
||||||
|
// "ingress/<ns>/…" and "ns/<ns>".
|
||||||
|
func namespaceOfID(id string) string {
|
||||||
|
parts := strings.SplitN(id, "/", 3)
|
||||||
|
if len(parts) >= 2 && parts[0] != "node" {
|
||||||
|
return parts[1]
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func stamp(t time.Time) string {
|
||||||
|
if t.IsZero() {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return t.UTC().Format(time.RFC3339)
|
||||||
|
}
|
||||||
|
|
||||||
|
func podProblems(s *problemSet, p corev1.Pod) {
|
||||||
|
id := OwnerWorkloadID(p)
|
||||||
|
if id == "" {
|
||||||
|
id = "ns/" + p.Namespace // a bare pod has no card; its namespace carries the badge
|
||||||
|
}
|
||||||
|
if p.Status.Phase == corev1.PodSucceeded {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if p.Status.Phase == corev1.PodFailed {
|
||||||
|
if p.Status.Reason == "Evicted" && now().Sub(p.CreationTimestamp.Time) < 24*time.Hour {
|
||||||
|
s.add(id, Problem{Severity: Warning, Source: "state", Reason: "Evicted", Message: p.Status.Message, Object: p.Name})
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
statuses := append(append([]corev1.ContainerStatus{}, p.Status.InitContainerStatuses...), p.Status.ContainerStatuses...)
|
||||||
|
for _, cs := range statuses {
|
||||||
|
obj := p.Name + "/" + cs.Name
|
||||||
|
if w := cs.State.Waiting; w != nil && waitingCritical[w.Reason] {
|
||||||
|
// a crash loop is also a restart; one problem, with the restart count
|
||||||
|
s.add(id, Problem{Severity: Critical, Source: "state", Reason: w.Reason, Message: w.Message, Object: obj, Count: int(cs.RestartCount)})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if t := cs.LastTerminationState.Terminated; t != nil && recentRestart(t.FinishedAt.Time, cs.RestartCount, t.Reason) {
|
||||||
|
reason := "Restarted"
|
||||||
|
if t.Reason == "OOMKilled" {
|
||||||
|
reason = "OOMKilled"
|
||||||
|
}
|
||||||
|
s.add(id, Problem{Severity: Warning, Source: "state", Reason: reason, Object: obj, Count: int(cs.RestartCount),
|
||||||
|
Message: strings.TrimSpace(t.Reason + " exit " + strconv.Itoa(int(t.ExitCode))), Since: stamp(t.FinishedAt.Time)})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if p.Status.Phase == corev1.PodPending && now().Sub(p.CreationTimestamp.Time) > pendingAfter {
|
||||||
|
pr := Problem{Severity: Warning, Source: "state", Reason: "Pending", Object: p.Name, Since: stamp(p.CreationTimestamp.Time)}
|
||||||
|
for _, c := range p.Status.Conditions {
|
||||||
|
if c.Type == corev1.PodScheduled && c.Status == corev1.ConditionFalse {
|
||||||
|
pr.Reason, pr.Message = "Unschedulable", c.Message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s.add(id, pr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// cronJobProblems: the last scheduled run ended without success. Job
|
||||||
|
// objects are not cached, so this reads the CronJob's own status: a
|
||||||
|
// schedule time later than the last success, with nothing running.
|
||||||
|
func cronJobProblems(s *problemSet, cj batchv1.CronJob) {
|
||||||
|
last := cj.Status.LastScheduleTime
|
||||||
|
if last == nil || len(cj.Status.Active) > 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ok := cj.Status.LastSuccessfulTime
|
||||||
|
if ok != nil && !ok.Before(last) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.add(WorkloadID(cj.Namespace, "cronjob", cj.Name), Problem{Severity: Warning, Source: "state", Reason: "LastRunFailed", Since: stamp(last.Time)})
|
||||||
|
}
|
||||||
|
|
||||||
|
func serviceProblems(s *problemSet, svc corev1.Service, pods []corev1.Pod, in problemInputs) {
|
||||||
|
if len(svc.Spec.Selector) == 0 || svc.Spec.Type == corev1.ServiceTypeExternalName {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, p := range pods {
|
||||||
|
if podReady(p) && SelectorMatches(svc.Spec.Selector, p.Labels) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
p := Problem{Severity: Warning, Source: "state", Reason: "NoEndpoints"}
|
||||||
|
if serviceControllersIdle(svc, in) {
|
||||||
|
p.Severity = Info
|
||||||
|
p.Message = "No endpoints expected: all matching workload controllers have observed their configuration and currently require zero pods."
|
||||||
|
}
|
||||||
|
s.add("service/"+svc.Namespace+"/"+svc.Name, p)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A zero-pod DaemonSet (no eligible nodes), or a scaled-down workload,
|
||||||
|
// can intentionally leave a Service empty. Unknown/stale state stays WARN.
|
||||||
|
func serviceControllersIdle(svc corev1.Service, in problemInputs) bool {
|
||||||
|
found := false
|
||||||
|
// A matching CronJob may be starting a Job whose pod is not listed yet.
|
||||||
|
for _, cj := range in.cronjobs {
|
||||||
|
if cj.Namespace == svc.Namespace && SelectorMatches(svc.Spec.Selector, cj.Spec.JobTemplate.Spec.Template.Labels) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, ds := range in.daemonsets {
|
||||||
|
if ds.Namespace != svc.Namespace || !SelectorMatches(svc.Spec.Selector, ds.Spec.Template.Labels) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
found = true
|
||||||
|
if ds.Status.ObservedGeneration < ds.Generation || ds.Status.DesiredNumberScheduled != 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, d := range in.deployments {
|
||||||
|
if d.Namespace != svc.Namespace || !SelectorMatches(svc.Spec.Selector, d.Spec.Template.Labels) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
found = true
|
||||||
|
if d.Status.ObservedGeneration < d.Generation || d.Spec.Replicas == nil || *d.Spec.Replicas != 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, st := range in.statefulsets {
|
||||||
|
if st.Namespace != svc.Namespace || !SelectorMatches(svc.Spec.Selector, st.Spec.Template.Labels) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
found = true
|
||||||
|
if st.Status.ObservedGeneration < st.Generation || st.Spec.Replicas == nil || *st.Spec.Replicas != 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// An unmanaged matching pod could still be genuinely broken.
|
||||||
|
for _, p := range in.pods {
|
||||||
|
if p.Namespace == svc.Namespace && SelectorMatches(svc.Spec.Selector, p.Labels) && p.DeletionTimestamp == nil && p.Status.Phase != corev1.PodSucceeded {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return found
|
||||||
|
}
|
||||||
|
|
||||||
|
func nodeProblems(s *problemSet, n corev1.Node) {
|
||||||
|
id := "node/" + n.Name
|
||||||
|
for _, c := range n.Status.Conditions {
|
||||||
|
switch {
|
||||||
|
case c.Type == corev1.NodeReady && c.Status != corev1.ConditionTrue:
|
||||||
|
s.add(id, Problem{Severity: Critical, Source: "state", Reason: "NodeNotReady", Message: c.Message, Since: stamp(c.LastTransitionTime.Time)})
|
||||||
|
case (c.Type == corev1.NodeMemoryPressure || c.Type == corev1.NodeDiskPressure || c.Type == corev1.NodePIDPressure) && c.Status == corev1.ConditionTrue:
|
||||||
|
s.add(id, Problem{Severity: Warning, Source: "state", Reason: string(c.Type), Message: c.Message, Since: stamp(c.LastTransitionTime.Time)})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if n.Spec.Unschedulable {
|
||||||
|
s.add(id, Problem{Severity: Info, Source: "state", Reason: "Cordoned"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// eventTime is when an event last happened, whichever field the emitter set.
|
||||||
|
func eventTime(e corev1.Event) time.Time {
|
||||||
|
switch {
|
||||||
|
case e.Series != nil && !e.Series.LastObservedTime.IsZero():
|
||||||
|
return e.Series.LastObservedTime.Time
|
||||||
|
case !e.LastTimestamp.IsZero():
|
||||||
|
return e.LastTimestamp.Time
|
||||||
|
case !e.EventTime.IsZero():
|
||||||
|
return e.EventTime.Time
|
||||||
|
}
|
||||||
|
return e.CreationTimestamp.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// EventTargetID maps an event's involved object to a card ID, the way pods
|
||||||
|
// and their controllers map to workloads.
|
||||||
|
func EventTargetID(ref corev1.ObjectReference, podOwner map[string]string) string {
|
||||||
|
ns := ref.Namespace
|
||||||
|
switch ref.Kind {
|
||||||
|
case "Pod":
|
||||||
|
if id := podOwner[ns+"/"+ref.Name]; id != "" {
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
return "ns/" + ns
|
||||||
|
case "ReplicaSet":
|
||||||
|
if i := strings.LastIndex(ref.Name, "-"); i > 0 {
|
||||||
|
return WorkloadID(ns, "deployment", ref.Name[:i])
|
||||||
|
}
|
||||||
|
case "Deployment", "StatefulSet", "DaemonSet", "CronJob":
|
||||||
|
return WorkloadID(ns, strings.ToLower(ref.Kind), ref.Name)
|
||||||
|
case "Job":
|
||||||
|
if cj, ok := CronJobFromJob(ref.Name); ok {
|
||||||
|
return WorkloadID(ns, "cronjob", cj)
|
||||||
|
}
|
||||||
|
case "Service":
|
||||||
|
return "service/" + ns + "/" + ref.Name
|
||||||
|
case "Ingress":
|
||||||
|
return "ingress/" + ns + "/" + ref.Name
|
||||||
|
case "Node":
|
||||||
|
return "node/" + ref.Name
|
||||||
|
}
|
||||||
|
if ns != "" {
|
||||||
|
return "ns/" + ns
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// eventsInWindow estimates how many occurrences of an event fell in the
|
||||||
|
// window. The event's count is cumulative since its first occurrence: 40
|
||||||
|
// probe failures over two days is one an hour, not 40 in half an hour, so
|
||||||
|
// a long-lived event is scaled by the part of its lifetime the window
|
||||||
|
// covers.
|
||||||
|
func eventsInWindow(e corev1.Event, last time.Time) int {
|
||||||
|
count := int(e.Count)
|
||||||
|
if e.Series != nil {
|
||||||
|
count = int(e.Series.Count)
|
||||||
|
}
|
||||||
|
count = max(count, 1)
|
||||||
|
first := e.FirstTimestamp.Time
|
||||||
|
if first.IsZero() {
|
||||||
|
first = e.EventTime.Time
|
||||||
|
}
|
||||||
|
span := last.Sub(first)
|
||||||
|
if first.IsZero() || span <= eventWindow {
|
||||||
|
return count
|
||||||
|
}
|
||||||
|
return max(1, int(float64(count)*float64(eventWindow)/float64(span)+0.5))
|
||||||
|
}
|
||||||
|
|
||||||
|
// eventProblems folds recent Warning events into one problem per card and
|
||||||
|
// reason, keeping the latest message.
|
||||||
|
func eventProblems(s *problemSet, events []corev1.Event, podOwner map[string]string, live, scheduled, ready map[string]bool) {
|
||||||
|
type key struct{ id, reason string }
|
||||||
|
folded := map[key]*Problem{}
|
||||||
|
var order []key
|
||||||
|
for _, e := range events {
|
||||||
|
at := eventTime(e)
|
||||||
|
if e.Type != corev1.EventTypeWarning || now().Sub(at) > eventWindow || syntheticProbe(e.InvolvedObject.Namespace, e.InvolvedObject.Name) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
id := EventTargetID(e.InvolvedObject, podOwner)
|
||||||
|
k := key{id, e.Reason}
|
||||||
|
count := eventsInWindow(e, at)
|
||||||
|
if p := folded[k]; p != nil {
|
||||||
|
p.Count += count
|
||||||
|
if stamp(at) > p.Since {
|
||||||
|
p.Since, p.Message, p.Object = stamp(at), e.Message, eventObject(e.InvolvedObject)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
folded[k] = &Problem{Severity: Warning, Source: "event", Reason: e.Reason, Message: e.Message, Object: eventObject(e.InvolvedObject), Count: count, Since: stamp(at)}
|
||||||
|
order = append(order, k)
|
||||||
|
}
|
||||||
|
for _, k := range order {
|
||||||
|
p := folded[k]
|
||||||
|
pod := namespaceOfID(k.id) + "/" + p.Object
|
||||||
|
// A pod that is running and ready already has its volumes: a
|
||||||
|
// FailedMount then is kubelet failing to refresh one, e.g. a
|
||||||
|
// ConfigMap prometheus-operator deletes and recreates. Once is
|
||||||
|
// noise; it has to repeat like any other event.
|
||||||
|
notable := eventNotable[p.Reason] && (p.Reason != "FailedMount" || !ready[pod])
|
||||||
|
if eventBenign[p.Reason] || (!notable && p.Count < eventRepeats) {
|
||||||
|
p.Severity = Info
|
||||||
|
}
|
||||||
|
// A failed probe of a pod that is ready now: the pod's own state
|
||||||
|
// already says more. Deckhouse answers /readyz with 500 while it
|
||||||
|
// works through its queue after a module change, and its probe
|
||||||
|
// tolerates 10 minutes of that (failureThreshold 120); a NotReady
|
||||||
|
// pod is reported from its state, a failed liveness probe as a
|
||||||
|
// restart.
|
||||||
|
if p.Reason == "Unhealthy" && ready[pod] {
|
||||||
|
p.Severity = Info
|
||||||
|
}
|
||||||
|
// the pod the latest event was about is gone: history, not state
|
||||||
|
if p.Object != "" && !live[pod] {
|
||||||
|
p.Severity = Info
|
||||||
|
}
|
||||||
|
// it could not be scheduled, but has been since: a CronJob pod
|
||||||
|
// that waited all night for its node to come back, then ran
|
||||||
|
if p.Reason == "FailedScheduling" && scheduled[pod] {
|
||||||
|
p.Severity = Info
|
||||||
|
}
|
||||||
|
s.add(k.id, *p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// readinessProblem turns the card's own health into a problem, so a
|
||||||
|
// workload with nothing ready is red even when no pod says why. It waits
|
||||||
|
// notReadyAfter for its pods, so a rollout or a restart does not flash.
|
||||||
|
// A workload short of replicas with no pod at all (none could be created)
|
||||||
|
// is reported at once.
|
||||||
|
func readinessProblem(s *problemSet, n Node, notReadySince map[string]time.Time) {
|
||||||
|
if !nativeKinds[n.Kind] && n.Facts["foreign"] != "true" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if since, ok := notReadySince[n.ID]; ok && now().Sub(since) < notReadyAfter {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch n.Facts["health"] {
|
||||||
|
case "error":
|
||||||
|
s.add(n.ID, Problem{Severity: Critical, Source: "state", Reason: "NotReady"})
|
||||||
|
case "warn":
|
||||||
|
s.add(n.ID, Problem{Severity: Warning, Source: "state", Reason: "PartlyReady"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// redirects reports whether an Ingress answers with a redirect instead of
|
||||||
|
// proxying, so its backends are never used. Deckhouse's prometheus module
|
||||||
|
// routes prometheus.<domain> to a Service that does not exist and sends
|
||||||
|
// every request to grafana.<domain> from a configuration snippet.
|
||||||
|
func redirects(ing networkingv1.Ingress) bool {
|
||||||
|
a := ing.Annotations
|
||||||
|
if a["nginx.ingress.kubernetes.io/permanent-redirect"] != "" || a["nginx.ingress.kubernetes.io/temporal-redirect"] != "" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
for _, key := range []string{"nginx.ingress.kubernetes.io/configuration-snippet", "nginx.ingress.kubernetes.io/server-snippet"} {
|
||||||
|
if redirectSnippet.MatchString(a[key]) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
var redirectSnippet = regexp.MustCompile(`(?m)\brewrite\s+\S+\s+\S+\s+(permanent|redirect)\s*;|\breturn\s+30[1278]\b`)
|
||||||
|
|
||||||
|
// podsLive: pods whose warnings still describe the present. A pod that
|
||||||
|
// finished successfully is history, like one that is gone.
|
||||||
|
func podsLive(pods []corev1.Pod) map[string]bool {
|
||||||
|
out := make(map[string]bool, len(pods))
|
||||||
|
for _, p := range pods {
|
||||||
|
if p.Status.Phase != corev1.PodSucceeded {
|
||||||
|
out[p.Namespace+"/"+p.Name] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func podsReady(pods []corev1.Pod) map[string]bool {
|
||||||
|
out := make(map[string]bool, len(pods))
|
||||||
|
for _, p := range pods {
|
||||||
|
for _, c := range p.Status.Conditions {
|
||||||
|
if c.Type == corev1.PodReady && c.Status == corev1.ConditionTrue {
|
||||||
|
out[p.Namespace+"/"+p.Name] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func podsScheduled(pods []corev1.Pod) map[string]bool {
|
||||||
|
out := make(map[string]bool, len(pods))
|
||||||
|
for _, p := range pods {
|
||||||
|
if p.Spec.NodeName != "" {
|
||||||
|
out[p.Namespace+"/"+p.Name] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// syntheticProbe: upmeter, Deckhouse's availability monitor, creates short
|
||||||
|
// lived StatefulSets and pods named upmeter-probe-* that are unschedulable
|
||||||
|
// on purpose (it measures how fast the control plane reacts). Their
|
||||||
|
// FailedScheduling and NotReady are the probe working, not a problem.
|
||||||
|
//
|
||||||
|
// smoke-mini-a…e are upmeter's synthetic service: upmeter moves their pods
|
||||||
|
// between nodes on purpose, so the Service goes without endpoints and the
|
||||||
|
// pods restart as part of the test. upmeter reports that availability
|
||||||
|
// itself.
|
||||||
|
func syntheticProbe(ns, name string) bool {
|
||||||
|
return ns == "d8-upmeter" && (strings.HasPrefix(name, "upmeter-probe-") || strings.HasPrefix(name, "smoke-mini-"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// notReadyFrom is when a running or pending pod stopped being ready (or
|
||||||
|
// was created, if it never was); ok is false for ready and finished pods.
|
||||||
|
func notReadyFrom(p corev1.Pod) (time.Time, bool) {
|
||||||
|
if p.Status.Phase == corev1.PodSucceeded || p.Status.Phase == corev1.PodFailed || p.DeletionTimestamp != nil {
|
||||||
|
return time.Time{}, false
|
||||||
|
}
|
||||||
|
for _, c := range p.Status.Conditions {
|
||||||
|
if c.Type == corev1.PodReady {
|
||||||
|
if c.Status == corev1.ConditionTrue {
|
||||||
|
return time.Time{}, false
|
||||||
|
}
|
||||||
|
if !c.LastTransitionTime.IsZero() {
|
||||||
|
return c.LastTransitionTime.Time, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return p.CreationTimestamp.Time, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func eventObject(ref corev1.ObjectReference) string {
|
||||||
|
if ref.Kind == "Pod" {
|
||||||
|
return ref.Name
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// AlertSeverity maps Deckhouse's severity_level (1 is the most severe) to a
|
||||||
|
// badge colour.
|
||||||
|
func AlertSeverity(level string) string {
|
||||||
|
n, err := strconv.Atoi(strings.TrimSpace(level))
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
return Warning
|
||||||
|
case n <= 3:
|
||||||
|
return Critical
|
||||||
|
case n <= 6:
|
||||||
|
return Warning
|
||||||
|
}
|
||||||
|
return Info
|
||||||
|
}
|
||||||
|
|
||||||
|
// AlertTargetID picks the card an alert is about from its labels, most
|
||||||
|
// specific first. Unknown objects fall back to the namespace, alerts
|
||||||
|
// without a namespace to the cluster.
|
||||||
|
func AlertTargetID(labels map[string]string, podOwner map[string]string) string {
|
||||||
|
ns := labels["namespace"]
|
||||||
|
if n := labels["node"]; n != "" && ns == "" {
|
||||||
|
return "node/" + n
|
||||||
|
}
|
||||||
|
if ns == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
for _, kind := range []string{"deployment", "statefulset", "daemonset", "cronjob"} {
|
||||||
|
if name := labels[kind]; name != "" {
|
||||||
|
return WorkloadID(ns, kind, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if pod := labels["pod"]; pod != "" {
|
||||||
|
if id := podOwner[ns+"/"+pod]; id != "" {
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if job := labels["job_name"]; job != "" {
|
||||||
|
if cj, ok := CronJobFromJob(job); ok {
|
||||||
|
return WorkloadID(ns, "cronjob", cj)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if svc := labels["service"]; svc != "" {
|
||||||
|
return "service/" + ns + "/" + svc
|
||||||
|
}
|
||||||
|
if ing := labels["ingress"]; ing != "" {
|
||||||
|
return "ingress/" + ns + "/" + ing
|
||||||
|
}
|
||||||
|
return "ns/" + ns
|
||||||
|
}
|
||||||
|
|
||||||
|
func alertProblem(s *problemSet, a *unstructured.Unstructured, podOwner map[string]string) {
|
||||||
|
if st, _, _ := unstructured.NestedString(a.Object, "status", "alertStatus"); st != "" && st != "firing" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
name, _, _ := unstructured.NestedString(a.Object, "alert", "name")
|
||||||
|
summary, _, _ := unstructured.NestedString(a.Object, "alert", "summary")
|
||||||
|
level, _, _ := unstructured.NestedString(a.Object, "alert", "severityLevel")
|
||||||
|
labels, _, _ := unstructured.NestedStringMap(a.Object, "alert", "labels")
|
||||||
|
since, _, _ := unstructured.NestedString(a.Object, "status", "startsAt")
|
||||||
|
s.add(AlertTargetID(labels, podOwner), Problem{Severity: AlertSeverity(level), Source: "alert", Reason: name, Message: strings.TrimSpace(summary), Since: since})
|
||||||
|
}
|
||||||
|
|
||||||
|
// recentRestart includes recent restarts and the extended OOM diagnostic.
|
||||||
|
func recentRestart(finished time.Time, restarts int32, reason string) bool {
|
||||||
|
age := now().Sub(finished)
|
||||||
|
// The count is lifetime, not a rate. A reboot can increment an old
|
||||||
|
// count without proving repeated recent failures. Keep the extended
|
||||||
|
// memory-pressure diagnostic only for an observed OOM termination.
|
||||||
|
return age < restartWindow || (reason == "OOMKilled" && restarts >= repeatedRestarts && age < repeatedWindow)
|
||||||
|
}
|
||||||
@@ -0,0 +1,372 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2026 The cluster-map Authors
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package topology
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
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/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
|
)
|
||||||
|
|
||||||
|
var t0 = time.Date(2026, 9, 24, 12, 0, 0, 0, time.UTC)
|
||||||
|
|
||||||
|
func TestLifetimeRestartCountDoesNotExtendOrdinaryRestartWindow(t *testing.T) {
|
||||||
|
withNow(t)
|
||||||
|
for _, reason := range []string{"Unknown", "Error", "Completed"} {
|
||||||
|
t.Run(reason, func(t *testing.T) {
|
||||||
|
g := &Graph{Nodes: []Node{{ID: "ns/system"}}}
|
||||||
|
attachProblems(g, problemInputs{pods: []corev1.Pod{{ObjectMeta: metav1.ObjectMeta{Name: "recovered", Namespace: "system"}, Status: corev1.PodStatus{Phase: corev1.PodRunning, Conditions: []corev1.PodCondition{{Type: corev1.PodReady, Status: corev1.ConditionTrue}}, ContainerStatuses: []corev1.ContainerStatus{{Name: "app", RestartCount: 12, LastTerminationState: corev1.ContainerState{Terminated: &corev1.ContainerStateTerminated{Reason: reason, FinishedAt: metav1.NewTime(t0.Add(-40 * time.Minute))}}}}}}}})
|
||||||
|
if p, ok := problemsOf(g, "ns/system")["Restarted"]; ok {
|
||||||
|
t.Fatalf("lifetime count treated as recent recurrence: %+v", p)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func withNow(t *testing.T) {
|
||||||
|
t.Helper()
|
||||||
|
old := now
|
||||||
|
now = func() time.Time { return t0 }
|
||||||
|
t.Cleanup(func() { now = old })
|
||||||
|
}
|
||||||
|
|
||||||
|
func ingressTo(ns, name, svc string) networkingv1.Ingress {
|
||||||
|
return networkingv1.Ingress{ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: ns}, Spec: networkingv1.IngressSpec{
|
||||||
|
DefaultBackend: &networkingv1.IngressBackend{Service: &networkingv1.IngressServiceBackend{Name: svc}},
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func problemsOf(g *Graph, id string) map[string]Problem {
|
||||||
|
out := map[string]Problem{}
|
||||||
|
for _, n := range g.Nodes {
|
||||||
|
if n.ID == id {
|
||||||
|
for _, p := range n.Problems {
|
||||||
|
out[p.Reason] = p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProblemsFromState(t *testing.T) {
|
||||||
|
withNow(t)
|
||||||
|
ready := []corev1.PodCondition{{Type: corev1.PodReady, Status: corev1.ConditionTrue}}
|
||||||
|
g := &Graph{Nodes: []Node{
|
||||||
|
{ID: "ns/shop"}, {ID: "wl/shop/deployment/web", Kind: "deployment", Facts: map[string]string{"health": "warn"}}, {ID: "wl/shop/deployment/api", Kind: "deployment"}, {ID: "service/shop/web"}, {ID: "service/shop/idle"},
|
||||||
|
{ID: "ingress/shop/web"}, {ID: "wl/shop/cronjob/backup"}, {ID: "node/node-a"},
|
||||||
|
}}
|
||||||
|
in := problemInputs{
|
||||||
|
pods: []corev1.Pod{
|
||||||
|
{ // crash-looping, restarted 10 min ago
|
||||||
|
ObjectMeta: metav1.ObjectMeta{Name: "web-5f-a", Namespace: "shop", Labels: map[string]string{"app": "web"}, OwnerReferences: owned("ReplicaSet", "web-5f")},
|
||||||
|
Status: corev1.PodStatus{Phase: corev1.PodRunning, ContainerStatuses: []corev1.ContainerStatus{{
|
||||||
|
Name: "app", RestartCount: 7,
|
||||||
|
State: corev1.ContainerState{Waiting: &corev1.ContainerStateWaiting{Reason: "CrashLoopBackOff"}},
|
||||||
|
LastTerminationState: corev1.ContainerState{Terminated: &corev1.ContainerStateTerminated{Reason: "Error", ExitCode: 1, FinishedAt: metav1.NewTime(t0.Add(-10 * time.Minute))}},
|
||||||
|
}}},
|
||||||
|
},
|
||||||
|
{ // ready, OOM-killed long ago: no problem
|
||||||
|
ObjectMeta: metav1.ObjectMeta{Name: "web-5f-b", Namespace: "shop", Labels: map[string]string{"app": "web"}, OwnerReferences: owned("ReplicaSet", "web-5f")},
|
||||||
|
Status: corev1.PodStatus{Phase: corev1.PodRunning, Conditions: ready, ContainerStatuses: []corev1.ContainerStatus{{
|
||||||
|
Name: "app", LastTerminationState: corev1.ContainerState{Terminated: &corev1.ContainerStateTerminated{Reason: "OOMKilled", FinishedAt: metav1.NewTime(t0.Add(-3 * time.Hour))}},
|
||||||
|
}}},
|
||||||
|
},
|
||||||
|
{ // OOM-killed five times, the last 70 minutes ago: still a problem
|
||||||
|
ObjectMeta: metav1.ObjectMeta{Name: "api-7c-a", Namespace: "shop", Labels: map[string]string{"app": "api"}, OwnerReferences: owned("ReplicaSet", "api-7c")},
|
||||||
|
Status: corev1.PodStatus{Phase: corev1.PodRunning, Conditions: ready, ContainerStatuses: []corev1.ContainerStatus{{
|
||||||
|
Name: "app", RestartCount: 5,
|
||||||
|
LastTerminationState: corev1.ContainerState{Terminated: &corev1.ContainerStateTerminated{Reason: "OOMKilled", ExitCode: 137, FinishedAt: metav1.NewTime(t0.Add(-70 * time.Minute))}},
|
||||||
|
}}},
|
||||||
|
},
|
||||||
|
{ // bare pod pending for 10 minutes: badge on the namespace
|
||||||
|
ObjectMeta: metav1.ObjectMeta{Name: "debug", Namespace: "shop", CreationTimestamp: metav1.NewTime(t0.Add(-10 * time.Minute))},
|
||||||
|
Status: corev1.PodStatus{Phase: corev1.PodPending, Conditions: []corev1.PodCondition{{Type: corev1.PodScheduled, Status: corev1.ConditionFalse, Message: "0/3 nodes are available"}}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
services: []corev1.Service{
|
||||||
|
{ObjectMeta: metav1.ObjectMeta{Name: "web", Namespace: "shop"}, Spec: corev1.ServiceSpec{Selector: map[string]string{"app": "web"}}},
|
||||||
|
{ObjectMeta: metav1.ObjectMeta{Name: "idle", Namespace: "shop"}, Spec: corev1.ServiceSpec{Selector: map[string]string{"app": "gone"}}},
|
||||||
|
},
|
||||||
|
ingresses: []networkingv1.Ingress{ingressTo("shop", "web", "missing")},
|
||||||
|
cronjobs: []batchv1.CronJob{{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{Name: "backup", Namespace: "shop"},
|
||||||
|
Status: batchv1.CronJobStatus{LastScheduleTime: &metav1.Time{Time: t0.Add(-time.Hour)}, LastSuccessfulTime: &metav1.Time{Time: t0.Add(-25 * time.Hour)}},
|
||||||
|
}},
|
||||||
|
nodes: []corev1.Node{{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{Name: "node-a"}, Spec: corev1.NodeSpec{Unschedulable: true},
|
||||||
|
Status: corev1.NodeStatus{Conditions: []corev1.NodeCondition{{Type: corev1.NodeReady, Status: corev1.ConditionTrue}, {Type: corev1.NodeDiskPressure, Status: corev1.ConditionTrue}}},
|
||||||
|
}},
|
||||||
|
deployments: []appsv1.Deployment{},
|
||||||
|
}
|
||||||
|
attachProblems(g, in)
|
||||||
|
|
||||||
|
web := problemsOf(g, "wl/shop/deployment/web")
|
||||||
|
if p := web["CrashLoopBackOff"]; p.Severity != Critical || p.Object != "web-5f-a/app" || p.Count != 7 {
|
||||||
|
t.Errorf("crash loop: %+v", web)
|
||||||
|
}
|
||||||
|
if web["PartlyReady"].Severity != Warning {
|
||||||
|
t.Errorf("partly ready workload: %+v", web)
|
||||||
|
}
|
||||||
|
if _, ok := web["Restarted"]; ok {
|
||||||
|
t.Error("a crash loop is reported once, not also as a restart")
|
||||||
|
}
|
||||||
|
if _, ok := web["OOMKilled"]; ok {
|
||||||
|
t.Error("an OOM kill three hours ago must not mark the card")
|
||||||
|
}
|
||||||
|
if p := problemsOf(g, "wl/shop/deployment/api")["OOMKilled"]; p.Severity != Warning || p.Count != 5 {
|
||||||
|
t.Errorf("repeated OOM kills 70 minutes ago: %+v", problemsOf(g, "wl/shop/deployment/api"))
|
||||||
|
}
|
||||||
|
if _, ok := problemsOf(g, "service/shop/web")["NoEndpoints"]; ok {
|
||||||
|
t.Error("service with a ready pod reported without endpoints")
|
||||||
|
}
|
||||||
|
if _, ok := problemsOf(g, "service/shop/idle")["NoEndpoints"]; !ok {
|
||||||
|
t.Error("service selecting nothing not reported")
|
||||||
|
}
|
||||||
|
if p := problemsOf(g, "ingress/shop/web")["BackendMissing"]; p.Object != "missing" {
|
||||||
|
t.Errorf("ingress to a missing service: %+v", p)
|
||||||
|
}
|
||||||
|
if _, ok := problemsOf(g, "wl/shop/cronjob/backup")["LastRunFailed"]; !ok {
|
||||||
|
t.Error("cronjob whose last run did not succeed")
|
||||||
|
}
|
||||||
|
if p := problemsOf(g, "ns/shop")["Unschedulable"]; p.Message == "" {
|
||||||
|
t.Errorf("bare pending pod on the namespace: %+v", problemsOf(g, "ns/shop"))
|
||||||
|
}
|
||||||
|
node := problemsOf(g, "node/node-a")
|
||||||
|
if node["DiskPressure"].Severity != Warning || node["Cordoned"].Severity != Info {
|
||||||
|
t.Errorf("node: %+v", node)
|
||||||
|
}
|
||||||
|
if got := g.Nodes[1].Problems[0].Severity; got != Critical {
|
||||||
|
t.Errorf("critical first, got %s", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadinessGraceAndProbes(t *testing.T) {
|
||||||
|
withNow(t)
|
||||||
|
notReady := func(name string, since time.Duration) corev1.Pod {
|
||||||
|
return corev1.Pod{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "shop", OwnerReferences: owned("ReplicaSet", name[:len(name)-2])},
|
||||||
|
Status: corev1.PodStatus{Phase: corev1.PodRunning, Conditions: []corev1.PodCondition{
|
||||||
|
{Type: corev1.PodReady, Status: corev1.ConditionFalse, LastTransitionTime: metav1.NewTime(t0.Add(-since))}}},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
g := &Graph{Nodes: []Node{
|
||||||
|
{ID: "ns/shop"}, {ID: "ns/d8-upmeter"},
|
||||||
|
{ID: "wl/shop/deployment/fresh", Kind: "deployment", Facts: map[string]string{"health": "error"}},
|
||||||
|
{ID: "wl/shop/deployment/stuck", Kind: "deployment", Facts: map[string]string{"health": "error"}},
|
||||||
|
{ID: "wl/shop/deployment/nopods", Kind: "deployment", Facts: map[string]string{"health": "error"}},
|
||||||
|
}}
|
||||||
|
attachProblems(g, problemInputs{
|
||||||
|
pods: []corev1.Pod{notReady("fresh-5f-a", 30*time.Second), notReady("stuck-5f-a", 10*time.Minute)},
|
||||||
|
events: []corev1.Event{{Type: corev1.EventTypeWarning, Reason: "FailedScheduling", LastTimestamp: metav1.NewTime(t0),
|
||||||
|
InvolvedObject: corev1.ObjectReference{Kind: "Pod", Namespace: "d8-upmeter", Name: "upmeter-probe-controller-manager-9b2cc8b5-0"}}},
|
||||||
|
})
|
||||||
|
if _, ok := problemsOf(g, "wl/shop/deployment/fresh")["NotReady"]; ok {
|
||||||
|
t.Error("a pod not ready for 30 seconds marks the card")
|
||||||
|
}
|
||||||
|
if _, ok := problemsOf(g, "wl/shop/deployment/stuck")["NotReady"]; !ok {
|
||||||
|
t.Error("a pod not ready for 10 minutes does not mark the card")
|
||||||
|
}
|
||||||
|
if _, ok := problemsOf(g, "wl/shop/deployment/nopods")["NotReady"]; !ok {
|
||||||
|
t.Error("a workload with no pod at all must be reported")
|
||||||
|
}
|
||||||
|
if len(problemsOf(g, "ns/d8-upmeter")) != 0 {
|
||||||
|
t.Error("upmeter's synthetic probe reported as a problem")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRedirectIngressAndGonePods(t *testing.T) {
|
||||||
|
withNow(t)
|
||||||
|
redirect := ingressTo("d8-monitoring", "grafana-v10", "grafana")
|
||||||
|
redirect.Annotations = map[string]string{"nginx.ingress.kubernetes.io/configuration-snippet": "if ($host = 'prometheus.example.com' ) {\n rewrite ^ https://grafana.example.com$request_uri permanent;\n}\n"}
|
||||||
|
g := &Graph{Nodes: []Node{{ID: "ns/shop"}, {ID: "ingress/d8-monitoring/grafana-v10"}, {ID: "wl/shop/deployment/web"}}}
|
||||||
|
attachProblems(g, problemInputs{
|
||||||
|
ingresses: []networkingv1.Ingress{redirect},
|
||||||
|
pods: []corev1.Pod{{ObjectMeta: metav1.ObjectMeta{Name: "web-5f-live", Namespace: "shop", OwnerReferences: owned("ReplicaSet", "web-5f")}}},
|
||||||
|
events: []corev1.Event{
|
||||||
|
{Type: corev1.EventTypeWarning, Reason: "FailedMount", LastTimestamp: metav1.NewTime(t0), InvolvedObject: corev1.ObjectReference{Kind: "Pod", Namespace: "shop", Name: "web-5f-live"}},
|
||||||
|
{Type: corev1.EventTypeWarning, Reason: "Failed", Count: 10, LastTimestamp: metav1.NewTime(t0), InvolvedObject: corev1.ObjectReference{Kind: "Pod", Namespace: "shop", Name: "verify-gone"}},
|
||||||
|
{Type: corev1.EventTypeWarning, Reason: "UnexpectedJob", Count: 6, LastTimestamp: metav1.NewTime(t0), InvolvedObject: corev1.ObjectReference{Kind: "CronJob", Namespace: "shop", Name: "backup"}},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if p := problemsOf(g, "ns/shop")["UnexpectedJob"]; p.Severity != Info {
|
||||||
|
t.Errorf("a manual CronJob run is not a problem: %+v", p)
|
||||||
|
}
|
||||||
|
if len(problemsOf(g, "ingress/d8-monitoring/grafana-v10")) != 0 {
|
||||||
|
t.Error("a redirecting Ingress reported for its unused backend")
|
||||||
|
}
|
||||||
|
if problemsOf(g, "wl/shop/deployment/web")["FailedMount"].Severity != Warning {
|
||||||
|
t.Error("event of a live pod must badge")
|
||||||
|
}
|
||||||
|
// the same pod once it is ready: a single FailedMount is a volume refresh
|
||||||
|
g2 := &Graph{Nodes: []Node{{ID: "ns/shop"}, {ID: "wl/shop/deployment/web"}}}
|
||||||
|
attachProblems(g2, problemInputs{
|
||||||
|
pods: []corev1.Pod{{ObjectMeta: metav1.ObjectMeta{Name: "web-5f-live", Namespace: "shop", OwnerReferences: owned("ReplicaSet", "web-5f")},
|
||||||
|
Status: corev1.PodStatus{Phase: corev1.PodRunning, Conditions: []corev1.PodCondition{{Type: corev1.PodReady, Status: corev1.ConditionTrue}}}}},
|
||||||
|
events: []corev1.Event{
|
||||||
|
{Type: corev1.EventTypeWarning, Reason: "FailedMount", LastTimestamp: metav1.NewTime(t0), InvolvedObject: corev1.ObjectReference{Kind: "Pod", Namespace: "shop", Name: "web-5f-live"}},
|
||||||
|
{Type: corev1.EventTypeWarning, Reason: "Unhealthy", Count: 11, LastTimestamp: metav1.NewTime(t0), InvolvedObject: corev1.ObjectReference{Kind: "Pod", Namespace: "shop", Name: "web-5f-live"}},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if p := problemsOf(g2, "wl/shop/deployment/web")["Unhealthy"]; p.Severity != Info {
|
||||||
|
t.Errorf("failed probes of a pod that is ready must be information: %+v", p)
|
||||||
|
}
|
||||||
|
if p := problemsOf(g2, "wl/shop/deployment/web")["FailedMount"]; p.Severity != Info {
|
||||||
|
t.Errorf("one FailedMount of a ready pod must be information: %+v", p)
|
||||||
|
}
|
||||||
|
if p := problemsOf(g, "ns/shop")["Failed"]; p.Severity != Info {
|
||||||
|
t.Errorf("event of a deleted pod must be information: %+v", p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEventRateAndSmokeMini(t *testing.T) {
|
||||||
|
withNow(t)
|
||||||
|
g := &Graph{Nodes: []Node{{ID: "ns/shop"}, {ID: "ns/d8-upmeter"}, {ID: "wl/shop/deployment/auth"}, {ID: "wl/shop/deployment/burst"}, {ID: "service/d8-upmeter/smoke-mini-a"}}}
|
||||||
|
attachProblems(g, problemInputs{
|
||||||
|
pods: []corev1.Pod{
|
||||||
|
{ObjectMeta: metav1.ObjectMeta{Name: "auth-5f-a", Namespace: "shop", OwnerReferences: owned("ReplicaSet", "auth-5f")}},
|
||||||
|
{ObjectMeta: metav1.ObjectMeta{Name: "burst-5f-a", Namespace: "shop", OwnerReferences: owned("ReplicaSet", "burst-5f")}},
|
||||||
|
},
|
||||||
|
services: []corev1.Service{{ObjectMeta: metav1.ObjectMeta{Name: "smoke-mini-a", Namespace: "d8-upmeter"}, Spec: corev1.ServiceSpec{Selector: map[string]string{"smoke-mini": "a"}}}},
|
||||||
|
events: []corev1.Event{
|
||||||
|
// 40 over two days: about one an hour
|
||||||
|
{Type: corev1.EventTypeWarning, Reason: "Unhealthy", Count: 40, FirstTimestamp: metav1.NewTime(t0.Add(-48 * time.Hour)), LastTimestamp: metav1.NewTime(t0.Add(-time.Minute)),
|
||||||
|
InvolvedObject: corev1.ObjectReference{Kind: "Pod", Namespace: "shop", Name: "auth-5f-a"}},
|
||||||
|
// 12 within ten minutes
|
||||||
|
{Type: corev1.EventTypeWarning, Reason: "Unhealthy", Count: 12, FirstTimestamp: metav1.NewTime(t0.Add(-10 * time.Minute)), LastTimestamp: metav1.NewTime(t0.Add(-time.Minute)),
|
||||||
|
InvolvedObject: corev1.ObjectReference{Kind: "Pod", Namespace: "shop", Name: "burst-5f-a"}},
|
||||||
|
{Type: corev1.EventTypeWarning, Reason: "FailedMount", LastTimestamp: metav1.NewTime(t0), InvolvedObject: corev1.ObjectReference{Kind: "Pod", Namespace: "d8-upmeter", Name: "smoke-mini-c-0"}},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if p := problemsOf(g, "wl/shop/deployment/auth")["Unhealthy"]; p.Severity != Info || p.Count > 2 {
|
||||||
|
t.Errorf("a slow trickle over two days must be information: %+v", p)
|
||||||
|
}
|
||||||
|
if p := problemsOf(g, "wl/shop/deployment/burst")["Unhealthy"]; p.Severity != Warning || p.Count != 12 {
|
||||||
|
t.Errorf("a burst inside the window must badge: %+v", p)
|
||||||
|
}
|
||||||
|
if len(problemsOf(g, "service/d8-upmeter/smoke-mini-a"))+len(problemsOf(g, "ns/d8-upmeter")) != 0 {
|
||||||
|
t.Error("upmeter's smoke-mini reported as a problem")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func alert(name, level string, labels map[string]string) *unstructured.Unstructured {
|
||||||
|
l := map[string]any{}
|
||||||
|
for k, v := range labels {
|
||||||
|
l[k] = v
|
||||||
|
}
|
||||||
|
return &unstructured.Unstructured{Object: map[string]any{
|
||||||
|
"alert": map[string]any{"name": name, "severityLevel": level, "summary": "summary of " + name, "labels": l},
|
||||||
|
"status": map[string]any{"alertStatus": "firing", "startsAt": "2026-09-24T10:00:00Z"},
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAlertsAndEvents(t *testing.T) {
|
||||||
|
withNow(t)
|
||||||
|
g := &Graph{Nodes: []Node{{ID: "ns/shop"}, {ID: "wl/shop/deployment/web"}, {ID: "node/node-a"}}}
|
||||||
|
in := problemInputs{
|
||||||
|
pods: []corev1.Pod{{ObjectMeta: metav1.ObjectMeta{Name: "web-5f-a", Namespace: "shop", OwnerReferences: owned("ReplicaSet", "web-5f")}}},
|
||||||
|
alerts: []*unstructured.Unstructured{
|
||||||
|
alert("KubernetesDeploymentReplicasUnavailable", "3", map[string]string{"namespace": "shop", "deployment": "web"}),
|
||||||
|
alert("PodHighMemory", "5", map[string]string{"namespace": "shop", "pod": "web-5f-a"}),
|
||||||
|
alert("NamespaceQuota", "6", map[string]string{"namespace": "shop"}),
|
||||||
|
alert("NodeDiskFull", "4", map[string]string{"node": "node-a"}),
|
||||||
|
alert("ModuleConfigObsoleteVersion", "4", map[string]string{"name": "operator-trivy"}),
|
||||||
|
alert("GoneNamespace", "4", map[string]string{"namespace": "deleted"}),
|
||||||
|
},
|
||||||
|
events: []corev1.Event{
|
||||||
|
{Type: corev1.EventTypeWarning, Reason: "ProbeWarning", Count: 1, InvolvedObject: corev1.ObjectReference{Kind: "Node", Name: "node-a"}, LastTimestamp: metav1.NewTime(t0.Add(-time.Minute))},
|
||||||
|
{Type: corev1.EventTypeWarning, Reason: "Unhealthy", Message: "old", Count: 2, InvolvedObject: corev1.ObjectReference{Kind: "Pod", Namespace: "shop", Name: "web-5f-a"}, LastTimestamp: metav1.NewTime(t0.Add(-20 * time.Minute))},
|
||||||
|
{Type: corev1.EventTypeWarning, Reason: "Unhealthy", Message: "latest", Count: 3, InvolvedObject: corev1.ObjectReference{Kind: "Pod", Namespace: "shop", Name: "web-5f-a"}, LastTimestamp: metav1.NewTime(t0.Add(-time.Minute))},
|
||||||
|
{Type: corev1.EventTypeWarning, Reason: "FailedMount", InvolvedObject: corev1.ObjectReference{Kind: "Pod", Namespace: "shop", Name: "web-5f-a"}, LastTimestamp: metav1.NewTime(t0.Add(-2 * time.Hour))},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
attachProblems(g, in)
|
||||||
|
web := problemsOf(g, "wl/shop/deployment/web")
|
||||||
|
if web["KubernetesDeploymentReplicasUnavailable"].Severity != Critical || web["PodHighMemory"].Severity != Warning {
|
||||||
|
t.Errorf("alerts on the workload: %+v", web)
|
||||||
|
}
|
||||||
|
if p := web["Unhealthy"]; p.Count != 5 || p.Message != "latest" || p.Severity != Warning {
|
||||||
|
t.Errorf("folded events: %+v", p)
|
||||||
|
}
|
||||||
|
if _, ok := web["FailedMount"]; ok {
|
||||||
|
t.Error("a warning two hours old must not mark the card")
|
||||||
|
}
|
||||||
|
if _, ok := problemsOf(g, "ns/shop")["NamespaceQuota"]; !ok {
|
||||||
|
t.Error("namespace-level alert")
|
||||||
|
}
|
||||||
|
if _, ok := problemsOf(g, "node/node-a")["NodeDiskFull"]; !ok {
|
||||||
|
t.Error("node alert")
|
||||||
|
}
|
||||||
|
if p := problemsOf(g, "node/node-a")["ProbeWarning"]; p.Severity != Info {
|
||||||
|
t.Errorf("a single ordinary warning is listed, not badged: %+v", p)
|
||||||
|
}
|
||||||
|
cluster := map[string]bool{}
|
||||||
|
for _, p := range g.ClusterProblems {
|
||||||
|
cluster[p.Reason] = true
|
||||||
|
}
|
||||||
|
if !cluster["ModuleConfigObsoleteVersion"] || !cluster["GoneNamespace"] || len(g.ClusterProblems) != 2 {
|
||||||
|
t.Errorf("cluster problems: %+v", g.ClusterProblems)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAlertSeverity(t *testing.T) {
|
||||||
|
for level, want := range map[string]string{"1": Critical, "3": Critical, "4": Warning, "6": Warning, "7": Info, "": Warning} {
|
||||||
|
if got := AlertSeverity(level); got != want {
|
||||||
|
t.Errorf("level %q: %s, want %s", level, got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScheduledAndFinishedPodEvents(t *testing.T) {
|
||||||
|
withNow(t)
|
||||||
|
pod := func(name string, node string, phase corev1.PodPhase) corev1.Pod {
|
||||||
|
return corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "games", OwnerReferences: owned("Job", name[:len(name)-2])},
|
||||||
|
Spec: corev1.PodSpec{NodeName: node}, Status: corev1.PodStatus{Phase: phase}}
|
||||||
|
}
|
||||||
|
warn := func(reason, podName string) corev1.Event {
|
||||||
|
return corev1.Event{Type: corev1.EventTypeWarning, Reason: reason, Count: 28, LastTimestamp: metav1.NewTime(t0),
|
||||||
|
InvolvedObject: corev1.ObjectReference{Kind: "Pod", Namespace: "games", Name: podName}}
|
||||||
|
}
|
||||||
|
g := &Graph{Nodes: []Node{{ID: "ns/games"}}}
|
||||||
|
attachProblems(g, problemInputs{
|
||||||
|
pods: []corev1.Pod{
|
||||||
|
pod("waited-a", "games-vm", corev1.PodSucceeded), // waited for its node overnight, then ran
|
||||||
|
pod("stuck-a", "", corev1.PodPending), // still cannot be placed
|
||||||
|
pod("done-a", "xeon", corev1.PodSucceeded),
|
||||||
|
},
|
||||||
|
events: []corev1.Event{warn("FailedScheduling", "waited-a"), warn("FailedScheduling", "stuck-a"), warn("FailedMount", "done-a")},
|
||||||
|
})
|
||||||
|
var stuck, waited, done bool
|
||||||
|
for _, p := range g.Nodes[0].Problems {
|
||||||
|
switch p.Object {
|
||||||
|
case "stuck-a":
|
||||||
|
stuck = p.Severity == Warning
|
||||||
|
case "waited-a":
|
||||||
|
waited = p.Severity == Info
|
||||||
|
case "done-a":
|
||||||
|
done = p.Severity == Info
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !stuck || !waited || !done {
|
||||||
|
t.Errorf("pending pod must badge, scheduled or finished must not (stuck=%v waited=%v done=%v): %+v", stuck, waited, done, g.Nodes[0].Problems)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package topology
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
appsv1 "k8s.io/api/apps/v1"
|
||||||
|
batchv1 "k8s.io/api/batch/v1"
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestServiceWithoutEndpointsExpectedZero(t *testing.T) {
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
desired int32
|
||||||
|
observed int64
|
||||||
|
activeDeployment bool
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{"idle-daemonset", 0, 3, false, Info},
|
||||||
|
{"missing-daemonset-pod", 1, 3, false, Warning},
|
||||||
|
{"stale-controller-status", 0, 2, false, Warning},
|
||||||
|
{"another-active-controller", 0, 3, true, Warning},
|
||||||
|
} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
labels := map[string]string{"app": "time"}
|
||||||
|
in := problemInputs{services: []corev1.Service{{ObjectMeta: metav1.ObjectMeta{Name: "time", Namespace: "system"}, Spec: corev1.ServiceSpec{Selector: labels}}},
|
||||||
|
daemonsets: []appsv1.DaemonSet{{ObjectMeta: metav1.ObjectMeta{Name: "masters", Namespace: "system", Generation: 3}, Spec: appsv1.DaemonSetSpec{Template: corev1.PodTemplateSpec{ObjectMeta: metav1.ObjectMeta{Labels: labels}}}, Status: appsv1.DaemonSetStatus{DesiredNumberScheduled: tc.desired, ObservedGeneration: tc.observed}}}}
|
||||||
|
if tc.activeDeployment {
|
||||||
|
one := int32(1)
|
||||||
|
in.deployments = []appsv1.Deployment{{ObjectMeta: metav1.ObjectMeta{Namespace: "system"}, Spec: appsv1.DeploymentSpec{Replicas: &one, Template: corev1.PodTemplateSpec{ObjectMeta: metav1.ObjectMeta{Labels: labels}}}}}
|
||||||
|
}
|
||||||
|
g := &Graph{Nodes: []Node{{ID: "service/system/time"}}}
|
||||||
|
attachProblems(g, in)
|
||||||
|
p := problemsOf(g, "service/system/time")["NoEndpoints"]
|
||||||
|
if p.Severity != tc.want {
|
||||||
|
t.Fatalf("got %+v, want %s", p, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIdleServiceStillWarnsForCronJobAndPendingPod(t *testing.T) {
|
||||||
|
zero := int32(0)
|
||||||
|
labels := map[string]string{"app": "time"}
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
cron bool
|
||||||
|
}{{"cronjob-starting", true}, {"pod-pending", false}} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
in := problemInputs{deployments: []appsv1.Deployment{{ObjectMeta: metav1.ObjectMeta{Namespace: "system"}, Spec: appsv1.DeploymentSpec{Replicas: &zero, Template: corev1.PodTemplateSpec{ObjectMeta: metav1.ObjectMeta{Labels: labels}}}}}}
|
||||||
|
if tc.cron {
|
||||||
|
in.cronjobs = []batchv1.CronJob{{ObjectMeta: metav1.ObjectMeta{Namespace: "system"}, Spec: batchv1.CronJobSpec{JobTemplate: batchv1.JobTemplateSpec{Spec: batchv1.JobSpec{Template: corev1.PodTemplateSpec{ObjectMeta: metav1.ObjectMeta{Labels: labels}}}}}}}
|
||||||
|
} else {
|
||||||
|
in.pods = []corev1.Pod{{ObjectMeta: metav1.ObjectMeta{Namespace: "system", Labels: labels}, Status: corev1.PodStatus{Phase: corev1.PodPending}}}
|
||||||
|
}
|
||||||
|
if serviceControllersIdle(corev1.Service{ObjectMeta: metav1.ObjectMeta{Namespace: "system"}, Spec: corev1.ServiceSpec{Selector: labels}}, in) {
|
||||||
|
t.Fatal("active controller/pod incorrectly treated as idle")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,203 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2026 The cluster-map Authors
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package topology
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sort"
|
||||||
|
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
|
)
|
||||||
|
|
||||||
|
// VulnSummary is what a card shows about its images: counts of distinct
|
||||||
|
// CRITICAL and HIGH CVEs (operator-trivy keeps only those here) and how
|
||||||
|
// many of them a newer package version fixes. One CVE in six subpackages
|
||||||
|
// of openssh is one CVE.
|
||||||
|
type VulnSummary struct {
|
||||||
|
Critical int `json:"critical"`
|
||||||
|
High int `json:"high"`
|
||||||
|
FixableCritical int `json:"fixableCritical"`
|
||||||
|
Fixable int `json:"fixable"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vuln is one finding in the details list.
|
||||||
|
type Vuln struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Severity string `json:"severity"`
|
||||||
|
Package string `json:"package"` // comma-separated when the CVE hits several packages of one image
|
||||||
|
Installed string `json:"installed"`
|
||||||
|
Fixed string `json:"fixed,omitempty"`
|
||||||
|
Title string `json:"title,omitempty"`
|
||||||
|
Link string `json:"link,omitempty"`
|
||||||
|
Score float64 `json:"score,omitempty"`
|
||||||
|
Image string `json:"image"`
|
||||||
|
Container string `json:"container"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// vulnTargetID maps a report to its card the way events are mapped: the
|
||||||
|
// report is labelled with the scanned object (a ReplicaSet, StatefulSet,
|
||||||
|
// CronJob, Job or bare Pod).
|
||||||
|
func vulnTargetID(r *unstructured.Unstructured, podOwner map[string]string) string {
|
||||||
|
l := r.GetLabels()
|
||||||
|
ref := corev1.ObjectReference{
|
||||||
|
Kind: l["trivy-operator.resource.kind"],
|
||||||
|
Name: l["trivy-operator.resource.name"],
|
||||||
|
Namespace: l["trivy-operator.resource.namespace"],
|
||||||
|
}
|
||||||
|
if ref.Namespace == "" {
|
||||||
|
ref.Namespace = r.GetNamespace()
|
||||||
|
}
|
||||||
|
return EventTargetID(ref, podOwner)
|
||||||
|
}
|
||||||
|
|
||||||
|
func reportImage(r *unstructured.Unstructured) string {
|
||||||
|
server, _, _ := unstructured.NestedString(r.Object, "report", "registry", "server")
|
||||||
|
repo, _, _ := unstructured.NestedString(r.Object, "report", "artifact", "repository")
|
||||||
|
tag, _, _ := unstructured.NestedString(r.Object, "report", "artifact", "tag")
|
||||||
|
img := repo
|
||||||
|
if server != "" {
|
||||||
|
img = server + "/" + repo
|
||||||
|
}
|
||||||
|
if tag != "" {
|
||||||
|
img += ":" + tag
|
||||||
|
}
|
||||||
|
return img
|
||||||
|
}
|
||||||
|
|
||||||
|
func reportVulns(r *unstructured.Unstructured) []Vuln {
|
||||||
|
raw, _, _ := unstructured.NestedSlice(r.Object, "report", "vulnerabilities")
|
||||||
|
image, container := reportImage(r), r.GetLabels()["trivy-operator.container.name"]
|
||||||
|
out := make([]Vuln, 0, len(raw))
|
||||||
|
for _, x := range raw {
|
||||||
|
m, ok := x.(map[string]any)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
str := func(k string) string { s, _ := m[k].(string); return s }
|
||||||
|
score, _ := m["score"].(float64)
|
||||||
|
out = append(out, Vuln{
|
||||||
|
ID: str("vulnerabilityID"), Severity: str("severity"), Package: str("resource"),
|
||||||
|
Installed: str("installedVersion"), Fixed: str("fixedVersion"), Title: str("title"),
|
||||||
|
Link: str("primaryLink"), Score: score, Image: image, Container: container,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func podOwners(pods []corev1.Pod) map[string]string {
|
||||||
|
out := make(map[string]string, len(pods))
|
||||||
|
for _, p := range pods {
|
||||||
|
out[p.Namespace+"/"+p.Name] = OwnerWorkloadID(p)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// attachVulns fills Node.Vulns. A CVE counts once per card, whatever the
|
||||||
|
// number of packages and containers it is found in.
|
||||||
|
func attachVulns(g *Graph, reports []*unstructured.Unstructured, pods []corev1.Pod) {
|
||||||
|
if len(reports) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
owners := podOwners(pods)
|
||||||
|
seen := map[string]map[string]bool{}
|
||||||
|
sums := map[string]*VulnSummary{}
|
||||||
|
for _, r := range reports {
|
||||||
|
card := vulnTargetID(r, owners)
|
||||||
|
if card == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if seen[card] == nil {
|
||||||
|
seen[card], sums[card] = map[string]bool{}, &VulnSummary{}
|
||||||
|
}
|
||||||
|
for _, v := range reportVulns(r) {
|
||||||
|
if seen[card][v.ID] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seen[card][v.ID] = true
|
||||||
|
s := sums[card]
|
||||||
|
switch v.Severity {
|
||||||
|
case "CRITICAL":
|
||||||
|
s.Critical++
|
||||||
|
if v.Fixed != "" {
|
||||||
|
s.FixableCritical++
|
||||||
|
}
|
||||||
|
case "HIGH":
|
||||||
|
s.High++
|
||||||
|
default:
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if v.Fixed != "" {
|
||||||
|
s.Fixable++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i := range g.Nodes {
|
||||||
|
if s, ok := sums[g.Nodes[i].ID]; ok && (s.Critical > 0 || s.High > 0) {
|
||||||
|
g.Nodes[i].Vulns = s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var severityOrder = map[string]int{"CRITICAL": 0, "HIGH": 1, "MEDIUM": 2, "LOW": 3}
|
||||||
|
|
||||||
|
// VulnsFor lists the findings of one card, most severe first, fixable
|
||||||
|
// before unfixable, one entry per CVE and image with its packages joined.
|
||||||
|
func VulnsFor(reports []*unstructured.Unstructured, pods []corev1.Pod, cardID string, limit int) []Vuln {
|
||||||
|
owners := podOwners(pods)
|
||||||
|
type key struct{ id, image string }
|
||||||
|
at := map[key]int{}
|
||||||
|
pkgs := map[key]map[string]bool{}
|
||||||
|
out := []Vuln{}
|
||||||
|
for _, r := range reports {
|
||||||
|
if vulnTargetID(r, owners) != cardID {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, v := range reportVulns(r) {
|
||||||
|
k := key{v.ID, v.Image}
|
||||||
|
i, ok := at[k]
|
||||||
|
if !ok {
|
||||||
|
at[k], pkgs[k] = len(out), map[string]bool{v.Package: true}
|
||||||
|
out = append(out, v)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !pkgs[k][v.Package] {
|
||||||
|
pkgs[k][v.Package] = true
|
||||||
|
out[i].Package += ", " + v.Package
|
||||||
|
}
|
||||||
|
if out[i].Fixed == "" {
|
||||||
|
out[i].Fixed = v.Fixed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.SliceStable(out, func(i, j int) bool {
|
||||||
|
a, b := out[i], out[j]
|
||||||
|
if severityOrder[a.Severity] != severityOrder[b.Severity] {
|
||||||
|
return severityOrder[a.Severity] < severityOrder[b.Severity]
|
||||||
|
}
|
||||||
|
if (a.Fixed != "") != (b.Fixed != "") {
|
||||||
|
return a.Fixed != ""
|
||||||
|
}
|
||||||
|
if a.Score != b.Score {
|
||||||
|
return a.Score > b.Score
|
||||||
|
}
|
||||||
|
return a.ID < b.ID
|
||||||
|
})
|
||||||
|
if limit > 0 && len(out) > limit {
|
||||||
|
out = out[:limit]
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2026 The cluster-map Authors
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package topology
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
|
)
|
||||||
|
|
||||||
|
func vulnReport(ns, kind, name, container, repo string, vulns ...map[string]any) *unstructured.Unstructured {
|
||||||
|
list := make([]any, len(vulns))
|
||||||
|
for i, v := range vulns {
|
||||||
|
list[i] = v
|
||||||
|
}
|
||||||
|
u := &unstructured.Unstructured{Object: map[string]any{
|
||||||
|
"report": map[string]any{
|
||||||
|
"registry": map[string]any{"server": "ghcr.io"},
|
||||||
|
"artifact": map[string]any{"repository": repo, "tag": "1.0"},
|
||||||
|
"vulnerabilities": list,
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
u.SetNamespace(ns)
|
||||||
|
u.SetLabels(map[string]string{"trivy-operator.resource.kind": kind, "trivy-operator.resource.name": name, "trivy-operator.resource.namespace": ns, "trivy-operator.container.name": container})
|
||||||
|
return u
|
||||||
|
}
|
||||||
|
|
||||||
|
func cve(id, sev, pkg, fixed string, score float64) map[string]any {
|
||||||
|
return map[string]any{"vulnerabilityID": id, "severity": sev, "resource": pkg, "installedVersion": "1.0", "fixedVersion": fixed, "score": score}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestVulns(t *testing.T) {
|
||||||
|
g := &Graph{Nodes: []Node{{ID: "wl/shop/deployment/web"}, {ID: "wl/shop/cronjob/backup"}, {ID: "wl/shop/deployment/clean"}}}
|
||||||
|
reports := []*unstructured.Unstructured{
|
||||||
|
vulnReport("shop", "ReplicaSet", "web-5f", "app", "acme/web",
|
||||||
|
cve("CVE-1", "CRITICAL", "openssl", "3.0.2", 9.8), cve("CVE-1", "CRITICAL", "libssl3", "3.0.2", 9.8),
|
||||||
|
cve("CVE-2", "CRITICAL", "zlib", "", 9.1), cve("CVE-3", "HIGH", "curl", "8.1", 7.5)),
|
||||||
|
// the sidecar on the same base image repeats CVE-1: counted once
|
||||||
|
vulnReport("shop", "ReplicaSet", "web-5f", "sidecar", "acme/web-sidecar", cve("CVE-1", "CRITICAL", "openssl", "3.0.2", 9.8)),
|
||||||
|
vulnReport("shop", "Job", "backup-29100000", "dump", "library/alpine", cve("CVE-4", "HIGH", "busybox", "1.37", 7.0)),
|
||||||
|
}
|
||||||
|
attachVulns(g, reports, nil)
|
||||||
|
web := g.Nodes[0].Vulns
|
||||||
|
if web == nil || web.Critical != 2 || web.High != 1 || web.FixableCritical != 1 || web.Fixable != 2 {
|
||||||
|
t.Errorf("web summary: %+v", web)
|
||||||
|
}
|
||||||
|
if b := g.Nodes[1].Vulns; b == nil || b.High != 1 {
|
||||||
|
t.Errorf("a CronJob's job report attaches to the CronJob: %+v", b)
|
||||||
|
}
|
||||||
|
if g.Nodes[2].Vulns != nil {
|
||||||
|
t.Error("a card without reports carries no summary")
|
||||||
|
}
|
||||||
|
list := VulnsFor(reports, []corev1.Pod{{ObjectMeta: metav1.ObjectMeta{Name: "x"}}}, "wl/shop/deployment/web", 0)
|
||||||
|
if len(list) != 4 || list[0].ID != "CVE-1" || list[0].Package != "openssl, libssl3" || list[0].Image != "ghcr.io/acme/web:1.0" || list[2].ID != "CVE-2" || list[3].Severity != "HIGH" {
|
||||||
|
t.Errorf("order (critical fixable, critical fixable sidecar, critical unfixable, high): %+v", list)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -28,8 +28,10 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"hash/fnv"
|
"hash/fnv"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -48,8 +50,8 @@ import (
|
|||||||
// count for that edge stops growing for the rest of the minute.
|
// count for that edge stops growing for the rest of the minute.
|
||||||
const maxTuplesPerKey = 20000
|
const maxTuplesPerKey = 20000
|
||||||
|
|
||||||
// InfraFilters are applied at the agents, so the relay never serialises
|
// InfraFilters (a blacklist) are applied at the agents, so the relay never
|
||||||
// these flows. On a 12k-event sample from a real cluster they were ~74% of
|
// serialises these flows. On a 12k-event sample from a real cluster they were ~74% of
|
||||||
// non-reply events: host-originated traffic (kubelet probes, hostNetwork
|
// non-reply events: host-originated traffic (kubelet probes, hostNetwork
|
||||||
// pods) 43%, DNS 19%, remote-node 11%, cilium health 1%. None of them map
|
// pods) 43%, DNS 19%, remote-node 11%, cilium health 1%. None of them map
|
||||||
// to a workload card, and streaming them kept hubble-relay at ~400m CPU.
|
// to a workload card, and streaming them kept hubble-relay at ~400m CPU.
|
||||||
@@ -59,9 +61,29 @@ var InfraFilters = []*flowpb.FlowFilter{
|
|||||||
{SourceLabel: []string{"reserved:health"}},
|
{SourceLabel: []string{"reserved:health"}},
|
||||||
{DestinationLabel: []string{"reserved:health"}},
|
{DestinationLabel: []string{"reserved:health"}},
|
||||||
{DestinationPort: []string{"53"}},
|
{DestinationPort: []string{"53"}},
|
||||||
|
// Pod-to-pod across nodes is traced twice: to-overlay on the sender's
|
||||||
|
// node and to-endpoint on the receiver's. The second copy carries the
|
||||||
|
// same tuple, so the first is dropped when the receiver is a pod; to a
|
||||||
|
// hostNetwork pod or the API server, to-overlay is the only trace and
|
||||||
|
// stays. Measured on six nodes: ~20% of the events that reach the relay.
|
||||||
|
{
|
||||||
|
EventType: []*flowpb.EventTypeFilter{{Type: monitorTrace, MatchSubType: true, SubType: traceToOverlay}},
|
||||||
|
DestinationLabel: []string{"k8s:io.kubernetes.pod.namespace"},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var flowFields = []string{"time", "verdict", "IP", "l4", "source", "destination", "source_names", "destination_names", "node_name"}
|
// Cilium datapath codes (pkg/monitor/api): message type "trace" and its
|
||||||
|
// observation point "to-overlay", and the L7 access log type.
|
||||||
|
const (
|
||||||
|
monitorTrace = 4
|
||||||
|
traceToOverlay = 4
|
||||||
|
monitorAccessLog = 129
|
||||||
|
)
|
||||||
|
|
||||||
|
// flowFields: from L7 events only what the HTTP statistics use -- never
|
||||||
|
// headers, which carry cookies and Authorization.
|
||||||
|
var flowFields = []string{"time", "verdict", "IP", "l4", "source", "destination", "source_names", "destination_names", "node_name",
|
||||||
|
"l7.type", "l7.latency_ns", "l7.http.code", "l7.http.method", "l7.http.url"}
|
||||||
|
|
||||||
// Endpoint is one side of a traffic edge: a workload, a pod or an address.
|
// Endpoint is one side of a traffic edge: a workload, a pod or an address.
|
||||||
type Endpoint struct {
|
type Endpoint struct {
|
||||||
@@ -80,6 +102,9 @@ type Edge struct {
|
|||||||
New int `json:"new"` // of those, opened by TCP SYN inside the window
|
New int `json:"new"` // of those, opened by TCP SYN inside the window
|
||||||
Dropped int `json:"dropped"` // distinct 5-tuples with a DROPPED verdict
|
Dropped int `json:"dropped"` // distinct 5-tuples with a DROPPED verdict
|
||||||
Last string `json:"last"`
|
Last string `json:"last"`
|
||||||
|
// HTTP is set where Hubble sees L7 on this port (an L7 visibility
|
||||||
|
// policy sends it through Envoy).
|
||||||
|
HTTP *HTTPStats `json:"http,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Result is the /api/flows response.
|
// Result is the /api/flows response.
|
||||||
@@ -112,6 +137,7 @@ type bucketEdge struct {
|
|||||||
type bucket struct {
|
type bucket struct {
|
||||||
minute int64
|
minute int64
|
||||||
edges map[edgeKey]*bucketEdge
|
edges map[edgeKey]*bucketEdge
|
||||||
|
http map[edgeKey]*httpAgg // only where L7 visibility is on
|
||||||
}
|
}
|
||||||
|
|
||||||
// Collector keeps minute buckets of distinct connections for the
|
// Collector keeps minute buckets of distinct connections for the
|
||||||
@@ -212,7 +238,9 @@ func (c *Collector) loop(ctx context.Context) {
|
|||||||
// Backfill whatever the ring buffers still hold; tuples seen twice
|
// Backfill whatever the ring buffers still hold; tuples seen twice
|
||||||
// after a reconnect collapse in the per-minute sets.
|
// after a reconnect collapse in the per-minute sets.
|
||||||
Since: timestamppb.New(c.now().Add(-time.Minute)),
|
Since: timestamppb.New(c.now().Add(-time.Minute)),
|
||||||
Whitelist: []*flowpb.FlowFilter{{Reply: []bool{false}}},
|
// requests and connections without replies, plus every L7 event:
|
||||||
|
// the HTTP status and latency are in the RESPONSE, a reply
|
||||||
|
Whitelist: []*flowpb.FlowFilter{{Reply: []bool{false}}, {EventType: []*flowpb.EventTypeFilter{{Type: monitorAccessLog}}}},
|
||||||
}
|
}
|
||||||
if !c.cfg.CollectInfra {
|
if !c.cfg.CollectInfra {
|
||||||
req.Blacklist = InfraFilters
|
req.Blacklist = InfraFilters
|
||||||
@@ -274,12 +302,20 @@ func (c *Collector) setErr(err error) {
|
|||||||
|
|
||||||
// Add records one flow. Exported for tests and for replaying captures.
|
// Add records one flow. Exported for tests and for replaying captures.
|
||||||
func (c *Collector) Add(f *flowpb.Flow) {
|
func (c *Collector) Add(f *flowpb.Flow) {
|
||||||
|
if proxiedLeg(f) {
|
||||||
|
metrics.FlowsProcessed.Inc()
|
||||||
|
return
|
||||||
|
}
|
||||||
src := c.endpoint(f.GetSource(), f.GetSourceNames(), f.GetNodeName())
|
src := c.endpoint(f.GetSource(), f.GetSourceNames(), f.GetNodeName())
|
||||||
dst := c.endpoint(f.GetDestination(), f.GetDestinationNames(), f.GetNodeName())
|
dst := c.endpoint(f.GetDestination(), f.GetDestinationNames(), f.GetNodeName())
|
||||||
metrics.FlowsProcessed.Inc()
|
metrics.FlowsProcessed.Inc()
|
||||||
if src.ID == dst.ID {
|
if src.ID == dst.ID {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// Not f.GetL7() != nil: with the field mask asking for l7.* subfields,
|
||||||
|
// Hubble returns an empty L7 message on every flow, L4 included; only
|
||||||
|
// a set type says the event is an L7 one.
|
||||||
|
l7 := f.GetL7().GetType() != flowpb.L7FlowType_UNKNOWN_L7_TYPE
|
||||||
port, tuple, syn := l4Info(f)
|
port, tuple, syn := l4Info(f)
|
||||||
ts := c.now()
|
ts := c.now()
|
||||||
if t := f.GetTime(); t != nil {
|
if t := f.GetTime(); t != nil {
|
||||||
@@ -305,6 +341,11 @@ func (c *Collector) Add(f *flowpb.Flow) {
|
|||||||
}
|
}
|
||||||
c.endpoints[src.ID] = src
|
c.endpoints[src.ID] = src
|
||||||
c.endpoints[dst.ID] = dst
|
c.endpoints[dst.ID] = dst
|
||||||
|
if l7 {
|
||||||
|
// a RESPONSE goes server -> client; statistics are kept client -> server
|
||||||
|
c.addHTTP(b, f, dst, src, ts)
|
||||||
|
return
|
||||||
|
}
|
||||||
k := edgeKey{src.ID, dst.ID, port}
|
k := edgeKey{src.ID, dst.ID, port}
|
||||||
e := b.edges[k]
|
e := b.edges[k]
|
||||||
if e == nil {
|
if e == nil {
|
||||||
@@ -339,6 +380,9 @@ func (c *Collector) pruneEndpoints() {
|
|||||||
used[k.src], used[k.dst] = true, true
|
used[k.src], used[k.dst] = true, true
|
||||||
tuples += len(e.tuples)
|
tuples += len(e.tuples)
|
||||||
}
|
}
|
||||||
|
for k := range b.http {
|
||||||
|
used[k.src], used[k.dst] = true, true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for id := range c.endpoints {
|
for id := range c.endpoints {
|
||||||
if !used[id] {
|
if !used[id] {
|
||||||
@@ -366,17 +410,24 @@ func (c *Collector) Query(namespaces map[string]bool, window time.Duration) Resu
|
|||||||
|
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
res := Result{Mode: c.cfg.TrafficMode, WindowSec: int(window.Seconds()), Connected: c.connected, Error: c.lastErr}
|
// Empty lists, not nil: the frontend iterates them, and JSON null is not iterable.
|
||||||
|
res := Result{Mode: c.cfg.TrafficMode, WindowSec: int(window.Seconds()), Connected: c.connected, Error: c.lastErr, Endpoints: []Endpoint{}, Edges: []Edge{}}
|
||||||
if c.running {
|
if c.running {
|
||||||
res.CoveredSec = min(res.WindowSec, int(now.Sub(c.coveredFrom).Seconds()))
|
res.CoveredSec = min(res.WindowSec, int(now.Sub(c.coveredFrom).Seconds()))
|
||||||
}
|
}
|
||||||
touches := func(id string) bool { return namespaces[c.endpoints[id].Namespace] }
|
touches := func(id string) bool { return namespaces[c.endpoints[id].Namespace] }
|
||||||
|
|
||||||
merged := map[edgeKey]*bucketEdge{}
|
merged := map[edgeKey]*bucketEdge{}
|
||||||
|
httpAggs := map[edgeKey][]*httpAgg{}
|
||||||
for _, b := range c.buckets {
|
for _, b := range c.buckets {
|
||||||
if b == nil || b.minute < fromMinute {
|
if b == nil || b.minute < fromMinute {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
for k, a := range b.http {
|
||||||
|
if touches(k.src) || touches(k.dst) {
|
||||||
|
httpAggs[k] = append(httpAggs[k], a)
|
||||||
|
}
|
||||||
|
}
|
||||||
for k, e := range b.edges {
|
for k, e := range b.edges {
|
||||||
if !touches(k.src) && !touches(k.dst) {
|
if !touches(k.src) && !touches(k.dst) {
|
||||||
continue
|
continue
|
||||||
@@ -403,9 +454,23 @@ func (c *Collector) Query(namespaces map[string]bool, window time.Duration) Resu
|
|||||||
e.Dropped++
|
e.Dropped++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if aggs, ok := httpAggs[k]; ok {
|
||||||
|
e.HTTP = httpStats(aggs)
|
||||||
|
delete(httpAggs, k)
|
||||||
|
}
|
||||||
res.Edges = append(res.Edges, e)
|
res.Edges = append(res.Edges, e)
|
||||||
eps[k.src], eps[k.dst] = true, true
|
eps[k.src], eps[k.dst] = true, true
|
||||||
}
|
}
|
||||||
|
// HTTP seen without a matching connection record (the proxy observed
|
||||||
|
// the requests, the L4 trace for that exact port did not reach us)
|
||||||
|
for k, aggs := range httpAggs {
|
||||||
|
last := int64(0)
|
||||||
|
for _, a := range aggs {
|
||||||
|
last = max(last, a.last)
|
||||||
|
}
|
||||||
|
res.Edges = append(res.Edges, Edge{Source: k.src, Target: k.dst, Port: k.port, Last: time.Unix(last, 0).UTC().Format(time.RFC3339), HTTP: httpStats(aggs)})
|
||||||
|
eps[k.src], eps[k.dst] = true, true
|
||||||
|
}
|
||||||
for id := range eps {
|
for id := range eps {
|
||||||
res.Endpoints = append(res.Endpoints, c.endpoints[id])
|
res.Endpoints = append(res.Endpoints, c.endpoints[id])
|
||||||
}
|
}
|
||||||
@@ -450,3 +515,101 @@ func l4Info(f *flowpb.Flow) (port, tuple string, syn bool) {
|
|||||||
}
|
}
|
||||||
return "?", "?", false
|
return "?", "?", false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ephemeralPorts is where Linux picks client ports (ip_local_port_range);
|
||||||
|
// NodePorts (30000-32767) stay below it.
|
||||||
|
const ephemeralPorts = 32768
|
||||||
|
|
||||||
|
// proxiedLeg: with L7 visibility Envoy re-opens each connection to the pod
|
||||||
|
// from the node, and both halves of that inner connection reach us as
|
||||||
|
// non-replies:
|
||||||
|
// - Envoy -> pod carries the client's security identity (its labels)
|
||||||
|
// but no pod name, so it cannot be attributed and would show as
|
||||||
|
// "world" (a pod that reached itself through its own Service looks
|
||||||
|
// the same: Cilium rewrites the source to 169.254.42.1);
|
||||||
|
// - the pod's answers to Envoy go to the node's host on an ephemeral
|
||||||
|
// port, and would show as one "pod -> node" edge per request.
|
||||||
|
//
|
||||||
|
// The connection itself is already counted on the client -> proxy leg.
|
||||||
|
// A pod that dials a host does so on a service port, never an ephemeral
|
||||||
|
// one (checked on the live cluster: API server 6443, kubelet 10250,
|
||||||
|
// exporters 42xx, Hubble 4244 all stay).
|
||||||
|
func proxiedLeg(f *flowpb.Flow) bool {
|
||||||
|
if f.GetL7().GetType() != flowpb.L7FlowType_UNKNOWN_L7_TYPE {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
s, d := f.GetSource(), f.GetDestination()
|
||||||
|
if s.GetPodName() == "" {
|
||||||
|
for _, l := range s.GetLabels() {
|
||||||
|
if strings.HasPrefix(l, "k8s:io.kubernetes.pod.namespace=") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if s.GetPodName() != "" && slices.Contains(d.GetLabels(), "reserved:host") {
|
||||||
|
if t := f.GetL4().GetTCP(); t != nil && t.GetDestinationPort() >= ephemeralPorts && t.GetSourcePort() < ephemeralPorts {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restricted stands for every peer in a namespace the viewer cannot see:
|
||||||
|
// the connection is shown, the workload behind it is not.
|
||||||
|
var Restricted = Endpoint{ID: "ext/restricted", Label: "restricted", Kind: "external"}
|
||||||
|
|
||||||
|
// Restrict hides peers outside the visible namespaces. Edges between two
|
||||||
|
// hidden endpoints are dropped (Query already returns only edges touching
|
||||||
|
// a requested namespace, so this is defence in depth); edges to a hidden
|
||||||
|
// peer keep their counts with the peer replaced by Restricted and merged.
|
||||||
|
func Restrict(res Result, visible func(string) bool) Result {
|
||||||
|
hide := map[string]bool{}
|
||||||
|
eps := []Endpoint{}
|
||||||
|
for _, e := range res.Endpoints {
|
||||||
|
if e.Namespace != "" && !visible(e.Namespace) {
|
||||||
|
hide[e.ID] = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
eps = append(eps, e)
|
||||||
|
}
|
||||||
|
if len(hide) == 0 {
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
type key struct{ src, dst, port string }
|
||||||
|
merged := map[key]*Edge{}
|
||||||
|
var order []key
|
||||||
|
for _, e := range res.Edges {
|
||||||
|
if hide[e.Source] && hide[e.Target] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if hide[e.Source] {
|
||||||
|
e.Source = Restricted.ID
|
||||||
|
}
|
||||||
|
if hide[e.Target] {
|
||||||
|
e.Target = Restricted.ID
|
||||||
|
}
|
||||||
|
k := key{e.Source, e.Target, e.Port}
|
||||||
|
if m := merged[k]; m != nil {
|
||||||
|
m.Connections += e.Connections
|
||||||
|
m.New += e.New
|
||||||
|
m.Dropped += e.Dropped
|
||||||
|
m.Last = max(m.Last, e.Last)
|
||||||
|
m.HTTP = mergeHTTP(m.HTTP, e.HTTP)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
c := e
|
||||||
|
merged[k] = &c
|
||||||
|
order = append(order, k)
|
||||||
|
}
|
||||||
|
res.Edges = make([]Edge, 0, len(order))
|
||||||
|
usedRestricted := false
|
||||||
|
for _, k := range order {
|
||||||
|
res.Edges = append(res.Edges, *merged[k])
|
||||||
|
usedRestricted = usedRestricted || k.src == Restricted.ID || k.dst == Restricted.ID
|
||||||
|
}
|
||||||
|
if usedRestricted {
|
||||||
|
eps = append(eps, Restricted)
|
||||||
|
}
|
||||||
|
res.Endpoints = eps
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|||||||
@@ -17,11 +17,15 @@ limitations under the License.
|
|||||||
package traffic
|
package traffic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
flowpb "github.com/cilium/cilium/api/v1/flow"
|
flowpb "github.com/cilium/cilium/api/v1/flow"
|
||||||
"google.golang.org/protobuf/types/known/timestamppb"
|
"google.golang.org/protobuf/types/known/timestamppb"
|
||||||
|
"google.golang.org/protobuf/types/known/wrapperspb"
|
||||||
|
|
||||||
"git.project-chimera.ru/fanchji/cluster-map/internal/config"
|
"git.project-chimera.ru/fanchji/cluster-map/internal/config"
|
||||||
)
|
)
|
||||||
@@ -163,3 +167,145 @@ func TestDisabledModeNeverStreams(t *testing.T) {
|
|||||||
t.Fatal("collector without a Hubble client must not start")
|
t.Fatal("collector without a Hubble client must not start")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// An empty window is sent as empty lists: the frontend iterates them, and a
|
||||||
|
// null there once left a namespace without traffic looking blank.
|
||||||
|
func TestEmptyWindowEncodesEmptyLists(t *testing.T) {
|
||||||
|
now := t0.Add(time.Minute)
|
||||||
|
c := newTestCollector(&now)
|
||||||
|
b, err := json.Marshal(c.Query(map[string]bool{"quiet": true}, 5*time.Minute))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, want := range []string{`"endpoints":[]`, `"edges":[]`} {
|
||||||
|
if !strings.Contains(string(b), want) {
|
||||||
|
t.Errorf("response %s lacks %s", b, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRestrictHidesForeignPeers(t *testing.T) {
|
||||||
|
res := Result{
|
||||||
|
Endpoints: []Endpoint{
|
||||||
|
{ID: "wl/shop/deployment/web", Namespace: "shop"}, {ID: "wl/bank/deployment/core", Namespace: "bank"},
|
||||||
|
{ID: "wl/bank/deployment/ledger", Namespace: "bank"}, {ID: "ext/world"},
|
||||||
|
},
|
||||||
|
Edges: []Edge{
|
||||||
|
{Source: "wl/shop/deployment/web", Target: "wl/bank/deployment/core", Port: "443/TCP", Connections: 2},
|
||||||
|
{Source: "wl/shop/deployment/web", Target: "wl/bank/deployment/ledger", Port: "443/TCP", Connections: 3},
|
||||||
|
{Source: "wl/bank/deployment/core", Target: "wl/bank/deployment/ledger", Port: "5432/TCP", Connections: 9},
|
||||||
|
{Source: "wl/shop/deployment/web", Target: "ext/world", Port: "443/TCP", Connections: 1},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
out := Restrict(res, func(ns string) bool { return ns == "shop" })
|
||||||
|
for _, e := range out.Endpoints {
|
||||||
|
if e.Namespace == "bank" {
|
||||||
|
t.Fatalf("hidden endpoint leaked: %+v", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(out.Edges) != 2 {
|
||||||
|
t.Fatalf("edges: %+v", out.Edges)
|
||||||
|
}
|
||||||
|
if e := out.Edges[0]; e.Target != Restricted.ID || e.Connections != 5 {
|
||||||
|
t.Errorf("two hidden peers on one port merge into one restricted edge: %+v", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPathTemplate(t *testing.T) {
|
||||||
|
for in, want := range map[string]string{
|
||||||
|
"http://web/": "/",
|
||||||
|
"http://web/api/42?token=secret": "/api/:id",
|
||||||
|
"/users/7f3a9c2e1b/avatar": "/users/:id/avatar",
|
||||||
|
"https://h/o/3fa85f64-5717-4562-b3fc-2c963f66afa6/x": "/o/:id/x",
|
||||||
|
"/static/app.js": "/static/app.js",
|
||||||
|
} {
|
||||||
|
if got := PathTemplate(in); got != want {
|
||||||
|
t.Errorf("%q: %q, want %q", in, got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func httpResponse(at time.Time, code uint32, url string, latency time.Duration) *flowpb.Flow {
|
||||||
|
return &flowpb.Flow{
|
||||||
|
Time: timestamppb.New(at),
|
||||||
|
Verdict: flowpb.Verdict_FORWARDED,
|
||||||
|
IP: &flowpb.IP{Source: "10.0.0.2", Destination: "10.0.0.1"},
|
||||||
|
L4: &flowpb.Layer4{Protocol: &flowpb.Layer4_TCP{TCP: &flowpb.TCP{SourcePort: 8080, DestinationPort: 40000}}},
|
||||||
|
Source: &flowpb.Endpoint{Namespace: "shop", PodName: "web"},
|
||||||
|
Destination: &flowpb.Endpoint{Namespace: "shop", PodName: "client"},
|
||||||
|
IsReply: &wrapperspb.BoolValue{Value: true},
|
||||||
|
L7: &flowpb.Layer7{Type: flowpb.L7FlowType_RESPONSE, LatencyNs: uint64(latency.Nanoseconds()), //nolint:gosec // test durations are small and positive
|
||||||
|
Record: &flowpb.Layer7_Http{Http: &flowpb.HTTP{Code: code, Method: "GET", Url: url}}},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHTTPStats(t *testing.T) {
|
||||||
|
now := t0.Add(2 * time.Minute)
|
||||||
|
c := newTestCollector(&now)
|
||||||
|
for i := 0; i < 18; i++ {
|
||||||
|
c.Add(httpResponse(t0, 200, "http://web/api/"+strconv.Itoa(i), 3*time.Millisecond))
|
||||||
|
}
|
||||||
|
c.Add(httpResponse(t0, 503, "http://web/api/1", 400*time.Millisecond))
|
||||||
|
c.Add(httpResponse(t0, 404, "http://web/missing", time.Millisecond))
|
||||||
|
// a REQUEST event carries no status and is not counted
|
||||||
|
req := httpResponse(t0, 0, "http://web/", 0)
|
||||||
|
req.L7.Type = flowpb.L7FlowType_REQUEST
|
||||||
|
c.Add(req)
|
||||||
|
|
||||||
|
res := c.Query(map[string]bool{"shop": true}, 5*time.Minute)
|
||||||
|
if len(res.Edges) != 1 {
|
||||||
|
t.Fatalf("edges: %+v", res.Edges)
|
||||||
|
}
|
||||||
|
e := res.Edges[0]
|
||||||
|
if e.Source != "wl/shop/deployment/client" || e.Target != "wl/shop/deployment/web" || e.Port != "8080/TCP" {
|
||||||
|
t.Errorf("a response must be recorded in the request's direction: %+v", e)
|
||||||
|
}
|
||||||
|
h := e.HTTP
|
||||||
|
if h == nil || h.Requests != 20 || h.Status["2xx"] != 18 || h.Status["5xx"] != 1 || h.Status["4xx"] != 1 {
|
||||||
|
t.Fatalf("http: %+v", h)
|
||||||
|
}
|
||||||
|
if h.P50Ms != 5 || h.P95Ms != 5 {
|
||||||
|
t.Errorf("p50/p95: %v/%v", h.P50Ms, h.P95Ms)
|
||||||
|
}
|
||||||
|
// 19 requests on the path: the 95th percentile is the 19th, the slow one
|
||||||
|
if top := h.Paths[0]; top.Path != "/api/:id" || top.Count != 19 || top.Errors != 1 || top.P95Ms != 500 {
|
||||||
|
t.Errorf("top path: %+v", top)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// With the field mask, Hubble sends an empty L7 message on L4 flows too;
|
||||||
|
// those must still count as connections (caught live: every edge in the
|
||||||
|
// cluster lost its connection count).
|
||||||
|
func TestMaskedL4FlowWithEmptyL7Counts(t *testing.T) {
|
||||||
|
now := t0.Add(time.Minute)
|
||||||
|
c := newTestCollector(&now)
|
||||||
|
f := tcpFlow(t0, "app", "web", "db", "pg", 40000, true, flowpb.Verdict_FORWARDED)
|
||||||
|
f.L7 = &flowpb.Layer7{}
|
||||||
|
c.Add(f)
|
||||||
|
res := c.Query(map[string]bool{"app": true}, 5*time.Minute)
|
||||||
|
if len(res.Edges) != 1 || res.Edges[0].Connections != 1 || res.Edges[0].HTTP != nil {
|
||||||
|
t.Fatalf("edges: %+v", res.Edges)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProxiedLegIsSkipped(t *testing.T) {
|
||||||
|
now := t0.Add(time.Minute)
|
||||||
|
c := newTestCollector(&now)
|
||||||
|
leg := tcpFlow(t0, "", "", "shop", "web", 40000, true, flowpb.Verdict_FORWARDED)
|
||||||
|
leg.Source = &flowpb.Endpoint{Labels: []string{"k8s:app=client", "k8s:io.kubernetes.pod.namespace=shop"}}
|
||||||
|
c.Add(leg)
|
||||||
|
// the pod answering Envoy: to the node, on an ephemeral port
|
||||||
|
answer := tcpFlow(t0, "shop", "web", "", "", 8080, false, flowpb.Verdict_FORWARDED)
|
||||||
|
answer.Destination = &flowpb.Endpoint{Labels: []string{"reserved:host"}}
|
||||||
|
answer.L4 = &flowpb.Layer4{Protocol: &flowpb.Layer4_TCP{TCP: &flowpb.TCP{SourcePort: 8080, DestinationPort: 49726}}}
|
||||||
|
c.Add(answer)
|
||||||
|
// a pod dialling the API server on the host: a real connection
|
||||||
|
api := tcpFlow(t0, "shop", "web", "", "", 40001, true, flowpb.Verdict_FORWARDED)
|
||||||
|
api.Destination = &flowpb.Endpoint{Labels: []string{"reserved:host"}}
|
||||||
|
api.L4 = &flowpb.Layer4{Protocol: &flowpb.Layer4_TCP{TCP: &flowpb.TCP{SourcePort: 40001, DestinationPort: 6443, Flags: &flowpb.TCPFlags{SYN: true}}}}
|
||||||
|
c.Add(api)
|
||||||
|
res := c.Query(map[string]bool{"shop": true}, 5*time.Minute)
|
||||||
|
if len(res.Edges) != 1 || res.Edges[0].Port != "6443/TCP" {
|
||||||
|
t.Errorf("want only the pod -> API server edge, got %+v", res.Edges)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,259 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2026 The cluster-map Authors
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package traffic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/url"
|
||||||
|
"regexp"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
flowpb "github.com/cilium/cilium/api/v1/flow"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HTTP statistics come from Hubble's L7 events, which exist only where a
|
||||||
|
// CiliumNetworkPolicy sends a port through Envoy (see docs: "HTTP
|
||||||
|
// details"). Only RESPONSE events are read: they carry the method, URL,
|
||||||
|
// status and latency together. Headers are never stored (and not even
|
||||||
|
// requested, see flowFields); the query string is cut off the URL, since
|
||||||
|
// tokens travel there too.
|
||||||
|
|
||||||
|
// latencyBoundsMs are the histogram buckets; percentiles are reported as
|
||||||
|
// the upper bound of the bucket they fall in.
|
||||||
|
var latencyBoundsMs = []float64{1, 2, 5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000}
|
||||||
|
|
||||||
|
// maxPathsPerEdge bounds memory per edge per minute; beyond it requests
|
||||||
|
// still count, the path breakdown just stops growing.
|
||||||
|
const maxPathsPerEdge = 64
|
||||||
|
|
||||||
|
type hist [14]int // len(latencyBoundsMs)+1
|
||||||
|
|
||||||
|
func (h *hist) add(ms float64) {
|
||||||
|
i := sort.SearchFloat64s(latencyBoundsMs, ms)
|
||||||
|
h[i]++
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *hist) merge(o *hist) {
|
||||||
|
for i := range h {
|
||||||
|
h[i] += o[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// quantile returns the upper bound (ms) of the bucket holding the q-th
|
||||||
|
// request; for the overflow bucket, the largest bound.
|
||||||
|
func (h *hist) quantile(q float64) float64 {
|
||||||
|
total := 0
|
||||||
|
for _, n := range h {
|
||||||
|
total += n
|
||||||
|
}
|
||||||
|
if total == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
target, seen := int(q*float64(total)+0.999999), 0
|
||||||
|
for i, n := range h {
|
||||||
|
seen += n
|
||||||
|
if seen >= target {
|
||||||
|
if i < len(latencyBoundsMs) {
|
||||||
|
return latencyBoundsMs[i]
|
||||||
|
}
|
||||||
|
return latencyBoundsMs[len(latencyBoundsMs)-1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return latencyBoundsMs[len(latencyBoundsMs)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
type pathKey struct{ method, path string }
|
||||||
|
|
||||||
|
type pathAgg struct {
|
||||||
|
count, errors int
|
||||||
|
lat hist
|
||||||
|
}
|
||||||
|
|
||||||
|
type httpAgg struct {
|
||||||
|
status [6]int // index = first digit of the code, 0 for anything else
|
||||||
|
lat hist
|
||||||
|
paths map[pathKey]*pathAgg
|
||||||
|
last int64
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTTPStats is the HTTP part of a traffic edge.
|
||||||
|
type HTTPStats struct {
|
||||||
|
Requests int `json:"requests"`
|
||||||
|
Status map[string]int `json:"status"` // "2xx", "3xx", "4xx", "5xx", "other"
|
||||||
|
P50Ms float64 `json:"p50Ms"`
|
||||||
|
P95Ms float64 `json:"p95Ms"`
|
||||||
|
Paths []HTTPPath `json:"paths"`
|
||||||
|
lat hist
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTTPPath is one method and path template on an edge.
|
||||||
|
type HTTPPath struct {
|
||||||
|
Method string `json:"method"`
|
||||||
|
Path string `json:"path"`
|
||||||
|
Count int `json:"count"`
|
||||||
|
Errors int `json:"errors"` // 5xx
|
||||||
|
P95Ms float64 `json:"p95Ms"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var idSegment = regexp.MustCompile(`^([0-9]+|[0-9a-fA-F]{8,}|[0-9a-fA-F]{8}-[0-9a-fA-F-]{27,}|.{41,})$`)
|
||||||
|
|
||||||
|
// PathTemplate reduces a URL to a low-cardinality path: no scheme, host
|
||||||
|
// or query, and segments that look like IDs (numbers, hex, UUIDs, long
|
||||||
|
// tokens) replaced by ":id".
|
||||||
|
func PathTemplate(raw string) string {
|
||||||
|
p := raw
|
||||||
|
if u, err := url.Parse(raw); err == nil {
|
||||||
|
p = u.Path
|
||||||
|
} else if i := strings.IndexAny(p, "?#"); i >= 0 {
|
||||||
|
p = p[:i]
|
||||||
|
}
|
||||||
|
if p == "" {
|
||||||
|
return "/"
|
||||||
|
}
|
||||||
|
segs := strings.Split(p, "/")
|
||||||
|
for i, s := range segs {
|
||||||
|
if s != "" && idSegment.MatchString(s) {
|
||||||
|
segs[i] = ":id"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out := strings.Join(segs, "/")
|
||||||
|
if len(out) > 100 {
|
||||||
|
out = out[:100] + "…"
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// addHTTP records one L7 RESPONSE event, turned around into the request's
|
||||||
|
// direction (client -> server). Called with c.mu held.
|
||||||
|
func (c *Collector) addHTTP(b *bucket, f *flowpb.Flow, client, server Endpoint, ts time.Time) {
|
||||||
|
h := f.GetL7().GetHttp()
|
||||||
|
if h == nil || f.GetL7().GetType() != flowpb.L7FlowType_RESPONSE {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
port := "?"
|
||||||
|
if t := f.GetL4().GetTCP(); t != nil {
|
||||||
|
port = strconv.FormatUint(uint64(t.GetSourcePort()), 10) + "/TCP" // the server's side of the response
|
||||||
|
}
|
||||||
|
k := edgeKey{client.ID, server.ID, port}
|
||||||
|
if b.http == nil {
|
||||||
|
b.http = map[edgeKey]*httpAgg{}
|
||||||
|
}
|
||||||
|
a := b.http[k]
|
||||||
|
if a == nil {
|
||||||
|
a = &httpAgg{paths: map[pathKey]*pathAgg{}}
|
||||||
|
b.http[k] = a
|
||||||
|
}
|
||||||
|
code := int(h.GetCode())
|
||||||
|
class := 0
|
||||||
|
if code >= 100 && code < 600 {
|
||||||
|
class = code / 100
|
||||||
|
}
|
||||||
|
a.status[class]++
|
||||||
|
ms := float64(f.GetL7().GetLatencyNs()) / 1e6
|
||||||
|
a.lat.add(ms)
|
||||||
|
a.last = max(a.last, ts.Unix())
|
||||||
|
pk := pathKey{h.GetMethod(), PathTemplate(h.GetUrl())}
|
||||||
|
pa := a.paths[pk]
|
||||||
|
if pa == nil {
|
||||||
|
if len(a.paths) >= maxPathsPerEdge {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
pa = &pathAgg{}
|
||||||
|
a.paths[pk] = pa
|
||||||
|
}
|
||||||
|
pa.count++
|
||||||
|
if class == 5 {
|
||||||
|
pa.errors++
|
||||||
|
}
|
||||||
|
pa.lat.add(ms)
|
||||||
|
}
|
||||||
|
|
||||||
|
// httpStats merges an edge's per-minute aggregates into its report.
|
||||||
|
func httpStats(aggs []*httpAgg) *HTTPStats {
|
||||||
|
var status [6]int
|
||||||
|
var lat hist
|
||||||
|
paths := map[pathKey]*pathAgg{}
|
||||||
|
for _, a := range aggs {
|
||||||
|
for i, n := range a.status {
|
||||||
|
status[i] += n
|
||||||
|
}
|
||||||
|
lat.merge(&a.lat)
|
||||||
|
for k, p := range a.paths {
|
||||||
|
m := paths[k]
|
||||||
|
if m == nil {
|
||||||
|
m = &pathAgg{}
|
||||||
|
paths[k] = m
|
||||||
|
}
|
||||||
|
m.count += p.count
|
||||||
|
m.errors += p.errors
|
||||||
|
m.lat.merge(&p.lat)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s := &HTTPStats{Status: map[string]int{}, lat: lat}
|
||||||
|
for i, n := range status {
|
||||||
|
if n == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
s.Requests += n
|
||||||
|
if i == 0 {
|
||||||
|
s.Status["other"] += n
|
||||||
|
} else {
|
||||||
|
s.Status[strconv.Itoa(i)+"xx"] = n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s.P50Ms, s.P95Ms = lat.quantile(0.5), lat.quantile(0.95)
|
||||||
|
for k, p := range paths {
|
||||||
|
s.Paths = append(s.Paths, HTTPPath{Method: k.method, Path: k.path, Count: p.count, Errors: p.errors, P95Ms: p.lat.quantile(0.95)})
|
||||||
|
}
|
||||||
|
sort.Slice(s.Paths, func(i, j int) bool {
|
||||||
|
if s.Paths[i].Count != s.Paths[j].Count {
|
||||||
|
return s.Paths[i].Count > s.Paths[j].Count
|
||||||
|
}
|
||||||
|
return s.Paths[i].Method+s.Paths[i].Path < s.Paths[j].Method+s.Paths[j].Path
|
||||||
|
})
|
||||||
|
if len(s.Paths) > 10 {
|
||||||
|
s.Paths = s.Paths[:10]
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// mergeHTTP folds b into a, for edges merged by Restrict.
|
||||||
|
func mergeHTTP(a, b *HTTPStats) *HTTPStats {
|
||||||
|
if a == nil {
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
if b == nil {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
out := &HTTPStats{Requests: a.Requests + b.Requests, Status: map[string]int{}, lat: a.lat}
|
||||||
|
out.lat.merge(&b.lat)
|
||||||
|
for k, n := range a.Status {
|
||||||
|
out.Status[k] += n
|
||||||
|
}
|
||||||
|
for k, n := range b.Status {
|
||||||
|
out.Status[k] += n
|
||||||
|
}
|
||||||
|
out.P50Ms, out.P95Ms = out.lat.quantile(0.5), out.lat.quantile(0.95)
|
||||||
|
out.Paths = append(append([]HTTPPath{}, a.Paths...), b.Paths...)
|
||||||
|
sort.Slice(out.Paths, func(i, j int) bool { return out.Paths[i].Count > out.Paths[j].Count })
|
||||||
|
if len(out.Paths) > 10 {
|
||||||
|
out.Paths = out.Paths[:10]
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
@@ -66,7 +66,9 @@ select, input[type="search"] {
|
|||||||
flex: none;
|
flex: none;
|
||||||
}
|
}
|
||||||
.brand { font-weight: 650; letter-spacing: 0.01em; }
|
.brand { font-weight: 650; letter-spacing: 0.01em; }
|
||||||
.crumbs { display: flex; flex-wrap: wrap; align-items: center; gap: 0.35rem; flex: 1; min-width: 0; }
|
.crumbs { display: flex; align-items: center; gap: 0.35rem; flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; }
|
||||||
|
.crumbs > * { flex: none; }
|
||||||
|
.crumbs > :last-child { flex: 0 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
|
||||||
.crumbs button {
|
.crumbs button {
|
||||||
background: none; border: none; color: var(--accent); font: inherit; cursor: pointer; padding: 0.1rem 0.2rem; border-radius: 4px;
|
background: none; border: none; color: var(--accent); font: inherit; cursor: pointer; padding: 0.1rem 0.2rem; border-radius: 4px;
|
||||||
}
|
}
|
||||||
@@ -147,6 +149,7 @@ select, input[type="search"] {
|
|||||||
.legend .line { width: 22px; height: 0; border-top: 2px solid var(--muted); }
|
.legend .line { width: 22px; height: 0; border-top: 2px solid var(--muted); }
|
||||||
.legend .line.traffic { border-top: 3px dashed var(--accent); }
|
.legend .line.traffic { border-top: 3px dashed var(--accent); }
|
||||||
.legend .line.drop { border-top: 3px dashed var(--error); }
|
.legend .line.drop { border-top: 3px dashed var(--error); }
|
||||||
|
.legend .line.http5xx { border-top: 3px dashed var(--warn); }
|
||||||
.legend .line.config { border-top: 1.5px solid var(--muted); }
|
.legend .line.config { border-top: 1.5px solid var(--muted); }
|
||||||
|
|
||||||
/* details panel */
|
/* details panel */
|
||||||
@@ -181,11 +184,12 @@ select, input[type="search"] {
|
|||||||
.section-title { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); margin: 1rem 0 0.4rem; }
|
.section-title { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); margin: 1rem 0 0.4rem; }
|
||||||
.link-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 0.2rem; }
|
.link-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 0.2rem; }
|
||||||
.link-list button {
|
.link-list button {
|
||||||
width: 100%; display: flex; align-items: center; gap: 0.5rem; text-align: left;
|
width: 100%; display: flex; flex-wrap: wrap; align-items: center; gap: 0.2rem 0.5rem; text-align: left;
|
||||||
background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px;
|
background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px;
|
||||||
padding: 0.35rem 0.5rem; color: var(--text); font: inherit; cursor: pointer;
|
padding: 0.35rem 0.5rem; color: var(--text); font: inherit; cursor: pointer;
|
||||||
}
|
}
|
||||||
.link-list button:hover { border-color: var(--accent); }
|
.link-list button:hover { border-color: var(--accent); }
|
||||||
|
.link-list button > span:nth-child(2) { flex: 1 1 10rem; min-width: 0; overflow-wrap: anywhere; }
|
||||||
.link-list .meta { margin-left: auto; color: var(--muted); font-size: 0.72rem; font-variant-numeric: tabular-nums; white-space: nowrap; }
|
.link-list .meta { margin-left: auto; color: var(--muted); font-size: 0.72rem; font-variant-numeric: tabular-nums; white-space: nowrap; }
|
||||||
.link-list .drop { color: var(--error); }
|
.link-list .drop { color: var(--error); }
|
||||||
.code {
|
.code {
|
||||||
@@ -197,6 +201,118 @@ select, input[type="search"] {
|
|||||||
.logs-bar { display: flex; gap: 0.4rem; margin-bottom: 0.5rem; flex-wrap: wrap; }
|
.logs-bar { display: flex; gap: 0.4rem; margin-bottom: 0.5rem; flex-wrap: wrap; }
|
||||||
.logs-bar select { max-width: 100%; }
|
.logs-bar select { max-width: 100%; }
|
||||||
|
|
||||||
|
/* problem badges */
|
||||||
|
.pbadge {
|
||||||
|
display: inline-grid; place-items: center; flex: none;
|
||||||
|
width: 15px; height: 15px; border-radius: 50%;
|
||||||
|
font-size: 0.65rem; font-weight: 800; line-height: 1; color: #fff;
|
||||||
|
}
|
||||||
|
.pbadge.warning { background: var(--warn); }
|
||||||
|
.pbadge.critical { background: var(--error); }
|
||||||
|
.pbadge.info { background: var(--idle); }
|
||||||
|
.tree .pbadge { width: 13px; height: 13px; font-size: 0.6rem; }
|
||||||
|
.legend .pbadge { display: inline-grid; width: 13px; height: 13px; font-size: 0.6rem; }
|
||||||
|
|
||||||
|
.problems-btn { display: inline-flex; align-items: center; gap: 0.4rem; }
|
||||||
|
.problems-btn.ok { color: var(--muted); }
|
||||||
|
.problems-btn.warning { border-color: var(--warn); }
|
||||||
|
.problems-btn.critical { border-color: var(--error); }
|
||||||
|
.whoami { max-width: 14rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||||
|
|
||||||
|
.summary { display: flex; align-items: center; gap: 0.45rem; margin: 0 0 0.8rem; font-weight: 600; }
|
||||||
|
.summary.ok { color: var(--ok); }
|
||||||
|
.summary.warning { color: var(--warn); }
|
||||||
|
.summary.critical { color: var(--error); }
|
||||||
|
.summary.idle { color: var(--muted); }
|
||||||
|
|
||||||
|
.problem-list { list-style: none; margin: 0 0 0.8rem; padding: 0; display: flex; flex-direction: column; gap: 0.25rem; }
|
||||||
|
.problem .p-row {
|
||||||
|
width: 100%; display: flex; align-items: flex-start; gap: 0.5rem; text-align: left;
|
||||||
|
background: var(--panel-2); border: 1px solid var(--line); border-left-width: 3px; border-radius: 6px;
|
||||||
|
padding: 0.4rem 0.5rem; color: var(--text); font: inherit;
|
||||||
|
}
|
||||||
|
button.p-row { cursor: pointer; }
|
||||||
|
button.p-row:hover { border-color: var(--accent); }
|
||||||
|
.problem.warning .p-row { border-left-color: var(--warn); }
|
||||||
|
.problem.critical .p-row { border-left-color: var(--error); }
|
||||||
|
.problem.info .p-row { border-left-color: var(--idle); }
|
||||||
|
.p-body { display: flex; flex-direction: column; gap: 0.1rem; min-width: 0; flex: 1; }
|
||||||
|
.p-target { display: inline-flex; align-items: center; gap: 0.3rem; font-weight: 600; }
|
||||||
|
.p-reason { overflow-wrap: anywhere; }
|
||||||
|
.p-src { margin-left: 0.4rem; font-size: 0.66rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); border: 1px solid var(--line); border-radius: 4px; padding: 0 0.25rem; }
|
||||||
|
.p-msg { font-size: 0.72rem; overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 3; line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
|
||||||
|
.problem .meta { margin-left: auto; white-space: nowrap; }
|
||||||
|
.p-info summary { cursor: pointer; color: var(--muted); margin: 0.6rem 0 0.4rem; }
|
||||||
|
.p-filter { margin: 0 0.9rem 0.3rem; }
|
||||||
|
|
||||||
|
/* actions */
|
||||||
|
.d-actions { display: flex; flex-wrap: wrap; gap: 0.35rem; padding: 0 0.9rem 0.6rem; }
|
||||||
|
.d-actions:empty { display: none; }
|
||||||
|
.btn.small { padding: 0.15rem 0.5rem; font-size: 0.75rem; }
|
||||||
|
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
|
||||||
|
.btn.primary.danger { background: var(--error); border-color: var(--error); }
|
||||||
|
#lock.unlocked { border-color: var(--warn); color: var(--warn); }
|
||||||
|
|
||||||
|
dialog {
|
||||||
|
background: var(--panel); color: var(--text);
|
||||||
|
border: 1px solid var(--line); border-radius: 10px; padding: 1rem 1.1rem;
|
||||||
|
width: min(28rem, calc(100vw - 2rem)); box-shadow: 0 10px 40px #0006;
|
||||||
|
}
|
||||||
|
dialog::backdrop { background: #0008; }
|
||||||
|
dialog h2 { margin: 0 0 0.5rem; font-size: 1rem; overflow-wrap: anywhere; }
|
||||||
|
dialog p { margin: 0 0 0.8rem; color: var(--muted); }
|
||||||
|
.confirm-input { display: flex; align-items: center; gap: 0.5rem; margin-bottom: 0.8rem; }
|
||||||
|
.confirm-input input { width: 6rem; background: var(--panel-2); color: var(--text); border: 1px solid var(--line); border-radius: 6px; padding: 0.3rem 0.5rem; font: inherit; }
|
||||||
|
.confirm-input[hidden] { display: none; }
|
||||||
|
.confirm-buttons { display: flex; justify-content: flex-end; gap: 0.5rem; }
|
||||||
|
|
||||||
|
.toast {
|
||||||
|
position: absolute; top: 2.6rem; left: 50%; transform: translateX(-50%); z-index: 5;
|
||||||
|
max-width: min(36rem, 90%); background: var(--panel); border: 1px solid var(--line); border-left: 3px solid var(--ok);
|
||||||
|
border-radius: 8px; padding: 0.45rem 0.8rem; box-shadow: 0 4px 16px #0004; overflow-wrap: anywhere;
|
||||||
|
}
|
||||||
|
.toast.error { border-left-color: var(--error); }
|
||||||
|
.toast[hidden] { display: none; }
|
||||||
|
|
||||||
|
/* load */
|
||||||
|
.usage-row { margin-bottom: 0.6rem; }
|
||||||
|
.usage-head { display: flex; align-items: baseline; gap: 0.5rem; flex-wrap: wrap; }
|
||||||
|
.usage-head span:first-child { color: var(--muted); min-width: 4rem; }
|
||||||
|
.usage-row meter { width: 100%; height: 0.55rem; }
|
||||||
|
table.pods { width: 100%; border-collapse: collapse; margin-top: 0.4rem; font-size: 0.75rem; }
|
||||||
|
table.pods th { text-align: left; color: var(--muted); font-weight: 500; padding: 0.2rem 0.3rem; border-bottom: 1px solid var(--line); }
|
||||||
|
table.pods td { padding: 0.3rem; border-bottom: 1px solid var(--line); vertical-align: top; overflow-wrap: anywhere; }
|
||||||
|
table.pods .warn-text { color: var(--warn); font-weight: 600; }
|
||||||
|
|
||||||
|
/* vulnerabilities */
|
||||||
|
.vuln-crit { color: var(--error); font-weight: 600; }
|
||||||
|
.sev-chip { display: inline-grid; place-items: center; width: 14px; height: 14px; border-radius: 3px; font-size: 0.6rem; font-weight: 800; color: #fff; }
|
||||||
|
.sev-chip.critical { background: var(--error); }
|
||||||
|
.sev-chip.high { background: var(--warn); }
|
||||||
|
table.vulns a { color: var(--accent); }
|
||||||
|
.shield-legend { display: inline-block; width: 10px; height: 12px; background: var(--error); clip-path: polygon(50% 0, 100% 15%, 100% 50%, 50% 100%, 0 50%, 0 15%); }
|
||||||
|
|
||||||
|
/* HTTP on traffic links */
|
||||||
|
.link-list .http-li { list-style: none; margin: -0.1rem 0 0.3rem 1.4rem; }
|
||||||
|
table.http-paths { margin-top: 0; font-size: 0.72rem; }
|
||||||
|
|
||||||
|
/* events */
|
||||||
|
.event-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 0.35rem; }
|
||||||
|
.event-list li { border-left: 3px solid var(--line); padding: 0.1rem 0 0.1rem 0.5rem; }
|
||||||
|
.event-list li.warning { border-left-color: var(--warn); }
|
||||||
|
.ev-head { display: flex; gap: 0.4rem; align-items: baseline; }
|
||||||
|
.ev-reason { font-weight: 600; }
|
||||||
|
.ev-time { margin-left: auto; white-space: nowrap; }
|
||||||
|
.ev-msg { overflow-wrap: anywhere; }
|
||||||
|
|
||||||
|
@media (max-width: 1280px) {
|
||||||
|
.whoami { display: none; }
|
||||||
|
}
|
||||||
|
@media (max-width: 1100px) {
|
||||||
|
#updated { display: none; }
|
||||||
|
.topbar { gap: 0.6rem; }
|
||||||
|
}
|
||||||
|
|
||||||
@media (prefers-reduced-motion: reduce) {
|
@media (prefers-reduced-motion: reduce) {
|
||||||
* { transition: none !important; animation: none !important; }
|
* { transition: none !important; animation: none !important; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -83,14 +83,18 @@ function fitText(text, font, max) {
|
|||||||
return text.slice(0, lo) + '…';
|
return text.slice(0, lo) + '…';
|
||||||
}
|
}
|
||||||
const imgCache = new Map();
|
const imgCache = new Map();
|
||||||
function cardImage({ kind, title, sub, health, ghost, w, h }) {
|
function cardImage({ kind, title, sub, health, ghost, w, h, badge, shield }) {
|
||||||
const key = [kind, title, sub, health, ghost, w, h].join('|');
|
const key = [kind, title, sub, health, ghost, w, h, badge, shield].join('|');
|
||||||
if (imgCache.has(key)) return imgCache.get(key);
|
if (imgCache.has(key)) return imgCache.get(key);
|
||||||
const c = (KIND[kind] || KIND.external).color;
|
const c = (KIND[kind] || KIND.external).color;
|
||||||
const name = fitText(title, `600 12.5px ${FONT}`, w - 58);
|
const name = fitText(title, `600 12.5px ${FONT}`, w - (badge ? 74 : 58));
|
||||||
const line2 = sub ? fitText(sub, `11px ${FONT}`, w - 50) : '';
|
const line2 = sub ? fitText(sub, `11px ${FONT}`, w - (shield ? 78 : 50)) : '';
|
||||||
const nameY = sub ? h / 2 - 3 : h / 2 + 4.5;
|
const nameY = sub ? h / 2 - 3 : h / 2 + 4.5;
|
||||||
const dot = health ? `<circle cx="${w - 12}" cy="12" r="4" fill="${T[health]}"/>` : '';
|
const dot = health ? `<circle cx="${w - 12}" cy="12" r="4" fill="${T[health]}"/>` : '';
|
||||||
|
// "!" in a circle left of the health dot: yellow warning, red critical
|
||||||
|
const bx = health ? w - 28 : w - 14;
|
||||||
|
const mark = badge ? `<circle cx="${bx}" cy="12" r="7" fill="${badge === 'critical' ? T.error : T.warn}"/>
|
||||||
|
<text x="${bx}" y="15.6" text-anchor="middle" font-family="${FONT}" font-size="10.5" font-weight="800" fill="#fff">!</text>` : '';
|
||||||
// Drawn at 2x so cards stay crisp up to the max zoom.
|
// Drawn at 2x so cards stay crisp up to the max zoom.
|
||||||
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${w * 2}" height="${h * 2}" viewBox="0 0 ${w} ${h}">
|
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${w * 2}" height="${h * 2}" viewBox="0 0 ${w} ${h}">
|
||||||
<defs><clipPath id="r"><rect x="0.5" y="0.5" width="${w - 1}" height="${h - 1}" rx="8"/></clipPath></defs>
|
<defs><clipPath id="r"><rect x="0.5" y="0.5" width="${w - 1}" height="${h - 1}" rx="8"/></clipPath></defs>
|
||||||
@@ -99,12 +103,22 @@ function cardImage({ kind, title, sub, health, ghost, w, h }) {
|
|||||||
<g transform="translate(13 ${h / 2 - 10})" fill="none" stroke="${c}" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round">${ICON[kind] || ''}</g>
|
<g transform="translate(13 ${h / 2 - 10})" fill="none" stroke="${c}" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round">${ICON[kind] || ''}</g>
|
||||||
<text x="41" y="${nameY}" font-family="${FONT}" font-size="12.5" font-weight="600" fill="${ghost ? T.muted : T.text}">${esc(name)}</text>
|
<text x="41" y="${nameY}" font-family="${FONT}" font-size="12.5" font-weight="600" fill="${ghost ? T.muted : T.text}">${esc(name)}</text>
|
||||||
${sub ? `<text x="41" y="${h / 2 + 12}" font-family="${FONT}" font-size="11" fill="${T.muted}">${esc(line2)}</text>` : ''}
|
${sub ? `<text x="41" y="${h / 2 + 12}" font-family="${FONT}" font-size="11" fill="${T.muted}">${esc(line2)}</text>` : ''}
|
||||||
${dot}</svg>`;
|
${dot}${mark}${shieldMark(shield, w, h)}</svg>`;
|
||||||
const url = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svg);
|
const url = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svg);
|
||||||
imgCache.set(key, url);
|
imgCache.set(key, url);
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Shield in the bottom-right corner: critical CVEs in the card's images
|
||||||
|
// (operator-trivy). Kept apart from the problem badge: nearly every image
|
||||||
|
// has some, and they would drown the "is it working" signal.
|
||||||
|
function shieldMark(n, w, h) {
|
||||||
|
if (!n) return '';
|
||||||
|
const x = w - 30, y = h - 17, label = n > 99 ? '99+' : String(n);
|
||||||
|
return `<g transform="translate(${x} ${y})"><path d="M5 0.5 L9.5 2 V5.5 C9.5 8.3 7.6 10.2 5 11.2 C2.4 10.2 0.5 8.3 0.5 5.5 V2 Z" fill="${T.error}" opacity="0.9"/></g>
|
||||||
|
<text x="${x + 12}" y="${y + 9.5}" font-family="${FONT}" font-size="10" font-weight="700" fill="${T.error}">${label}</text>`;
|
||||||
|
}
|
||||||
|
|
||||||
/* ---------- data ---------- */
|
/* ---------- data ---------- */
|
||||||
const G = {
|
const G = {
|
||||||
raw: null, byId: new Map(), byNs: new Map(), nsNodes: [], hostNodes: [],
|
raw: null, byId: new Map(), byNs: new Map(), nsNodes: [], hostNodes: [],
|
||||||
@@ -159,6 +173,42 @@ function nsHealth(ns) {
|
|||||||
}
|
}
|
||||||
return any ? worst : 'idle';
|
return any ? worst : 'idle';
|
||||||
}
|
}
|
||||||
|
/* ---------- problems ---------- */
|
||||||
|
// Info-level problems (a cordoned node, a one-off probe failure) are listed
|
||||||
|
// but never badge a card.
|
||||||
|
const SEV_RANK = { critical: 3, warning: 2, info: 1 };
|
||||||
|
const worse = (a, b) => ((SEV_RANK[b] || 0) > (SEV_RANK[a] || 0) ? b : a);
|
||||||
|
function badgeSev(problems) {
|
||||||
|
let w = '';
|
||||||
|
for (const p of problems || []) if (p.severity !== 'info') w = worse(w, p.severity);
|
||||||
|
return w;
|
||||||
|
}
|
||||||
|
const cardSev = (n) => badgeSev(n?.problems);
|
||||||
|
function nsSeverity(ns) {
|
||||||
|
let w = cardSev(G.byId.get('ns/' + ns));
|
||||||
|
for (const n of G.byNs.get(ns) || []) w = worse(w, cardSev(n));
|
||||||
|
return w;
|
||||||
|
}
|
||||||
|
function problemText(p) {
|
||||||
|
const k = 'problem.' + p.reason;
|
||||||
|
const s = t(k, { object: p.object || '', n: p.count || 0 });
|
||||||
|
return s === k ? p.reason : s;
|
||||||
|
}
|
||||||
|
function ago(iso) {
|
||||||
|
if (!iso) return '';
|
||||||
|
const sec = Math.max(0, (Date.now() - Date.parse(iso)) / 1000);
|
||||||
|
return t('ago', { d: fmtDuration(sec) });
|
||||||
|
}
|
||||||
|
// Every problem with the card it belongs to, for the Problems panel and the
|
||||||
|
// top-bar counter.
|
||||||
|
function allProblems() {
|
||||||
|
const out = [];
|
||||||
|
for (const p of G.raw?.clusterProblems || []) out.push({ p, n: null });
|
||||||
|
for (const n of G.raw?.nodes || []) for (const p of n.problems || []) out.push({ p, n });
|
||||||
|
out.sort((a, b) => (SEV_RANK[b.p.severity] - SEV_RANK[a.p.severity]) || String(b.p.since || '').localeCompare(String(a.p.since || '')));
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
function subline(n) {
|
function subline(n) {
|
||||||
const f = n.facts || {};
|
const f = n.facts || {};
|
||||||
if (f.foreign === 'true') return `${t('sub.ready', { ready: f.ready, want: f.pods })} · ${controllerKind(n)}`;
|
if (f.foreign === 'true') return `${t('sub.ready', { ready: f.ready, want: f.pods })} · ${controllerKind(n)}`;
|
||||||
@@ -207,7 +257,7 @@ function writeHash() {
|
|||||||
|
|
||||||
/* ---------- inventory tree ---------- */
|
/* ---------- inventory tree ---------- */
|
||||||
function objectItem(n) {
|
function objectItem(n) {
|
||||||
return { id: 't:obj:' + n.id, label: n.label, icon: n.kind, health: n.facts?.health, match: [n.label, n.facts?.image, n.facts?.hosts].join(' '), activate: () => selectObject(n.id) };
|
return { id: 't:obj:' + n.id, label: n.label, icon: n.kind, health: n.facts?.health, sev: cardSev(n), match: [n.label, n.facts?.image, n.facts?.hosts].join(' '), activate: () => selectObject(n.id) };
|
||||||
}
|
}
|
||||||
function nsKindFolders(ns, objs) {
|
function nsKindFolders(ns, objs) {
|
||||||
const folders = [];
|
const folders = [];
|
||||||
@@ -229,12 +279,12 @@ function buildTreeModel() {
|
|||||||
children: byNs.get(ns).sort((a, b) => a.label.localeCompare(b.label)).map(objectItem),
|
children: byNs.get(ns).sort((a, b) => a.label.localeCompare(b.label)).map(objectItem),
|
||||||
activate: () => scopeNode(h.label),
|
activate: () => scopeNode(h.label),
|
||||||
}));
|
}));
|
||||||
return { id: 't:node:' + h.label, label: h.label, icon: 'node', health: h.facts?.health, count: wls.length, children: nsItems, activate: () => scopeNode(h.label) };
|
return { id: 't:node:' + h.label, label: h.label, icon: 'node', health: h.facts?.health, sev: cardSev(h), count: wls.length, children: nsItems, activate: () => scopeNode(h.label) };
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
const nsItem = (n) => {
|
const nsItem = (n) => {
|
||||||
const objs = G.byNs.get(n.label) || [];
|
const objs = G.byNs.get(n.label) || [];
|
||||||
return { id: 't:ns:' + n.label, label: n.label, icon: 'namespace', health: nsHealth(n.label), count: objs.length, children: nsKindFolders(n.label, objs), activate: (add) => scopeNs(n.label, add), nsName: n.label };
|
return { id: 't:ns:' + n.label, label: n.label, icon: 'namespace', health: nsHealth(n.label), sev: nsSeverity(n.label), count: objs.length, children: nsKindFolders(n.label, objs), activate: (add) => scopeNs(n.label, add), nsName: n.label };
|
||||||
};
|
};
|
||||||
const user = G.nsNodes.filter((n) => n.facts?.system !== 'true').map(nsItem);
|
const user = G.nsNodes.filter((n) => n.facts?.system !== 'true').map(nsItem);
|
||||||
const sys = G.nsNodes.filter((n) => n.facts?.system === 'true').map(nsItem);
|
const sys = G.nsNodes.filter((n) => n.facts?.system === 'true').map(nsItem);
|
||||||
@@ -287,6 +337,7 @@ function renderTree() {
|
|||||||
it.health ? el('span', { class: 'dot ' + it.health, title: HEALTH_TEXT[it.health] }) : null,
|
it.health ? el('span', { class: 'dot ' + it.health, title: HEALTH_TEXT[it.health] }) : null,
|
||||||
el('span', { html: iconSvg(it.icon) }).firstChild,
|
el('span', { html: iconSvg(it.icon) }).firstChild,
|
||||||
el('span', { class: 'label' }, highlight(it.label, q)),
|
el('span', { class: 'label' }, highlight(it.label, q)),
|
||||||
|
it.sev ? el('span', { class: 'pbadge ' + it.sev, title: t('sev.' + it.sev), 'aria-label': t('sev.' + it.sev) }, '!') : null,
|
||||||
it.count !== undefined ? el('span', { class: 'count' }, String(it.count)) : null,
|
it.count !== undefined ? el('span', { class: 'count' }, String(it.count)) : null,
|
||||||
);
|
);
|
||||||
li.append(row);
|
li.append(row);
|
||||||
@@ -459,6 +510,8 @@ function mapStyle() {
|
|||||||
} },
|
} },
|
||||||
{ selector: ':parent.ghost', style: { 'border-style': 'dashed', 'background-opacity': 0.2 } },
|
{ selector: ':parent.ghost', style: { 'border-style': 'dashed', 'background-opacity': 0.2 } },
|
||||||
{ selector: ':parent.hostbox', style: { 'border-width': 1.5, 'border-color': KIND.node.color, 'background-opacity': 0.25, color: T.text } },
|
{ selector: ':parent.hostbox', style: { 'border-width': 1.5, 'border-color': KIND.node.color, 'background-opacity': 0.25, color: T.text } },
|
||||||
|
{ selector: ':parent.sev-warning', style: { 'border-width': 1.5, 'border-color': T.warn, color: T.warn } },
|
||||||
|
{ selector: ':parent.sev-critical', style: { 'border-width': 1.5, 'border-color': T.error, color: T.error } },
|
||||||
{ selector: ':parent.group', style: { 'background-opacity': 0, 'border-width': 0, 'font-size': 13, color: T.text } },
|
{ selector: ':parent.group', style: { 'background-opacity': 0, 'border-width': 0, 'font-size': 13, color: T.text } },
|
||||||
{ selector: 'node:childless:selected', style: { 'border-width': 2, 'border-color': T.accent, 'border-opacity': 1 } },
|
{ selector: 'node:childless:selected', style: { 'border-width': 2, 'border-color': T.accent, 'border-opacity': 1 } },
|
||||||
{ selector: 'edge', style: {
|
{ selector: 'edge', style: {
|
||||||
@@ -470,6 +523,7 @@ function mapStyle() {
|
|||||||
width: 'data(width)', opacity: 0.9,
|
width: 'data(width)', opacity: 0.9,
|
||||||
} },
|
} },
|
||||||
{ selector: 'edge.drop', style: { 'line-color': T.error, 'target-arrow-color': T.error } },
|
{ selector: 'edge.drop', style: { 'line-color': T.error, 'target-arrow-color': T.error } },
|
||||||
|
{ selector: 'edge.http5xx', style: { 'line-color': T.warn, 'target-arrow-color': T.warn } },
|
||||||
{ selector: 'edge.labelled, edge:selected', style: {
|
{ selector: 'edge.labelled, edge:selected', style: {
|
||||||
label: 'data(label)', 'font-size': 10, 'font-family': FONT, color: T.text, 'text-rotation': 'autorotate',
|
label: 'data(label)', 'font-size': 10, 'font-family': FONT, color: T.text, 'text-rotation': 'autorotate',
|
||||||
'text-background-color': T.panel, 'text-background-opacity': 0.92, 'text-background-padding': 2, 'text-background-shape': 'round-rectangle',
|
'text-background-color': T.panel, 'text-background-opacity': 0.92, 'text-background-padding': 2, 'text-background-shape': 'round-rectangle',
|
||||||
@@ -487,7 +541,7 @@ function cardNode(n, parent, ghost) {
|
|||||||
group: 'nodes', classes: ghost ? 'ghostcard' : '',
|
group: 'nodes', classes: ghost ? 'ghostcard' : '',
|
||||||
data: {
|
data: {
|
||||||
id: n.id, ref: n.id, parent, kind: n.kind, w: size.w, h: size.h,
|
id: n.id, ref: n.id, parent, kind: n.kind, w: size.w, h: size.h,
|
||||||
img: cardImage({ kind: n.kind, title: n.label, sub: ghost ? n.namespace + ' · ' + KIND[n.kind].title : subline(n), health: ghost ? null : n.facts?.health, ghost, w: size.w, h: size.h }),
|
img: cardImage({ kind: n.kind, title: n.label, sub: ghost ? n.namespace + ' · ' + KIND[n.kind].title : subline(n), health: ghost ? null : n.facts?.health, ghost, w: size.w, h: size.h, badge: cardSev(n), shield: ghost ? 0 : n.vulns?.critical }),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -509,7 +563,8 @@ function externalNode(ep) {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const nsBox = (ns, ghost, parent) => ({ group: 'nodes', classes: ghost ? 'ghost' : '', data: { id: 'box:' + ns, label: ns, kind: 'nsbox', ns, w: 1, h: 1, img: 'none', lmx: 0, parent } });
|
const sevClass = (sev) => (sev ? ' sev-' + sev : '');
|
||||||
|
const nsBox = (ns, ghost, parent) => ({ group: 'nodes', classes: ghost ? 'ghost' : sevClass(nsSeverity(ns)).trim(), data: { id: 'box:' + ns, label: ns, kind: 'nsbox', ns, w: 1, h: 1, img: 'none', lmx: 0, parent } });
|
||||||
|
|
||||||
function scopeNamespaces() {
|
function scopeNamespaces() {
|
||||||
if (state.scope.mode === 'ns') return state.scope.ns;
|
if (state.scope.mode === 'ns') return state.scope.ns;
|
||||||
@@ -528,15 +583,33 @@ function aggregateFlows(flows) {
|
|||||||
for (const e of flows.edges) {
|
for (const e of flows.edges) {
|
||||||
const key = e.source + '→' + e.target;
|
const key = e.source + '→' + e.target;
|
||||||
let p = pairs.get(key);
|
let p = pairs.get(key);
|
||||||
if (!p) { p = { source: e.source, target: e.target, count: 0, fresh: 0, dropped: 0, ports: [], last: '' }; pairs.set(key, p); }
|
if (!p) { p = { source: e.source, target: e.target, count: 0, fresh: 0, dropped: 0, ports: [], last: '', http: null }; pairs.set(key, p); }
|
||||||
p.count += e.connections;
|
p.count += e.connections;
|
||||||
p.fresh += e.new;
|
p.fresh += e.new;
|
||||||
p.dropped += e.dropped;
|
p.dropped += e.dropped;
|
||||||
if (!p.ports.includes(e.port)) p.ports.push(e.port);
|
if (!p.ports.includes(e.port)) p.ports.push(e.port);
|
||||||
if (e.last > p.last) p.last = e.last;
|
if (e.last > p.last) p.last = e.last;
|
||||||
|
if (e.http) p.http = mergeHttp(p.http, e.http);
|
||||||
}
|
}
|
||||||
return [...pairs.values()];
|
return [...pairs.values()];
|
||||||
}
|
}
|
||||||
|
// HTTP from several ports of one pair: counts add up, percentiles take the
|
||||||
|
// worse port (an upper bound, like the histogram buckets they come from).
|
||||||
|
function mergeHttp(a, b) {
|
||||||
|
if (!a) return { ...b, status: { ...b.status }, paths: [...(b.paths || [])] };
|
||||||
|
a.requests += b.requests;
|
||||||
|
for (const [k, n] of Object.entries(b.status || {})) a.status[k] = (a.status[k] || 0) + n;
|
||||||
|
a.p50Ms = Math.max(a.p50Ms, b.p50Ms);
|
||||||
|
a.p95Ms = Math.max(a.p95Ms, b.p95Ms);
|
||||||
|
a.paths = [...a.paths, ...(b.paths || [])].sort((x, y) => y.count - x.count).slice(0, 10);
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
const errShare = (h) => (h.requests ? (h.status['5xx'] || 0) / h.requests : 0);
|
||||||
|
function httpSummary(h) {
|
||||||
|
const err = h.status['5xx'] || 0;
|
||||||
|
return t('http.summary', { n: h.requests, p95: fmtMs(h.p95Ms) }) + (err ? t('http.errors', { pct: Math.max(1, Math.round(errShare(h) * 100)) }) : '');
|
||||||
|
}
|
||||||
|
const fmtMs = (ms) => (ms >= 1000 ? `≤${ms / 1000} s` : `≤${ms} ms`);
|
||||||
|
|
||||||
function buildElements() {
|
function buildElements() {
|
||||||
const els = [];
|
const els = [];
|
||||||
@@ -552,7 +625,7 @@ function buildElements() {
|
|||||||
} else {
|
} else {
|
||||||
const host = state.scope.node;
|
const host = state.scope.node;
|
||||||
members = [...(G.onNode.get(host) || [])].map((id) => G.byId.get(id)).filter(Boolean);
|
members = [...(G.onNode.get(host) || [])].map((id) => G.byId.get(id)).filter(Boolean);
|
||||||
add({ group: 'nodes', classes: 'hostbox', data: { id: 'host:' + host, label: t('node', { name: host }), kind: 'hostbox', w: 1, h: 1, img: 'none', lmx: 0 } });
|
add({ group: 'nodes', classes: 'hostbox' + sevClass(cardSev(G.byId.get('node/' + host))), data: { id: 'host:' + host, label: t('node', { name: host }), kind: 'hostbox', w: 1, h: 1, img: 'none', lmx: 0 } });
|
||||||
for (const ns of new Set(members.map((m) => m.namespace))) add(nsBox(ns, false, 'host:' + host));
|
for (const ns of new Set(members.map((m) => m.namespace))) add(nsBox(ns, false, 'host:' + host));
|
||||||
}
|
}
|
||||||
const memberIds = new Set(members.map((m) => m.id));
|
const memberIds = new Set(members.map((m) => m.id));
|
||||||
@@ -588,14 +661,14 @@ function buildElements() {
|
|||||||
|
|
||||||
function trafficEdgeElement(e) {
|
function trafficEdgeElement(e) {
|
||||||
return {
|
return {
|
||||||
group: 'edges', classes: 'traffic' + (e.dropped ? ' drop' : ''),
|
group: 'edges', classes: 'traffic' + (e.dropped ? ' drop' : '') + (e.http && errShare(e.http) > 0 ? ' http5xx' : ''),
|
||||||
data: { id: 'tr:' + e.source + '→' + e.target, source: e.source, target: e.target, ...trafficEdgeData(e) },
|
data: { id: 'tr:' + e.source + '→' + e.target, source: e.source, target: e.target, ...trafficEdgeData(e) },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
function trafficEdgeData(e) {
|
function trafficEdgeData(e) {
|
||||||
return {
|
return {
|
||||||
width: 1.6 + Math.min(4.5, Math.log10(e.count + 1) * 1.7),
|
width: 1.6 + Math.min(4.5, Math.log10(e.count + 1) * 1.7),
|
||||||
label: `${e.ports.slice(0, 3).join(', ')}${e.ports.length > 3 ? '…' : ''} · ${t('edge.conns', { n: e.count })}${e.dropped ? t('edge.drop', { n: e.dropped }) : ''}`,
|
label: `${e.ports.slice(0, 3).join(', ')}${e.ports.length > 3 ? '…' : ''} · ${e.http ? httpSummary(e.http) : t('edge.conns', { n: e.count })}${e.dropped ? t('edge.drop', { n: e.dropped }) : ''}`,
|
||||||
flow: e,
|
flow: e,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -636,6 +709,14 @@ async function refreshTraffic() {
|
|||||||
}
|
}
|
||||||
setInterval(refreshTraffic, 30000);
|
setInterval(refreshTraffic, 30000);
|
||||||
|
|
||||||
|
function nsCardImage(ns) {
|
||||||
|
const objs = G.byNs.get(ns) || [];
|
||||||
|
const wl = objs.filter(hasPods).length;
|
||||||
|
const ing = objs.filter((o) => o.kind === 'ingress').length;
|
||||||
|
const svc = objs.filter((o) => o.kind === 'service').length;
|
||||||
|
const sub = `${wl} workload · ${svc} svc · ${ing} ingress`;
|
||||||
|
return cardImage({ kind: 'namespace', title: ns, sub, health: nsHealth(ns), ghost: false, w: NSCARD.w, h: NSCARD.h, badge: nsSeverity(ns) });
|
||||||
|
}
|
||||||
function buildClusterElements() {
|
function buildClusterElements() {
|
||||||
const els = [];
|
const els = [];
|
||||||
const groups = [
|
const groups = [
|
||||||
@@ -649,15 +730,10 @@ function buildClusterElements() {
|
|||||||
const cols = Math.max(2, Math.min(8, Math.round(Math.sqrt(list.length * aspect * (NSCARD.h + gapY) / (NSCARD.w + gapX)) * 1.4)));
|
const cols = Math.max(2, Math.min(8, Math.round(Math.sqrt(list.length * aspect * (NSCARD.h + gapY) / (NSCARD.w + gapX)) * 1.4)));
|
||||||
els.push({ group: 'nodes', classes: 'group', data: { id: gid, label, kind: 'group', w: 1, h: 1, img: 'none', lmx: 0 } });
|
els.push({ group: 'nodes', classes: 'group', data: { id: gid, label, kind: 'group', w: 1, h: 1, img: 'none', lmx: 0 } });
|
||||||
list.forEach((n, i) => {
|
list.forEach((n, i) => {
|
||||||
const objs = G.byNs.get(n.label) || [];
|
|
||||||
const wl = objs.filter(hasPods).length;
|
|
||||||
const ing = objs.filter((o) => o.kind === 'ingress').length;
|
|
||||||
const svc = objs.filter((o) => o.kind === 'service').length;
|
|
||||||
const sub = `${wl} workload · ${svc} svc · ${ing} ingress`;
|
|
||||||
els.push({
|
els.push({
|
||||||
group: 'nodes', position: { x: (i % cols) * (NSCARD.w + gapX), y: y0 + Math.floor(i / cols) * (NSCARD.h + gapY) },
|
group: 'nodes', position: { x: (i % cols) * (NSCARD.w + gapX), y: y0 + Math.floor(i / cols) * (NSCARD.h + gapY) },
|
||||||
data: { id: 'nscard:' + n.label, ns: n.label, parent: gid, kind: 'nscard', w: NSCARD.w, h: NSCARD.h,
|
data: { id: 'nscard:' + n.label, ns: n.label, parent: gid, kind: 'nscard', w: NSCARD.w, h: NSCARD.h,
|
||||||
img: cardImage({ kind: 'namespace', title: n.label, sub, health: nsHealth(n.label), ghost: false, w: NSCARD.w, h: NSCARD.h }) },
|
img: nsCardImage(n.label) },
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
y0 += Math.ceil(list.length / cols) * (NSCARD.h + gapY) + 90;
|
y0 += Math.ceil(list.length / cols) * (NSCARD.h + gapY) + 90;
|
||||||
@@ -700,6 +776,14 @@ async function layoutLevel(container, edges, aspect) {
|
|||||||
const s = owner.get(e.source().id()), t = owner.get(e.target().id());
|
const s = owner.get(e.source().id()), t = owner.get(e.target().id());
|
||||||
if (s && t && s !== t) lifted.set(s + '>' + t, { id: 'l' + lifted.size, sources: [s], targets: [t] });
|
if (s && t && s !== t) lifted.set(s + '>' + t, { id: 'l' + lifted.size, sources: [s], targets: [t] });
|
||||||
});
|
});
|
||||||
|
// Cards of one size and no edges between them: nothing for ELK to rank,
|
||||||
|
// and each ELK call costs ~8 ms even for one card, which on a node view
|
||||||
|
// with 40 small namespaces was most of the layout time. packGrid gives
|
||||||
|
// the grid ELK would. Boxes of mixed sizes still go to ELK, whose
|
||||||
|
// packing of those is tighter than a grid.
|
||||||
|
if (!lifted.size && blocks.every((b) => !b.inner && b.w === blocks[0].w && b.h === blocks[0].h)) {
|
||||||
|
return packGrid(blocks, aspect, container ? 22 : 48);
|
||||||
|
}
|
||||||
const graph = {
|
const graph = {
|
||||||
id: 'root',
|
id: 'root',
|
||||||
layoutOptions: {
|
layoutOptions: {
|
||||||
@@ -722,6 +806,21 @@ async function layoutLevel(container, edges, aspect) {
|
|||||||
for (const b of blocks) { const p = pos.get(b.node.id()); b.x = p.x; b.y = p.y; }
|
for (const b of blocks) { const p = pos.get(b.node.id()); b.x = p.x; b.y = p.y; }
|
||||||
return { blocks, w: out.width, h: out.height };
|
return { blocks, w: out.width, h: out.height };
|
||||||
}
|
}
|
||||||
|
// A row-major grid of equal blocks with the column count whose width to
|
||||||
|
// height is closest to the requested aspect ratio: what ELK's component
|
||||||
|
// packing produces for unconnected cards, measured on 2 to 20 of them.
|
||||||
|
function packGrid(blocks, aspect, gap) {
|
||||||
|
const { w, h } = blocks[0];
|
||||||
|
let best = 1, bestDiff = Infinity;
|
||||||
|
for (let cols = 1; cols <= blocks.length; cols++) {
|
||||||
|
const rows = Math.ceil(blocks.length / cols);
|
||||||
|
const diff = Math.abs((cols * (w + gap) - gap) / (rows * (h + gap) - gap) - aspect);
|
||||||
|
if (diff < bestDiff) { best = cols; bestDiff = diff; }
|
||||||
|
}
|
||||||
|
blocks.forEach((b, i) => { b.x = (i % best) * (w + gap); b.y = Math.floor(i / best) * (h + gap); });
|
||||||
|
const rows = Math.ceil(blocks.length / best);
|
||||||
|
return { blocks, w: best * (w + gap) - gap, h: rows * (h + gap) - gap };
|
||||||
|
}
|
||||||
function place(plan, ox, oy) {
|
function place(plan, ox, oy) {
|
||||||
for (const b of plan.blocks) {
|
for (const b of plan.blocks) {
|
||||||
const x = ox + b.x, y = oy + b.y;
|
const x = ox + b.x, y = oy + b.y;
|
||||||
@@ -760,7 +859,20 @@ function focusOnMap(id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let renderToken = 0;
|
let renderToken = 0;
|
||||||
|
// A failed draw must say so: otherwise the previous scope's map stays on
|
||||||
|
// screen under the new breadcrumbs, which reads as "this namespace is empty".
|
||||||
async function renderMap() {
|
async function renderMap() {
|
||||||
|
const token = renderToken + 1;
|
||||||
|
try {
|
||||||
|
await drawMap();
|
||||||
|
} catch (err) {
|
||||||
|
if (token !== renderToken) return;
|
||||||
|
console.error(err);
|
||||||
|
cy.elements().remove();
|
||||||
|
setStatus(t('status.renderFailed', { error: err.message }), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
async function drawMap() {
|
||||||
const token = ++renderToken;
|
const token = ++renderToken;
|
||||||
renderTree();
|
renderTree();
|
||||||
renderCrumbs();
|
renderCrumbs();
|
||||||
@@ -830,7 +942,11 @@ async function fetchFlows(namespaces) {
|
|||||||
const p = new URLSearchParams({ namespaces: namespaces.join(','), window: state.trafficWindow });
|
const p = new URLSearchParams({ namespaces: namespaces.join(','), window: state.trafficWindow });
|
||||||
const r = await fetch('/api/flows?' + p);
|
const r = await fetch('/api/flows?' + p);
|
||||||
if (!r.ok) throw new Error((await r.text()).trim() || 'HTTP ' + r.status);
|
if (!r.ok) throw new Error((await r.text()).trim() || 'HTTP ' + r.status);
|
||||||
return r.json();
|
const flows = await r.json();
|
||||||
|
// the server sends [] for an empty window since v0.7.2; up to v0.7.1 it sent null
|
||||||
|
flows.edges ??= [];
|
||||||
|
flows.endpoints ??= [];
|
||||||
|
return flows;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* hover: highlight a card's neighbourhood, like NSX "logical path" */
|
/* hover: highlight a card's neighbourhood, like NSX "logical path" */
|
||||||
@@ -859,6 +975,7 @@ cy.on('tap', 'node', (e) => {
|
|||||||
if (d.kind === 'nscard') { scopeNs(d.ns); return; }
|
if (d.kind === 'nscard') { scopeNs(d.ns); return; }
|
||||||
if (e.target.isParent()) {
|
if (e.target.isParent()) {
|
||||||
if (d.kind === 'nsbox' && !(state.scope.mode === 'ns' && state.scope.ns.includes(d.ns))) scopeNs(d.ns);
|
if (d.kind === 'nsbox' && !(state.scope.mode === 'ns' && state.scope.ns.includes(d.ns))) scopeNs(d.ns);
|
||||||
|
if (d.kind === 'hostbox') openDetails('node/' + d.id.slice('host:'.length));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (G.byId.has(d.ref)) {
|
if (G.byId.has(d.ref)) {
|
||||||
@@ -927,6 +1044,7 @@ function renderLegend() {
|
|||||||
l.replaceChildren(
|
l.replaceChildren(
|
||||||
el('span', {}, t('legend.cluster')),
|
el('span', {}, t('legend.cluster')),
|
||||||
...['ok', 'warn', 'error', 'idle'].map((h) => el('span', {}, el('span', { class: 'dot ' + h }), HEALTH_TEXT[h])),
|
...['ok', 'warn', 'error', 'idle'].map((h) => el('span', {}, el('span', { class: 'dot ' + h }), HEALTH_TEXT[h])),
|
||||||
|
...badgeLegend(),
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -934,10 +1052,17 @@ function renderLegend() {
|
|||||||
el('span', {}, el('span', { class: 'line config' }), t('legend.config')),
|
el('span', {}, el('span', { class: 'line config' }), t('legend.config')),
|
||||||
el('span', {}, el('span', { class: 'line traffic' }), t('legend.traffic')),
|
el('span', {}, el('span', { class: 'line traffic' }), t('legend.traffic')),
|
||||||
el('span', {}, el('span', { class: 'line drop' }), t('legend.drop')),
|
el('span', {}, el('span', { class: 'line drop' }), t('legend.drop')),
|
||||||
|
el('span', {}, el('span', { class: 'line http5xx' }), t('legend.http5xx')),
|
||||||
el('span', {}, t('legend.ghost')),
|
el('span', {}, t('legend.ghost')),
|
||||||
el('span', {}, t('legend.notCollected')),
|
el('span', {}, t('legend.notCollected')),
|
||||||
|
...badgeLegend(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
function badgeLegend() {
|
||||||
|
const items = ['warning', 'critical'].map((sev) => el('span', {}, el('span', { class: 'pbadge ' + sev, 'aria-hidden': 'true' }, '!'), t('legend.' + sev)));
|
||||||
|
if (G.raw?.vulnsAvailable) items.push(el('span', {}, el('span', { class: 'shield-legend', 'aria-hidden': 'true' }), t('legend.vulns')));
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
/* ---------- details panel ---------- */
|
/* ---------- details panel ---------- */
|
||||||
const yamlCache = new Map();
|
const yamlCache = new Map();
|
||||||
@@ -951,10 +1076,11 @@ $('#d-close').addEventListener('click', () => { state.selected = null; cy.$(':se
|
|||||||
|
|
||||||
function setTab(name) {
|
function setTab(name) {
|
||||||
for (const t of document.querySelectorAll('.tabs [role="tab"]')) t.setAttribute('aria-selected', String(t.dataset.tab === name));
|
for (const t of document.querySelectorAll('.tabs [role="tab"]')) t.setAttribute('aria-selected', String(t.dataset.tab === name));
|
||||||
for (const p of document.querySelectorAll('.tab-panel')) p.hidden = p.dataset.panel !== name;
|
for (const p of $('#details').querySelectorAll('.tab-panel')) p.hidden = p.dataset.panel !== name;
|
||||||
const n = G.byId.get(detailsId);
|
const n = G.byId.get(detailsId);
|
||||||
if (name === 'yaml' && n) loadYaml(n);
|
if (name === 'yaml' && n) loadYaml(n);
|
||||||
if (name === 'logs' && n) loadPods(n);
|
if (name === 'logs' && n) loadPods(n);
|
||||||
|
if (name === 'events' && detailsId) loadEvents(detailsId);
|
||||||
}
|
}
|
||||||
for (const t of document.querySelectorAll('.tabs [role="tab"]')) t.addEventListener('click', () => setTab(t.dataset.tab));
|
for (const t of document.querySelectorAll('.tabs [role="tab"]')) t.addEventListener('click', () => setTab(t.dataset.tab));
|
||||||
|
|
||||||
@@ -991,28 +1117,15 @@ const FACT_LABELS = {
|
|||||||
function openDetails(id) {
|
function openDetails(id) {
|
||||||
const n = G.byId.get(id);
|
const n = G.byId.get(id);
|
||||||
if (!n) return;
|
if (!n) return;
|
||||||
|
closeProblems();
|
||||||
detailsId = id;
|
detailsId = id;
|
||||||
showPanelHead(n.kind, n.label, n.namespace ? t('details.namespace', { ns: n.namespace }) : '');
|
showPanelHead(n.kind, n.label, n.namespace ? t('details.namespace', { ns: n.namespace }) : '');
|
||||||
const foreign = n.facts?.foreign === 'true';
|
const foreign = n.facts?.foreign === 'true';
|
||||||
tabVisible('yaml', !foreign);
|
tabVisible('yaml', !foreign);
|
||||||
tabVisible('logs', hasPods(n));
|
tabVisible('logs', hasPods(n));
|
||||||
|
tabVisible('events', true);
|
||||||
const panel = document.querySelector('[data-panel="overview"]');
|
renderOverview(n);
|
||||||
const dl = el('dl', { class: 'facts' });
|
renderActions(n);
|
||||||
const h = n.facts?.health;
|
|
||||||
if (h) dl.append(el('dt', {}, t('details.state')), el('dd', {}, el('span', { class: 'status' }, el('span', { class: 'dot ' + h }), HEALTH_TEXT[h])));
|
|
||||||
for (const [k, v] of Object.entries(n.facts || {})) {
|
|
||||||
if (k === 'health' || k === 'system' || k === 'foreign' || v === '') continue;
|
|
||||||
dl.append(el('dt', {}, FACT_LABELS[k] || k), el('dd', {}, v));
|
|
||||||
}
|
|
||||||
const parts = [dl];
|
|
||||||
const hosts = [...(G.placement.get(id) || [])].sort();
|
|
||||||
if (hosts.length) {
|
|
||||||
parts.push(el('div', { class: 'section-title' }, t('details.runsOn')),
|
|
||||||
el('ul', { class: 'link-list' }, hosts.map((host) => el('li', {}, el('button', { type: 'button', onclick: () => scopeNode(host) },
|
|
||||||
el('span', { html: iconSvg('node') }).firstChild, host)))));
|
|
||||||
}
|
|
||||||
panel.replaceChildren(...parts);
|
|
||||||
renderLinks(id);
|
renderLinks(id);
|
||||||
$('#d-yaml').textContent = '';
|
$('#d-yaml').textContent = '';
|
||||||
$('#d-logs').textContent = '';
|
$('#d-logs').textContent = '';
|
||||||
@@ -1020,11 +1133,174 @@ function openDetails(id) {
|
|||||||
setTab(document.querySelector(`.tabs [data-tab="${active}"]`).hidden ? 'overview' : active);
|
setTab(document.querySelector(`.tabs [data-tab="${active}"]`).hidden ? 'overview' : active);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A sentence first, for whoever just wants to know whether it works; the
|
||||||
|
// facts, problems and load below it are for whoever has to fix it.
|
||||||
|
function summaryLine(n) {
|
||||||
|
const ps = (n.problems || []).filter((p) => p.severity !== 'info');
|
||||||
|
const crit = ps.filter((p) => p.severity === 'critical').length;
|
||||||
|
const sev = badgeSev(n.problems);
|
||||||
|
if (ps.length) {
|
||||||
|
return el('p', { class: 'summary ' + sev }, el('span', { class: 'pbadge ' + sev, 'aria-hidden': 'true' }, '!'),
|
||||||
|
crit ? t('summary.critical', { n: t('plural.problem', { n: ps.length }) }) : t('summary.warning', { n: t('plural.problem', { n: ps.length }) }));
|
||||||
|
}
|
||||||
|
const h = n.facts?.health;
|
||||||
|
if (h === 'idle') return el('p', { class: 'summary idle' }, t('summary.idle'));
|
||||||
|
if (h && h !== 'ok') return el('p', { class: 'summary warning' }, HEALTH_TEXT[h]);
|
||||||
|
return el('p', { class: 'summary ok' }, t('summary.ok'));
|
||||||
|
}
|
||||||
|
|
||||||
|
function problemItem(p, n, withTarget) {
|
||||||
|
const text = problemText(p);
|
||||||
|
const detail = [p.object && !text.includes(p.object) ? p.object : '', p.message].filter(Boolean).join(' — ');
|
||||||
|
const body = [
|
||||||
|
el('span', { class: 'pbadge ' + p.severity, 'aria-label': t('sev.' + p.severity) }, p.severity === 'info' ? 'i' : '!'),
|
||||||
|
el('span', { class: 'p-body' },
|
||||||
|
withTarget ? el('span', { class: 'p-target' }, n ? el('span', { html: iconSvg(n.kind) }).firstChild : null,
|
||||||
|
n ? (n.kind === 'namespace' ? n.label : `${n.label}`) : t('problems.cluster'),
|
||||||
|
n?.namespace ? el('span', { class: 'muted' }, ' · ' + n.namespace) : null) : null,
|
||||||
|
el('span', { class: 'p-reason' }, text, p.source === 'alert' ? el('span', { class: 'p-src' }, t('problems.alert')) : null,
|
||||||
|
p.count > 1 ? el('span', { class: 'muted' }, ' ×' + p.count) : null),
|
||||||
|
detail ? el('span', { class: 'p-msg muted' }, detail) : null),
|
||||||
|
p.since ? el('span', { class: 'meta muted small' }, ago(p.since)) : null,
|
||||||
|
];
|
||||||
|
if (!withTarget || !n) return el('li', { class: 'problem ' + p.severity }, el('div', { class: 'p-row' }, body));
|
||||||
|
return el('li', { class: 'problem ' + p.severity }, el('button', { type: 'button', class: 'p-row', onclick: () => goTo(n) }, body));
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderOverview(n) {
|
||||||
|
const id = n.id;
|
||||||
|
const panel = document.querySelector('[data-panel="overview"]');
|
||||||
|
const dl = el('dl', { class: 'facts' });
|
||||||
|
const h = n.facts?.health;
|
||||||
|
if (h) dl.append(el('dt', {}, t('details.state')), el('dd', {}, el('span', { class: 'status' }, el('span', { class: 'dot ' + h }), HEALTH_TEXT[h])));
|
||||||
|
for (const [k, v] of Object.entries(n.facts || {})) {
|
||||||
|
if (k === 'health' || k === 'system' || k === 'foreign' || k === 'unschedulable' || v === '') continue;
|
||||||
|
dl.append(el('dt', {}, FACT_LABELS[k] || k), el('dd', {}, v));
|
||||||
|
}
|
||||||
|
const parts = [summaryLine(n)];
|
||||||
|
if (n.problems?.length) {
|
||||||
|
parts.push(el('div', { class: 'section-title' }, t('details.problems')),
|
||||||
|
el('ul', { class: 'problem-list' }, n.problems.map((p) => problemItem(p, n, false))));
|
||||||
|
}
|
||||||
|
parts.push(dl);
|
||||||
|
if (hasPods(n) || n.kind === 'node') parts.push(el('div', { class: 'section-title' }, t('usage.title')), el('div', { id: 'd-usage' }, el('p', { class: 'muted small' }, t('loading'))));
|
||||||
|
if (n.id.startsWith('wl/') && G.raw?.vulnsAvailable) parts.push(el('div', { class: 'section-title' }, t('vulns.title')), el('div', { id: 'd-vulns' }, vulnSummary(n)));
|
||||||
|
const hosts = [...(G.placement.get(id) || [])].sort();
|
||||||
|
if (hosts.length) {
|
||||||
|
parts.push(el('div', { class: 'section-title' }, t('details.runsOn')),
|
||||||
|
el('ul', { class: 'link-list' }, hosts.map((host) => el('li', {}, el('button', { type: 'button', onclick: () => scopeNode(host) },
|
||||||
|
el('span', { html: iconSvg('node') }).firstChild, host)))));
|
||||||
|
}
|
||||||
|
panel.replaceChildren(...parts);
|
||||||
|
if (hasPods(n) || n.kind === 'node') loadUsage(n);
|
||||||
|
if (n.vulns && G.raw?.vulnsAvailable) loadVulns(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------- usage ---------- */
|
||||||
|
const fmtCPU = (m) => (m >= 1000 ? `${(m / 1000).toFixed(m >= 10000 ? 0 : 1)}` : `${m}m`);
|
||||||
|
function fmtBytes(b) {
|
||||||
|
const u = ['B', 'KiB', 'MiB', 'GiB', 'TiB'];
|
||||||
|
let i = 0;
|
||||||
|
while (b >= 1024 && i < u.length - 1) { b /= 1024; i++; }
|
||||||
|
return `${b >= 100 || i === 0 ? Math.round(b) : b.toFixed(1)} ${u[i]}`;
|
||||||
|
}
|
||||||
|
function usageRow(label, a, fmt, metrics) {
|
||||||
|
const ceiling = a.capacity || a.limit || a.request || 0;
|
||||||
|
const bits = [];
|
||||||
|
if (a.request) bits.push(t('usage.request', { v: fmt(a.request) }));
|
||||||
|
if (a.limit) bits.push(t('usage.limit', { v: fmt(a.limit) }));
|
||||||
|
if (a.capacity) bits.push(t('usage.capacity', { v: fmt(a.capacity) }));
|
||||||
|
const used = metrics && a.usage !== null ? fmt(a.usage) : '—';
|
||||||
|
const row = el('div', { class: 'usage-row' },
|
||||||
|
el('div', { class: 'usage-head' }, el('span', {}, label), el('strong', {}, used), el('span', { class: 'muted small' }, bits.join(' · '))));
|
||||||
|
if (metrics && a.usage !== null && ceiling) {
|
||||||
|
const m = el('meter', { min: '0', max: String(ceiling), value: String(Math.min(a.usage, ceiling)), low: String(ceiling * 0.7), high: String(ceiling * 0.9), optimum: '0' });
|
||||||
|
row.append(m);
|
||||||
|
}
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
async function loadUsage(n) {
|
||||||
|
const r = await fetch('/api/usage?' + new URLSearchParams({ id: n.id }));
|
||||||
|
const box = $('#d-usage');
|
||||||
|
if (detailsId !== n.id || !box) return;
|
||||||
|
if (!r.ok) { box.replaceChildren(el('p', { class: 'muted small' }, t('usage.failed', { code: r.status }))); return; }
|
||||||
|
const u = await r.json();
|
||||||
|
const parts = [usageRow('CPU', u.cpu, fmtCPU, u.metrics), usageRow(t('usage.memory'), u.memory, fmtBytes, u.metrics)];
|
||||||
|
if (!u.metrics) parts.push(el('p', { class: 'muted small' }, t('usage.noMetrics')));
|
||||||
|
if (u.pods.length) {
|
||||||
|
const canDelete = await podDeleteAllowed(n);
|
||||||
|
const table = el('table', { class: 'pods' },
|
||||||
|
el('thead', {}, el('tr', {}, ...[t('usage.pod'), t('usage.restarts'), 'CPU', t('usage.memory'), ''].map((h) => el('th', {}, h)))),
|
||||||
|
el('tbody', {}, u.pods.map((pod) => el('tr', {},
|
||||||
|
el('td', {}, el('span', { class: 'dot ' + (pod.ready ? 'ok' : 'warn'), title: pod.ready ? t('usage.ready') : t('usage.notReady') }), ' ', pod.name,
|
||||||
|
el('div', { class: 'muted small' }, pod.node)),
|
||||||
|
el('td', { class: pod.restarts ? 'warn-text' : '' }, String(pod.restarts)),
|
||||||
|
el('td', {}, pod.cpu === null ? '—' : fmtCPU(pod.cpu)),
|
||||||
|
el('td', {}, pod.memory === null ? '—' : fmtBytes(pod.memory)),
|
||||||
|
el('td', {}, canDelete ? el('button', { type: 'button', class: 'btn small', title: t('action.deletePodHint'),
|
||||||
|
onclick: () => askAction({ action: 'delete-pod', kind: 'pod', namespace: n.namespace, name: pod.name }, n) }, t('action.delete-pod')) : null),
|
||||||
|
))));
|
||||||
|
parts.push(table);
|
||||||
|
}
|
||||||
|
if (detailsId === n.id) box.replaceChildren(...parts);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------- vulnerabilities ---------- */
|
||||||
|
function vulnSummary(n) {
|
||||||
|
const v = n.vulns;
|
||||||
|
if (!v) return el('p', { class: 'muted small' }, t('vulns.none'));
|
||||||
|
return el('p', { class: 'small' },
|
||||||
|
el('span', { class: v.critical ? 'vuln-crit' : '' }, t('plural.critical_cve', { n: v.critical })), ', ',
|
||||||
|
t('plural.high_cve', { n: v.high }), ' · ', t('vulns.fixable', { n: v.fixable }));
|
||||||
|
}
|
||||||
|
async function loadVulns(n) {
|
||||||
|
const r = await fetch('/api/vulns?' + new URLSearchParams({ id: n.id }));
|
||||||
|
const box = $('#d-vulns');
|
||||||
|
if (detailsId !== n.id || !box) return;
|
||||||
|
if (!r.ok) { box.append(el('p', { class: 'muted small' }, t('vulns.failed', { code: r.status }))); return; }
|
||||||
|
const { vulns } = await r.json();
|
||||||
|
if (!vulns.length) return;
|
||||||
|
const shown = vulns.slice(0, 40);
|
||||||
|
const table = el('table', { class: 'pods vulns' },
|
||||||
|
el('thead', {}, el('tr', {}, ...[t('vulns.cve'), t('vulns.package'), t('vulns.version')].map((x) => el('th', {}, x)))),
|
||||||
|
el('tbody', {}, shown.map((v) => el('tr', {},
|
||||||
|
el('td', {}, el('span', { class: 'sev-chip ' + v.severity.toLowerCase() }, v.severity === 'CRITICAL' ? 'C' : 'H'), ' ',
|
||||||
|
v.link ? el('a', { href: v.link, target: '_blank', rel: 'noopener noreferrer', title: v.title || '' }, v.id) : v.id),
|
||||||
|
el('td', {}, v.package, el('div', { class: 'muted small' }, shortImage(v.image))),
|
||||||
|
el('td', {}, v.installed, ' → ', v.fixed ? el('strong', {}, v.fixed) : el('span', { class: 'muted' }, t('vulns.noFix')))))));
|
||||||
|
const parts = [table];
|
||||||
|
if (vulns.length > shown.length) parts.push(el('p', { class: 'muted small' }, t('vulns.more', { n: vulns.length - shown.length })));
|
||||||
|
box.append(...parts);
|
||||||
|
}
|
||||||
|
// drop the registry host (a first segment with a dot) and any digest
|
||||||
|
const shortImage = (img) => img.replace(/^[^/]*\.[^/]*\//, '').replace(/@sha256:.*/, '');
|
||||||
|
|
||||||
|
/* ---------- events ---------- */
|
||||||
|
async function loadEvents(id) {
|
||||||
|
const panel = document.querySelector('[data-panel="events"]');
|
||||||
|
panel.replaceChildren(el('p', { class: 'muted' }, t('loading')));
|
||||||
|
const r = await fetch('/api/events?' + new URLSearchParams({ id }));
|
||||||
|
if (detailsId !== id) return;
|
||||||
|
if (!r.ok) { panel.replaceChildren(el('p', { class: 'muted' }, t('events.failed', { code: r.status }))); return; }
|
||||||
|
const list = await r.json();
|
||||||
|
if (!list.length) { panel.replaceChildren(el('p', { class: 'muted' }, t('events.none'))); return; }
|
||||||
|
panel.replaceChildren(el('ul', { class: 'event-list' }, list.map((e) => el('li', { class: e.type === 'Warning' ? 'warning' : '' },
|
||||||
|
el('div', { class: 'ev-head' },
|
||||||
|
el('span', { class: 'ev-reason' }, e.reason),
|
||||||
|
e.count > 1 ? el('span', { class: 'muted small' }, '×' + e.count) : null,
|
||||||
|
el('span', { class: 'muted small ev-time', title: e.last }, ago(e.last))),
|
||||||
|
el('div', { class: 'ev-msg' }, e.message),
|
||||||
|
el('div', { class: 'muted small' }, e.object)))));
|
||||||
|
}
|
||||||
|
|
||||||
function openExternalDetails(ep) {
|
function openExternalDetails(ep) {
|
||||||
detailsId = ep.id;
|
detailsId = ep.id;
|
||||||
showPanelHead(ep.namespace ? (KIND[ep.kind] ? ep.kind : 'pod') : 'external', ep.label, ep.namespace ? t('details.namespace', { ns: ep.namespace }) : t('details.outside'));
|
showPanelHead(ep.namespace ? (KIND[ep.kind] ? ep.kind : 'pod') : 'external', ep.label, ep.namespace ? t('details.namespace', { ns: ep.namespace }) : t('details.outside'));
|
||||||
|
closeProblems();
|
||||||
tabVisible('yaml', false);
|
tabVisible('yaml', false);
|
||||||
tabVisible('logs', false);
|
tabVisible('logs', false);
|
||||||
|
tabVisible('events', false);
|
||||||
|
$('#d-actions').replaceChildren();
|
||||||
document.querySelector('[data-panel="overview"]').replaceChildren(
|
document.querySelector('[data-panel="overview"]').replaceChildren(
|
||||||
el('p', { class: 'muted' }, ep.namespace
|
el('p', { class: 'muted' }, ep.namespace
|
||||||
? t('details.ghostPod')
|
? t('details.ghostPod')
|
||||||
@@ -1033,6 +1309,15 @@ function openExternalDetails(ep) {
|
|||||||
setTab('overview');
|
setTab('overview');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function httpPaths(h) {
|
||||||
|
if (!h?.paths?.length) return null;
|
||||||
|
return el('table', { class: 'pods http-paths' },
|
||||||
|
el('thead', {}, el('tr', {}, ...[t('http.path'), t('http.requests'), 'p95', '5xx'].map((x) => el('th', {}, x)))),
|
||||||
|
el('tbody', {}, h.paths.map((p) => el('tr', {},
|
||||||
|
el('td', {}, el('span', { class: 'muted' }, p.method + ' '), p.path),
|
||||||
|
el('td', {}, String(p.count)), el('td', {}, fmtMs(p.p95Ms)),
|
||||||
|
el('td', { class: p.errors ? 'warn-text' : '' }, String(p.errors))))));
|
||||||
|
}
|
||||||
function linkButton(id, meta, dropped) {
|
function linkButton(id, meta, dropped) {
|
||||||
const n = G.byId.get(id);
|
const n = G.byId.get(id);
|
||||||
const ep = state.flows?.endpoints.find((x) => x.id === id);
|
const ep = state.flows?.endpoints.find((x) => x.id === id);
|
||||||
@@ -1054,9 +1339,10 @@ function renderLinks(id) {
|
|||||||
}
|
}
|
||||||
const tIn = [], tOut = [];
|
const tIn = [], tOut = [];
|
||||||
for (const e of state.trafficEdges || []) {
|
for (const e of state.trafficEdges || []) {
|
||||||
const meta = `${e.ports.join(', ')} · ${t('edge.conns', { n: e.count })}${e.fresh ? t('edge.new', { n: e.fresh }) : ''}${e.dropped ? t('edge.drop', { n: e.dropped }) : ''}`;
|
const meta = `${e.ports.join(', ')} · ${t('edge.conns', { n: e.count })}${e.fresh ? t('edge.new', { n: e.fresh }) : ''}${e.dropped ? t('edge.drop', { n: e.dropped }) : ''}${e.http ? ' · ' + httpSummary(e.http) : ''}`;
|
||||||
if (e.target === id) tIn.push(linkButton(e.source, meta, e.dropped));
|
const bad = e.dropped || (e.http && errShare(e.http) > 0);
|
||||||
if (e.source === id) tOut.push(linkButton(e.target, meta, e.dropped));
|
if (e.target === id) { tIn.push(linkButton(e.source, meta, bad)); const tb = httpPaths(e.http); if (tb) tIn.push(el('li', { class: 'http-li' }, tb)); }
|
||||||
|
if (e.source === id) { tOut.push(linkButton(e.target, meta, bad)); const tb = httpPaths(e.http); if (tb) tOut.push(el('li', { class: 'http-li' }, tb)); }
|
||||||
}
|
}
|
||||||
const section = (title, items, empty) => [el('div', { class: 'section-title' }, title),
|
const section = (title, items, empty) => [el('div', { class: 'section-title' }, title),
|
||||||
items.length ? el('ul', { class: 'link-list' }, items) : el('p', { class: 'muted small' }, empty)];
|
items.length ? el('ul', { class: 'link-list' }, items) : el('p', { class: 'muted small' }, empty)];
|
||||||
@@ -1120,6 +1406,250 @@ async function loadLogs() {
|
|||||||
}
|
}
|
||||||
$('#logs-reload').addEventListener('click', loadLogs);
|
$('#logs-reload').addEventListener('click', loadLogs);
|
||||||
|
|
||||||
|
/* ---------- actions ---------- */
|
||||||
|
// Buttons appear only for what the viewer's own RBAC allows: the server
|
||||||
|
// asks the API server with the viewer's token and runs the action with it.
|
||||||
|
let me = null;
|
||||||
|
const ACTIONS = {
|
||||||
|
deployment: ['restart', 'scale'],
|
||||||
|
statefulset: ['restart', 'scale'],
|
||||||
|
daemonset: ['restart'],
|
||||||
|
cronjob: ['run', 'suspend', 'resume'],
|
||||||
|
node: ['cordon', 'uncordon'],
|
||||||
|
};
|
||||||
|
const actionTarget = (n) => ({ kind: n.kind, namespace: n.namespace || '', name: n.label });
|
||||||
|
const permCache = new Map();
|
||||||
|
async function permissions(kind, namespace, name) {
|
||||||
|
const key = [kind, namespace, name].join('/');
|
||||||
|
if (permCache.has(key)) return permCache.get(key);
|
||||||
|
const r = await fetch('/api/permissions?' + new URLSearchParams({ kind, namespace, name }));
|
||||||
|
const allowed = r.ok ? (await r.json()).allowed : {};
|
||||||
|
permCache.set(key, allowed);
|
||||||
|
return allowed;
|
||||||
|
}
|
||||||
|
const actionsUsable = () => me?.actionsEnabled && me.user;
|
||||||
|
async function podDeleteAllowed(n) {
|
||||||
|
if (!actionsUsable() || !n.namespace) return false;
|
||||||
|
return !!(await permissions('pod', n.namespace, ''))['delete-pod'];
|
||||||
|
}
|
||||||
|
async function renderActions(n) {
|
||||||
|
const box = $('#d-actions');
|
||||||
|
box.replaceChildren();
|
||||||
|
const list = ACTIONS[n.facts?.foreign === 'true' ? '' : n.kind];
|
||||||
|
if (!list || !me?.actionsEnabled) return;
|
||||||
|
if (!me.user) { box.append(el('span', { class: 'muted small' }, t('action.unavailable.' + (me.reason || 'noToken')))); return; }
|
||||||
|
const allowed = await permissions(n.kind, n.namespace || '', n.label);
|
||||||
|
if (detailsId !== n.id) return;
|
||||||
|
const suspended = n.facts?.suspended === 'true', cordoned = n.facts?.unschedulable === 'true';
|
||||||
|
const shown = list.filter((a) => !(a === 'suspend' && suspended) && !(a === 'resume' && !suspended) && !(a === 'cordon' && cordoned) && !(a === 'uncordon' && !cordoned));
|
||||||
|
const buttons = shown.filter((a) => allowed[a]).map((a) => el('button', { type: 'button', class: 'btn small', onclick: () => askAction({ action: a, ...actionTarget(n) }, n) }, t('action.' + a)));
|
||||||
|
box.replaceChildren(...(buttons.length ? buttons : [el('span', { class: 'muted small' }, t('action.noRights'))]));
|
||||||
|
}
|
||||||
|
|
||||||
|
const dialog = $('#confirm');
|
||||||
|
function confirmDialog({ title, text, ok, danger, value }) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
$('#confirm-title').textContent = title;
|
||||||
|
$('#confirm-text').textContent = text;
|
||||||
|
const row = $('#confirm-input-row'), input = $('#confirm-input');
|
||||||
|
row.hidden = value === undefined;
|
||||||
|
if (value !== undefined) input.value = String(value);
|
||||||
|
const okBtn = $('#confirm-ok');
|
||||||
|
okBtn.textContent = ok;
|
||||||
|
okBtn.classList.toggle('danger', !!danger);
|
||||||
|
const done = (result) => { dialog.close(); resolve(result); };
|
||||||
|
okBtn.onclick = () => done({ ok: true, value: Number(input.value) });
|
||||||
|
$('#confirm-cancel').onclick = () => done({ ok: false });
|
||||||
|
dialog.onclose = () => resolve({ ok: false });
|
||||||
|
dialog.showModal();
|
||||||
|
(value !== undefined ? input : $('#confirm-cancel')).focus();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
async function askAction(req, n) {
|
||||||
|
const target = req.kind === 'pod' ? req.name : `${KIND[n.kind]?.title || n.kind} ${n.label}`;
|
||||||
|
const scale = req.action === 'scale';
|
||||||
|
const current = Number(n.facts?.replicas ?? 1);
|
||||||
|
const answer = await confirmDialog({
|
||||||
|
title: t('action.' + req.action) + ' · ' + target,
|
||||||
|
text: t('confirm.' + req.action, { target, ns: n.namespace || '' }),
|
||||||
|
ok: t('action.' + req.action),
|
||||||
|
danger: ['delete-pod', 'cordon', 'suspend'].includes(req.action),
|
||||||
|
value: scale ? current : undefined,
|
||||||
|
});
|
||||||
|
if (!answer.ok) return;
|
||||||
|
if (scale) {
|
||||||
|
if (!Number.isInteger(answer.value) || answer.value < 0 || answer.value > 100) { toast(t('action.badReplicas'), true); return; }
|
||||||
|
req.replicas = answer.value;
|
||||||
|
}
|
||||||
|
const r = await fetch('/api/action', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(req) });
|
||||||
|
const body = (await r.text()).trim();
|
||||||
|
if (!r.ok) { toast(t('action.failed', { err: body || 'HTTP ' + r.status }), true); return; }
|
||||||
|
toast(t('action.done', { what: t('action.' + req.action), target }));
|
||||||
|
permCache.clear();
|
||||||
|
setTimeout(() => refreshGraph(true), 1500);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------- toast ---------- */
|
||||||
|
let toastTimer;
|
||||||
|
function toast(text, isError = false) {
|
||||||
|
const box = $('#toast');
|
||||||
|
box.textContent = text;
|
||||||
|
box.classList.toggle('error', isError);
|
||||||
|
box.hidden = false;
|
||||||
|
clearTimeout(toastTimer);
|
||||||
|
toastTimer = setTimeout(() => { box.hidden = true; }, isError ? 9000 : 5000);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------- problems panel ---------- */
|
||||||
|
let problemsScope = 'all';
|
||||||
|
const problemsPanel = $('#problems-panel');
|
||||||
|
function problemNs(x) {
|
||||||
|
if (!x.n) return null;
|
||||||
|
return x.n.kind === 'namespace' ? x.n.label : x.n.namespace || null;
|
||||||
|
}
|
||||||
|
function inProblemsScope(x) {
|
||||||
|
if (problemsScope === 'all') return true;
|
||||||
|
const ns = problemNs(x);
|
||||||
|
const system = !ns || isSystemNs(ns);
|
||||||
|
return problemsScope === 'system' ? system : !system;
|
||||||
|
}
|
||||||
|
function renderProblemsButton() {
|
||||||
|
const all = allProblems().filter((x) => x.p.severity !== 'info');
|
||||||
|
const btn = $('#problems-btn');
|
||||||
|
if (!all.length) {
|
||||||
|
btn.hidden = false;
|
||||||
|
btn.className = 'btn problems-btn ok';
|
||||||
|
btn.textContent = t('problems.none');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const crit = all.filter((x) => x.p.severity === 'critical').length;
|
||||||
|
btn.hidden = false;
|
||||||
|
btn.className = 'btn problems-btn ' + (crit ? 'critical' : 'warning');
|
||||||
|
btn.replaceChildren(el('span', { class: 'pbadge ' + (crit ? 'critical' : 'warning'), 'aria-hidden': 'true' }, '!'), t('problems.button', { n: all.length }));
|
||||||
|
}
|
||||||
|
function openProblems() {
|
||||||
|
closeDetails();
|
||||||
|
problemsPanel.hidden = false;
|
||||||
|
$('#problems-btn').setAttribute('aria-expanded', 'true');
|
||||||
|
renderProblems();
|
||||||
|
requestAnimationFrame(() => cy.resize());
|
||||||
|
}
|
||||||
|
function closeProblems() {
|
||||||
|
if (problemsPanel.hidden) return;
|
||||||
|
problemsPanel.hidden = true;
|
||||||
|
$('#problems-btn').setAttribute('aria-expanded', 'false');
|
||||||
|
requestAnimationFrame(() => cy.resize());
|
||||||
|
}
|
||||||
|
function renderProblems() {
|
||||||
|
const list = allProblems().filter(inProblemsScope);
|
||||||
|
const badged = list.filter((x) => x.p.severity !== 'info');
|
||||||
|
const info = list.filter((x) => x.p.severity === 'info');
|
||||||
|
const crit = badged.filter((x) => x.p.severity === 'critical').length;
|
||||||
|
$('#p-sub').textContent = badged.length
|
||||||
|
? t('problems.sub', { crit: t('plural.critical', { n: crit }), warn: t('plural.warning', { n: badged.length - crit }) })
|
||||||
|
: t('problems.allGood');
|
||||||
|
const parts = [];
|
||||||
|
if (badged.length) parts.push(el('ul', { class: 'problem-list' }, badged.map((x) => problemItem(x.p, x.n, true))));
|
||||||
|
else parts.push(el('p', { class: 'summary ok' }, t('problems.allGood')));
|
||||||
|
if (info.length) {
|
||||||
|
parts.push(el('details', { class: 'p-info' }, el('summary', {}, t('problems.info', { n: info.length })),
|
||||||
|
el('ul', { class: 'problem-list' }, info.map((x) => problemItem(x.p, x.n, true)))));
|
||||||
|
}
|
||||||
|
if (G.raw && !G.raw.alertsAvailable) parts.push(el('p', { class: 'muted small' }, t('problems.noAlerts')));
|
||||||
|
$('#p-list').replaceChildren(...parts);
|
||||||
|
}
|
||||||
|
function goTo(n) {
|
||||||
|
if (n.kind === 'namespace') { scopeNs(n.label); return; }
|
||||||
|
if (n.kind === 'node') { scopeNode(n.label); openDetails(n.id); return; }
|
||||||
|
selectObject(n.id);
|
||||||
|
}
|
||||||
|
$('#problems-btn').addEventListener('click', () => (problemsPanel.hidden ? openProblems() : closeProblems()));
|
||||||
|
$('#p-close').addEventListener('click', closeProblems);
|
||||||
|
for (const b of document.querySelectorAll('.p-filter [role="tab"]')) {
|
||||||
|
b.addEventListener('click', () => {
|
||||||
|
problemsScope = b.dataset.scope;
|
||||||
|
for (const x of document.querySelectorAll('.p-filter [role="tab"]')) x.setAttribute('aria-selected', String(x === b));
|
||||||
|
renderProblems();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------- lock, export ---------- */
|
||||||
|
// Cards are locked by default so a stray drag does not rearrange the map;
|
||||||
|
// the padlock unlocks them until it is clicked again.
|
||||||
|
let locked = true;
|
||||||
|
const LOCK_ICON = {
|
||||||
|
true: '<svg viewBox="0 0 20 20" width="15" height="15" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" aria-hidden="true"><rect x="4" y="9" width="12" height="8" rx="1.5"/><path d="M7 9V6.5a3 3 0 0 1 6 0V9"/></svg>',
|
||||||
|
false: '<svg viewBox="0 0 20 20" width="15" height="15" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" aria-hidden="true"><rect x="4" y="9" width="12" height="8" rx="1.5"/><path d="M7 9V6.5a3 3 0 0 1 5.8-1"/></svg>',
|
||||||
|
};
|
||||||
|
function applyLock() {
|
||||||
|
cy.autoungrabify(locked);
|
||||||
|
const b = $('#lock');
|
||||||
|
b.innerHTML = LOCK_ICON[locked];
|
||||||
|
b.setAttribute('aria-pressed', String(locked));
|
||||||
|
b.title = t(locked ? 'html.lockedTitle' : 'html.unlockedTitle');
|
||||||
|
b.classList.toggle('unlocked', !locked);
|
||||||
|
}
|
||||||
|
$('#lock').addEventListener('click', () => { locked = !locked; applyLock(); toast(t(locked ? 'lock.on' : 'lock.off')); });
|
||||||
|
|
||||||
|
$('#export-png').addEventListener('click', () => {
|
||||||
|
try {
|
||||||
|
const blob = cy.png({ output: 'blob', full: true, scale: 2, maxWidth: 8000, maxHeight: 8000, bg: getComputedStyle(document.body).backgroundColor });
|
||||||
|
const scope = state.scope.mode === 'ns' ? state.scope.ns.join('+') : state.scope.mode === 'node' ? state.scope.node : 'cluster';
|
||||||
|
const d = new Date(), p2 = (x) => String(x).padStart(2, '0');
|
||||||
|
const stamp = `${d.getFullYear()}-${p2(d.getMonth() + 1)}-${p2(d.getDate())}-${p2(d.getHours())}${p2(d.getMinutes())}`;
|
||||||
|
const a = el('a', { href: URL.createObjectURL(blob), download: `cluster-map-${scope}-${stamp}.png` });
|
||||||
|
document.body.append(a);
|
||||||
|
a.click();
|
||||||
|
a.remove();
|
||||||
|
setTimeout(() => URL.revokeObjectURL(a.href), 10000);
|
||||||
|
} catch (err) {
|
||||||
|
toast(t('png.failed', { err: err.message }), true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
/* ---------- background refresh ---------- */
|
||||||
|
// Every minute the graph is re-read and cards, boxes, the tree and the
|
||||||
|
// Problems panel are redrawn in place. The layout is left alone; new or
|
||||||
|
// removed objects appear with the Refresh button.
|
||||||
|
async function refreshGraph(force = false) {
|
||||||
|
if (document.hidden && !force) return;
|
||||||
|
let raw;
|
||||||
|
try {
|
||||||
|
const r = await fetch('/api/graph');
|
||||||
|
if (!r.ok) return;
|
||||||
|
raw = await r.json();
|
||||||
|
} catch { return; }
|
||||||
|
indexGraph(raw);
|
||||||
|
$('#updated').textContent = t('updated', { time: new Date().toLocaleTimeString(LANG, { hour: '2-digit', minute: '2-digit' }) });
|
||||||
|
renderTree();
|
||||||
|
renderProblemsButton();
|
||||||
|
if (!problemsPanel.hidden) renderProblems();
|
||||||
|
cy.batch(() => {
|
||||||
|
cy.nodes().forEach((x) => {
|
||||||
|
const d = x.data();
|
||||||
|
if (d.kind === 'nscard') { x.data('img', nsCardImage(d.ns)); return; }
|
||||||
|
if (d.kind === 'nsbox') {
|
||||||
|
x.removeClass('sev-warning sev-critical');
|
||||||
|
if (!x.hasClass('ghost')) x.addClass(sevClass(nsSeverity(d.ns)).trim());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (d.kind === 'hostbox') {
|
||||||
|
x.removeClass('sev-warning sev-critical');
|
||||||
|
x.addClass(sevClass(cardSev(G.byId.get('node/' + d.id.slice('host:'.length)))).trim());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const n = G.byId.get(d.ref);
|
||||||
|
if (n && !x.isParent()) x.data('img', cardNode(n, d.parent, x.hasClass('ghostcard')).data.img);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
const n = detailsId && G.byId.get(detailsId);
|
||||||
|
if (n && !$('#details').hidden && document.querySelector('.tabs [data-tab="overview"]').getAttribute('aria-selected') === 'true') {
|
||||||
|
renderOverview(n);
|
||||||
|
renderActions(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setInterval(refreshGraph, 60000);
|
||||||
|
|
||||||
/* ---------- top bar ---------- */
|
/* ---------- top bar ---------- */
|
||||||
$('#traffic-on').addEventListener('change', (e) => { state.traffic = e.target.checked; renderMap(); });
|
$('#traffic-on').addEventListener('change', (e) => { state.traffic = e.target.checked; renderMap(); });
|
||||||
$('#traffic-window').addEventListener('change', (e) => { state.trafficWindow = Number(e.target.value); if (state.traffic) renderMap(); });
|
$('#traffic-window').addEventListener('change', (e) => { state.trafficWindow = Number(e.target.value); if (state.traffic) renderMap(); });
|
||||||
@@ -1135,6 +1665,7 @@ matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
|
|||||||
async function load() {
|
async function load() {
|
||||||
setStatus(t('status.loadingGraph'));
|
setStatus(t('status.loadingGraph'));
|
||||||
const r = await fetch('/api/graph');
|
const r = await fetch('/api/graph');
|
||||||
|
if (r.status === 401) { setStatus(t('status.noViewerToken'), true); return; }
|
||||||
if (!r.ok) { setStatus(t('status.graphFailed', { code: r.status }), true); return; }
|
if (!r.ok) { setStatus(t('status.graphFailed', { code: r.status }), true); return; }
|
||||||
indexGraph(await r.json());
|
indexGraph(await r.json());
|
||||||
podsFor = null;
|
podsFor = null;
|
||||||
@@ -1144,6 +1675,8 @@ async function load() {
|
|||||||
if (state.selected && !G.byId.has(state.selected)) state.selected = null;
|
if (state.selected && !G.byId.has(state.selected)) state.selected = null;
|
||||||
$('#updated').textContent = t('updated', { time: new Date().toLocaleTimeString(LANG, { hour: '2-digit', minute: '2-digit' }) });
|
$('#updated').textContent = t('updated', { time: new Date().toLocaleTimeString(LANG, { hour: '2-digit', minute: '2-digit' }) });
|
||||||
if (state.selected) revealInTree(state.selected);
|
if (state.selected) revealInTree(state.selected);
|
||||||
|
renderProblemsButton();
|
||||||
|
if (!problemsPanel.hidden) renderProblems();
|
||||||
await renderMap();
|
await renderMap();
|
||||||
if (state.selected) { openDetails(state.selected); focusOnMap(state.selected); }
|
if (state.selected) { openDetails(state.selected); focusOnMap(state.selected); }
|
||||||
}
|
}
|
||||||
@@ -1165,4 +1698,16 @@ cy.style(mapStyle());
|
|||||||
readHash();
|
readHash();
|
||||||
$('#view-ns').setAttribute('aria-selected', String(state.treeView === 'ns'));
|
$('#view-ns').setAttribute('aria-selected', String(state.treeView === 'ns'));
|
||||||
$('#view-nodes').setAttribute('aria-selected', String(state.treeView === 'nodes'));
|
$('#view-nodes').setAttribute('aria-selected', String(state.treeView === 'nodes'));
|
||||||
|
applyLock();
|
||||||
|
fetch('/api/me').then((r) => (r.ok ? r.json() : null)).then((m) => {
|
||||||
|
me = m;
|
||||||
|
if (me?.user) $('#whoami').textContent = me.user;
|
||||||
|
if (me?.visibility === 'namespaces') {
|
||||||
|
const w = $('#whoami');
|
||||||
|
w.textContent = (me.user ? me.user + ' · ' : '') + t('plural.visibleNs', { n: me.namespaces });
|
||||||
|
w.title = t('visibility.namespaces');
|
||||||
|
}
|
||||||
|
const n = detailsId && G.byId.get(detailsId);
|
||||||
|
if (n) renderActions(n);
|
||||||
|
}).catch(() => {});
|
||||||
load();
|
load();
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ const I18N = {
|
|||||||
'node': 'node {name}',
|
'node': 'node {name}',
|
||||||
'edge.conns': '{n} conn.', 'edge.new': ', {n} new', 'edge.drop': ' · drop {n}',
|
'edge.conns': '{n} conn.', 'edge.new': ', {n} new', 'edge.drop': ' · drop {n}',
|
||||||
'status.loadingTraffic': 'loading traffic…', 'status.loadingGraph': 'loading graph…',
|
'status.loadingTraffic': 'loading traffic…', 'status.loadingGraph': 'loading graph…',
|
||||||
'status.graphFailed': 'could not load the graph: HTTP {code}', 'status.empty': 'nothing here',
|
'status.graphFailed': 'could not load the graph: HTTP {code}', 'status.renderFailed': 'could not draw the map: {error}', 'status.empty': 'nothing here',
|
||||||
'status.noRelay': 'no connection to Hubble Relay', 'status.trafficFailed': 'traffic unavailable: {err}',
|
'status.noRelay': 'no connection to Hubble Relay', 'status.trafficFailed': 'traffic unavailable: {err}',
|
||||||
'status.partial': '{covered} (of {window}: collecting since start)',
|
'status.partial': '{covered} (of {window}: collecting since start)',
|
||||||
'status.onDemandPartial': '{covered} (of {window}: collecting since the map was opened)',
|
'status.onDemandPartial': '{covered} (of {window}: collecting since the map was opened)',
|
||||||
@@ -57,6 +57,72 @@ const I18N = {
|
|||||||
'html.details': 'Object details', 'html.close': 'Close panel', 'html.sections': 'Sections',
|
'html.details': 'Object details', 'html.close': 'Close panel', 'html.sections': 'Sections',
|
||||||
'html.overview': 'Overview', 'html.links': 'Links', 'html.logs': 'Logs', 'html.pod': 'Pod', 'html.container': 'Container',
|
'html.overview': 'Overview', 'html.links': 'Links', 'html.logs': 'Logs', 'html.pod': 'Pod', 'html.container': 'Container',
|
||||||
'html.view': 'Current view', 'html.lang': 'Language',
|
'html.view': 'Current view', 'html.lang': 'Language',
|
||||||
|
'html.lock': 'Lock card positions', 'html.lockedTitle': 'Cards are locked. Click to move them', 'html.unlockedTitle': 'Cards can be dragged. Click to lock',
|
||||||
|
'html.exportPng': 'Save the map as a picture', 'html.actions': 'Actions', 'html.events': 'Events',
|
||||||
|
'html.problems': 'Problems', 'html.problemsScope': 'Which problems', 'html.scopeAll': 'All',
|
||||||
|
'lock.on': 'Cards are locked', 'lock.off': 'Cards unlocked: drag them, then lock again',
|
||||||
|
'png.failed': 'could not save the picture: {err}',
|
||||||
|
'sev.critical': 'error', 'sev.warning': 'warning', 'sev.info': 'for information',
|
||||||
|
'legend.warning': 'warning', 'legend.critical': 'error',
|
||||||
|
'ago': '{d} ago',
|
||||||
|
'summary.ok': 'Everything works', 'summary.idle': 'Stopped: nothing is supposed to run',
|
||||||
|
'summary.critical': 'Not working properly: {n}', 'summary.warning': 'Works, with {n}',
|
||||||
|
'plural.problem': { one: '{n} problem', other: '{n} problems' },
|
||||||
|
'plural.critical': { one: '{n} error', other: '{n} errors' },
|
||||||
|
'plural.warning': { one: '{n} warning', other: '{n} warnings' },
|
||||||
|
'details.problems': 'Problems',
|
||||||
|
'problems.none': 'No problems', 'problems.button': 'Problems: {n}', 'problems.cluster': 'Cluster',
|
||||||
|
'problems.alert': 'alert', 'problems.sub': '{crit}, {warn}', 'problems.allGood': 'No problems right now.',
|
||||||
|
'problems.info': 'For information, no badge: {n}',
|
||||||
|
'problems.noAlerts': 'Prometheus alerts are not shown: the ClusterAlert resource is not served in this cluster.',
|
||||||
|
'problem.NotReady': 'No replica is ready', 'problem.PartlyReady': 'Not all replicas are ready',
|
||||||
|
'problem.CrashLoopBackOff': 'Container keeps crashing', 'problem.ImagePullBackOff': 'Image cannot be pulled',
|
||||||
|
'problem.ErrImagePull': 'Image cannot be pulled', 'problem.InvalidImageName': 'Invalid image name',
|
||||||
|
'problem.CreateContainerConfigError': 'Container configuration error (a missing Secret or ConfigMap?)',
|
||||||
|
'problem.CreateContainerError': 'Container cannot be created', 'problem.RunContainerError': 'Container cannot be started',
|
||||||
|
'problem.OOMKilled': 'Killed for running out of memory', 'problem.Restarted': 'Restarted recently',
|
||||||
|
'problem.Pending': 'Pod has been pending for over 5 minutes', 'problem.Unschedulable': 'Pod cannot be scheduled',
|
||||||
|
'problem.Evicted': 'Pod was evicted', 'problem.ProgressDeadlineExceeded': 'Rollout is stuck',
|
||||||
|
'problem.LastRunFailed': 'Last run did not succeed', 'problem.NoEndpoints': 'No ready pod behind the Service',
|
||||||
|
'problem.BackendMissing': 'Routes to a Service that does not exist', 'problem.NodeNotReady': 'Node is not ready',
|
||||||
|
'problem.MemoryPressure': 'Node is short of memory', 'problem.DiskPressure': 'Node is short of disk',
|
||||||
|
'problem.PIDPressure': 'Node is short of process IDs', 'problem.Cordoned': 'Node is cordoned: no new pods',
|
||||||
|
'usage.title': 'Load', 'usage.memory': 'Memory', 'usage.request': 'request {v}', 'usage.limit': 'limit {v}',
|
||||||
|
'usage.capacity': 'of {v}', 'usage.failed': 'could not read the load: HTTP {code}',
|
||||||
|
'usage.noMetrics': 'Current usage is unknown: the metrics.k8s.io API did not answer.',
|
||||||
|
'usage.pod': 'Pod', 'usage.restarts': 'Restarts', 'usage.ready': 'ready', 'usage.notReady': 'not ready',
|
||||||
|
'events.failed': 'could not read events: HTTP {code}', 'events.none': 'No events in the last hour.',
|
||||||
|
'action.restart': 'Restart', 'action.scale': 'Scale', 'action.run': 'Run now', 'action.suspend': 'Suspend',
|
||||||
|
'action.resume': 'Resume', 'action.cordon': 'Cordon', 'action.uncordon': 'Uncordon', 'action.delete-pod': 'Delete',
|
||||||
|
'action.deletePodHint': 'Delete this pod; its controller starts a new one',
|
||||||
|
'action.cancel': 'Cancel', 'action.replicas': 'Replicas',
|
||||||
|
'action.noRights': 'Your role allows no actions here.',
|
||||||
|
'action.unavailable.noToken': 'Actions need the user token from the DexAuthenticator; it is not in the request.',
|
||||||
|
'action.unavailable.rejected': 'The API server did not accept your token; sign in again.',
|
||||||
|
'action.unavailable.disabled': '',
|
||||||
|
'action.badReplicas': 'Replicas must be a whole number from 0 to 100.',
|
||||||
|
'action.failed': 'Not done: {err}', 'action.done': '{what}: {target} — done',
|
||||||
|
'confirm.restart': 'All pods of {target} are replaced one by one, as with kubectl rollout restart.',
|
||||||
|
'confirm.scale': 'How many replicas should {target} run?',
|
||||||
|
'confirm.run': 'A Job is created from the {target} template right now, outside the schedule.',
|
||||||
|
'confirm.suspend': 'No new runs of {target} start until it is resumed.',
|
||||||
|
'confirm.resume': '{target} runs on its schedule again.',
|
||||||
|
'confirm.cordon': 'No new pods are scheduled on {target}. Running pods stay.',
|
||||||
|
'confirm.uncordon': 'New pods can be scheduled on {target} again.',
|
||||||
|
'confirm.delete-pod': 'Pod {target} in {ns} is deleted; its controller starts a new one.',
|
||||||
|
'vulns.title': 'Vulnerabilities', 'vulns.none': 'No CRITICAL or HIGH vulnerabilities found in the scanned images.',
|
||||||
|
'vulns.fixable': 'fixed in a newer version: {n}', 'vulns.failed': 'could not read vulnerabilities: HTTP {code}',
|
||||||
|
'vulns.cve': 'CVE', 'vulns.package': 'Package', 'vulns.version': 'Installed → fixed', 'vulns.noFix': 'no fix yet',
|
||||||
|
'vulns.more': 'and {n} more',
|
||||||
|
'plural.critical_cve': { one: '{n} critical', other: '{n} critical' },
|
||||||
|
'plural.high_cve': { one: '{n} high', other: '{n} high' },
|
||||||
|
'legend.vulns': 'critical vulnerabilities in the images (trivy)',
|
||||||
|
'ext.restricted': 'hidden by your permissions',
|
||||||
|
'status.noViewerToken': 'The map shows what your own permissions allow, but the request carries no user token. Sign in again; if it repeats, the DexAuthenticator does not pass the Authorization header.',
|
||||||
|
'plural.visibleNs': { one: '{n} namespace visible', other: '{n} namespaces visible' },
|
||||||
|
'visibility.namespaces': 'You see the namespaces where your role can list pods; peers elsewhere are shown as hidden.',
|
||||||
|
'http.summary': 'HTTP {n} req, p95 {p95}', 'http.errors': ', {pct}% 5xx', 'http.path': 'Path', 'http.requests': 'Requests',
|
||||||
|
'legend.http5xx': 'HTTP 5xx on the link',
|
||||||
},
|
},
|
||||||
ru: {
|
ru: {
|
||||||
'kind.pod': 'Контроллер', 'kind.external': 'Внешний', 'folder.pod': 'Другие контроллеры',
|
'kind.pod': 'Контроллер', 'kind.external': 'Внешний', 'folder.pod': 'Другие контроллеры',
|
||||||
@@ -66,7 +132,7 @@ const I18N = {
|
|||||||
'node': 'нода {name}',
|
'node': 'нода {name}',
|
||||||
'edge.conns': '{n} соед.', 'edge.new': ', новых {n}', 'edge.drop': ' · drop {n}',
|
'edge.conns': '{n} соед.', 'edge.new': ', новых {n}', 'edge.drop': ' · drop {n}',
|
||||||
'status.loadingTraffic': 'загрузка трафика…', 'status.loadingGraph': 'загрузка графа…',
|
'status.loadingTraffic': 'загрузка трафика…', 'status.loadingGraph': 'загрузка графа…',
|
||||||
'status.graphFailed': 'не удалось загрузить граф: HTTP {code}', 'status.empty': 'здесь нет объектов',
|
'status.graphFailed': 'не удалось загрузить граф: HTTP {code}', 'status.renderFailed': 'не удалось нарисовать карту: {error}', 'status.empty': 'здесь нет объектов',
|
||||||
'status.noRelay': 'нет связи с Hubble Relay', 'status.trafficFailed': 'трафик недоступен: {err}',
|
'status.noRelay': 'нет связи с Hubble Relay', 'status.trafficFailed': 'трафик недоступен: {err}',
|
||||||
'status.partial': '{covered} (из {window}: сбор идёт с запуска)',
|
'status.partial': '{covered} (из {window}: сбор идёт с запуска)',
|
||||||
'status.onDemandPartial': '{covered} (из {window}: сбор идёт с открытия карты)',
|
'status.onDemandPartial': '{covered} (из {window}: сбор идёт с открытия карты)',
|
||||||
@@ -111,6 +177,72 @@ const I18N = {
|
|||||||
'html.details': 'Детали объекта', 'html.close': 'Закрыть панель', 'html.sections': 'Разделы',
|
'html.details': 'Детали объекта', 'html.close': 'Закрыть панель', 'html.sections': 'Разделы',
|
||||||
'html.overview': 'Обзор', 'html.links': 'Связи', 'html.logs': 'Логи', 'html.pod': 'Под', 'html.container': 'Контейнер',
|
'html.overview': 'Обзор', 'html.links': 'Связи', 'html.logs': 'Логи', 'html.pod': 'Под', 'html.container': 'Контейнер',
|
||||||
'html.view': 'Текущий вид', 'html.lang': 'Язык',
|
'html.view': 'Текущий вид', 'html.lang': 'Язык',
|
||||||
|
'html.lock': 'Закрепить карточки', 'html.lockedTitle': 'Карточки закреплены. Нажмите, чтобы двигать', 'html.unlockedTitle': 'Карточки можно перетаскивать. Нажмите, чтобы закрепить',
|
||||||
|
'html.exportPng': 'Сохранить карту картинкой', 'html.actions': 'Действия', 'html.events': 'События',
|
||||||
|
'html.problems': 'Проблемы', 'html.problemsScope': 'Какие проблемы', 'html.scopeAll': 'Все',
|
||||||
|
'lock.on': 'Карточки закреплены', 'lock.off': 'Карточки откреплены: передвиньте и закрепите снова',
|
||||||
|
'png.failed': 'не удалось сохранить картинку: {err}',
|
||||||
|
'sev.critical': 'ошибка', 'sev.warning': 'предупреждение', 'sev.info': 'для сведения',
|
||||||
|
'legend.warning': 'предупреждение', 'legend.critical': 'ошибка',
|
||||||
|
'ago': '{d} назад',
|
||||||
|
'summary.ok': 'Всё работает', 'summary.idle': 'Остановлено: ничего не должно работать',
|
||||||
|
'summary.critical': 'Работает с ошибками: {n}', 'summary.warning': 'Работает, но есть {n}',
|
||||||
|
'plural.problem': { one: '{n} проблема', few: '{n} проблемы', many: '{n} проблем', other: '{n} проблемы' },
|
||||||
|
'plural.critical': { one: '{n} ошибка', few: '{n} ошибки', many: '{n} ошибок', other: '{n} ошибки' },
|
||||||
|
'plural.warning': { one: '{n} предупреждение', few: '{n} предупреждения', many: '{n} предупреждений', other: '{n} предупреждения' },
|
||||||
|
'details.problems': 'Проблемы',
|
||||||
|
'problems.none': 'Проблем нет', 'problems.button': 'Проблемы: {n}', 'problems.cluster': 'Кластер',
|
||||||
|
'problems.alert': 'алерт', 'problems.sub': '{crit}, {warn}', 'problems.allGood': 'Сейчас проблем нет.',
|
||||||
|
'problems.info': 'Для сведения, без значка: {n}',
|
||||||
|
'problems.noAlerts': 'Алерты Prometheus не показаны: в кластере нет ресурса ClusterAlert.',
|
||||||
|
'problem.NotReady': 'Ни одна реплика не готова', 'problem.PartlyReady': 'Готовы не все реплики',
|
||||||
|
'problem.CrashLoopBackOff': 'Контейнер падает и перезапускается', 'problem.ImagePullBackOff': 'Не скачивается образ',
|
||||||
|
'problem.ErrImagePull': 'Не скачивается образ', 'problem.InvalidImageName': 'Неверное имя образа',
|
||||||
|
'problem.CreateContainerConfigError': 'Ошибка конфигурации контейнера (нет Secret или ConfigMap?)',
|
||||||
|
'problem.CreateContainerError': 'Контейнер не создаётся', 'problem.RunContainerError': 'Контейнер не запускается',
|
||||||
|
'problem.OOMKilled': 'Убит за нехватку памяти', 'problem.Restarted': 'Недавно перезапускался',
|
||||||
|
'problem.Pending': 'Под больше 5 минут ждёт запуска', 'problem.Unschedulable': 'Под некуда запустить',
|
||||||
|
'problem.Evicted': 'Под выселен с ноды', 'problem.ProgressDeadlineExceeded': 'Выкатка застряла',
|
||||||
|
'problem.LastRunFailed': 'Последний запуск не удался', 'problem.NoEndpoints': 'За сервисом нет готовых подов',
|
||||||
|
'problem.BackendMissing': 'Ведёт на несуществующий сервис', 'problem.NodeNotReady': 'Нода не готова',
|
||||||
|
'problem.MemoryPressure': 'На ноде не хватает памяти', 'problem.DiskPressure': 'На ноде не хватает диска',
|
||||||
|
'problem.PIDPressure': 'На ноде не хватает PID', 'problem.Cordoned': 'Нода закрыта для новых подов',
|
||||||
|
'usage.title': 'Нагрузка', 'usage.memory': 'Память', 'usage.request': 'запрос {v}', 'usage.limit': 'лимит {v}',
|
||||||
|
'usage.capacity': 'из {v}', 'usage.failed': 'не удалось получить нагрузку: HTTP {code}',
|
||||||
|
'usage.noMetrics': 'Текущее потребление неизвестно: API metrics.k8s.io не ответил.',
|
||||||
|
'usage.pod': 'Под', 'usage.restarts': 'Рестарты', 'usage.ready': 'готов', 'usage.notReady': 'не готов',
|
||||||
|
'events.failed': 'не удалось получить события: HTTP {code}', 'events.none': 'За последний час событий нет.',
|
||||||
|
'action.restart': 'Перезапустить', 'action.scale': 'Масштабировать', 'action.run': 'Запустить сейчас', 'action.suspend': 'Приостановить',
|
||||||
|
'action.resume': 'Возобновить', 'action.cordon': 'Закрыть для подов', 'action.uncordon': 'Открыть для подов', 'action.delete-pod': 'Удалить',
|
||||||
|
'action.deletePodHint': 'Удалить под; контроллер запустит новый',
|
||||||
|
'action.cancel': 'Отмена', 'action.replicas': 'Реплик',
|
||||||
|
'action.noRights': 'Ваша роль не разрешает здесь действий.',
|
||||||
|
'action.unavailable.noToken': 'Для действий нужен токен пользователя от DexAuthenticator, в запросе его нет.',
|
||||||
|
'action.unavailable.rejected': 'API-сервер не принял ваш токен, войдите заново.',
|
||||||
|
'action.unavailable.disabled': '',
|
||||||
|
'action.badReplicas': 'Число реплик — целое от 0 до 100.',
|
||||||
|
'action.failed': 'Не выполнено: {err}', 'action.done': '{what}: {target} — готово',
|
||||||
|
'confirm.restart': 'Все поды {target} заменяются по очереди, как при kubectl rollout restart.',
|
||||||
|
'confirm.scale': 'Сколько реплик должно работать у {target}?',
|
||||||
|
'confirm.run': 'Из шаблона {target} прямо сейчас создаётся Job, вне расписания.',
|
||||||
|
'confirm.suspend': 'Новые запуски {target} не начнутся, пока его не возобновят.',
|
||||||
|
'confirm.resume': '{target} снова запускается по расписанию.',
|
||||||
|
'confirm.cordon': 'На {target} не будут назначаться новые поды. Работающие остаются.',
|
||||||
|
'confirm.uncordon': 'На {target} снова можно назначать новые поды.',
|
||||||
|
'confirm.delete-pod': 'Под {target} в {ns} удаляется; контроллер запустит новый.',
|
||||||
|
'vulns.title': 'Уязвимости', 'vulns.none': 'В проверенных образах нет уязвимостей уровня CRITICAL и HIGH.',
|
||||||
|
'vulns.fixable': 'исправлено в новой версии: {n}', 'vulns.failed': 'не удалось получить уязвимости: HTTP {code}',
|
||||||
|
'vulns.cve': 'CVE', 'vulns.package': 'Пакет', 'vulns.version': 'Стоит → исправлено', 'vulns.noFix': 'исправления нет',
|
||||||
|
'vulns.more': 'и ещё {n}',
|
||||||
|
'plural.critical_cve': { one: '{n} критическая', few: '{n} критические', many: '{n} критических', other: '{n} критической' },
|
||||||
|
'plural.high_cve': { one: '{n} высокая', few: '{n} высокие', many: '{n} высоких', other: '{n} высокой' },
|
||||||
|
'legend.vulns': 'критические уязвимости в образах (trivy)',
|
||||||
|
'ext.restricted': 'скрыто вашими правами',
|
||||||
|
'status.noViewerToken': 'Карта показывает то, что разрешают ваши права, но в запросе нет токена пользователя. Войдите заново; если повторится — DexAuthenticator не передаёт заголовок Authorization.',
|
||||||
|
'plural.visibleNs': { one: 'виден {n} namespace', few: 'видны {n} namespace', many: 'видно {n} namespace', other: 'видны {n} namespace' },
|
||||||
|
'visibility.namespaces': 'Видны namespace, где ваша роль может смотреть поды; соседи из остальных показаны скрытыми.',
|
||||||
|
'http.summary': 'HTTP {n} запр., p95 {p95}', 'http.errors': ', {pct}% 5xx', 'http.path': 'Путь', 'http.requests': 'Запросов',
|
||||||
|
'legend.http5xx': 'есть ответы HTTP 5xx',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
<div class="brand">cluster-map</div>
|
<div class="brand">cluster-map</div>
|
||||||
<nav class="crumbs" id="crumbs" data-i18n-attr="aria-label:html.view"></nav>
|
<nav class="crumbs" id="crumbs" data-i18n-attr="aria-label:html.view"></nav>
|
||||||
<div class="topbar-actions">
|
<div class="topbar-actions">
|
||||||
|
<button type="button" id="problems-btn" class="btn problems-btn" aria-controls="problems-panel" aria-expanded="false" hidden></button>
|
||||||
<label class="toggle"><input type="checkbox" id="traffic-on" checked> <span data-i18n="html.traffic"></span></label>
|
<label class="toggle"><input type="checkbox" id="traffic-on" checked> <span data-i18n="html.traffic"></span></label>
|
||||||
<select id="traffic-window" data-i18n-attr="aria-label:html.window">
|
<select id="traffic-window" data-i18n-attr="aria-label:html.window">
|
||||||
<option value="300" data-i18n="html.min5"></option>
|
<option value="300" data-i18n="html.min5"></option>
|
||||||
@@ -24,6 +25,7 @@
|
|||||||
</select>
|
</select>
|
||||||
<button type="button" id="reload" class="btn" data-i18n="html.refresh"></button>
|
<button type="button" id="reload" class="btn" data-i18n="html.refresh"></button>
|
||||||
<span class="muted small" id="updated"></span>
|
<span class="muted small" id="updated"></span>
|
||||||
|
<span class="muted small whoami" id="whoami"></span>
|
||||||
<select id="lang" data-i18n-attr="aria-label:html.lang">
|
<select id="lang" data-i18n-attr="aria-label:html.lang">
|
||||||
<option value="en">EN</option>
|
<option value="en">EN</option>
|
||||||
<option value="ru">RU</option>
|
<option value="ru">RU</option>
|
||||||
@@ -48,8 +50,11 @@
|
|||||||
<div class="zoom" role="group" data-i18n-attr="aria-label:html.zoom">
|
<div class="zoom" role="group" data-i18n-attr="aria-label:html.zoom">
|
||||||
<button type="button" class="btn icon" id="zoom-in" data-i18n-attr="aria-label:html.zoomIn">+</button>
|
<button type="button" class="btn icon" id="zoom-in" data-i18n-attr="aria-label:html.zoomIn">+</button>
|
||||||
<button type="button" class="btn icon" id="zoom-out" data-i18n-attr="aria-label:html.zoomOut">−</button>
|
<button type="button" class="btn icon" id="zoom-out" data-i18n-attr="aria-label:html.zoomOut">−</button>
|
||||||
<button type="button" class="btn icon" id="zoom-fit" data-i18n-attr="aria-label:html.fit">⤢</button>
|
<button type="button" class="btn icon" id="zoom-fit" data-i18n-attr="aria-label:html.fit;title:html.fit">⤢</button>
|
||||||
|
<button type="button" class="btn icon" id="lock" aria-pressed="true" data-i18n-attr="aria-label:html.lock;title:html.lockedTitle"></button>
|
||||||
|
<button type="button" class="btn icon" id="export-png" data-i18n-attr="aria-label:html.exportPng;title:html.exportPng">⤓</button>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="toast" id="toast" role="status" aria-live="polite" hidden></div>
|
||||||
<div class="legend" id="legend"></div>
|
<div class="legend" id="legend"></div>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
@@ -62,15 +67,18 @@
|
|||||||
</div>
|
</div>
|
||||||
<button type="button" class="btn icon" id="d-close" data-i18n-attr="aria-label:html.close">×</button>
|
<button type="button" class="btn icon" id="d-close" data-i18n-attr="aria-label:html.close">×</button>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="d-actions" id="d-actions" role="group" data-i18n-attr="aria-label:html.actions"></div>
|
||||||
<div class="tabs" role="tablist" data-i18n-attr="aria-label:html.sections">
|
<div class="tabs" role="tablist" data-i18n-attr="aria-label:html.sections">
|
||||||
<button type="button" role="tab" data-tab="overview" aria-selected="true" data-i18n="html.overview"></button>
|
<button type="button" role="tab" data-tab="overview" aria-selected="true" data-i18n="html.overview"></button>
|
||||||
<button type="button" role="tab" data-tab="links" aria-selected="false" data-i18n="html.links"></button>
|
<button type="button" role="tab" data-tab="links" aria-selected="false" data-i18n="html.links"></button>
|
||||||
<button type="button" role="tab" data-tab="yaml" aria-selected="false">YAML</button>
|
<button type="button" role="tab" data-tab="yaml" aria-selected="false">YAML</button>
|
||||||
|
<button type="button" role="tab" data-tab="events" aria-selected="false" data-i18n="html.events"></button>
|
||||||
<button type="button" role="tab" data-tab="logs" aria-selected="false" data-i18n="html.logs"></button>
|
<button type="button" role="tab" data-tab="logs" aria-selected="false" data-i18n="html.logs"></button>
|
||||||
</div>
|
</div>
|
||||||
<section class="tab-panel" data-panel="overview" role="tabpanel"></section>
|
<section class="tab-panel" data-panel="overview" role="tabpanel"></section>
|
||||||
<section class="tab-panel" data-panel="links" role="tabpanel" hidden></section>
|
<section class="tab-panel" data-panel="links" role="tabpanel" hidden></section>
|
||||||
<section class="tab-panel" data-panel="yaml" role="tabpanel" hidden><pre class="code" id="d-yaml"></pre></section>
|
<section class="tab-panel" data-panel="yaml" role="tabpanel" hidden><pre class="code" id="d-yaml"></pre></section>
|
||||||
|
<section class="tab-panel" data-panel="events" role="tabpanel" hidden></section>
|
||||||
<section class="tab-panel" data-panel="logs" role="tabpanel" hidden>
|
<section class="tab-panel" data-panel="logs" role="tabpanel" hidden>
|
||||||
<div class="logs-bar">
|
<div class="logs-bar">
|
||||||
<select id="pod-select" data-i18n-attr="aria-label:html.pod"></select>
|
<select id="pod-select" data-i18n-attr="aria-label:html.pod"></select>
|
||||||
@@ -80,7 +88,33 @@
|
|||||||
<pre class="code" id="d-logs"></pre>
|
<pre class="code" id="d-logs"></pre>
|
||||||
</section>
|
</section>
|
||||||
</aside>
|
</aside>
|
||||||
|
|
||||||
|
<aside class="details" id="problems-panel" data-i18n-attr="aria-label:html.problems" hidden>
|
||||||
|
<div class="details-head">
|
||||||
|
<div class="details-title">
|
||||||
|
<h2 data-i18n="html.problems"></h2>
|
||||||
|
<div class="muted small" id="p-sub"></div>
|
||||||
</div>
|
</div>
|
||||||
|
<button type="button" class="btn icon" id="p-close" data-i18n-attr="aria-label:html.close">×</button>
|
||||||
|
</div>
|
||||||
|
<div class="seg p-filter" role="tablist" data-i18n-attr="aria-label:html.problemsScope">
|
||||||
|
<button type="button" role="tab" data-scope="all" aria-selected="true" data-i18n="html.scopeAll"></button>
|
||||||
|
<button type="button" role="tab" data-scope="apps" aria-selected="false" data-i18n="tree.apps"></button>
|
||||||
|
<button type="button" role="tab" data-scope="system" aria-selected="false" data-i18n="group.system"></button>
|
||||||
|
</div>
|
||||||
|
<section class="tab-panel" id="p-list"></section>
|
||||||
|
</aside>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<dialog id="confirm" closedby="any" aria-labelledby="confirm-title">
|
||||||
|
<h2 id="confirm-title"></h2>
|
||||||
|
<p id="confirm-text"></p>
|
||||||
|
<label class="confirm-input" id="confirm-input-row" hidden><span data-i18n="action.replicas"></span> <input type="number" id="confirm-input" min="0" max="100" step="1"></label>
|
||||||
|
<div class="confirm-buttons">
|
||||||
|
<button type="button" class="btn" id="confirm-cancel" data-i18n="action.cancel"></button>
|
||||||
|
<button type="button" class="btn primary" id="confirm-ok"></button>
|
||||||
|
</div>
|
||||||
|
</dialog>
|
||||||
<script src="app.js"></script>
|
<script src="app.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -37,6 +37,20 @@ properties:
|
|||||||
Also collect kubelet probes and other traffic originating from node hosts, node-to-node traffic, Cilium health checks and DNS.
|
Also collect kubelet probes and other traffic originating from node hosts, node-to-node traffic, Cilium health checks and DNS.
|
||||||
|
|
||||||
Off by default: on a typical cluster this is about three quarters of all flows, none of it maps to a workload, and filtering it at the Hubble agents keeps hubble-relay load several times lower.
|
Off by default: on a typical cluster this is about three quarters of all flows, none of it maps to a workload, and filtering it at the Hubble agents keeps hubble-relay load several times lower.
|
||||||
|
actions:
|
||||||
|
type: object
|
||||||
|
default: {}
|
||||||
|
description: Changes the map can make to the cluster.
|
||||||
|
properties:
|
||||||
|
enabled:
|
||||||
|
type: boolean
|
||||||
|
default: true
|
||||||
|
description: |
|
||||||
|
Buttons to restart a workload, scale a Deployment or StatefulSet, delete a pod, run a CronJob now or suspend it, cordon a node.
|
||||||
|
|
||||||
|
Every action runs with the viewer's own Kubernetes identity: the DexAuthenticator passes the user's token to the module, and the API server checks it against the user's RBAC. The module's service account stays read-only, a button is shown only when the user's role allows it, and the API server's audit log records the user.
|
||||||
|
|
||||||
|
Needs the `user-authn` module, or an `externalAuthentication` that puts a token the API server accepts into the `Authorization` header. When disabled, the user's token is not passed to the module at all.
|
||||||
logs:
|
logs:
|
||||||
type: object
|
type: object
|
||||||
default: {}
|
default: {}
|
||||||
@@ -95,8 +109,19 @@ properties:
|
|||||||
description: |
|
description: |
|
||||||
Access to the web interface.
|
Access to the web interface.
|
||||||
|
|
||||||
Anyone admitted here sees every namespace, the manifests of workloads, Services and Ingresses (never Secrets) and container logs, through the module's own read-only service account. Restrict access with `allowedUserGroups`.
|
With `visibility: ViewerRBAC` (the default) each viewer sees only the namespaces their own RBAC lets them list pods in; with `Everything`, anyone admitted here sees every namespace, the manifests of workloads, Services and Ingresses (never Secrets) and container logs. Either way, restrict who is admitted with `allowedUserGroups`.
|
||||||
properties:
|
properties:
|
||||||
|
visibility:
|
||||||
|
type: string
|
||||||
|
default: ViewerRBAC
|
||||||
|
enum: ["ViewerRBAC", "Everything"]
|
||||||
|
description: |
|
||||||
|
What a viewer sees.
|
||||||
|
|
||||||
|
- `ViewerRBAC` — what the viewer's own permissions allow: the whole cluster for a viewer who can list pods in all namespaces, otherwise only the namespaces where they can list pods. Container logs additionally need `get pods/log`, node cards `list nodes`. Traffic to a hidden namespace is shown as coming from or going to a hidden peer. Checked with SelfSubjectAccessReviews as the viewer and cached for a minute.
|
||||||
|
- `Everything` — every admitted viewer sees the whole cluster, read with the module's service account.
|
||||||
|
|
||||||
|
`ViewerRBAC` needs the viewer's token: the `user-authn` module, or an `externalAuthentication` that puts a token the API server accepts into the `Authorization` header.
|
||||||
externalAuthentication:
|
externalAuthentication:
|
||||||
type: object
|
type: object
|
||||||
description: |
|
description: |
|
||||||
|
|||||||
@@ -22,6 +22,16 @@ properties:
|
|||||||
Собирать также пробы kubelet и прочий трафик с хостов нод, трафик между нодами, проверки Cilium health и DNS.
|
Собирать также пробы kubelet и прочий трафик с хостов нод, трафик между нодами, проверки Cilium health и DNS.
|
||||||
|
|
||||||
По умолчанию выключено: на обычном кластере это около трёх четвертей всех потоков, ни один из них не привязан к workload'у, а отсев на агентах Hubble снижает нагрузку на hubble-relay в несколько раз.
|
По умолчанию выключено: на обычном кластере это около трёх четвертей всех потоков, ни один из них не привязан к workload'у, а отсев на агентах Hubble снижает нагрузку на hubble-relay в несколько раз.
|
||||||
|
actions:
|
||||||
|
description: Изменения, которые карта может вносить в кластер.
|
||||||
|
properties:
|
||||||
|
enabled:
|
||||||
|
description: |
|
||||||
|
Кнопки: перезапустить workload, масштабировать Deployment или StatefulSet, удалить под, запустить CronJob сейчас или приостановить его, закрыть ноду для новых подов (cordon).
|
||||||
|
|
||||||
|
Каждое действие выполняется от имени самого пользователя: DexAuthenticator передаёт модулю его токен, и API-сервер проверяет права по RBAC этого пользователя. Сервисный аккаунт модуля остаётся только на чтение, кнопка показывается, только если роль пользователя разрешает действие, а в аудит-логе API-сервера записан пользователь.
|
||||||
|
|
||||||
|
Нужен модуль `user-authn` или `externalAuthentication`, который кладёт в заголовок `Authorization` токен, принимаемый API-сервером. Если выключено, токен пользователя модулю не передаётся вовсе.
|
||||||
logs:
|
logs:
|
||||||
properties:
|
properties:
|
||||||
tailLines:
|
tailLines:
|
||||||
@@ -48,8 +58,16 @@ properties:
|
|||||||
description: |
|
description: |
|
||||||
Доступ к веб-интерфейсу.
|
Доступ к веб-интерфейсу.
|
||||||
|
|
||||||
Любой, кого сюда пустили, видит все namespace, манифесты workload'ов, Service и Ingress (Secret — никогда) и логи контейнеров — через собственную read-only-учётку модуля. Ограничивайте доступ через `allowedUserGroups`.
|
При `visibility: ViewerRBAC` (по умолчанию) каждый видит только namespace, где его собственный RBAC разрешает смотреть поды; при `Everything` любой, кого сюда пустили, видит все namespace, манифесты workload'ов, Service и Ingress (Secret — никогда) и логи контейнеров. В обоих случаях ограничивайте круг допущенных через `allowedUserGroups`.
|
||||||
properties:
|
properties:
|
||||||
|
visibility:
|
||||||
|
description: |
|
||||||
|
Что видит пользователь.
|
||||||
|
|
||||||
|
- `ViewerRBAC` — то, что разрешают его собственные права: весь кластер, если он может смотреть поды во всех namespace, иначе только namespace, где он может смотреть поды. Для логов контейнеров нужно ещё `get pods/log`, для карточек нод — `list nodes`. Трафик в скрытый namespace показывается как соединение со скрытым соседом. Проверяется через SelfSubjectAccessReview от имени пользователя, ответ кешируется на минуту.
|
||||||
|
- `Everything` — каждый допущенный пользователь видит весь кластер, чтение идёт сервисным аккаунтом модуля.
|
||||||
|
|
||||||
|
Для `ViewerRBAC` нужен токен пользователя: модуль `user-authn` или `externalAuthentication`, который кладёт в заголовок `Authorization` токен, принимаемый API-сервером.
|
||||||
externalAuthentication:
|
externalAuthentication:
|
||||||
description: |
|
description: |
|
||||||
Параметры для подключения внешней аутентификации на основе механизма Ingress NGINX [external-auth](https://kubernetes.github.io/ingress-nginx/examples/auth/external-auth/).
|
Параметры для подключения внешней аутентификации на основе механизма Ingress NGINX [external-auth](https://kubernetes.github.io/ingress-nginx/examples/auth/external-auth/).
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
{{- /* "true" when the module needs the viewer's own token in each request:
|
||||||
|
for actions (they run as the viewer) and for ViewerRBAC visibility
|
||||||
|
(what the viewer may see is asked as the viewer). */}}
|
||||||
|
{{- define "cluster_map_viewer_token" -}}
|
||||||
|
{{- or .Values.clusterMap.actions.enabled (eq .Values.clusterMap.auth.visibility "ViewerRBAC") -}}
|
||||||
|
{{- end -}}
|
||||||
@@ -96,6 +96,10 @@ spec:
|
|||||||
value: {{ printf "hubble-relay.d8-cni-cilium.svc.%s:443" .Values.global.discovery.clusterDomain | quote }}
|
value: {{ printf "hubble-relay.d8-cni-cilium.svc.%s:443" .Values.global.discovery.clusterDomain | quote }}
|
||||||
- name: HUBBLE_CA_FILE
|
- name: HUBBLE_CA_FILE
|
||||||
value: /etc/hubble-ca/ca.crt
|
value: /etc/hubble-ca/ca.crt
|
||||||
|
- name: ACTIONS_ENABLED
|
||||||
|
value: {{ .Values.clusterMap.actions.enabled | quote }}
|
||||||
|
- name: VISIBILITY
|
||||||
|
value: {{ .Values.clusterMap.auth.visibility | quote }}
|
||||||
- name: LOG_TAIL_LINES
|
- name: LOG_TAIL_LINES
|
||||||
value: {{ .Values.clusterMap.logs.tailLines | quote }}
|
value: {{ .Values.clusterMap.logs.tailLines | quote }}
|
||||||
- name: SYSTEM_NAMESPACE_PREFIXES
|
- name: SYSTEM_NAMESPACE_PREFIXES
|
||||||
|
|||||||
@@ -10,6 +10,13 @@ metadata:
|
|||||||
name: cluster-map
|
name: cluster-map
|
||||||
namespace: d8-cluster-map
|
namespace: d8-cluster-map
|
||||||
{{- include "helm_lib_module_labels" (list . (dict "app" "dex-authenticator" "name" "cluster-map")) | nindent 2 }}
|
{{- include "helm_lib_module_labels" (list . (dict "app" "dex-authenticator" "name" "cluster-map")) | nindent 2 }}
|
||||||
|
{{- if eq (include "cluster_map_viewer_token" .) "true" }}
|
||||||
|
annotations:
|
||||||
|
# user-authn then asks Dex for a token with the kubernetes audience,
|
||||||
|
# which the API server accepts: actions and ViewerRBAC visibility use it
|
||||||
|
# (as the console does)
|
||||||
|
dexauthenticator.deckhouse.io/allow-access-to-kubernetes: "true"
|
||||||
|
{{- end }}
|
||||||
spec:
|
spec:
|
||||||
applicationDomain: {{ include "helm_lib_module_public_domain" (list . "cluster-map") }}
|
applicationDomain: {{ include "helm_lib_module_public_domain" (list . "cluster-map") }}
|
||||||
{{- if and $ingressEnabled (include "helm_lib_module_https_ingress_tls_enabled" .) }}
|
{{- if and $ingressEnabled (include "helm_lib_module_https_ingress_tls_enabled" .) }}
|
||||||
@@ -20,6 +27,11 @@ spec:
|
|||||||
applicationHTTPRouteListenerSetName: cluster-map
|
applicationHTTPRouteListenerSetName: cluster-map
|
||||||
{{- end }}
|
{{- end }}
|
||||||
applicationIngressClassName: {{ include "helm_lib_module_ingress_class" . | quote }}
|
applicationIngressClassName: {{ include "helm_lib_module_ingress_class" . | quote }}
|
||||||
|
{{- if eq (include "cluster_map_viewer_token" .) "true" }}
|
||||||
|
# actions and visibility run as the viewer: the user's token goes to the
|
||||||
|
# module in the Authorization header
|
||||||
|
sendAuthorizationHeader: true
|
||||||
|
{{- end }}
|
||||||
{{- with .Values.clusterMap.auth.allowedUserEmails }}
|
{{- with .Values.clusterMap.auth.allowedUserEmails }}
|
||||||
allowedEmails:
|
allowedEmails:
|
||||||
{{- . | toYaml | nindent 2 }}
|
{{- . | toYaml | nindent 2 }}
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ metadata:
|
|||||||
{{- if and (ne (include "helm_lib_module_https_mode" .) "Disabled") .Values.clusterMap.auth.externalAuthentication }}
|
{{- if and (ne (include "helm_lib_module_https_mode" .) "Disabled") .Values.clusterMap.auth.externalAuthentication }}
|
||||||
alb.network.deckhouse.io/auth-signin: {{ replace "$host" (include "helm_lib_module_public_domain" (list . "cluster-map")) .Values.clusterMap.auth.externalAuthentication.authSignInURL | quote }}
|
alb.network.deckhouse.io/auth-signin: {{ replace "$host" (include "helm_lib_module_public_domain" (list . "cluster-map")) .Values.clusterMap.auth.externalAuthentication.authSignInURL | quote }}
|
||||||
alb.network.deckhouse.io/auth-url: {{ .Values.clusterMap.auth.externalAuthentication.authURL | quote }}
|
alb.network.deckhouse.io/auth-url: {{ .Values.clusterMap.auth.externalAuthentication.authURL | quote }}
|
||||||
alb.network.deckhouse.io/auth-response-headers: "X-Auth-Request-User,X-Auth-Request-Email"
|
alb.network.deckhouse.io/auth-response-headers: {{ ternary "X-Auth-Request-User,X-Auth-Request-Email,Authorization" "X-Auth-Request-User,X-Auth-Request-Email" (eq (include "cluster_map_viewer_token" .) "true") | quote }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- if .Values.clusterMap.auth.whitelistSourceRanges }}
|
{{- if .Values.clusterMap.auth.whitelistSourceRanges }}
|
||||||
alb.network.deckhouse.io/whitelist-source-range: {{ .Values.clusterMap.auth.whitelistSourceRanges | join "," | quote }}
|
alb.network.deckhouse.io/whitelist-source-range: {{ .Values.clusterMap.auth.whitelistSourceRanges | join "," | quote }}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ metadata:
|
|||||||
{{- if and (ne (include "helm_lib_module_https_mode" .) "Disabled") .Values.clusterMap.auth.externalAuthentication }}
|
{{- if and (ne (include "helm_lib_module_https_mode" .) "Disabled") .Values.clusterMap.auth.externalAuthentication }}
|
||||||
nginx.ingress.kubernetes.io/auth-signin: {{ .Values.clusterMap.auth.externalAuthentication.authSignInURL | quote }}
|
nginx.ingress.kubernetes.io/auth-signin: {{ .Values.clusterMap.auth.externalAuthentication.authSignInURL | quote }}
|
||||||
nginx.ingress.kubernetes.io/auth-url: {{ .Values.clusterMap.auth.externalAuthentication.authURL | quote }}
|
nginx.ingress.kubernetes.io/auth-url: {{ .Values.clusterMap.auth.externalAuthentication.authURL | quote }}
|
||||||
nginx.ingress.kubernetes.io/auth-response-headers: "X-Auth-Request-User,X-Auth-Request-Email"
|
nginx.ingress.kubernetes.io/auth-response-headers: {{ ternary "X-Auth-Request-User,X-Auth-Request-Email,Authorization" "X-Auth-Request-User,X-Auth-Request-Email" (eq (include "cluster_map_viewer_token" .) "true") | quote }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- if .Values.clusterMap.auth.whitelistSourceRanges }}
|
{{- if .Values.clusterMap.auth.whitelistSourceRanges }}
|
||||||
nginx.ingress.kubernetes.io/whitelist-source-range: {{ .Values.clusterMap.auth.whitelistSourceRanges | join "," | quote }}
|
nginx.ingress.kubernetes.io/whitelist-source-range: {{ .Values.clusterMap.auth.whitelistSourceRanges | join "," | quote }}
|
||||||
|
|||||||
@@ -27,4 +27,10 @@ spec:
|
|||||||
regex: "true"
|
regex: "true"
|
||||||
action: keep
|
action: keep
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- include "helm_lib_prometheus_rules" (list . "d8-cluster-map") }}
|
{{- /* helm_lib_prometheus_rules only renders when "operator-prometheus-crd"
|
||||||
|
is among enabled modules; on DKP 1.76 the CRDs ship with
|
||||||
|
operator-prometheus and that module name no longer exists, so the
|
||||||
|
rules were silently dropped. Gate on the API itself instead. */}}
|
||||||
|
{{- if (.Capabilities.APIVersions.Has "monitoring.coreos.com/v1/PrometheusRule") }}
|
||||||
|
{{- include "helm_lib_prometheus_rules_recursion" (list . "d8-cluster-map" "monitoring/prometheus-rules") }}
|
||||||
|
{{- end }}
|
||||||
|
|||||||
@@ -8,7 +8,10 @@ metadata:
|
|||||||
automountServiceAccountToken: true
|
automountServiceAccountToken: true
|
||||||
---
|
---
|
||||||
# Read-only on exactly what the map shows. No Secrets, no ConfigMaps, no
|
# Read-only on exactly what the map shows. No Secrets, no ConfigMaps, no
|
||||||
# write verbs. pods/log backs the Logs tab.
|
# write verbs. pods/log backs the Logs tab, events and clusteralerts the
|
||||||
|
# problem badges, vulnerabilityreports (operator-trivy) the shield on
|
||||||
|
# cards, metrics.k8s.io the Load section. Actions (restart, scale…) never
|
||||||
|
# use this account: they run with the viewer's own token.
|
||||||
apiVersion: rbac.authorization.k8s.io/v1
|
apiVersion: rbac.authorization.k8s.io/v1
|
||||||
kind: ClusterRole
|
kind: ClusterRole
|
||||||
metadata:
|
metadata:
|
||||||
@@ -21,6 +24,18 @@ rules:
|
|||||||
- apiGroups: [""]
|
- apiGroups: [""]
|
||||||
resources: ["pods/log"]
|
resources: ["pods/log"]
|
||||||
verbs: ["get"]
|
verbs: ["get"]
|
||||||
|
- apiGroups: [""]
|
||||||
|
resources: ["events"]
|
||||||
|
verbs: ["get", "list", "watch"]
|
||||||
|
- apiGroups: ["deckhouse.io"]
|
||||||
|
resources: ["clusteralerts"]
|
||||||
|
verbs: ["get", "list", "watch"]
|
||||||
|
- apiGroups: ["aquasecurity.github.io"]
|
||||||
|
resources: ["vulnerabilityreports"]
|
||||||
|
verbs: ["get", "list", "watch"]
|
||||||
|
- apiGroups: ["metrics.k8s.io"]
|
||||||
|
resources: ["pods", "nodes"]
|
||||||
|
verbs: ["get", "list"]
|
||||||
- apiGroups: ["apps"]
|
- apiGroups: ["apps"]
|
||||||
resources: ["deployments", "statefulsets", "daemonsets"]
|
resources: ["deployments", "statefulsets", "daemonsets"]
|
||||||
verbs: ["get", "list", "watch"]
|
verbs: ["get", "list", "watch"]
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
# CertManager https, cilium-hubble installed. Used by `make render`.
|
# CertManager https, cilium-hubble installed. Used by `make render`.
|
||||||
global:
|
global:
|
||||||
clusterIsBootstrapped: true
|
clusterIsBootstrapped: true
|
||||||
enabledModules: ["user-authn", "cert-manager", "vertical-pod-autoscaler", "operator-prometheus-crd", "cilium-hubble"]
|
enabledModules: ["user-authn", "cert-manager", "vertical-pod-autoscaler", "operator-prometheus", "cilium-hubble"]
|
||||||
discovery:
|
discovery:
|
||||||
clusterDomain: cluster.local
|
clusterDomain: cluster.local
|
||||||
clusterControlPlaneIsHighlyAvailable: false
|
clusterControlPlaneIsHighlyAvailable: false
|
||||||
@@ -28,10 +28,13 @@ clusterMap:
|
|||||||
retentionMinutes: 60
|
retentionMinutes: 60
|
||||||
onDemandIdleMinutes: 10
|
onDemandIdleMinutes: 10
|
||||||
collectInfraTraffic: false
|
collectInfraTraffic: false
|
||||||
|
actions:
|
||||||
|
enabled: true
|
||||||
logs:
|
logs:
|
||||||
tailLines: 500
|
tailLines: 500
|
||||||
systemNamespacePrefixes: ["d8-", "kube-"]
|
systemNamespacePrefixes: ["d8-", "kube-"]
|
||||||
auth:
|
auth:
|
||||||
|
visibility: ViewerRBAC
|
||||||
allowedUserGroups: ["admins"]
|
allowedUserGroups: ["admins"]
|
||||||
externalAuthentication:
|
externalAuthentication:
|
||||||
authURL: https://cluster-map-dex-authenticator.d8-cluster-map.svc.cluster.local/dex-authenticator/auth
|
authURL: https://cluster-map-dex-authenticator.d8-cluster-map.svc.cluster.local/dex-authenticator/auth
|
||||||
|
|||||||
Reference in New Issue
Block a user