v0.8.0: problem badges, actions as the viewer, load, events, lock

Problems: cards, namespace and node boxes and the tree carry a yellow or
red badge, computed on the server from object state (crash loops, image
pulls, pending and unschedulable pods, readiness older than 2 minutes, a
stuck rollout, a failed CronJob run, Services without endpoints,
Ingresses to missing Services, node conditions), recent Warning events
(notable reasons at once, others after five repeats) and Deckhouse
ClusterAlerts mapped by their labels. A Problems panel lists them all.
Checked against the live cluster: upmeter's synthetic probe pods and
ingress-nginx's fakeservice Ingresses are excluded, they fail on purpose.

Actions: restart, scale, run a CronJob now, suspend/resume, delete a
pod, cordon/uncordon. They run with the viewer's bearer token from the
DexAuthenticator (sendAuthorizationHeader), buttons follow a
SelfSubjectAccessReview, the service account stays read-only. Cross-site
writes are refused by Sec-Fetch-Site/Origin/content type. Scale retries
on conflict: right after a restart a single Get+Update got 409.

Load (metrics.k8s.io against requests/limits/allocatable, per-pod table)
and an Events tab. Cards are locked until the padlock is opened; the map
exports to PNG; the page refreshes badges every minute in place.

Performance, measured on the live cluster:
- layout of big views 3-4x faster: equal-size cards with no edges are
  laid out on the grid ELK would produce (same sizes, checked against
  ELK on a frozen node view), skipping ~8 ms per ELK call;
- hubble-relay gets ~20% fewer flows: the to-overlay copy of pod-to-pod
  traffic is dropped at the agents; 331/331 directions unchanged;
- static files carry ETags, a reload revalidates instead of pulling
  ~2 MB of vendored scripts.

KUBECONFIG is honoured for running the binary on a workstation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Виталий Чефанов
2026-09-24 17:25:36 +03:00
co-authored by Claude Opus 5.5
parent 8b8fe341d5
commit 3cdc2583ee
36 changed files with 2909 additions and 83 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
name: cluster-map name: cluster-map
version: 0.7.2 version: 0.8.0
dependencies: dependencies:
- name: deckhouse_lib_helm - name: deckhouse_lib_helm
version: 1.72.24 version: 1.72.24
+9 -4
View File
@@ -25,13 +25,18 @@ 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/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`), never the module's service account.
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 +54,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.8.0 # build, push bundle and release, register, channel alpha
make release VERSION=v0.7.0 CHANNEL=stable make release VERSION=v0.8.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):
+13
View File
@@ -1,3 +1,16 @@
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: v0.7.2:
fixes: 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 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.
+11
View File
@@ -40,3 +40,14 @@ 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, and the user's token is no longer passed to the module.
+11
View File
@@ -40,3 +40,14 @@ spec:
``` ```
Память растёт вместе с глубиной хранения — следите за `cluster_map_traffic_tracked_tuples` и алертом `ClusterMapTrackedTuplesHigh`. Память растёт вместе с глубиной хранения — следите за `cluster_map_traffic_tracked_tuples` и алертом `ClusterMapTrackedTuplesHigh`.
## Карта только для просмотра
```yaml
spec:
settings:
actions:
enabled: false
```
Кнопок действий нет, и токен пользователя модулю больше не передаётся.
+36 -4
View File
@@ -14,10 +14,39 @@ 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, recent restarts and OOM kills, 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 and is listed without a badge before that, so a single failed probe during a restart does not colour the map.
- **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.
## 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 +55,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 |
@@ -44,9 +75,9 @@ 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 and Deckhouse `clusteralerts`, `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`). This means **anyone admitted to the web interface sees every namespace and can read container logs**, whatever their own RBAC permissions are; what they can change is limited by their own RBAC. Restrict access with `auth.allowedUserGroups` (and optionally `auth.allowedUserEmails` and `auth.whitelistSourceRanges`).
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 +93,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. - Reading is all-or-nothing, as described in [Security](#security). Showing each user only what their RBAC allows to read is planned; actions already follow the user's RBAC.
- Alerts need the `prometheus` module, which creates `ClusterAlert` objects. Without it the map shows problems from object state and events only.
- Traffic is L3/L4. HTTP-level details (paths, status codes) need Cilium L7 visibility and are not shown yet. - Traffic is L3/L4. HTTP-level details (paths, status codes) need Cilium L7 visibility and are not shown yet.
+36 -4
View File
@@ -14,10 +14,39 @@ 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` и подобном, недавние перезапуски и убийства за нехватку памяти, поды в ожидании или без места для запуска, workload'ы, у которых больше 2 минут не готовы все или часть реплик, застрявшая выкатка, CronJob с неудачным последним запуском, Service без готовых подов, Ingress на несуществующий Service, нода не готова или ей не хватает памяти, диска или PID.
- **Warning-события** за последние 30 минут, сгруппированные по объекту и причине. Сбои планирования, томов и back-off дают значок сразу; любой другой причине нужно пять повторов, до этого она в списке без значка — одна проваленная проба во время рестарта не красит карту.
- **Алерты Prometheus**, которые Deckhouse зеркалирует в объекты `ClusterAlert`. Алерт привязывается по меткам (`deployment`, `statefulset`, `daemonset`, `pod`, `service`, `ingress`, `node`, `namespace`); алерт без объекта и namespace попадает в список кластера. `severity_level` 1–3 — ошибка, 4–6 — предупреждение, 7–9 — информация.
Записи уровня «для сведения» (нода закрыта для новых подов, одиночное обычное предупреждение) есть в списке, но значка не дают. Проблемы пересчитываются при каждом запросе карты, открытая страница обновляет их раз в минуту.
## Действия
При `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 +55,8 @@ Workload'ы под нестандартными контроллерами то
Пробы kubelet и прочий трафик с хостов нод, трафик между нодами, проверки Cilium health и DNS отсекаются на агентах и до модуля не доходят. На обычном кластере это около трёх четвертей всех потоков, и ни один из них не привязан к workload'у. Чтобы всё же собирать их, включите `traffic.collectInfraTraffic`. Пробы kubelet и прочий трафик с хостов нод, трафик между нодами, проверки Cilium health и DNS отсекаются на агентах и до модуля не доходят. На обычном кластере это около трёх четвертей всех потоков, и ни один из них не привязан к workload'у. Чтобы всё же собирать их, включите `traffic.collectInfraTraffic`.
Трафик между подами на разных нодах трассируется дважды: при выходе с ноды отправителя (`to-overlay`) и при доставке в под получателя (`to-endpoint`). Первая копия тоже отсекается на агентах, если получатель — под; это убирает около пятой части оставшихся потоков, ни одно число при этом не меняется.
Режимы сбора — параметр `traffic.mode`: Режимы сбора — параметр `traffic.mode`:
| Режим | Цена | Окна трафика | | Режим | Цена | Окна трафика |
@@ -44,9 +75,9 @@ 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, `get` и `list` на pods и nodes из `metrics.k8s.io` и `get` на `pods/log`. Secret и ConfigMap он не читает, прав на запись нет. Изменения идут через [действия](#действия), которые выполняются от имени пользователя.
Отсюда следует: **любой, кого пустили в веб-интерфейс, видит все namespace и может читать логи контейнеров** — независимо от собственных прав RBAC. Ограничивайте доступ через `auth.allowedUserGroups` (а также `auth.allowedUserEmails` и `auth.whitelistSourceRanges`). Отсюда следует: **любой, кого пустили в веб-интерфейс, видит все namespace и может читать логи контейнеров** — независимо от собственных прав RBAC; изменить он может только то, что разрешает его RBAC. Ограничивайте доступ через `auth.allowedUserGroups` (а также `auth.allowedUserEmails` и `auth.whitelistSourceRanges`).
Фронтенд отдаётся со строгой Content Security Policy: только собственные скрипты, стили и API, без inline-кода, без встраивания во фреймы. Фронтенд отдаётся со строгой Content Security Policy: только собственные скрипты, стили и API, без inline-кода, без встраивания во фреймы.
@@ -62,5 +93,6 @@ Workload'ы под нестандартными контроллерами то
## Ограничения ## Ограничения
- Одна реплика: история трафика живёт в процессе. Вторая реплика держала бы свою историю и удвоила бы нагрузку на hubble-relay. - Одна реплика: история трафика живёт в процессе. Вторая реплика держала бы свою историю и удвоила бы нагрузку на hubble-relay.
- Доступ по принципу «всё или ничего», см. раздел [Безопасность](#безопасность). Показ каждому пользователю только того, что разрешает его RBAC, — в планах. - Чтение по принципу «всё или ничего», см. раздел [Безопасность](#безопасность). Показ каждому пользователю только того, что его RBAC разрешает читать, — в планах; действия уже следуют RBAC пользователя.
- Для алертов нужен модуль `prometheus`, он создаёт объекты `ClusterAlert`. Без него карта показывает проблемы только по состоянию объектов и событиям.
- Трафик на уровне L3/L4. Подробности HTTP (пути, коды ответа) требуют L7-видимости Cilium и пока не показываются. - Трафик на уровне L3/L4. Подробности HTTP (пути, коды ответа) требуют L7-видимости Cilium и пока не показываются.
+31 -7
View File
@@ -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 {
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)
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.
+1
View File
@@ -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,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,11 @@ 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
} }
// Load reads and validates the configuration from the environment. // Load reads and validates the configuration from the environment.
@@ -83,6 +88,9 @@ 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 c.TrafficMode { switch c.TrafficMode {
case TrafficContinuous, TrafficOnDemand, TrafficDisabled: case TrafficContinuous, TrafficOnDemand, TrafficDisabled:
default: default:
+90 -6
View File
@@ -21,22 +21,41 @@ 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"}
// 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
Namespaces corelisters.NamespaceLister Namespaces corelisters.NamespaceLister
Pods corelisters.PodLister Pods corelisters.PodLister
Services corelisters.ServiceLister Services corelisters.ServiceLister
@@ -48,16 +67,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 +97,20 @@ 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 && served(client, ClusterAlerts) {
df = dynamicinformer.NewDynamicSharedInformerFactory(dyn, 0)
c.Alerts = df.ForResource(ClusterAlerts).Lister()
} else {
slog.Info("ClusterAlert resource is not served; alerts 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 +119,54 @@ 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
}
// 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,172 @@
/*
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
}
func (s *Server) handleMe(w http.ResponseWriter, r *http.Request) {
me := Me{ActionsEnabled: s.cfg.ActionsEnabled && s.asViewer != nil}
token := viewerToken(r)
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,314 @@
/*
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()
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, "/")
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
}
@@ -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,7 @@ 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/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 +51,52 @@ 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 actions are off
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} func New(cfg config.Config, static fs.FS, asViewer actions.ClientFactory) *Server {
return &Server{cfg: cfg, static: static, etags: etags(static), asViewer: asViewer}
}
// 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 +122,12 @@ 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/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)
} }
@@ -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)
} }
@@ -41,6 +41,7 @@ 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"`
} }
// Edge is a configuration or placement link between two nodes. // Edge is a configuration or placement link between two nodes.
@@ -55,6 +56,12 @@ 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"`
} }
// 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
@@ -163,6 +170,16 @@ 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, cronjobs: cronjobs,
services: services, ingresses: ingresses, nodes: hostNodes, events: events, alerts: c.AlertList(),
})
g.AlertsAvailable = c.Alerts != 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 +479,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,509 @@
/*
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"
"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
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
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
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 {
serviceProblems(s, svc, podsByNs[svc.Namespace])
}
services := map[string]bool{}
for _, svc := range in.services {
services[svc.Namespace+"/"+svc.Name] = true
}
for _, ing := range in.ingresses {
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)
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 && now().Sub(t.FinishedAt.Time) < restartWindow {
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) {
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
}
}
s.add("service/"+svc.Namespace+"/"+svc.Name, Problem{Severity: Warning, Source: "state", Reason: "NoEndpoints"})
}
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 ""
}
// 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) {
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 := int(e.Count)
if e.Series != nil {
count = int(e.Series.Count)
}
count = max(count, 1)
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]
if !eventNotable[p.Reason] && p.Count < eventRepeats {
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"})
}
}
// 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.
func syntheticProbe(ns, name string) bool {
return ns == "d8-upmeter" && strings.HasPrefix(name, "upmeter-probe-")
}
// 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})
}
@@ -0,0 +1,240 @@
/*
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 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: "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))}},
}}},
},
{ // 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 _, 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 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)
}
}
}
@@ -48,8 +48,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,8 +59,24 @@ 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"},
},
} }
// Cilium datapath codes (pkg/monitor/api): message type "trace" and its
// observation point "to-overlay".
const (
monitorTrace = 4
traceToOverlay = 4
)
var flowFields = []string{"time", "verdict", "IP", "l4", "source", "destination", "source_names", "destination_names", "node_name"} var flowFields = []string{"time", "verdict", "IP", "l4", "source", "destination", "source_names", "destination_names", "node_name"}
// 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.
+103 -1
View File
@@ -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;
} }
@@ -197,6 +199,106 @@ 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; }
/* 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; }
} }
+482 -34
View File
@@ -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 }) {
const key = [kind, title, sub, health, ghost, w, h].join('|'); const key = [kind, title, sub, health, ghost, w, h, badge].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 - 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,7 +103,7 @@ 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}</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;
@@ -159,6 +163,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 +247,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 +269,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 +327,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 +500,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: {
@@ -487,7 +530,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) }),
}, },
}; };
} }
@@ -509,7 +552,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;
@@ -552,7 +596,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));
@@ -636,6 +680,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 +701,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 +747,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 +777,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;
@@ -876,6 +946,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)) {
@@ -944,6 +1015,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;
} }
@@ -953,8 +1025,12 @@ function renderLegend() {
el('span', {}, el('span', { class: 'line drop' }), t('legend.drop')), el('span', {}, el('span', { class: 'line drop' }), t('legend.drop')),
el('span', {}, t('legend.ghost')), el('span', {}, t('legend.ghost')),
el('span', {}, t('legend.notCollected')), el('span', {}, t('legend.notCollected')),
...badgeLegend(),
); );
} }
function badgeLegend() {
return ['warning', 'critical'].map((sev) => el('span', {}, el('span', { class: 'pbadge ' + sev, 'aria-hidden': 'true' }, '!'), t('legend.' + sev)));
}
/* ---------- details panel ---------- */ /* ---------- details panel ---------- */
const yamlCache = new Map(); const yamlCache = new Map();
@@ -968,10 +1044,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));
@@ -1008,28 +1085,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 = '';
@@ -1037,11 +1101,142 @@ 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'))));
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);
}
/* ---------- 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);
}
/* ---------- 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')
@@ -1137,6 +1332,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(); });
@@ -1161,6 +1600,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); }
} }
@@ -1182,4 +1623,11 @@ 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;
const n = detailsId && G.byId.get(detailsId);
if (n) renderActions(n);
}).catch(() => {});
load(); load();
+106
View File
@@ -57,6 +57,59 @@ 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.',
}, },
ru: { ru: {
'kind.pod': 'Контроллер', 'kind.external': 'Внешний', 'folder.pod': 'Другие контроллеры', 'kind.pod': 'Контроллер', 'kind.external': 'Внешний', 'folder.pod': 'Другие контроллеры',
@@ -111,6 +164,59 @@ 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} удаляется; контроллер запустит новый.',
}, },
}; };
+35 -1
View File
@@ -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>
+14
View File
@@ -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: {}
+10
View File
@@ -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:
+2
View File
@@ -96,6 +96,8 @@ 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: 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
+5
View File
@@ -20,6 +20,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 .Values.clusterMap.actions.enabled }}
# actions run as the viewer: the user's token goes to the module in the
# Authorization header (and the token's audience includes kubernetes)
sendAuthorizationHeader: true
{{- end }}
{{- with .Values.clusterMap.auth.allowedUserEmails }} {{- with .Values.clusterMap.auth.allowedUserEmails }}
allowedEmails: allowedEmails:
{{- . | toYaml | nindent 2 }} {{- . | toYaml | nindent 2 }}
+1 -1
View File
@@ -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" .Values.clusterMap.actions.enabled | 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 }}
+1 -1
View File
@@ -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" .Values.clusterMap.actions.enabled | 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 }}
+12 -1
View File
@@ -8,7 +8,9 @@ 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, 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 +23,15 @@ 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: ["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
View File
@@ -28,6 +28,8 @@ 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-"]