Watch
0
0
Fork
You've already forked hyperhive
0

vm-tests: a running collector keeps its cursor when it switches to the static user

This commit is contained in:
atlas 2026-11-21 15:04:35 +01:00 • committed by mara
commit c90636714d
6 changed files with 148 additions and 1 deletions

View file

@ -55,6 +55,7 @@ jobs:
- semi-spread
- k8s-swarm-images
- k8s-node
- k8s-collector-switch
steps:
- uses: actions/checkout@v3
if: inputs.test == '' || inputs.test == matrix.test

View file

@ -53,10 +53,17 @@ creates it. The user reads nothing else the file mode doesn't already let
every user read. With the node off, the unit and the collector's configuration
are the ones it has without the module.
A collector that already runs as a dynamic user when you turn the node on keeps
its state, such as the journald cursor. systemd moves the state directory out
of `/var/lib/private` for the static user, and the unit sees the files as its
own, so the receiver resumes from its cursor and ships nothing twice.
## Validation
`module-eval-k8s` checks the configuration with the option on and off, the
rendered endpoints, and the ACL's path. The VM test `k8s-node` runs a pod that
rendered endpoints, and the ACL's path. The VM test `k8s-collector-switch`
switches a running collector from a dynamic to the static user and checks that
its cursor survives. The VM test `k8s-node` runs a pod that
writes a `WARN` and an `INFO` line, and checks that the collector's user reads
the pod's log file, that the lines arrive with those severities under the
container's name, and that a metric a pod pushes arrives.

View file

@ -122,3 +122,6 @@ and delete a pod while recording its exit and how long the delete took.
test on its own, by name: the all-tests run can pass the one-hour job limit.
`k8s-node` boots a host with `services.hyperhive.deploy.k8s.enable` instead,
so the node is the module's own and not the harness's.
`k8s-collector-switch` boots a host whose collector runs as a dynamic user,
switches it to the node's configuration with `switch-to-configuration`, and
checks that the collector keeps its state as the static user.

View file

@ -137,6 +137,7 @@ let
swarm-builder-offline
k8s-swarm-images
k8s-node
k8s-collector-switch
;
};
in

View file

@ -17,5 +17,6 @@
full-setup = import ./vm-tests/full-setup.nix { inherit pkgs self; };
k8s-swarm-images = import ./vm-tests/k8s-swarm-images.nix { inherit pkgs self; };
k8s-node = import ./vm-tests/k8s-node.nix { inherit pkgs self; };
k8s-collector-switch = import ./vm-tests/k8s-collector-switch.nix { inherit pkgs self; };
semi-spread = import ./vm-tests/semi-spread.nix { inherit pkgs self; };
}

View file

@ -0,0 +1,134 @@
# `vmTests.<system>.k8s-collector-switch`: a host whose collector runs as a
# dynamic user switches to `services.hyperhive.deploy.k8s.enable`, which runs it
# as the static user the pod logs ACL names (../host-modules/k8s/pod-logs.nix).
# The collector keeps its state across the switch. The script asserts:
#
# - before the switch the collector runs as a dynamic user, its state directory
# is a symlink into /var/lib/private, and its journald receiver has written a
# cursor there;
# - after `switch-to-configuration test` into the node's configuration the
# collector's process runs as the static user, its state directory is a plain
# directory, and the cursor file is still there;
# - the receiver's `journalctl` starts `--after-cursor`, a journal entry the
# collector shipped before the switch is in its output once, not twice, and
# one logged after the switch arrives. The receiver starts at the journal's
# beginning when it holds no cursor, so a lost cursor ships every entry
# again, and a state file the new user couldn't write would stop the output.
#
# systemd moves the state directory out of /var/lib/private when the dynamic
# user goes away. The files then belong to the nobody uid on disk, which the
# unit sees as its own user's, so the test checks what the collector does, not
# who owns the files from the host's side.
#
# The node's k3s is off in the switched configuration: the switch is about the
# collector's unit, and k3s starting would only slow the test down.
{ pkgs, self }:
let
collectorOut = "/var/lib/opentelemetry-collector/out.json";
collectorUser = "opentelemetry-collector";
in
pkgs.testers.runNixOSTest {
name = "k8s-collector-switch";
globalTimeout = 900;
nodes.machine =
{ lib, ... }:
{
imports = [ self.nixosModules.default ];
services.hyperhive = {
deploy.ci-metrics.enable = true;
deploy.bao.ciMetricsClientCertFile = "/var/lib/ci-metrics/client.pem";
deploy.bao.ciMetricsClientKeyFile = "/var/lib/ci-metrics/client-key.pem";
deploy.hive-controller.hiveName = "h1";
swarm.domain = "t.local";
swarm.bao.domain = "bao.t.local";
swarm.hives.h1.domain = "h1.t.local";
};
system.stateVersion = "25.11";
# A collector that reads the journal and keeps its cursor, as the hive
# collector does, and writes to a file where the real one has the swarm's
# collector. The cursor is the state the switch must not lose.
services.opentelemetry-collector = {
enable = true;
package = pkgs.opentelemetry-collector-contrib;
settings = {
extensions.file_storage = {
directory = "/var/lib/opentelemetry-collector";
create_directory = true;
};
receivers.journald = {
start_at = "beginning";
storage = "file_storage";
};
exporters.file = {
path = collectorOut;
append = true;
};
service.extensions = [ "file_storage" ];
service.pipelines.logs = {
receivers = [ "journald" ];
exporters = [ "file" ];
};
};
};
services.hyperhive.deploy.k8s.podLogsExporters = [ "file" ];
specialisation.node.configuration = {
services.hyperhive.deploy.k8s.enable = true;
services.k3s.enable = lib.mkForce false;
};
virtualisation.memorySize = 2048;
};
testScript = ''
import shlex
machine.wait_for_unit("opentelemetry-collector.service")
def shipped(marker: str) -> int:
# The entry's MESSAGE field, not its `_CMDLINE`, which holds the
# marker too.
pattern = f'"stringValue":"{marker}"'
return int(machine.succeed(f"grep -o -F {shlex.quote(pattern)} ${collectorOut} | wc -l"))
def cursors() -> list[str]:
out = machine.succeed("find /var/lib/opentelemetry-collector/ -type f ! -name out.json")
return out.split()
with subtest("before the switch the collector is a dynamic user with a cursor in its private state"):
show = machine.succeed("systemctl show -p DynamicUser opentelemetry-collector")
assert "DynamicUser=yes" in show, show
machine.succeed("test -L /var/lib/opentelemetry-collector")
machine.succeed("logger -t switchmark marker-before-switch")
machine.wait_until_succeeds("grep -F marker-before-switch ${collectorOut}", timeout=60)
assert shipped("marker-before-switch") == 1, shipped("marker-before-switch")
before = cursors()
assert before, "the receiver wrote no cursor: the check below finds nothing"
print(before)
with subtest("after the switch it is the static user and resumes from its cursor"):
machine.succeed("/run/current-system/specialisation/node/bin/switch-to-configuration test")
machine.wait_for_unit("opentelemetry-collector.service")
show = machine.succeed("systemctl show -p User -p DynamicUser opentelemetry-collector")
assert "User=${collectorUser}" in show and "DynamicUser=no" in show, show
machine.succeed("test ! -L /var/lib/opentelemetry-collector")
after = cursors()
assert set(before) <= set(after), (before, after)
pid = machine.succeed("systemctl show -p MainPID --value opentelemetry-collector").strip()
assert machine.succeed(f"stat -c %U /proc/{pid}").strip() == "${collectorUser}"
# The receiver's `journalctl` follows from the stored cursor: with none
# it would start from the journal's beginning, which is `start_at` here.
journal = machine.succeed("journalctl -u opentelemetry-collector -o cat --boot")
assert "--after-cursor" in journal, journal
with subtest("the cursor survived: nothing shipped twice, and new entries arrive"):
machine.succeed("logger -t switchmark marker-after-switch")
machine.wait_until_succeeds("grep -F marker-after-switch ${collectorOut}", timeout=60)
assert shipped("marker-after-switch") == 1
assert shipped("marker-before-switch") == 1
'';
}