Skip to content

eBPF-based security platform

backend: give a node the union of its label sets

John Fastabende1db658d0958
diff --git a/Documentation/contribute/testing/testing.rst b/Documentation/contribute/testing/testing.rst
index 3039f8d1..e4f88994 100644
--- a/Documentation/contribute/testing/testing.rst
+++ b/Documentation/contribute/testing/testing.rst
@@ -1208,7 +1208,8 @@ is given. ``test/sbom-put`` stores two of those SBOMs with
into an ``s3://`` one, runs ``--scan`` on the first, and checks what it
refuses. ``test/labels-put`` loads label sets with ``jobs/labels-put``
by ``node_id``, by ``ak_name`` and by ``config_root``, replaces and
-clears some, and checks what it refuses. A script that exits with status 77 skips its test and says
+clears some, checks the union each node reads and the conflicts the job
+reports, and checks what it refuses. A script that exits with status 77 skips its test and says
why. ``test/sbom-scan-trivy`` is skipped unless ``trivy`` is on the host
and ``SBOM_TRIVY_CACHE`` names a Trivy cache directory with a database,
since its findings would change with every database CI could fetch.
diff --git a/Documentation/operate/backend/labels-put.rst b/Documentation/operate/backend/labels-put.rst
index c9b1be3f..b83347d8 100644
--- a/Documentation/operate/backend/labels-put.rst
+++ b/Documentation/operate/backend/labels-put.rst
@@ -62,18 +62,38 @@ is gone. An empty set, ``"labels": {}``, clears it. A node the file does
not name keeps its labels. The file is loaded in one insert, whole or
not at all.
-A node gets the set put under its ``ak_name``, else the one put under its
-``node_id``, else the one put under the ``config_root`` its newest
-keepalive reports, else none, as the view ``shirudo.node_labels`` reads
-them (:doc:`tables/status`): the most specific name wins.
+A node's labels are the union of the sets put under its ``ak_name``, its
+``node_id`` and the ``config_root`` its newest keepalive reports, as the
+view ``shirudo.node_labels`` reads them (:doc:`tables/status`). Where two
+of them give one key different values, the node takes the value of the
+more specific name, ``ak_name`` over ``node_id`` over ``config_root``,
+and the key is a conflict:
+
+.. code-block:: none
+
+ config_root role=lab image=base
+ node_id role=edge site=fra
+ labels role=edge image=base site=fra conflict: role
+
+The view ``shirudo.label_conflicts`` has one row per node and key in
+conflict, with the value the node took, the name it came from and every
+value given; query it to report them. After every load the job names each
+one on standard error, whether or not the file caused it:
+
+.. code-block:: none
+
+ labels-put: conflict: edge-01 (000b23602ab98bfb): role=edge from node_id over lab from config_root
+
+A conflict is not an error of the load: the sets stay loaded, and the
+status is 0. Resolve it by changing one of the sets.
Name the sets by ``config_root`` where you can. A config image holds
``etc/shirudo/node.labels``, so the pipeline that builds the image can
load the same labels under its root, and they are then the ones the
node's tree sync matches for as long as the node runs that image. A node
that boots another image takes that image's set, or none. Name a node by
-its ``node_id``, the name :doc:`maintain` gives it, to label it apart
-from its image, and so that the labels stay with it when it gets a new
+its ``node_id``, the name :doc:`maintain` gives it, to add labels its
+image does not have, and so that they stay with it when it gets a new
key; name it by its ``ak_name`` to give one key labels of its own. A name
no node has yet keeps its set until one has it.
@@ -98,12 +118,15 @@ refuses the whole file, with status 1 and a message naming the set, when:
Exit status
-----------
-0 when every set is loaded, or checked with ``--check``; 1 when the file
-is refused or the load fails, with nothing loaded; 2 for wrong usage.
+0 when every set is loaded, conflicts or not, or checked with
+``--check``; 1 when the file is refused or the load fails, with nothing
+loaded, or the conflicts cannot be read after the load; 2 for wrong
+usage.
Connection
----------
As :src:`tools/backend/jobs/tree-ingest`, the job runs
``clickhouse-client`` or the command ``CLICKHOUSE_CLIENT`` names. Its user
-needs ``SELECT`` and ``INSERT`` on ``shirudo.label_sets``.
+needs ``SELECT`` and ``INSERT`` on ``shirudo.label_sets`` and ``SELECT``
+on the views ``shirudo.label_conflicts`` reads.
diff --git a/Documentation/operate/backend/status.rst b/Documentation/operate/backend/status.rst
index a3e04d35..262a56c5 100644
--- a/Documentation/operate/backend/status.rst
+++ b/Documentation/operate/backend/status.rst
@@ -25,9 +25,15 @@ follows. The objects that do it are in :src:`tools/backend/sql/40-status.sql`, a
* - ``shirudo.latest``
- view
- The newest record of every node, from ``keepalive_latest``, with the node's ``node_id`` and ``trust``.
+ * - ``shirudo.node_label_items``
+ - view
+ - Every label of every node, one row per set it comes from, see :doc:`labels-put`.
* - ``shirudo.node_labels``
- view
- - The labels of every node, from ``label_sets``, see :doc:`labels-put`.
+ - The labels of every node: the union of its sets, the most specific name's value where they disagree.
+ * - ``shirudo.label_conflicts``
+ - view
+ - Per node, each key its sets give different values, and those values.
* - ``shirudo.node_ops``
- ReplacingMergeTree, ``ORDER BY (ak_name, tree_id, seq)``, no TTL
- The history of every node: what it did at each operation of its tree the backend has seen it reach, see `History`_.
diff --git a/Documentation/operate/backend/tables/reference.rst b/Documentation/operate/backend/tables/reference.rst
index c8366786..47a84ddd 100644
--- a/Documentation/operate/backend/tables/reference.rst
+++ b/Documentation/operate/backend/tables/reference.rst
@@ -223,8 +223,8 @@ shirudo.label_sets
- Read with FINAL, or through node_labels: the newest put of a name
replaces the set.
- - An empty map clears the set; node_labels then falls back to the
- other name, or none.
+ - An empty map clears the set. A node's labels are the union of its
+ sets in node_labels; label_conflicts lists the keys they disagree on.
:by: Whether node names a node_id, an ak_name, or the config_root of the nodes that booted that config image.
:node: The node_id, ak_name or config_root.
diff --git a/Documentation/operate/backend/tables/status.rst b/Documentation/operate/backend/tables/status.rst
index 64a5ffcb..41b55b41 100644
--- a/Documentation/operate/backend/tables/status.rst
+++ b/Documentation/operate/backend/tables/status.rst
@@ -30,13 +30,27 @@ Views, functions and jobs
keepalive_latest FINAL joined to nodes: the newest record of every node
with node_id and trust.
+``shirudo.node_label_items``
+ view.
+
+ label_sets FINAL joined to nodes and keepalive_latest: one row per
+ node, set and label, from the sets put under the node's ak_name, its
+ node_id and the config_root of its newest record, with rank 3, 2 and 1.
+
``shirudo.node_labels``
view.
- label_sets FINAL joined to nodes and keepalive_latest: per key, the
- labels put under its ak_name, else those put under its node_id, else
- those put under the config_root of its newest record, else none, and
- by, which of the four it is. An empty set counts as none.
+ node_label_items per node: labels, the union of its sets, where a key
+ in several takes the value of the highest rank; sources, per key, the
+ name that value was put under; conflicts, how many keys the sets gave
+ different values.
+
+``shirudo.label_conflicts``
+ view.
+
+ node_label_items per node and key whose sets give different values:
+ the value of the highest rank, by, its name, and given, every
+ (rank, by, value), highest first.
``shirudo.history_conflicts``
view.
diff --git a/tools/backend/jobs/labels-put b/tools/backend/jobs/labels-put
index aaab2fd2..7526496b 100755
--- a/tools/backend/jobs/labels-put
+++ b/tools/backend/jobs/labels-put
@@ -12,7 +12,10 @@
# labels, which replaces the one stored under that name; an empty set
# clears it. Nodes the file does not name keep theirs. The file is
# loaded whole or not at all. --check reads and checks it, and loads
-# nothing.
+# nothing. A node's labels are the union of its sets; once loaded, every
+# key whose sets give a node different values is named on standard error,
+# as shirudo.label_conflicts has it, and the most specific name's value
+# is the one the node takes.
#
# CLICKHOUSE_CLIENT the client command and its options, split at
# spaces. Default: clickhouse-client, which reads
@@ -20,8 +23,8 @@
# CLICKHOUSE_PASSWORD itself.
#
# Exit status: 0 when every set is loaded, or checked with --check; 1
-# when the file is refused or the load fails, with nothing loaded; 2 for
-# wrong usage.
+# when the file is refused or the load fails, with nothing loaded, or the
+# conflicts cannot be read after the load; 2 for wrong usage.
set -eu
@@ -87,3 +90,14 @@ if [ -s "$work/rows.jsonl" ]; then
fi
cat "$work/summary"
printf 'labels-put: loaded sets: %d\n' "$(wc -l <"$work/rows.jsonl")"
+
+# The sets are in; a conflict is the operator's to resolve, not a failed
+# load.
+client --query "SELECT concat('labels-put: conflict: ', node_id, ' (', ak_name, '): ',
+ key, '=', value, ' from ', by, ' over ',
+ arrayStringConcat(arrayMap(x -> concat(x.3, ' from ', x.2),
+ arrayFilter(x -> x.3 != value, given)), ', '))
+ FROM shirudo.label_conflicts ORDER BY node_id, ak_name, key FORMAT TSVRaw" \
+ </dev/null >"$work/conflicts" ||
+ die 'the sets are loaded, but shirudo.label_conflicts cannot be read'
+cat "$work/conflicts" >&2
diff --git a/tools/backend/sql/40-status.sql b/tools/backend/sql/40-status.sql
index d30d9594..a6fdd5e5 100644
--- a/tools/backend/sql/40-status.sql
+++ b/tools/backend/sql/40-status.sql
@@ -27,34 +27,119 @@ INNER JOIN
GROUP BY ak_name
) AS n ON n.ak_name = k.ak_name;
--- The labels of every node: the set put under its ak_name, else the one
--- put under its node_id, else the one put under the config_root of its
--- newest record, else none. by says which.
+-- Every label of every node, one row per set it comes from: the set put
+-- under the node's ak_name, the one under its node_id, and the one under
+-- the config_root of its newest record. rank orders them by how specific
+-- the name is, the ak_name highest.
+CREATE VIEW IF NOT EXISTS shirudo.node_label_items AS
+WITH
+sets AS
+(
+ SELECT toString(by) AS by, node, CAST(labels, 'Array(Tuple(String, String))') AS items
+ FROM shirudo.label_sets FINAL
+),
+named AS
+(
+ SELECT n.ak_name AS ak_name, n.node_id AS node_id, ifNull(r.config_root, '') AS config_root
+ FROM
+ (
+ SELECT ak_name, argMinMerge(node_id) AS node_id
+ FROM shirudo.nodes
+ GROUP BY ak_name
+ ) AS n
+ LEFT JOIN
+ (
+ SELECT k.ak_name AS ak_name, k.config_root AS config_root
+ FROM shirudo.keepalive_latest AS k FINAL
+ ) AS r ON r.ak_name = n.ak_name
+),
+found AS
+(
+ SELECT n.ak_name AS ak_name, n.node_id AS node_id, s.by AS by, s.items AS items
+ FROM named AS n
+ INNER JOIN (SELECT * FROM sets WHERE by = 'ak_name') AS s ON s.node = n.ak_name
+ UNION ALL
+ SELECT n.ak_name, n.node_id, s.by, s.items
+ FROM named AS n
+ INNER JOIN (SELECT * FROM sets WHERE by = 'node_id') AS s ON s.node = n.node_id
+ UNION ALL
+ SELECT n.ak_name, n.node_id, s.by, s.items
+ FROM named AS n
+ INNER JOIN (SELECT * FROM sets WHERE by = 'config_root') AS s ON s.node = n.config_root
+ WHERE n.config_root != ''
+)
+SELECT
+ f.ak_name AS ak_name,
+ f.node_id AS node_id,
+ f.by AS by,
+ multiIf(f.by = 'ak_name', 3, f.by = 'node_id', 2, 1) AS rank,
+ item.1 AS key,
+ item.2 AS value
+FROM found AS f
+ARRAY JOIN f.items AS item;
+
+-- The labels of every node: the union of its sets, where a key two sets
+-- give takes the value of the more specific name. sources says, per key,
+-- which name its value came from, and conflicts how many keys the sets
+-- gave different values; label_conflicts has them.
CREATE VIEW IF NOT EXISTS shirudo.node_labels AS
-WITH sets AS
+WITH
+won AS
+(
+ SELECT
+ i.ak_name AS ak_name,
+ i.key AS key,
+ argMax(i.value, i.rank) AS value,
+ argMax(i.by, i.rank) AS by,
+ uniqExact(i.value) > 1 AS conflict
+ FROM shirudo.node_label_items AS i
+ GROUP BY i.ak_name, i.key
+),
+sorted AS
(
- SELECT by, node, labels FROM shirudo.label_sets FINAL WHERE notEmpty(labels)
+ SELECT
+ w.ak_name AS ak_name,
+ arraySort(groupArray(tuple(w.key, w.value, w.by))) AS items,
+ countIf(w.conflict) AS conflicts
+ FROM won AS w
+ GROUP BY w.ak_name
+),
+merged AS
+(
+ SELECT
+ ak_name,
+ mapFromArrays(arrayMap(x -> x.1, items), arrayMap(x -> x.2, items)) AS labels,
+ mapFromArrays(arrayMap(x -> x.1, items), arrayMap(x -> x.3, items)) AS sources,
+ conflicts
+ FROM sorted
)
SELECT
n.ak_name AS ak_name,
n.node_id AS node_id,
- multiIf(a.node != '', a.labels, i.node != '', i.labels, c.labels) AS labels,
- multiIf(a.node != '', 'ak_name', i.node != '', 'node_id', c.node != '', 'config_root', 'none') AS by
+ m.labels AS labels,
+ m.sources AS sources,
+ m.conflicts AS conflicts
FROM
(
SELECT ak_name, argMinMerge(node_id) AS node_id
FROM shirudo.nodes
GROUP BY ak_name
) AS n
-LEFT JOIN
-(
- SELECT k.ak_name AS ak_name, ifNull(k.config_root, '') AS config_root
- FROM shirudo.keepalive_latest AS k FINAL
-) AS r ON r.ak_name = n.ak_name
-LEFT JOIN (SELECT node, labels FROM sets WHERE by = 'ak_name') AS a ON a.node = n.ak_name
-LEFT JOIN (SELECT node, labels FROM sets WHERE by = 'node_id') AS i ON i.node = n.node_id
-LEFT JOIN (SELECT node, labels FROM sets WHERE by = 'config_root') AS c
- ON c.node = r.config_root AND r.config_root != '';
+LEFT JOIN merged AS m ON m.ak_name = n.ak_name;
+
+-- The keys whose sets give a node different values: the value that won,
+-- the name it came from, and every value with the name that gave it.
+CREATE VIEW IF NOT EXISTS shirudo.label_conflicts AS
+SELECT
+ i.ak_name AS ak_name,
+ any(i.node_id) AS node_id,
+ i.key AS key,
+ argMax(i.value, i.rank) AS value,
+ argMax(i.by, i.rank) AS by,
+ arraySort(x -> -x.1, groupArray(tuple(i.rank, i.by, i.value))) AS given
+FROM shirudo.node_label_items AS i
+GROUP BY i.ak_name, i.key
+HAVING uniqExact(i.value) > 1;
-- History: what each node did at every operation of its tree that the
-- backend has seen it reach, so that checks 4 and 6 and node_vulns can
diff --git a/tools/backend/test/labels-put.expected b/tools/backend/test/labels-put.expected
index f99f0004..e94439a3 100644
--- a/tools/backend/test/labels-put.expected
+++ b/tools/backend/test/labels-put.expected
@@ -1,17 +1,20 @@
labels-put: node_id edge-01: 2 labels
labels-put: node_id edge-02: 2 labels
labels-put: ak_name 000b03: 1 label
-labels-put: config_root c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1: 1 label
+labels-put: config_root c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1: 2 labels
labels-put: loaded sets: 4
+labels-put: conflict: edge-01 (000b01): role=edge from node_id over lab from config_root
+labels-put: conflict: edge-02 (000b02): role=edge from node_id over lab from config_root
first put: status 0
labels-put: node_id edge-02: cleared
labels-put: ak_name 000b01: 3 labels
labels-put: loaded sets: 2
+labels-put: conflict: edge-01 (000b01): role=edge from ak_name over lab from config_root
second put: status 0
labels-put: node_id edge-01: 2 labels
labels-put: node_id edge-02: 2 labels
labels-put: ak_name 000b03: 1 label
-labels-put: config_root c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1: 1 label
+labels-put: config_root c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1: 2 labels
check: status 0
labels-put: the file is not a JSON value, nothing loaded
not json: status 1
@@ -49,8 +52,10 @@ labels-put: cannot read none.json, nothing loaded
no file: status 1
usage: labels-put [--check] <sets.json>|-
no argument: status 2
-core-01 000b03 {'role':'core'} ak_name
-edge-01 000b01 {'role':'edge','site':'fra','rack':'r 7'} ak_name
-edge-02 000b02 {'image':'base'} config_root
-lab-01 000b04 {'image':'base'} config_root
+core-01 000b03 {'role':'core'} {'role':'ak_name'} 0
+edge-01 000b01 {'image':'base','rack':'r 7','role':'edge','site':'fra'} {'image':'config_root','rack':'ak_name','role':'ak_name','site':'ak_name'} 1
+edge-02 000b02 {'image':'base','role':'lab'} {'image':'config_root','role':'config_root'} 0
+lab-01 000b04 {'image':'base','role':'lab'} {'image':'config_root','role':'config_root'} 0
+edge-01 000b01 role edge ak_name [(3,'ak_name','edge'),(2,'node_id','edge'),(1,'config_root','lab')]
+items 12
sets 6
diff --git a/tools/backend/test/labels-put.sh b/tools/backend/test/labels-put.sh
index c042095c..cc672ee2 100644
--- a/tools/backend/test/labels-put.sh
+++ b/tools/backend/test/labels-put.sh
@@ -5,7 +5,8 @@
# jobs/labels-put: a file of sets by node_id, by ak_name and by
# config_root loads whole, from a file and from standard input; a second
# put replaces the sets it names, clears one with an empty set, and keeps
-# those it does not name; --check loads nothing. Then what it refuses, each with a message and
+# those it does not name; each put names the keys whose sets disagree;
+# --check loads nothing. Then what it refuses, each with a message and
# nothing loaded: a file that is not JSON or not of the form, a set that
# names its node twice or not at all, two sets for one name, a key or
# value node.labels could not hold, too many labels, a missing file and
@@ -41,7 +42,7 @@ cat >"$work/first.json" <<'SETS'
{"node_id": "edge-01", "labels": {"role": "edge", "site": "fra"}},
{"node_id": "edge-02", "labels": {"role": "edge", "site": "ams"}},
{"ak_name": "000b03", "labels": {"role": "core"}},
- {"config_root": "c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1", "labels": {"image": "base"}}
+ {"config_root": "c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1c1", "labels": {"image": "base", "role": "lab"}}
]}
SETS
run 'first put' "$job" "$work/first.json"
diff --git a/tools/backend/test/labels-put.sql b/tools/backend/test/labels-put.sql
index 32906327..f6d5157f 100644
--- a/tools/backend/test/labels-put.sql
+++ b/tools/backend/test/labels-put.sql
@@ -1,11 +1,15 @@
-- SPDX-License-Identifier: BSD-2-Clause
-- Copyright (c) 2026 Authors of shirudo
--
--- The labels of each node after test/labels-put.sh: edge-01 by its key,
--- which the second put gave it over its node_id's set; edge-02, whose
--- node_id set the second put cleared, and lab-01, which has no set of its
--- own, by the config image they booted; core-01 by its key from the first
--- put, which the second left alone. The refused files loaded nothing.
+-- The labels of each node after test/labels-put.sh, the union of its
+-- sets. edge-01 has its key's set, which the second put gave it, its
+-- node_id's, and its config image's, whose role=lab loses to the key's
+-- role=edge: a conflict. edge-02, whose node_id set the second put
+-- cleared, and lab-01, which has no set of its own, have their config
+-- image's. core-01 has its key's set from the first put, which the second
+-- left alone. The refused files loaded nothing.
-SELECT node_id, ak_name, labels, by FROM shirudo.node_labels ORDER BY node_id;
+SELECT node_id, ak_name, labels, sources, conflicts FROM shirudo.node_labels ORDER BY node_id;
+SELECT node_id, ak_name, key, value, by, given FROM shirudo.label_conflicts ORDER BY node_id, key;
+SELECT 'items', count() FROM shirudo.node_label_items;
SELECT 'sets', count() FROM shirudo.label_sets;
diff --git a/tools/backend/test/schema.expected b/tools/backend/test/schema.expected
index d8108151..313e31c2 100644
--- a/tools/backend/test/schema.expected
+++ b/tools/backend/test/schema.expected
@@ -13,8 +13,10 @@ shirudo keepalive_latest ReplacingMergeTree ak_name
shirudo keepalive_latest_mv MaterializedView
shirudo keepalive_mv MaterializedView
shirudo kernel_vulns ReplacingMergeTree vuln_id, introduced
+shirudo label_conflicts View
shirudo label_sets ReplacingMergeTree by, node
shirudo latest View
+shirudo node_label_items View
shirudo node_labels View
shirudo node_ops ReplacingMergeTree ak_name, tree_id, seq
shirudo node_ops_mv MaterializedView