Watch
0
0
Fork
You've already forked hyperhive
0

let a second machine join the main node's k3s and run pods from images only the main node holds

This commit is contained in:
atlas 2026-11-25 22:36:35 +01:00 • committed by maia
commit 7819ed2dbd
2 changed files with 1106 additions and 96 deletions

View file

@ -1603,9 +1603,9 @@ let
# One role, policy and login role per hive in the directory, each
# naming only its own hive: the policy text in the unit is exactly the
# `update` on its own role and `read` on its own two cache facts and its own
# squid self-check credential, and each
# squid self-check credential, and on the k3s join token every hive shares, and each
# hive's block names neither the other's role nor its subject.
name = "each hive's gateway-leaf identity may issue from its own role, read its own two cache facts, squid self-check credential and archive grants, and list those grants, and nothing else";
name = "each hive's gateway-leaf identity may issue from its own role, read its own two cache facts, squid self-check credential and archive grants, list those grants, read the k3s join token, and nothing else";
ok =
let
s = baoGrantTwoHives.systemd.services.swarm-bao-hive-leaf-policy.script;
@ -1647,6 +1647,10 @@ let
path = "secret/data/swarm/hives/h1/squid-selfcheck";
caps = [ "read" ];
}
{
path = "secret/data/swarm/services/k8s/agent-token";
caps = [ "read" ];
}
{
path = "secret/data/swarm/hives/h1/archive/*";
caps = [ "read" ];
@ -1671,6 +1675,10 @@ let
path = "secret/data/swarm/hives/h2/squid-selfcheck";
caps = [ "read" ];
}
{
path = "secret/data/swarm/services/k8s/agent-token";
caps = [ "read" ];
}
{
path = "secret/data/swarm/hives/h2/archive/*";
caps = [ "read" ];

View file

@ -51,9 +51,46 @@ let
deploy.claude-login.runtime = "k8s";
deploy.swarm-controller.runtime = "k8s";
deploy.nats.runtime = "k8s";
deploy.k8s.address = "192.168.1.10";
deploy.k8s.nodeAddresses = [
"192.168.1.11"
"192.168.1.12"
];
}
);
# Two hives on a store host with the node: the policy of each differs by the
# join token. Shares nothing of `onCluster`'s controller, which lists the
# hives.
twoHives = hive {
deploy.bao.enable = true;
deploy.k8s.enable = true;
swarm.hives.h2.domain = "h2.t.local";
};
# A host that joins that cluster: an agent with the store identity its join
# token is read under. The only fixture without `deploy.k8s.enable`, which
# is what it claims about.
joined = hive {
deploy.k8s.join.server = "192.168.1.10";
deploy.k8s.address = "192.168.1.11";
deploy.hive-controller.tls = {
baoHiveIdentityCertFile = "/var/lib/hive-leaf/leaf.pem";
baoHiveIdentityKeyFile = "/var/lib/hive-leaf/leaf-key.pem";
};
};
joinedUnit = joined.systemd.services.k3s-join-token;
# Every image the cluster's pods name, and where the pull policy is.
registryPrefix = "registry.hyperhive.invalid/";
clusterText =
builtins.toJSON (render onCluster)
+ builtins.toJSON onCluster.services.hyperhive.k8s.rendered.services;
imageRefs = map (s: lib.head (lib.splitString "\"" s)) (
lib.tail (lib.splitString "\"image\":\"" clusterText)
);
occurrences = needle: text: lib.length (lib.splitString needle text) - 1;
# A host that asks for the cluster with no node, and a controller in it with
# no TCP listener.
runtimeRefused = hive {
@ -414,6 +451,7 @@ let
"opentelemetry-collector"
"swarm-bao-controller-policy"
"swarm-bao-granter-role"
"swarm-bao-hive-leaf-policy"
"swarm-bao-services-issuer-policy"
] (names allLocal.systemd.services)
)
@ -2295,6 +2333,129 @@ let
&& lib.elem rule onCluster.systemd.tmpfiles.rules
&& !(lib.elem rule withNode.systemd.tmpfiles.rules);
}
{
# The registry host is spelled out here, so the module is not compared
# with itself. `withNode` is the control for the flags that follow an
# address: it sets none, and k3s keeps its own default.
name = "the server serves the embedded registry and mirrors the images' host alone, and follows the node's address when it has one";
ok =
let
registries = onCluster.environment.etc."rancher/k3s/registries.yaml".text;
in
registries == "mirrors:\n registry.hyperhive.invalid:\n"
&& withNode.environment.etc."rancher/k3s/registries.yaml".text == registries
&& !(lib.hasInfix "*" registries)
&& lib.elem "--embedded-registry" onCluster.services.k3s.extraFlags
&& lib.elem "--embedded-registry" withNode.services.k3s.extraFlags
&& onCluster.services.k3s.nodeIP == "192.168.1.10"
&& lib.elem "--tls-san=192.168.1.10" onCluster.services.k3s.extraFlags
&& withNode.services.k3s.nodeIP == null
&& !(lib.any (lib.hasPrefix "--tls-san") withNode.services.k3s.extraFlags);
}
{
# `withNode` lists no node: the control that the ports are not open
# for everyone, and the `-s` on every rule that they are for the nodes.
name = "the nodes' ports are open to the listed node addresses and to no other source";
ok =
let
rules = onCluster.networking.firewall.extraCommands;
rule =
address: proto: port:
"iptables -A nixos-fw -s ${address} -p ${proto} --dport ${port} -j nixos-fw-accept\n";
forNodes = address: [
(rule address "tcp" "6443")
(rule address "tcp" "10250")
(rule address "udp" "8472")
(rule address "tcp" "5001")
];
nodeLines = lib.filter (l: lib.hasInfix "--dport" l && lib.hasInfix "nixos-fw -s" l) (
lib.splitString "\n" rules
);
in
lib.all (r: lib.hasInfix r rules) (forNodes "192.168.1.11" ++ forNodes "192.168.1.12")
&& lib.length nodeLines == 8
&& !(lib.hasInfix "--dport 6443" withNode.networking.firewall.extraCommands)
&& !(lib.elem 6443 onCluster.networking.firewall.allowedTCPPorts)
&& !(lib.elem 8472 onCluster.networking.firewall.allowedUDPPorts);
}
{
# The main node is the control for each absence.
name = "a host that joins runs a k3s agent with the token the store gave it, none of the server's units or labels, and the same mirror";
ok =
joined.services.k3s.enable
&& joined.services.k3s.role == "agent"
&& joined.services.k3s.serverAddr == "https://192.168.1.10:6443"
&& joined.services.k3s.tokenFile == "/run/k3s-join/token"
&& joined.services.k3s.agentTokenFile == null
&& joined.services.k3s.nodeIP == "192.168.1.11"
&& joined.services.k3s.nodeLabel == [ ]
&& joined.services.k3s.manifests == { }
&& !(lib.elem "--embedded-registry" joined.services.k3s.extraFlags)
&&
joined.environment.etc."rancher/k3s/registries.yaml".text
== onCluster.environment.etc."rancher/k3s/registries.yaml".text
&& lib.hasInfix "iptables -A nixos-fw -s 192.168.1.10 -p tcp --dport 6443 -j nixos-fw-accept" joined.networking.firewall.extraCommands
&& joinedUnit.before == [ "k3s.service" ]
&& joinedUnit.wantedBy == [ "k3s.service" ]
&& lib.hasInfix "secret/swarm/services/k8s/agent-token" joinedUnit.script
&& lib.hasInfix "/run/k3s-join/token" joinedUnit.script
&& !(joined.systemd.services ? k3s-join-token-publish)
&& withNode.systemd.services ? k3s-join-token-publish
&& !(withNode.systemd.services ? k3s-join-token)
&& withNode.services.k3s.role == "server"
&& withNode.services.k3s.nodeLabel != [ ]
&& !(joined.systemd.services ? swarm-notify-router-k8s-prune)
&& withNode.systemd.services ? swarm-notify-router-k8s-prune;
}
{
name = "a host that is both the main node and a joined node is refused, and so is a join with no store identity";
ok =
let
both = hive {
deploy.k8s.enable = true;
deploy.k8s.join.server = "192.168.1.10";
};
noIdentity = hive { deploy.k8s.join.server = "192.168.1.10"; };
messages = c: map (a: a.message) (lib.filter (a: !a.assertion) c.assertions);
mentions = needle: c: lib.any (lib.hasInfix needle) (messages c);
in
mentions "k8s.join as a node of another" both
&& mentions "baoHiveIdentityCertFile" noIdentity
&& !(mentions "k8s.join as a node of another" joined)
&& !(mentions "baoHiveIdentityCertFile" joined);
}
{
# The main hive writes and h2 only reads. A count, not a match: the
# same path is in both policies, so a stanza in the wrong one still
# leaves the other in the text.
name = "every hive's identity may read the join token, and the main node's hive alone may write it";
ok =
let
script = twoHives.systemd.services.swarm-bao-hive-leaf-policy.script;
path = "path \"secret/data/swarm/services/k8s/agent-token\" {\n capabilities = ";
in
occurrences "${path}[\"create\", \"read\", \"update\"]" script == 1
&& occurrences "${path}[\"read\"]" script == 1
&&
occurrences "agent-token\" {\n capabilities = [\"create\"" allLocal.systemd.services.swarm-bao-hive-leaf-policy.script
== 0
&&
occurrences "agent-token\" {\n capabilities = [\"read\"]" allLocal.systemd.services.swarm-bao-hive-leaf-policy.script
== 1;
}
{
# Presence controls: the lists are not empty, and the queue's three images
# and a leaf's are among them.
name = "every image the pods name carries the registry host that resolves nowhere, and none is pulled from a registry or never";
ok =
lib.length imageRefs >= 8
&& lib.all (r: lib.hasPrefix registryPrefix r) imageRefs
&& lib.any (r: lib.hasSuffix "-server" (lib.head (lib.splitString ":" r))) imageRefs
&& lib.any (r: lib.hasInfix "-tls:" r) imageRefs
&& occurrences "\"imagePullPolicy\":\"IfNotPresent\"" clusterText >= 8
&& !(lib.hasInfix "\"imagePullPolicy\":\"Never\"" clusterText)
&& !(lib.hasInfix "docker.io" clusterText);
}
];
in
runGroup "k8s" cases