Watch
0
0
Fork
You've already forked hyperhive
0

k8s: pods resolve the swarm names of host-run services to the hive bridge

This commit is contained in:
atlas 2026-11-21 13:13:29 +01:00 • committed by mara
commit cb54519be8
3 changed files with 168 additions and 11 deletions

View file

@ -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 {

View file

@ -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;
};
})
];
}

View file

@ -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,