Watch
0
0
Fork
You've already forked hyperhive
0

grafana pod: open the database the earlier container left

The pod's volume is the earlier container's data directory, and the NixOS
grafana module keeps its database at `<dataDir>/data/grafana.db`. The pod set
`paths.data` to the directory itself, so it opened a new, empty
`grafana.db` beside the carried one, and the secret-key guard read that new
file. Datasources with secrets, API tokens, hand-made dashboards, users and
teams of a switched host were orphaned.

`paths.data` and the guard's database path are now `<dataDir>/data`. The VM
test's earlier Grafana keeps its database in the NixOS layout, and the test
asserts the carried file is the only `grafana.db` in the volume.

Closes #6979
This commit is contained in:
maia 2026-11-28 15:45:38 +01:00
commit fe6221138c
4 changed files with 41 additions and 8 deletions

View file

@ -605,8 +605,10 @@ dials the pod's ClusterIP Service by name, as the stores' vhosts do, so
- **State.** Grafana's data directory on the host is
`/var/lib/nixos-containers/swarm-grafana/var/lib/grafana`: the claim
`swarm-grafana-store` binds a static local volume on it, so the database the
container kept is the pod's, with its dashboards, annotations and users. The
node label is `hyperhive.darkest.space/state-grafana`
container kept is the pod's, with its dashboards, annotations and users.
Grafana's `paths.data` is the `data` directory in it, where the NixOS module
keeps `grafana.db` (a `paths.data` of the volume itself opens a new, empty
database beside the carried one). The node label is `hyperhive.darkest.space/state-grafana`
([node labels](node.md#node-labels)), and the pod's `fsGroup` is `65532` with
the `OnRootMismatch` policy, as for the stores.
- **Who reaches what.** The namespace's `default-deny-ingress` and the pod's own

View file

@ -379,7 +379,13 @@ let
legacyKeyDir = "/var/lib/nixos-containers/swarm-grafana/var/lib/grafana-secret";
legacyKeyClaim = "${service}-legacy-key";
legacyKeyMount = "/legacy-secret-key";
databasePath = "${dataDir}/grafana.db";
# Grafana's `paths.data`, and so where its database is. It's the NixOS
# grafana module's layout (`<dataDir>/data/grafana.db`), which is the earlier
# container's, because the pod's volume is that container's data directory: a
# `paths.data` of `dataDir` itself would open a new, empty database beside the
# carried one.
dataPath = "${dataDir}/data";
databasePath = "${dataPath}/grafana.db";
grafanaSecretKey = import ./lib/grafana-secret-key.nix { };
# The built-in `admin` account's password. Grafana's API takes basic auth
@ -683,7 +689,7 @@ let
};
paths = {
data = dataDir;
data = dataPath;
plugins = pluginDir;
provisioning = provisioningDir;
};

View file

@ -304,7 +304,7 @@ let
r = rendered grafanaRemoteAuthelia;
init = r.scripts.fetch;
at = needle: lib.stringLength (lib.head (lib.splitString needle init));
call = "grafana_secret_key secret/swarm/services/swarm-grafana/secret-key /legacy-secret-key/secret_key /var/lib/grafana/grafana.db openssl rand -hex 32 || exit 1";
call = "grafana_secret_key secret/swarm/services/swarm-grafana/secret-key /legacy-secret-key/secret_key /var/lib/grafana/data/grafana.db openssl rand -hex 32 || exit 1";
# The helper's text is part of the script: store, then file, then
# the check of the database, then the mint.
readStore = "bao kv get -field=value \"$leaf\"";
@ -336,6 +336,22 @@ let
lib.filter (lib.hasPrefix "r ") grafanaRemoteAuthelia.systemd.tmpfiles.rules
));
}
{
# The pod's volume is the earlier container's data directory, and the
# NixOS grafana module keeps that container's database at
# `<dataDir>/data/grafana.db`. A `paths.data` of the directory itself
# opens a new, empty database beside it: the datasources with secrets,
# the tokens and the dashboards made by hand are not in it. The guard
# over the key reads the same file Grafana opens.
name = "grafana opens the database the earlier container left, and the key's guard reads that file";
ok =
let
r = rendered grafanaRemoteAuthelia;
in
r.settings.paths.data == "/var/lib/grafana/data"
&& !(r.settings ? database)
&& lib.hasInfix " ${r.settings.paths.data}/grafana.db openssl rand -hex 32" r.scripts.fetch;
}
{
# swarm-controller reads alerts with this token, so the account it
# belongs to is created and kept a Viewer, the token lands at the path

View file

@ -604,6 +604,8 @@ pkgs.testers.runNixOSTest {
mkdir -p ${grafanaDir}
chown ${legacyGrafanaUser}:${legacyGrafanaUser} ${grafanaDir}
chmod 0700 ${grafanaDir}
mkdir -p ${grafanaDir}/data
chown ${legacyGrafanaUser}:${legacyGrafanaUser} ${grafanaDir}/data
mkdir -p ${legacyKeyDir}
chown ${legacyGrafanaUser}:${legacyGrafanaUser} ${legacyKeyDir}
chmod 0700 ${legacyKeyDir}
@ -611,7 +613,9 @@ pkgs.testers.runNixOSTest {
RuntimeDirectory = "grafana-legacy";
};
environment = {
GF_PATHS_DATA = grafanaDir;
# The NixOS grafana module's layout, which is what the earlier
# container left on a real host.
GF_PATHS_DATA = "${grafanaDir}/data";
GF_PATHS_LOGS = "/run/grafana-legacy";
GF_PATHS_PLUGINS = "/run/grafana-legacy/plugins";
GF_PATHS_PROVISIONING = "/run/grafana-legacy/provisioning";
@ -660,7 +664,7 @@ pkgs.testers.runNixOSTest {
fi
kill -TERM "$pid"
wait "$pid" || true
test -s ${grafanaDir}/grafana.db
test -s ${grafanaDir}/data/grafana.db
test -s ${legacyKeyFile}
'';
};
@ -1812,7 +1816,12 @@ pkgs.testers.runNixOSTest {
pvc = json.loads(kube(machine, f"get pvc {GRAFANA}-store -o json"))
assert pvc["status"]["phase"] == "Bound" and pvc["spec"]["volumeName"] == pv["metadata"]["name"], pvc
assert machine.succeed(f"stat -c %g {GRAFANA_DIR}").strip() == "65532"
assert machine.succeed(f"find {GRAFANA_DIR} -name grafana.db -user ${legacyGrafanaUser} -print -quit").strip() != ""
# The earlier container's database is where the NixOS module keeps it
# (`data/grafana.db`), and it is the only one: a pod that opened its own
# beside it would start empty.
dbs = machine.succeed(f"find {GRAFANA_DIR} -name grafana.db").split()
assert dbs == [f"{GRAFANA_DIR}/data/grafana.db"], dbs
assert machine.succeed(f"stat -c %U {dbs[0]}").strip() == "${legacyGrafanaUser}"
status = json.loads(kube(machine, f"get pod {grafana_pod} -o json"))
main = next(c for c in status["spec"]["containers"] if c["name"] == GRAFANA)
assert main["startupProbe"]["failureThreshold"] == 30, main