diff --git a/docs/4-implementation/k8s/runtime.md b/docs/4-implementation/k8s/runtime.md index 12e959c4f..31b6404c0 100644 --- a/docs/4-implementation/k8s/runtime.md +++ b/docs/4-implementation/k8s/runtime.md @@ -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 diff --git a/nix/host-modules/swarm-grafana.nix b/nix/host-modules/swarm-grafana.nix index 31a7b7a7e..375e1cbff 100644 --- a/nix/host-modules/swarm-grafana.nix +++ b/nix/host-modules/swarm-grafana.nix @@ -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 (`/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; }; diff --git a/nix/module-eval/grafana.nix b/nix/module-eval/grafana.nix index 35970f769..807247755 100644 --- a/nix/module-eval/grafana.nix +++ b/nix/module-eval/grafana.nix @@ -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 + # `/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 diff --git a/nix/vm-tests/k8s-services.nix b/nix/vm-tests/k8s-services.nix index f89959e9b..c1b8b2ddc 100644 --- a/nix/vm-tests/k8s-services.nix +++ b/nix/vm-tests/k8s-services.nix @@ -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