nix: render migrate.json and wrap swarmctl with kubectl
This commit is contained in:
parent
9396941f24
commit
5779bc5719
1 changed files with 291 additions and 1 deletions
|
|
@ -271,6 +271,7 @@ let
|
|||
&& usesSecret { projected.sources = [ { secret.name = "x"; } ]; }
|
||||
&& !(usesSecret { projected.sources = [ { configMap.name = "x"; } ]; });
|
||||
|
||||
migrateOf = c: builtins.fromJSON c.environment.etc."hyperhive/migrate.json".text;
|
||||
routerUnit = c: c.services.hyperhive.deploy.notify-router.unit;
|
||||
manifestNames = c: lib.sort lib.lessThan (lib.attrNames c.services.k3s.manifests);
|
||||
routerPrune = withNode.systemd.services.swarm-notify-router-k8s-prune;
|
||||
|
|
@ -1854,6 +1855,128 @@ let
|
|||
&& has "-t raw -A nixos-fw-nats -p tcp --dport 4222 -m addrtype --dst-type LOCAL -j DROP" onCluster.networking.firewall.extraCommands
|
||||
&& has "-t raw -F nixos-fw-nats" onCluster.networking.firewall.extraStopCommands;
|
||||
}
|
||||
{
|
||||
# Each state block is read against what the render makes: the claim is
|
||||
# the PVC's name and the mount is where the state gate looks. The
|
||||
# stateless services are the control that an entry can carry no state.
|
||||
name = "migrate.json names every service, with a state block exactly for the ones whose pod has a state volume";
|
||||
ok =
|
||||
let
|
||||
services = (migrateOf withNode).services;
|
||||
stateful = [
|
||||
"swarm-controller"
|
||||
"swarm-nats"
|
||||
"swarm-stt"
|
||||
"swarm-ci-metrics"
|
||||
];
|
||||
stateless = [
|
||||
"swarm-notify-router"
|
||||
"swarm-review-classifier"
|
||||
"swarm-claude-login"
|
||||
];
|
||||
objs = render withNode;
|
||||
gateDir =
|
||||
n:
|
||||
let
|
||||
o = objs."${n}/deployment" or objs."${n}/cronjob";
|
||||
spec = if o.kind == "CronJob" then o.spec.jobTemplate.spec.template.spec else o.spec.template.spec;
|
||||
gate = lib.head (lib.filter (c: c.name == "state-gate") spec.initContainers);
|
||||
in
|
||||
(lib.head (lib.filter (e: e.name == "STATE_GATE_DIR") gate.env)).value;
|
||||
in
|
||||
lib.sort lib.lessThan (lib.attrNames services) == lib.sort lib.lessThan (stateful ++ stateless)
|
||||
&& lib.all (
|
||||
n:
|
||||
let
|
||||
s = services.${n}.state;
|
||||
in
|
||||
s != null
|
||||
&& s.claim == objs."${n}/persistentvolumeclaim".metadata.name
|
||||
&& s.mount == gateDir n
|
||||
&& s ? hostPath
|
||||
&& s ? excludes
|
||||
) stateful
|
||||
&& lib.all (n: services.${n}.state == null && !(objs ? "${n}/persistentvolumeclaim")) stateless
|
||||
&& lib.all (n: lib.hasPrefix "services.hyperhive.deploy." services.${n}.option) (
|
||||
lib.attrNames services
|
||||
);
|
||||
}
|
||||
{
|
||||
name = "migrate.json gives the host path of each state, the host units of each service and the runtime the configuration wants";
|
||||
ok =
|
||||
let
|
||||
host = (migrateOf withNode).services;
|
||||
cluster = (migrateOf onCluster).services;
|
||||
in
|
||||
(migrateOf withNode).namespace == "hyperhive"
|
||||
&& host.swarm-controller.state.hostPath == "/var/lib/swarm-controller"
|
||||
&& host.swarm-nats.state.hostPath == "/var/lib/nixos-containers/swarm-nats/var/lib/nats"
|
||||
&& host.swarm-nats.state.mount == "/var/lib/nats"
|
||||
# A DynamicUser unit's data is under `private`.
|
||||
&& host.swarm-ci-metrics.state.hostPath == "/var/lib/private/swarm-ci-metrics"
|
||||
&& host.swarm-stt.state.hostPath == null
|
||||
&&
|
||||
host.swarm-ci-metrics.hostUnits == [
|
||||
"swarm-ci-metrics.timer"
|
||||
"swarm-ci-metrics.service"
|
||||
]
|
||||
&& host.swarm-nats.hostUnits == [ "container@swarm-nats.service" ]
|
||||
&& host.swarm-ci-metrics.option == "services.hyperhive.deploy.ci-metrics.runtime"
|
||||
&& lib.all (s: s.runtime == "host") (lib.attrValues host)
|
||||
# The runtime follows the option: the cluster fixture has every one on k8s.
|
||||
&& lib.all (s: s.runtime == "k8s") (lib.attrValues cluster)
|
||||
&& cluster.swarm-nats.state == host.swarm-nats.state;
|
||||
}
|
||||
{
|
||||
# A pure function, so the case needs no host of its own. A path inside the
|
||||
# directory is the presence control; a path elsewhere, a null and the
|
||||
# directory itself are left out.
|
||||
name = "the controller's bao identity is excluded from its copy only where it sits in the state directory";
|
||||
ok =
|
||||
let
|
||||
within = import ../host-modules/lib/paths-within.nix { inherit lib; };
|
||||
dir = "/var/lib/swarm-controller";
|
||||
in
|
||||
within dir [
|
||||
"/var/lib/swarm-controller/bao-client.pem"
|
||||
"/var/lib/swarm-controller/sub/key.pem"
|
||||
] == [
|
||||
"bao-client.pem"
|
||||
"sub/key.pem"
|
||||
]
|
||||
&&
|
||||
within dir [
|
||||
"/var/lib/swarm-bao-pki/controller.pem"
|
||||
null
|
||||
dir
|
||||
"/var/lib/swarm-controller-tls/x.pem"
|
||||
] == [ ]
|
||||
# The module's own default puts them outside it.
|
||||
&& (migrateOf withNode).services.swarm-controller.state.excludes == [ ];
|
||||
}
|
||||
{
|
||||
name = "swarmctl is wrapped with kubectl and the cluster's kubeconfig, by one wrapper per host";
|
||||
ok =
|
||||
let
|
||||
env = c: c.services.hyperhive.deploy.swarm-controller.swarmctlEnvironment;
|
||||
wrappers =
|
||||
c:
|
||||
map lib.getName (
|
||||
lib.filter (p: lib.hasPrefix "swarmctl-" (lib.getName p)) c.environment.systemPackages
|
||||
);
|
||||
in
|
||||
(env withNode).SWARMCTL_KUBECTL == "${pkgs.kubectl}/bin/kubectl"
|
||||
&& (env withNode).SWARMCTL_KUBECONFIG == "/etc/rancher/k3s/k3s.yaml"
|
||||
# The controller's own wrapper carries them where the controller is
|
||||
# enabled; elsewhere the k8s module installs one.
|
||||
&& wrappers withNode == [ "swarmctl-configured" ]
|
||||
&& wrappers nodeNoBridge == [ "swarmctl-kube" ]
|
||||
# Without the node there is no cluster to name, though the manifests render.
|
||||
&& allLocal.services.hyperhive.k8s.enable
|
||||
&& !(env allLocal ? SWARMCTL_KUBECTL)
|
||||
&& !(allLocal.environment.etc ? "hyperhive/migrate.json")
|
||||
&& wrappers allLocal == [ "swarmctl-configured" ];
|
||||
}
|
||||
];
|
||||
in
|
||||
runGroup "k8s" cases
|
||||
|
|
|
|||
Loading…
Reference in a new issue