From cb54519be8df6a48942e328564fe1899b26ddf99 Mon Sep 17 00:00:00 2001 From: atlas Date: Sat, 21 Nov 2026 13:13:29 +0100 Subject: [PATCH] k8s: pods resolve the swarm names of host-run services to the hive bridge --- docs/4-implementation/k8s/node.md | 35 +++++++--- nix/host-modules/k8s/default.nix | 5 +- nix/host-modules/k8s/host-services.nix | 94 ++++++++++++++++++++++++++ nix/host-modules/k8s/node.nix | 2 +- nix/module-eval/k8s.nix | 43 ++++++++++++ 5 files changed, 168 insertions(+), 11 deletions(-) create mode 100644 nix/host-modules/k8s/host-services.nix diff --git a/docs/4-implementation/k8s/node.md b/docs/4-implementation/k8s/node.md index 77775eecf..22296075f 100644 --- a/docs/4-implementation/k8s/node.md +++ b/docs/4-implementation/k8s/node.md @@ -4,7 +4,7 @@ single-node k3s server for the swarm services that run in the cluster. It's the Kubernetes counterpart of `services.hyperhive.deploy.singleHostSwarm`: set it on the host that runs the swarm's services, and the cluster comes up with the -service images loaded and nothing applied. +service images loaded and no service's objects applied. ## What the host gets @@ -32,7 +32,7 @@ service images loaded and nothing applied. ## What it leaves alone The cluster starts with no hyperhive workload. Each service keeps running as -its systemd unit, and nothing applies the manifests: two copies of +its systemd unit, and nothing applies the services' manifests: two copies of `swarm-notify-router`, `swarm-ci-metrics` or `swarm-controller` would share one queue consumer, one cursor or one swarm. A service's objects go into the cluster when that service moves in (see [`swarmctl migrate`](migrate.md)). @@ -40,13 +40,30 @@ cluster when that service moves in (see [`swarmctl migrate`](migrate.md)). With the option off, the host's configuration is the one it has without the module. +## Reaching host-run services + +A pod reaches a service that runs on the host by the service's swarm name. The +cluster's CoreDNS answers each such name with the hive bridge's address +(`network.bridgeIp`), the address the agents' resolver gives, so a pod reaches +the same listeners an agent does: the gateway's vhosts, the secret store's TLS +port, the queue and the hive's OTLP collector. The names are the hive domain +and every name a host module contributes to `gateway.localNames` because the +host runs that service. `services.hyperhive.deploy.k8s.hostServices` is that +table, and it holds the only host address the cluster knows. + +The node applies the table to k3s as a `coredns-custom` ConfigMap, which is a +node object and not a service manifest. A name outside the table, and every +cluster name, resolves as before. A host without the hive bridge has no +address to answer with, so it gets no ConfigMap. + +The firewall trusts the pod network's interfaces, so a pod reaches +these listeners without a firewall rule per port. Each service still +authenticates its callers: the secret store refuses a connection without a +client certificate. + ## Validation `module-eval-k8s` compares an all-local host with the option on against the -same host with it off. The off host has no k3s, no trusted interface and no -image list; the on host adds the k3s unit and nothing else. The VM test -`k8s-node` boots a node through the module, and asserts that the node is Ready, -that containerd holds every image, that the cluster has no hyperhive -workload, and that the node and the hive bridge hold addresses from -different ranges. The module-eval also checks that overlapping ranges fail the -evaluation. +same host with it off, and checks the address ranges and the CoreDNS +ConfigMap. The VM test `k8s-node` boots a node through the module and runs a +pod against it. diff --git a/nix/host-modules/k8s/default.nix b/nix/host-modules/k8s/default.nix index 1a998d5c2..caf394ac7 100644 --- a/nix/host-modules/k8s/default.nix +++ b/nix/host-modules/k8s/default.nix @@ -21,7 +21,10 @@ let rendered = import ./render.nix { inherit lib pkgs config; }; in { - imports = [ ./node.nix ]; + imports = [ + ./host-services.nix + ./node.nix + ]; options.services.hyperhive.k8s = { enable = lib.mkOption { diff --git a/nix/host-modules/k8s/host-services.nix b/nix/host-modules/k8s/host-services.nix new file mode 100644 index 000000000..258f9ee92 --- /dev/null +++ b/nix/host-modules/k8s/host-services.nix @@ -0,0 +1,94 @@ +# The host-run services the cluster's pods reach by their swarm names. +# +# A pod resolves a swarm name to the hive bridge's address, the answer the +# agents' resolver gives (../hive-gateway/dnsmasq.nix), so a pod reaches the +# same listeners an agent does: the gateway's vhosts, the store's TLS +# passthrough, the queue and the hive collector. Which names those are is +# `gateway.localNames` plus the hive domain: each module adds its name there +# only when this host runs the service. docs/4-implementation/k8s/node.md has +# the design. +{ + config, + lib, + ... +}: +let + cfg = config.services.hyperhive.deploy.k8s; + networkCfg = config.services.hyperhive.network; + hiveDomain = config.services.hyperhive.domain; + + names = + lib.optional (hiveDomain != null) hiveDomain + ++ lib.unique config.services.hyperhive.gateway.localNames; + + # The names a template matches: the exact name, in any case, as a regex. + matchOf = + names: + "(?i)^(${lib.concatMapStringsSep "|" (builtins.replaceStrings [ "." ] [ "[.]" ]) names})[.]$"; + + # CoreDNS imports every `*.override` key of the `coredns-custom` ConfigMap + # into its default server block, after the node's own `hosts` block and + # before the forwarder. A second `hosts` block there stops CoreDNS ("this + # plugin can only be used once per Server Block"), so the answers are + # `template`s: an A answer per address, and an empty AAAA answer, as the + # agents' resolver gives. `fallthrough` hands every other name on. + byAddress = lib.groupBy (name: cfg.hostServices.${name}.address) (lib.attrNames cfg.hostServices); + templates = + lib.mapAttrsToList (address: ns: '' + template IN A { + match "${matchOf ns}" + answer "{{ .Name }} 60 IN A ${address}" + fallthrough + } + '') byAddress + ++ [ + '' + template IN AAAA { + match "${matchOf (lib.attrNames cfg.hostServices)}" + rcode NOERROR + fallthrough + } + '' + ]; +in +{ + options.services.hyperhive.deploy.k8s.hostServices = lib.mkOption { + type = lib.types.attrsOf ( + lib.types.submodule { + options.address = lib.mkOption { + type = lib.types.str; + description = "Address a pod reaches the service on."; + }; + } + ); + readOnly = true; + internal = true; + description = '' + The swarm names the cluster's pods resolve to a listener on this host, + each with the address it resolves to. Empty without the hive bridge, + which is the address. + ''; + }; + + config = lib.mkMerge [ + { + services.hyperhive.deploy.k8s.hostServices = lib.optionalAttrs networkCfg.enable ( + lib.genAttrs names (_: { + address = networkCfg.bridgeIp; + }) + ); + } + + (lib.mkIf (cfg.enable && cfg.hostServices != { }) { + services.k3s.manifests.coredns-custom.content = { + apiVersion = "v1"; + kind = "ConfigMap"; + metadata = { + name = "coredns-custom"; + namespace = "kube-system"; + }; + data."hyperhive-host-services.override" = lib.concatStrings templates; + }; + }) + ]; +} diff --git a/nix/host-modules/k8s/node.nix b/nix/host-modules/k8s/node.nix index 95248a537..07d9e5c4d 100644 --- a/nix/host-modules/k8s/node.nix +++ b/nix/host-modules/k8s/node.nix @@ -3,7 +3,7 @@ # all-local swarm. docs/4-implementation/k8s/node.md has the design. # # The node holds the images of the services ./render.nix renders and has no -# registry. Nothing is applied to the cluster: a service's host unit keeps +# registry. No service's objects are applied: a service's host unit keeps # running, and its objects go in when the service moves in. { config, diff --git a/nix/module-eval/k8s.nix b/nix/module-eval/k8s.nix index 00c0d2a06..ec55fb3ca 100644 --- a/nix/module-eval/k8s.nix +++ b/nix/module-eval/k8s.nix @@ -55,6 +55,9 @@ let deploy.k8s.podCidr = "10.42.0.0/16"; deploy.k8s.serviceCidr = "10.42.128.0/17"; }; + # The node on a host with no hive bridge, so no address to resolve names to. + nodeNoBridge = hive { deploy.k8s.enable = true; }; + coreDnsKey = "hyperhive-host-services.override"; refusals = lib.filter (a: !a.assertion) nodeRefused.assertions; refuses = needle: lib.any (a: lib.hasInfix needle a.message) refusals; @@ -262,6 +265,46 @@ let && refuses "overlap the hive bridge" && refuses "overlap each other"; } + { + # The names are the control that the search finds something: the swarm's + # own bao name and the hive domain are among them, and every one is in the + # A and AAAA templates. + name = "the node's CoreDNS answers every swarm name this host serves with the hive bridge's address"; + ok = + let + h = withNode.services.hyperhive; + cm = withNode.services.k3s.manifests.coredns-custom.content; + text = cm.data.${coreDnsKey}; + names = lib.unique ([ h.domain ] ++ h.gateway.localNames); + in + cm.kind == "ConfigMap" + && cm.metadata.name == "coredns-custom" + && cm.metadata.namespace == "kube-system" + && lib.length names >= 6 + && lib.elem h.swarm.bao.domain names + && lib.all (n: lib.hasInfix (builtins.replaceStrings [ "." ] [ "[.]" ] n) text) names + && lib.hasInfix "template IN A {" text + && lib.hasInfix "template IN AAAA {" text + && lib.hasInfix "answer \"{{ .Name }} 60 IN A ${h.network.bridgeIp}\"" text + && lib.hasInfix " fallthrough\n}\n" text + && !(lib.hasInfix "127.0.0.1" text) + && !(lib.hasInfix "hosts" text) + && lib.attrNames h.deploy.k8s.hostServices == lib.sort lib.lessThan names + && lib.all (s: s.address == h.network.bridgeIp) (lib.attrValues h.deploy.k8s.hostServices); + } + { + # The host with the node on and a bridge is the control that the manifest + # is there to be absent. + name = "the node applies the CoreDNS override only on a host with the hive bridge"; + ok = + withNode.services.hyperhive.network.enable + && withNode.services.k3s.manifests ? coredns-custom + && allLocal.services.k3s.manifests == { } + && !nodeNoBridge.services.hyperhive.network.enable + && nodeNoBridge.services.hyperhive.domain == "h1.t.local" + && nodeNoBridge.services.hyperhive.deploy.k8s.hostServices == { } + && !(nodeNoBridge.services.k3s.manifests ? coredns-custom); + } ]; in runGroup "k8s" cases