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

94 lines
3 KiB
Nix

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