Watch
0
0
Fork
You've already forked hyperhive
0

k8s: stt's pod gets a network policy: dns and four host ports out, nothing in

This commit is contained in:
atlas 2026-11-23 13:36:51 +01:00 • committed by maia
commit 02cc38d89e
2 changed files with 262 additions and 22 deletions

View file

@ -153,6 +153,9 @@ let
# containers, init containers, volumes, host aliases, pod settings, objects and
# and images. An `otlp` of `"none"` is a daemon that pushes no telemetry of its
# own.
# `egress`, where a service has it, names the host services its pod may reach
# (`hostPorts`). The pod then gets a NetworkPolicy that lets it reach those
# and cluster DNS and nothing else, and that admits no ingress.
services = {
swarm-controller = {
inherit (controllerCfg) enable package unit;
@ -219,6 +222,15 @@ let
healthEnv = "SWARM_STT_HEALTH_ADDR";
etcFiles = [ ];
publicCredentials.stt-bao-server-ca = baoServerCaKey;
# The audio it decodes comes from strangers. It reaches the store, the
# identity provider and the queue it logs in to and answers on, and
# pushes telemetry to the hive collector.
egress = [
"store"
"identity"
"queue"
"collector"
];
otlp = "hive";
ephemeralState = true;
switch = {
@ -342,6 +354,71 @@ let
labels = labelsFor name;
};
# The host services a pod's `egress` names, each the TCP port it is reached
# on at the bridge's address (./host-services.nix). The node trusts the pod
# network's interfaces, so a NetworkPolicy is the only thing that limits
# which of the host's ports a pod reaches. The identity provider is the
# gateway's vhost: `swarm.authelia.url` names no port.
hostPorts = {
store = baoDeployCfg.podListenerPort;
identity = 443;
queue = hyperhiveCfg.swarm.nats.port;
collector = hyperhiveCfg.swarm.hiveOtel.collectorPort;
};
# What the policy of a pod that reaches `reaches` allows: the cluster's DNS,
# those host ports, and the queue's own pod where the queue runs in the
# cluster, because its `hostPort` is forwarded to the pod before the policy
# sees the packet. The policy selects every pod of the service for ingress
# too and allows none, so only the node's own traffic (the kubelet's probes,
# which the policy engine always admits) reaches it.
networkPolicyOf =
name: reaches:
let
tcp = port: {
protocol = "TCP";
inherit port;
};
queueOnCluster = natsCfg.enable && natsCfg.runtime == "k8s";
in
{
apiVersion = "networking.k8s.io/v1";
kind = "NetworkPolicy";
metadata = metaFor name;
spec = {
podSelector.matchLabels = labelsFor name;
policyTypes = [
"Ingress"
"Egress"
];
egress = [
{
to = [
{
namespaceSelector.matchLabels."kubernetes.io/metadata.name" = "kube-system";
podSelector.matchLabels.k8s-app = "kube-dns";
}
];
ports = [
{
protocol = "UDP";
port = 53;
}
(tcp 53)
];
}
{
to = [ { ipBlock.cidr = "${hyperhiveCfg.network.bridgeIp}/32"; } ];
ports = map (r: tcp hostPorts.${r}) reaches;
}
]
++ lib.optional (queueOnCluster && lib.elem "queue" reaches) {
to = [ { podSelector.matchLabels = labelsFor "swarm-nats"; } ];
ports = [ (tcp hostPorts.queue) ];
};
};
};
# systemd duration "<n>min", the only form these units use.
minutesOf =
name: what: value:
@ -909,6 +986,9 @@ let
"${name}/${lib.toLower svc.kind}" = main;
}
// extra.objects
// lib.optionalAttrs (svc ? egress) {
"${name}/networkpolicy" = networkPolicyOf name svc.egress;
}
// lib.optionalAttrs baoLogin {
# Named after the service: the store's jwt role binds this account.
"${name}/serviceaccount" = {
@ -944,6 +1024,8 @@ let
fail name "the unit names no OTLP endpoint of its own, which the pod would push to"
else if svc.healthEnv != null && unitEnv ? ${svc.healthEnv} then
fail name "the unit already sets ${svc.healthEnv}"
else if svc ? egress && !(lib.all (r: hostPorts ? ${r}) svc.egress) then
fail name "egress names a host service the renderer has no port for (${lib.concatStringsSep ", " (lib.attrNames hostPorts)})"
else if lib.head execWords != entrypoint then
fail name "ExecStart is not ${entrypoint} with plain arguments"
else if lib.any (w: lib.hasInfix "\"" w) execWords then

View file

@ -126,7 +126,7 @@ in
exit 0
fi
${apiReady}
${kubectl} --namespace ${cfg.namespace} delete deployment,cronjob,serviceaccount \
${kubectl} --namespace ${cfg.namespace} delete deployment,cronjob,serviceaccount,networkpolicy \
--selector=app.kubernetes.io/name=${name} \
--cascade=foreground --wait=true --timeout=180s --ignore-not-found
${kubectl} --namespace kube-system delete addons.k3s.cattle.io ${name} --ignore-not-found