# What `swarmctl migrate` reads of this host: `/etc/hyperhive/migrate.json`, and # the environment that tells `swarmctl` which `kubectl` and kubeconfig to use. # Imported by ./default.nix. docs/4-implementation/k8s/migrate.md has the design. # # The file has one entry per service that has a `runtime` option: the # option's path, the runtime the configuration wants, the host units that run # the service when it is on the host. The file and the wrapper exist only on a # host with the node (./node.nix): without a cluster there is nothing to read. { lib, pkgs, config, ... }: let cfg = config.services.hyperhive.k8s; deployCfg = config.services.hyperhive.deploy; node = deployCfg.k8s; controllerCfg = deployCfg.swarm-controller; # Each service's name under `services.hyperhive.deploy`. optionOf = { swarm-controller = "swarm-controller"; swarm-nats = "nats"; swarm-notify-router = "notify-router"; swarm-stt = "stt"; swarm-review-classifier = "review-classifier"; swarm-claude-login = "claude-login"; swarm-ci-metrics = "ci-metrics"; }; entry = name: switch: { option = "services.hyperhive.deploy.${optionOf.${name}}.runtime"; inherit (switch) runtime; hostUnits = lib.toList switch.hostUnit; }; migrateJson = { inherit (cfg) namespace; services = lib.mapAttrs entry cfg.rendered.switches; }; # The `swarmctl` wrapper's variables. The same `kubectl` that ./runtime.nix # runs, with the cluster's admin kubeconfig. kubeEnv = { SWARMCTL_KUBECTL = "${pkgs.kubectl}/bin/kubectl"; SWARMCTL_KUBECONFIG = "/etc/rancher/k3s/k3s.yaml"; }; # `swarmctl` where the controller's module does not install one. swarmctlKube = pkgs.symlinkJoin { name = "swarmctl-kube"; paths = [ deployCfg.swarm-controller.swarmctlPackage ]; nativeBuildInputs = [ pkgs.makeWrapper ]; postBuild = '' wrapProgram $out/bin/swarmctl ${ lib.concatStringsSep " " ( lib.mapAttrsToList (name: value: "--set ${name} ${lib.escapeShellArg value}") kubeEnv ) } ''; }; in { config = lib.mkIf (node.enable && cfg.enable) { environment.etc."hyperhive/migrate.json".text = builtins.toJSON migrateJson; # The controller module's wrapper carries these where the controller is # enabled, which it stays at `runtime = "k8s"`. services.hyperhive.deploy.swarm-controller.swarmctlEnvironment = kubeEnv; environment.systemPackages = lib.mkIf (!controllerCfg.enable) [ swarmctlKube ]; }; }