k8s: pods resolve the swarm names of host-run services to the hive bridge
This commit is contained in:
parent
242df5faba
commit
cb54519be8
4 changed files with 168 additions and 11 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
94
nix/host-modules/k8s/host-services.nix
Normal file
94
nix/host-modules/k8s/host-services.nix
Normal 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;
|
||||
};
|
||||
})
|
||||
];
|
||||
}
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue