Watch
0
0
Fork
You've already forked hyperhive
0

run grafana as a pod in the cluster

The Deployment swarm-grafana replaces the swarm-grafana container: one
replica on a static local volume that is the container's data directory,
an init container that fetches the OIDC client secret and creates the
administrator password, the datasources' bearer and the encryption key in
the secret store, and a sidecar that keeps the controller's Viewer token.
The datasources reach the stores through the gateway with that bearer, and
Grafana's /metrics asks for the stores' kind of credential, which the vhost
sends. The five host units, the journal forwarder, its client glue and the
unix-socket directory go with the container.
This commit is contained in:
atlas 2026-11-27 22:51:25 +01:00
commit 9f62e71dae

View file

@ -55,6 +55,7 @@ let
deploy.nats.enable = false;
deploy.victorialogs.enable = false;
deploy.victoriametrics.enable = false;
deploy.grafana.enable = false;
}
);
@ -449,10 +450,10 @@ let
loaded = map toString withNode.services.k3s.images;
rendered = map toString withNode.system.build.k8sImages;
in
lib.length rendered == 17
lib.length rendered == 19
&& lib.all (i: lib.elem i loaded) rendered
&& lib.elem (toString withNode.services.k3s.package.airgap-images) loaded
&& lib.length loaded == 18;
&& lib.length loaded == 20;
}
{
# The all-local host's assertions are the control: they all hold.
@ -766,13 +767,14 @@ let
lib.filter lib.isList (builtins.split "auth/jwt-k8s/role/([a-z-]+)" script)
);
# The queue's two roles share one account, and the log and metrics stores'
# responders hold one role each of their own: a case each.
# responders and Grafana hold one role each of their own: a case each.
roles = lib.filter (
r:
!(lib.hasPrefix "swarm-nats" r)
&& !(lib.elem r [
"swarm-logs-auth"
"swarm-metrics-auth"
"swarm-grafana-oidc"
])
) allRoles;
ttl = "token_ttl=1h \\\n token_max_ttl=1h";
@ -950,10 +952,11 @@ let
&& !(onCluster.systemd.services ? "container@swarm-claude-login")
&& !(onCluster.containers ? swarm-nats)
&& !(onCluster.systemd.services ? "container-nats")
# The log and metrics stores and their responders: no container, no
# unit, no socket directory. The Grafana container beside them, the CA
# The log and metrics stores, Grafana and their responders: no container,
# no unit, no socket directory. The swarm collector's container, the CA
# publisher and a runtime directory of the host's own are the controls
# that the lookups find what a host still runs.
# that the lookups find what a host still runs.
&& !(onCluster.containers ? swarm-victorialogs)
&& !(onCluster.systemd.services ? "container@swarm-victorialogs")
&& !(onCluster.systemd.services ? swarm-logs-auth)
@ -966,8 +969,17 @@ let
&& !(onCluster.systemd.services ? swarm-victoriametrics-forwarder-oidc)
&& !(onCluster.systemd.timers ? swarm-victoriametrics-forwarder-oidc)
&& !(lib.any (lib.hasPrefix "d /run/swarm-victoriametrics") onCluster.systemd.tmpfiles.rules)
&& onCluster.containers ? swarm-grafana
&& onCluster.systemd.services ? "container@swarm-grafana"
&& !(onCluster.containers ? swarm-grafana)
&& !(onCluster.systemd.services ? "container@swarm-grafana")
&& !(onCluster.systemd.services ? swarm-bao-grafana-oidc)
&& !(onCluster.systemd.services ? swarm-bao-grafana-admin)
&& !(onCluster.systemd.services ? swarm-bao-grafana-metrics-bearer)
&& !(onCluster.systemd.services ? swarm-bao-grafana-secret-key)
&& !(onCluster.systemd.services ? swarm-bao-grafana-controller-token)
&& !(onCluster.systemd.timers ? swarm-bao-grafana-oidc)
&& !(lib.any (lib.hasPrefix "d /run/swarm-grafana") onCluster.systemd.tmpfiles.rules)
&& onCluster.containers ? swarm-otel
&& onCluster.systemd.services ? "container@swarm-otel"
&& onCluster.systemd.services ? hyperhive-k8s-ca-trust
&& lib.any (lib.hasPrefix "d /run/swarm-") onCluster.systemd.tmpfiles.rules
&&
@ -977,6 +989,7 @@ let
"swarm-ci-metrics"
"swarm-claude-login"
"swarm-controller"
"swarm-grafana"
"swarm-logs-auth"
"swarm-metrics-auth"
"swarm-nats"
@ -1270,6 +1283,7 @@ let
== [
"default-deny-ingress/networkpolicy"
"swarm-controller/networkpolicy"
"swarm-grafana/networkpolicy"
"swarm-logs-auth/networkpolicy"
"swarm-metrics-auth/networkpolicy"
"swarm-nats/networkpolicy"
@ -2168,7 +2182,7 @@ let
&& ns."pod-security.kubernetes.io/warn" == "restricted"
&& ns."pod-security.kubernetes.io/audit" == "restricted"
&& ns."app.kubernetes.io/part-of" == "hyperhive"
&& lib.length specs == 11
&& lib.length specs == 12
&& lib.all (s: problems s == [ ]) specs
&& problems withHostPort != [ ]
&& problems withCaps != [ ]
@ -2328,6 +2342,10 @@ let
variable = "$swarm_metrics_store_upstream";
store = "victoriametrics";
};
grafanaLocations = allLocal.services.nginx.virtualHosts.${h.swarm.grafana.domain}.locations;
grafanaSent = lib.filterAttrs (
_: l: hasInfix "/var/lib/swarm-store-auth/grafana.conf" (l.extraConfig or "")
) grafanaLocations;
in
# The log store's four: the operator's `/`, the collector's ingest, the
# query API and the store's own metrics.
@ -2366,20 +2384,24 @@ let
&& lib.all (store: hasInfix "secret/swarm/services/${store}/store-auth" unit.script) [
"victorialogs"
"victoriametrics"
"grafana"
]
&& lib.all (store: hasInfix "/var/lib/swarm-store-auth/${store}.conf" unit.script) [
"victorialogs"
"victoriametrics"
"grafana"
]
&& lib.all (store: hasInfix "swarm-${store}-credentials" unit.script) [
"victorialogs"
"victoriametrics"
"grafana"
]
&& hasInfix "--from-file=store-auth=/var/lib/swarm-store-auth/victoriametrics.password" unit.script
&& unit.environment.BAO_CLIENT_CERT == h.deploy.bao.storeAuthClientCertFile
&& hasInfix "store-auth.pem" h.deploy.bao.storeAuthClientCertFile
&& lib.elem (rule "victorialogs") allLocal.systemd.tmpfiles.rules
&& lib.elem (rule "victoriametrics") allLocal.systemd.tmpfiles.rules
&& lib.elem (rule "grafana") allLocal.systemd.tmpfiles.rules
# nginx runs as `User=nginx`: the directory and the nginx files are its
# group's to read, written that way and fixed when they already hold the
# value, and the passwords stay root's.
@ -2393,9 +2415,19 @@ let
&& !(noNode.systemd.services ? swarm-bao-store-auth)
&& !(lib.elem (rule "victorialogs") noNode.systemd.tmpfiles.rules)
&& !(lib.elem (rule "victoriametrics") noNode.systemd.tmpfiles.rules)
&& !(lib.elem (rule "grafana") noNode.systemd.tmpfiles.rules)
&& !(lib.elem "d /var/lib/swarm-store-auth 0750 root nginx - -" noNode.systemd.tmpfiles.rules)
&& !(noNode.services.nginx.virtualHosts ? ${h.swarm.victorialogs.domain})
&& !(noNode.services.nginx.virtualHosts ? ${h.swarm.victoriametrics.domain});
&& !(noNode.services.nginx.virtualHosts ? ${h.swarm.victoriametrics.domain})
# Grafana's vhost sends it on `/metrics` alone, after the identity
# provider's check; `/` dials the same Service and carries none, which
# is Grafana's own login's. The Service's variable is the control that
# the lookup finds the location.
&& lib.attrNames grafanaSent == [ "= /metrics" ]
&& hasInfix "auth_request /__grafana_metrics_authz;" grafanaLocations."= /metrics".extraConfig
&& hasInfix "$swarm_grafana_upstream" grafanaLocations."/".proxyPass
&& !(hasInfix "swarm-store-auth" grafanaLocations."/".extraConfig)
&& !(noNode.services.nginx.virtualHosts ? ${h.swarm.grafana.domain});
}
{
# The exporter's login has the same three ends as the stores', with the
@ -2773,6 +2805,125 @@ let
&& !(spec ? initContainers)
&& !(lib.any (v: v.name == "tls") spec.volumes);
}
{
# Grafana's pod: the server on a static local volume that is its data
# directory, an init container that fetches its secrets into memory
# before it starts, and a sidecar that keeps the controller's token. The
# server's is the only port, and the pod logs in with its account's token.
# The metrics store's pod, which has neither container, is the control
# for the lookups that name them.
name = "Grafana's pod is the server on a static local volume that is its data directory, with an init container that fetches its secrets into memory and a sidecar that keeps the controller's token";
ok =
let
spec = podSpec "swarm-grafana";
named = n: lib.findFirst (c: c.name == n) null spec.containers;
main = named "swarm-grafana";
side = named "controller-token";
init = lib.head spec.initContainers;
dir = "/var/lib/nixos-containers/swarm-grafana/var/lib/grafana";
pv = objects."swarm-grafana/persistentvolume";
label = "hyperhive.darkest.space/state-grafana";
probe = {
path = "/api/health";
port = "api";
};
envOfC = c: lib.listToAttrs (map (e: lib.nameValuePair e.name e.value) c.env);
mountsOf = c: map (m: m.name) c.volumeMounts;
script = withNode.systemd.services.swarm-bao-k8s-jwt-policy.script;
credentials = lib.findFirst (v: v.name == "credentials") null spec.volumes;
policy = objects."swarm-grafana/networkpolicy".spec;
in
map (c: c.name) spec.containers == [
"swarm-grafana"
"controller-token"
]
&& map (c: c.name) spec.initContainers == [ "fetch-secrets" ]
&& !((podSpec "swarm-victoriametrics") ? initContainers)
&& main.livenessProbe.httpGet == probe
&& main.readinessProbe.httpGet == probe
&& main.startupProbe.httpGet == probe
&& main.startupProbe.failureThreshold == 30
# One port, named once: the init container and the sidecar declare none.
&&
main.ports == [
{
name = "api";
containerPort = 3000;
}
]
&& !(init ? ports)
&& !(side ? ports)
&& objects."swarm-grafana/deployment".spec.strategy.type == "Recreate"
&& pv.spec.local.path == dir
&& !(lib.hasInfix "/private/" pv.spec.local.path)
&& spec.nodeSelector == { ${label} = "true"; }
&& spec.securityContext.fsGroupChangePolicy == "OnRootMismatch"
# The secrets are in memory, and all three containers mount them where
# the settings name them. The `tmp` volume is the control that the
# lookup finds a plain one.
&&
(lib.findFirst (v: v.name == "runtime") null spec.volumes) == {
name = "runtime";
emptyDir.medium = "Memory";
}
&&
(lib.findFirst (v: v.name == "tmp") null spec.volumes) == {
name = "tmp";
emptyDir = { };
}
&& lib.all (c: lib.elem "runtime" (mountsOf c)) [
main
init
side
]
&&
(lib.findFirst (m: m.name == "runtime") null main.volumeMounts).mountPath == "/run/grafana-secrets"
# It logs in with its account's token, under the role its certificate has.
&& spec.serviceAccountName == "swarm-grafana"
&& objects."swarm-grafana/serviceaccount".metadata.name == "swarm-grafana"
&& lib.all (c: (envOfC c).BAO_JWT_ROLE == "swarm-grafana-oidc") [
main
init
side
]
&& !(lib.any (e: lib.hasPrefix "BAO_CLIENT" e.name) (init.env ++ side.env))
&& hasInfix "auth/jwt-k8s/role/swarm-grafana-oidc" script
&& hasInfix "bound_subject=system:serviceaccount:hyperhive:swarm-grafana" script
&& lib.elem "swarm-grafana-oidc" withNode.services.hyperhive.deploy.bao.k8sPrincipals
# The listener credential comes from the Secret the host publishes.
&& lib.any (s: (s.secret.name or null) == "swarm-grafana-credentials") credentials.projected.sources
# The Service and the policy: only the node in, and out to DNS, the
# store and the gateway.
&&
objects."swarm-grafana/service".spec.ports == [
{
name = "api";
protocol = "TCP";
port = 3000;
targetPort = "api";
}
]
&&
policy.policyTypes == [
"Ingress"
"Egress"
]
&& !(policy ? ingress)
&& lib.length policy.egress == 2
&&
(lib.elemAt policy.egress 1).ports == [
{
protocol = "TCP";
port = 8208;
}
{
protocol = "TCP";
port = 443;
}
]
&& lib.elem "d ${dir} - - - -" onCluster.systemd.tmpfiles.rules
&& !(lib.any (lib.hasPrefix "d ${dir} ") noNode.systemd.tmpfiles.rules);
}
{
# A named `targetPort` resolves to the first container of the pod that
# declares the name, so a second container declaring it too is reached by
@ -2826,6 +2977,7 @@ let
"hyperhive.darkest.space/state-ci-metrics=true"
"hyperhive.darkest.space/state-victorialogs=true"
"hyperhive.darkest.space/state-victoriametrics=true"
"hyperhive.darkest.space/state-grafana=true"
]
)
&&
@ -2836,6 +2988,7 @@ let
"hyperhive.darkest.space/state-ci-metrics" = "true";
"hyperhive.darkest.space/state-victorialogs" = "true";
"hyperhive.darkest.space/state-victoriametrics" = "true";
"hyperhive.darkest.space/state-grafana" = "true";
}
&& noNode.services.k3s.nodeLabel == [ ];
}