The controller's api port was a hostPort on the node, which Pod Security baseline and restricted forbid. It is a ClusterIP Service now, behind a NetworkPolicy that admits only the node. The host's resolver (the hive's dnsmasq) forwards cluster.local to CoreDNS, and the gateway dials the Service by name through nginx's resolver. tcp.hostAddress goes.
261 lines
8.5 KiB
Nix
261 lines
8.5 KiB
Nix
# The single-node k3s cluster this host runs for the swarm services that run
|
|
# in the cluster, the Kubernetes counterpart of `deploy.singleHostSwarm`'s
|
|
# 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. No service's objects are applied until its `runtime` is `k8s`
|
|
# (./runtime.nix): until then its host unit runs.
|
|
{
|
|
config,
|
|
lib,
|
|
...
|
|
}:
|
|
let
|
|
cfg = config.services.hyperhive.deploy.k8s;
|
|
networkCfg = config.services.hyperhive.network;
|
|
|
|
# IPv4 CIDR arithmetic on nix's 64-bit integers.
|
|
ipToInt =
|
|
ip:
|
|
builtins.foldl' (acc: x: acc * 256 + x) 0 (
|
|
map lib.strings.toIntBase10 (lib.strings.splitString "." ip)
|
|
);
|
|
pow2 = n: if n == 0 then 1 else 2 * (pow2 (n - 1));
|
|
parseCidr =
|
|
cidr:
|
|
let
|
|
parts = lib.strings.splitString "/" cidr;
|
|
in
|
|
{
|
|
base = ipToInt (lib.head parts);
|
|
len = lib.strings.toIntBase10 (lib.last parts);
|
|
};
|
|
# Two ranges overlap when they agree on the leading bits of the shorter one.
|
|
overlap =
|
|
a: b:
|
|
let
|
|
pa = parseCidr a;
|
|
pb = parseCidr b;
|
|
keep = 4294967296 - pow2 (32 - lib.min pa.len pb.len);
|
|
in
|
|
builtins.bitAnd pa.base keep == builtins.bitAnd pb.base keep;
|
|
|
|
# The first address of a range, and the dotted form of an integer.
|
|
networkOf =
|
|
cidr:
|
|
let
|
|
p = parseCidr cidr;
|
|
in
|
|
builtins.bitAnd p.base (4294967296 - pow2 (32 - p.len));
|
|
intToIp =
|
|
n:
|
|
lib.concatMapStringsSep "." (d: toString (lib.mod (n / d) 256)) [
|
|
16777216
|
|
65536
|
|
256
|
|
1
|
|
];
|
|
|
|
cidrType = lib.types.strMatching "([0-9]{1,3}\\.){3}[0-9]{1,3}/[0-9]{1,2}";
|
|
|
|
# k3s's own defaults, named so the flags below and the ServiceAccount
|
|
# issuer and key paths read from one place.
|
|
dataDir = "/var/lib/rancher/k3s";
|
|
clusterDomain = "cluster.local";
|
|
# k3s's default for `--cluster-dns` is the tenth address of the service
|
|
# range (`GetIndexedIP(svcCIDR, 10)` in k3s' `pkg/cli/server/server.go`). It
|
|
# is passed explicitly below, so the address the host's resolver forwards
|
|
# to and the one CoreDNS gets agree by construction.
|
|
clusterDns = intToIp (networkOf cfg.serviceCidr + 10);
|
|
in
|
|
{
|
|
options.services.hyperhive.deploy.k8s = {
|
|
enable = lib.mkOption {
|
|
type = lib.types.bool;
|
|
default = false;
|
|
description = ''
|
|
Run a single-node k3s server on this host, with the images of the
|
|
swarm services that run in the cluster loaded into it, and render
|
|
their manifests (`services.hyperhive.k8s.enable`). Set it on the host
|
|
that runs the swarm's services, which is the one
|
|
`services.hyperhive.deploy.singleHostSwarm` names.
|
|
|
|
The cluster starts with no hyperhive workload: a service keeps
|
|
running as its systemd unit unless its `runtime` option is `k8s`.
|
|
Its secrets are encrypted at rest, and traefik and metrics-server
|
|
stay off. Pods and services take their
|
|
addresses from {option}`services.hyperhive.deploy.k8s.podCidr` and
|
|
{option}`services.hyperhive.deploy.k8s.serviceCidr`. The firewall
|
|
trusts the pod network's interfaces (`cni0`, `flannel.1`), so the node
|
|
reaches its pods.
|
|
'';
|
|
};
|
|
|
|
podCidr = lib.mkOption {
|
|
type = cidrType;
|
|
default = "10.200.0.0/16";
|
|
description = ''
|
|
IPv4 range k3s hands pod addresses out of (`--cluster-cidr`). It must
|
|
overlap neither the hive bridge's subnet
|
|
({option}`services.hyperhive.network.bridgeIp`) nor
|
|
{option}`services.hyperhive.deploy.k8s.serviceCidr`, nor any network
|
|
the host routes to. k3s's own default, `10.42.0.0/16`, contains the
|
|
hive bridge's default subnet.
|
|
'';
|
|
};
|
|
|
|
serviceCidr = lib.mkOption {
|
|
type = cidrType;
|
|
default = "10.201.0.0/16";
|
|
description = ''
|
|
IPv4 range k3s hands service cluster IPs out of (`--service-cidr`);
|
|
the cluster DNS address is the tenth address of it. The same
|
|
constraints as {option}`services.hyperhive.deploy.k8s.podCidr` apply.
|
|
'';
|
|
};
|
|
|
|
clusterDomain = lib.mkOption {
|
|
type = lib.types.str;
|
|
readOnly = true;
|
|
internal = true;
|
|
default = clusterDomain;
|
|
description = ''
|
|
The cluster's DNS domain (`--cluster-domain`). A Service is
|
|
`<name>.<namespace>.svc.<this>`.
|
|
'';
|
|
};
|
|
|
|
clusterDns = lib.mkOption {
|
|
type = lib.types.str;
|
|
readOnly = true;
|
|
internal = true;
|
|
default = clusterDns;
|
|
description = ''
|
|
The address of the cluster's DNS service (`--cluster-dns`): the tenth
|
|
address of {option}`services.hyperhive.deploy.k8s.serviceCidr`. The
|
|
host's resolver forwards the cluster's domain to it.
|
|
'';
|
|
};
|
|
|
|
cniInterfaces = lib.mkOption {
|
|
type = lib.types.listOf lib.types.str;
|
|
default = [
|
|
"cni0"
|
|
"flannel.1"
|
|
];
|
|
readOnly = true;
|
|
internal = true;
|
|
description = ''
|
|
The pod network's interfaces. The firewall trusts them, and a
|
|
listener meant for pods only is opened on these and no other.
|
|
'';
|
|
};
|
|
|
|
serviceAccountIssuer = lib.mkOption {
|
|
type = lib.types.str;
|
|
readOnly = true;
|
|
internal = true;
|
|
default = "https://kubernetes.default.svc.${clusterDomain}";
|
|
description = ''
|
|
The `iss` claim of the ServiceAccount tokens the cluster issues, the
|
|
issuer k3s gives its API server for the cluster domain it runs with.
|
|
'';
|
|
};
|
|
|
|
serviceAccountKeyFiles = lib.mkOption {
|
|
type = lib.types.listOf lib.types.str;
|
|
readOnly = true;
|
|
internal = true;
|
|
default = [
|
|
"${dataDir}/server/tls/service.key"
|
|
"${dataDir}/server/tls/service.current.key"
|
|
];
|
|
description = ''
|
|
The files k3s keeps its ServiceAccount keys in: the key that verifies
|
|
tokens and the one that signs them, which differ after a rotation.
|
|
'';
|
|
};
|
|
|
|
manifestDir = lib.mkOption {
|
|
type = lib.types.str;
|
|
readOnly = true;
|
|
internal = true;
|
|
default = "${dataDir}/server/manifests";
|
|
description = ''
|
|
The directory k3s applies manifests from, which
|
|
{option}`services.k3s.manifests` links its files into.
|
|
'';
|
|
};
|
|
};
|
|
|
|
config = lib.mkIf cfg.enable {
|
|
assertions = [
|
|
{
|
|
assertion = config.services.hyperhive.k8s.enable;
|
|
message = ''
|
|
services.hyperhive.deploy.k8s.enable loads the images
|
|
services.hyperhive.k8s.enable renders into the node, so it needs
|
|
services.hyperhive.k8s.enable to be true.
|
|
'';
|
|
}
|
|
{
|
|
assertion =
|
|
!networkCfg.enable
|
|
|| !(lib.any (overlap networkCfg.networkCidr) [
|
|
cfg.podCidr
|
|
cfg.serviceCidr
|
|
]);
|
|
message = ''
|
|
services.hyperhive.deploy.k8s.podCidr (${cfg.podCidr}) and
|
|
services.hyperhive.deploy.k8s.serviceCidr (${cfg.serviceCidr}) must
|
|
not overlap the hive bridge's subnet (${networkCfg.networkCidr}, from
|
|
services.hyperhive.network.bridgeIp and bridgePrefixLength): the
|
|
bridge and the cluster would route the same addresses.
|
|
'';
|
|
}
|
|
{
|
|
assertion = !(overlap cfg.podCidr cfg.serviceCidr);
|
|
message = ''
|
|
services.hyperhive.deploy.k8s.podCidr (${cfg.podCidr}) and
|
|
services.hyperhive.deploy.k8s.serviceCidr (${cfg.serviceCidr}) must
|
|
not overlap each other.
|
|
'';
|
|
}
|
|
];
|
|
|
|
services.hyperhive.k8s.enable = lib.mkDefault true;
|
|
|
|
services.k3s = {
|
|
enable = true;
|
|
role = "server";
|
|
disable = [
|
|
"traefik"
|
|
"metrics-server"
|
|
];
|
|
extraFlags = [
|
|
"--secrets-encryption"
|
|
"--cluster-cidr=${cfg.podCidr}"
|
|
"--service-cidr=${cfg.serviceCidr}"
|
|
"--data-dir=${dataDir}"
|
|
"--cluster-domain=${clusterDomain}"
|
|
"--cluster-dns=${clusterDns}"
|
|
];
|
|
# k3s's own images come first: with no registry the node pulls nothing,
|
|
# not even CoreDNS.
|
|
images = [
|
|
config.services.k3s.package.airgap-images
|
|
]
|
|
++ (config.system.build.k8sImages or [ ]);
|
|
};
|
|
|
|
networking.firewall.trustedInterfaces = cfg.cniInterfaces;
|
|
|
|
# The host's resolver (the hive's dnsmasq, which the host's own
|
|
# `/etc/resolv.conf` names) hands the cluster's domain to CoreDNS, so a
|
|
# Service is reachable from the host by its name. The bridge's agents ask
|
|
# the same resolver and get the same answers.
|
|
services.dnsmasq.settings.server = lib.mkIf config.services.hyperhive.gateway.dns.enable [
|
|
"/${clusterDomain}/${clusterDns}"
|
|
];
|
|
};
|
|
}
|