Watch
0
0
Fork
You've already forked hyperhive
0

run the metrics store and its auth responders as pods

This commit is contained in:
atlas 2026-11-27 09:11:27 +01:00 • committed by maia
commit 07a004fad1

View file

@ -53,6 +53,7 @@ let
deploy.swarm-controller.enable = false;
deploy.nats.enable = false;
deploy.victorialogs.enable = false;
deploy.victoriametrics.enable = false;
}
);
@ -376,18 +377,18 @@ let
{
# The images are the ones the manifests run, one per rendered service, the
# controller's listener leaf, the queue's three more (server, exporter,
# init), the log store's and its two responders': the fourteen are the
# control that the list is not empty.
# init), the log store's and its two responders', and the metrics store's and
# its two: the seventeen are the control that the list is not empty.
name = "the node loads the image of every service the manifests run, and k3s's own";
ok =
let
loaded = map toString withNode.services.k3s.images;
rendered = map toString withNode.system.build.k8sImages;
in
lib.length rendered == 14
lib.length rendered == 17
&& lib.all (i: lib.elem i loaded) rendered
&& lib.elem (toString withNode.services.k3s.package.airgap-images) loaded
&& lib.length loaded == 15;
&& lib.length loaded == 18;
}
{
# The all-local host's assertions are the control: they all hold.
@ -618,9 +619,16 @@ let
allRoles = map lib.head (
lib.filter lib.isList (builtins.split "auth/jwt-k8s/role/([a-z-]+)" script)
);
# The queue's two roles share one account, and the log store's
# responders hold one role of their own: a case each.
roles = lib.filter (r: !(lib.hasPrefix "swarm-nats" r) && r != "swarm-logs-auth") allRoles;
# 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.
roles = lib.filter (
r:
!(lib.hasPrefix "swarm-nats" r)
&& !(lib.elem r [
"swarm-logs-auth"
"swarm-metrics-auth"
])
) allRoles;
ttl = "token_ttl=1h \\\n token_max_ttl=1h";
roleText = name: ''
bao write auth/jwt-k8s/role/${name} \
@ -796,16 +804,26 @@ let
&& !(onCluster.systemd.services ? "container@swarm-claude-login")
&& !(onCluster.containers ? swarm-nats)
&& !(onCluster.systemd.services ? "container-nats")
# The log store and its responders: no container, no unit, no socket
# directory. The metrics store beside it is the control that the lookups
# find what a host still runs.
# The log and metrics stores and their responders: no container, no
# unit, no socket directory. The Grafana container beside them, the CA
# publisher and a runtime directory of the host's own are the controls
# 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)
&& !(onCluster.systemd.services ? swarm-logs-grafana-auth)
&& !(lib.any (lib.hasPrefix "d /run/swarm-victorialogs ") onCluster.systemd.tmpfiles.rules)
&& onCluster.containers ? swarm-victoriametrics
&& onCluster.systemd.services ? swarm-metrics-auth
&& !(onCluster.containers ? swarm-victoriametrics)
&& !(onCluster.systemd.services ? "container@swarm-victoriametrics")
&& !(onCluster.systemd.services ? swarm-metrics-auth)
&& !(onCluster.systemd.services ? swarm-metrics-agent-auth)
&& !(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.systemd.services ? hyperhive-k8s-ca-trust
&& lib.any (lib.hasPrefix "d /run/swarm-") onCluster.systemd.tmpfiles.rules
&&
manifestNames onCluster == [
"coredns-custom"
@ -814,11 +832,13 @@ let
"swarm-claude-login"
"swarm-controller"
"swarm-logs-auth"
"swarm-metrics-auth"
"swarm-nats"
"swarm-notify-router"
"swarm-review-classifier"
"swarm-stt"
"swarm-victorialogs"
"swarm-victoriametrics"
]
&&
map (o: o.kind) onCluster.services.k3s.manifests.hyperhive-namespace.content == [
@ -854,6 +874,15 @@ let
"Service"
"ServiceAccount"
]
&&
lib.sort lib.lessThan (map (o: o.kind) onCluster.services.k3s.manifests.swarm-metrics-auth.content)
== [
"Deployment"
"NetworkPolicy"
"Service"
"Service"
"ServiceAccount"
]
&&
lib.sort lib.lessThan (map (o: o.kind) onCluster.services.k3s.manifests.swarm-victorialogs.content)
== [
@ -862,6 +891,16 @@ let
"PersistentVolume"
"PersistentVolumeClaim"
"Service"
]
&&
lib.sort lib.lessThan (
map (o: o.kind) onCluster.services.k3s.manifests.swarm-victoriametrics.content
) == [
"Deployment"
"NetworkPolicy"
"PersistentVolume"
"PersistentVolumeClaim"
"Service"
];
}
{
@ -1077,18 +1116,20 @@ let
ports = [ (tcp 4222) ];
}
]
# The namespace's default, the controller (its listener), the log store
# and its responders (listeners), the queue and this pod have a policy:
# the others have no egress and no listener, and rely on the default.
# The namespace's default, the controller (its listener), the log and metrics
# stores and their responders (listeners), the queue and this pod have a
# policy: the others have no egress and no listener, and rely on the default.
&&
lib.sort lib.lessThan (lib.filter (n: lib.hasSuffix "/networkpolicy" n) (lib.attrNames objects))
== [
"default-deny-ingress/networkpolicy"
"swarm-controller/networkpolicy"
"swarm-logs-auth/networkpolicy"
"swarm-metrics-auth/networkpolicy"
"swarm-nats/networkpolicy"
"swarm-stt/networkpolicy"
"swarm-victorialogs/networkpolicy"
"swarm-victoriametrics/networkpolicy"
];
}
{
@ -1908,7 +1949,7 @@ let
{
# The checker is the control that it finds a violation: the same pod with
# a host port, a kept capability or a missing seccomp profile is flagged,
# and the workloads it reads are all seven.
# and the workloads it reads are all eleven.
name = "the namespace enforces Pod Security restricted, and no pod the module renders breaks it";
ok =
let
@ -1982,7 +2023,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 == 9
&& lib.length specs == 11
&& lib.all (s: problems s == [ ]) specs
&& problems withHostPort != [ ]
&& problems withCaps != [ ]
@ -2164,6 +2205,224 @@ let
&& !(spec ? initContainers)
&& !(lib.any (v: v.name == "tls") spec.volumes);
}
{
# The data is the container's own directory, so an upgrade moves the
# process and not the bytes. The path is the plain one, never the
# DynamicUser layout's `private/` directory. Every arm has a control: the
# log store's pod, which has no startup probe, and the pods that have no
# volume, no selector or no ServiceAccount.
name = "the metrics store's pod is the daemon on a static local volume that is its old container's data directory, with a startup probe and no store identity";
ok =
let
spec = podSpec "swarm-victoriametrics";
c = container "swarm-victoriametrics";
dir = "/var/lib/nixos-containers/swarm-victoriametrics/var/lib/victoriametrics";
pv = objects."swarm-victoriametrics/persistentvolume";
pvc = objects."swarm-victoriametrics/persistentvolumeclaim";
dep = objects."swarm-victoriametrics/deployment".spec;
label = "hyperhive.darkest.space/state-victoriametrics";
probe = {
path = "/health";
port = "api";
};
in
c.args == [
"-storageDataPath=/var/lib/victoriametrics"
"-httpListenAddr=:8428"
"-retentionPeriod=5y"
]
&& c.livenessProbe.httpGet == probe
&& c.readinessProbe.httpGet == probe
# The store opens its storage before it serves HTTP: liveness waits for
# ten minutes of tries.
&& c.startupProbe.httpGet == probe
&& c.startupProbe.failureThreshold == 60
&& !((container "swarm-victorialogs") ? startupProbe)
&&
c.ports == [
{
name = "api";
containerPort = 8428;
}
]
&& dep.replicas == 1
&& dep.strategy.type == "Recreate"
&& pv.spec.local.path == dir
&& !(lib.hasInfix "/private/" pv.spec.local.path)
&& pv.spec.persistentVolumeReclaimPolicy == "Retain"
&& pvc.spec.volumeName == pv.metadata.name
&&
pv.spec.nodeAffinity.required.nodeSelectorTerms == [
{
matchExpressions = [
{
key = label;
operator = "In";
values = [ "true" ];
}
];
}
]
&& spec.nodeSelector == { ${label} = "true"; }
&&
(lib.findFirst (v: v.name == "state") null spec.volumes).persistentVolumeClaim.claimName
== pvc.metadata.name
&& spec.securityContext.fsGroupChangePolicy == "OnRootMismatch"
&& !(spec ? initContainers)
&& !(lib.any (v: v.name == "tls") spec.volumes)
&& lib.any (v: v.name == "tls") (podSpec "swarm-controller").volumes
&& !(spec ? serviceAccountName)
&& !(objects ? "swarm-victoriametrics/serviceaccount")
&& !(lib.any (v: v.name == "bao-token" || v.name == "credentials") spec.volumes)
&& (podSpec "swarm-stt") ? serviceAccountName
&&
objects."swarm-victoriametrics/service".spec.ports == [
{
name = "api";
protocol = "TCP";
port = 8428;
targetPort = "api";
}
]
&& objects."swarm-victoriametrics/networkpolicy".spec.policyTypes == [ "Ingress" ]
&& !(objects."swarm-victoriametrics/networkpolicy".spec ? ingress)
&& lib.elem "d ${dir} - - - -" onCluster.systemd.tmpfiles.rules
&& !(lib.any (lib.hasPrefix "d ${dir} ") noNode.systemd.tmpfiles.rules);
}
{
# The metrics store's responders: one unpinned pod, one account, two
# containers whose Services each reach their own. The log store's pod is
# the control for the shape; a `--socket` would be a listener no Service
# reaches.
name = "the metrics store's responders are one unpinned pod under one account, reached by the node alone, with the store and DNS as its only way out";
ok =
let
spec = podSpec "swarm-metrics-auth";
named = n: lib.findFirst (c: c.name == n) null spec.containers;
agents = named "swarm-metrics-auth";
grafana = named "grafana-auth";
policy = objects."swarm-metrics-auth/networkpolicy".spec;
s = withNode.systemd.services;
script = s.swarm-bao-k8s-jwt-policy.script;
podLabels = {
"app.kubernetes.io/name" = "swarm-metrics-auth";
"app.kubernetes.io/part-of" = "hyperhive";
};
service = n: objects.${n}.spec;
envOfC = c: lib.listToAttrs (map (e: lib.nameValuePair e.name e.value) c.env);
in
map (c: c.name) spec.containers == [
"swarm-metrics-auth"
"grafana-auth"
]
&&
agents.args == [
"--listen"
"0.0.0.0:8083"
"--store-cert-role"
"swarm-metrics-auth"
"--surface"
"metrics"
"--verdict-ttl"
"60"
]
&&
grafana.args == [
"--listen"
"0.0.0.0:8084"
"--store-cert-role"
"swarm-metrics-auth"
"--store-path"
"swarm/services/swarm-grafana/metrics/bearer"
]
&& !(lib.elem "--socket" (agents.args ++ grafana.args))
# Each container logs in with the pod's token, on the same role.
&& (envOfC agents).BAO_JWT_ROLE == "swarm-metrics-auth"
&& (envOfC grafana).BAO_JWT_ROLE == "swarm-metrics-auth"
&& !(lib.any (e: lib.hasPrefix "BAO_CLIENT" e.name) (agents.env ++ grafana.env))
&& grafana.livenessProbe.httpGet.port == "grafana-health"
&& spec.serviceAccountName == "swarm-metrics-auth"
&& objects."swarm-metrics-auth/serviceaccount".metadata.name == "swarm-metrics-auth"
&& hasInfix "bound_subject=system:serviceaccount:hyperhive:swarm-metrics-auth" script
&& hasInfix "auth/jwt-k8s/role/swarm-metrics-auth" script
&& hasInfix "token_type=batch" s.swarm-bao-metrics-auth-policy.script
&& lib.elem "swarm-metrics-auth" withNode.services.hyperhive.deploy.bao.k8sPrincipals
# No state label: the store's pod is pinned, this one is not.
&& !(spec ? nodeSelector)
&& (podSpec "swarm-victoriametrics") ? nodeSelector
&& (service "swarm-metrics-auth/service").selector == podLabels
&&
(service "swarm-metrics-auth/service").ports == [
{
name = "api";
protocol = "TCP";
port = 8083;
targetPort = "api";
}
]
&& (service "swarm-metrics-grafana-auth/service").selector == podLabels
&&
(service "swarm-metrics-grafana-auth/service").ports == [
{
name = "api";
protocol = "TCP";
port = 8084;
targetPort = "grafana-api";
}
]
&&
policy.policyTypes == [
"Ingress"
"Egress"
]
&& !(policy ? ingress)
&& lib.length policy.egress == 2
&&
(lib.elemAt policy.egress 1).ports == [
{
protocol = "TCP";
port = 8208;
}
]
&& !(objects."swarm-victoriametrics/networkpolicy".spec ? egress)
&& !(spec ? initContainers)
&& !(lib.any (v: v.name == "tls") spec.volumes);
}
{
# 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
# the wrong Service. The first clauses are the control: the lookup finds
# the Services whose pods hold two containers, and the store's, and
# finds the name once in each.
name = "every Service that targets a port by name reaches a name that exactly one container of its pod declares";
ok =
let
all = lib.attrValues objects;
services = lib.filter (o: o.kind == "Service") all;
deployments = lib.filter (o: o.kind == "Deployment") all;
named = lib.filter (svc: lib.any (p: builtins.isString p.targetPort) svc.spec.ports) services;
podOf =
svc: lib.findFirst (d: d.spec.template.metadata.labels == svc.spec.selector) null deployments;
declared =
pod: port:
lib.length (
lib.filter (p: p.name == port) (lib.concatMap (c: c.ports or [ ]) pod.spec.template.spec.containers)
);
reached = lib.concatMap (
svc:
map (p: {
service = svc.metadata.name;
count = declared (podOf svc) p.targetPort;
}) (lib.filter (p: builtins.isString p.targetPort) svc.spec.ports)
) named;
countOf = n: (lib.findFirst (r: r.service == n) { count = 0; } reached).count;
in
lib.length (lib.filter (svc: lib.length (podOf svc).spec.template.spec.containers > 1) named) >= 4
&& countOf "swarm-metrics-grafana-auth" == 1
&& countOf "swarm-logs-grafana-auth" == 1
&& countOf "swarm-victoriametrics" == 1
&& lib.all (r: r.count == 1) reached;
}
{
# The host without the node is the control: it labels nothing.
name = "the node labels itself as the entry node, and with a state label for each service whose data the cluster keeps";
@ -2181,6 +2440,7 @@ let
"hyperhive.darkest.space/state-nats=true"
"hyperhive.darkest.space/state-ci-metrics=true"
"hyperhive.darkest.space/state-victorialogs=true"
"hyperhive.darkest.space/state-victoriametrics=true"
]
)
&&
@ -2190,6 +2450,7 @@ let
"hyperhive.darkest.space/state-nats" = "true";
"hyperhive.darkest.space/state-ci-metrics" = "true";
"hyperhive.darkest.space/state-victorialogs" = "true";
"hyperhive.darkest.space/state-victoriametrics" = "true";
}
&& noNode.services.k3s.nodeLabel == [ ];
}