Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/nix/host-modules/k8s/join.nix

142 lines
5.1 KiB
Nix

# The join token, through the swarm secret store: the main node's k3s server
# writes it, and a host that sets `deploy.k8s.join` reads it before its k3s
# agent starts. Both log in as the hive, the way `squid-selfcheck-credential`
# does (../hive-squid-credential.nix). The leaf and the file are
# ../lib/k8s-join-token.nix; ../swarm-bao.nix grants the read and, to the
# store host's own hive, the write. docs/4-implementation/k8s/node.md has the
# design.
{
pkgs,
lib,
config,
...
}:
let
hyperhiveCfg = config.services.hyperhive;
cfg = hyperhiveCfg.deploy.k8s;
hiveController = hyperhiveCfg.deploy.hive-controller;
tls = hiveController.tls;
baoCfg = hyperhiveCfg.swarm.bao;
baoDeploy = hyperhiveCfg.deploy.bao;
token = import ../lib/k8s-join-token.nix;
haveIdentity = tls.baoHiveIdentityCertFile != null && tls.baoHiveIdentityKeyFile != null;
# The file k3s's server keeps the token agents join with in.
serverTokenFile = "/var/lib/rancher/k3s/server/agent-token";
# Where the store runs on this host, the first login waits for its container.
# No `wants`: a pull would start the container mid-switch on the old closure.
storeContainer = lib.optional baoDeploy.enable "container@${baoCfg.machine}.service";
storeRetry = import ../lib/store-retry.nix { };
common = {
path = [
baoDeploy.package
pkgs.coreutils
pkgs.gnugrep
];
environment = {
BAO_ADDR = "https://${baoCfg.domain}:${toString baoCfg.port}";
BAO_CLIENT_CERT = tls.baoHiveIdentityCertFile;
BAO_CLIENT_KEY = tls.baoHiveIdentityKeyFile;
}
// lib.optionalAttrs (baoDeploy.serverCaFile != null) {
BAO_CACERT = baoDeploy.serverCaFile;
};
# ./lib/store-retry.nix: the login fails for as long as the store is
# sealed or down.
inherit (storeRetry) startLimitBurst startLimitIntervalSec;
wants = [ "network-online.target" ];
after = [ "network-online.target" ] ++ storeContainer;
};
in
{
config = lib.mkMerge [
# The main node: the token k3s generated, in the store.
(lib.mkIf (cfg.enable && haveIdentity) {
systemd.services.k3s-join-token-publish = common // {
description = "Write the k3s join token to the swarm secret store";
wantedBy = [ "k3s.service" ];
after = common.after ++ [ "k3s.service" ];
wants = common.wants ++ [ "k3s.service" ];
serviceConfig = storeRetry.serviceConfig // {
Type = "oneshot";
UMask = "0077";
SyslogIdentifier = "k3s-join-token-publish";
TimeoutStartSec = "5min";
};
script = ''
set -euo pipefail
${import ../lib/store-seeded-secret.nix { }}
# k3s writes the file as it starts.
for _ in $(seq 120); do
[ -s ${serverTokenFile} ] && break
sleep 2
done
if [ ! -s ${serverTokenFile} ]; then
echo "k3s has not written ${serverTokenFile}." >&2
exit 1
fi
value="$(< ${serverTokenFile})"
store_init
store_login
if [ "$STORE_UP" != 1 ]; then
echo "could not log in to the swarm secret store; the join token is not published." >&2
exit 1
fi
if current="$(store_bao kv get -field=value ${lib.escapeShellArg "secret/${token.leaf}"} 2>"$STORE_ERR")" \
&& [ "$current" = "$value" ]; then
exit 0
fi
printf '%s' "$value" |
store_bao kv put ${lib.escapeShellArg "secret/${token.leaf}"} value=- >/dev/null
echo "published the k3s join token at ${token.leaf}."
'';
};
})
# A node that joins: the token, in a file before k3s starts.
(lib.mkIf (cfg.join != null && haveIdentity) {
systemd.services.k3s-join-token = common // {
description = "Read the k3s join token from the swarm secret store";
wantedBy = [ "k3s.service" ];
before = [ "k3s.service" ];
serviceConfig = storeRetry.serviceConfig // {
Type = "oneshot";
UMask = "0077";
SyslogIdentifier = "k3s-join-token";
TimeoutStartSec = "5min";
};
script = ''
set -euo pipefail
${import ../lib/store-seeded-secret.nix { }}
${import ../lib/atomic-write-secret.nix { }}
${import ../lib/refresh-consumer.nix { }}
install -d -m 0700 ${builtins.dirOf token.file}
store_init
store_login
if [ "$STORE_UP" != 1 ]; then
echo "could not log in to the swarm secret store; ${token.file} stays as it is." >&2
exit 1
fi
if ! value="$(store_read ${lib.escapeShellArg "secret/${token.leaf}"})" || [ -z "$value" ]; then
cat "$STORE_ERR" >&2
echo "the swarm secret store holds no k3s join token at ${token.leaf} yet; ${token.file} stays as it is." >&2
exit 1
fi
# Written only when the value changed, so the file keeps its mtime.
if secret_differs ${token.file} "$value"; then
atomic_write_secret 0400 root:root ${token.file} "$value"
fi
'';
};
})
];
}