2021-12-19 01:06:50 +00:00
|
|
|
{ config, lib, options, pkgs, ... }:
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
top = config.services.kubernetes;
|
2021-12-19 01:06:50 +00:00
|
|
|
otop = options.services.kubernetes;
|
2020-04-24 23:36:52 +00:00
|
|
|
cfg = top.kubelet;
|
|
|
|
|
|
|
|
cniConfig =
|
|
|
|
if cfg.cni.config != [] && cfg.cni.configDir != null then
|
|
|
|
throw "Verbatim CNI-config and CNI configDir cannot both be set."
|
|
|
|
else if cfg.cni.configDir != null then
|
|
|
|
cfg.cni.configDir
|
|
|
|
else
|
|
|
|
(pkgs.buildEnv {
|
|
|
|
name = "kubernetes-cni-config";
|
|
|
|
paths = imap (i: entry:
|
|
|
|
pkgs.writeTextDir "${toString (10+i)}-${entry.type}.conf" (builtins.toJSON entry)
|
|
|
|
) cfg.cni.config;
|
|
|
|
});
|
|
|
|
|
|
|
|
infraContainer = pkgs.dockerTools.buildImage {
|
|
|
|
name = "pause";
|
|
|
|
tag = "latest";
|
2022-07-18 16:21:45 +00:00
|
|
|
copyToRoot = pkgs.buildEnv {
|
|
|
|
name = "image-root";
|
|
|
|
pathsToLink = [ "/bin" ];
|
|
|
|
paths = [ top.package.pause ];
|
|
|
|
};
|
2021-03-09 03:18:52 +00:00
|
|
|
config.Cmd = ["/bin/pause"];
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2022-01-13 20:06:32 +00:00
|
|
|
kubeconfig = top.lib.mkKubeConfig "kubelet" cfg.kubeconfig;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
manifestPath = "kubernetes/manifests";
|
|
|
|
|
|
|
|
taintOptions = with lib.types; { name, ... }: {
|
|
|
|
options = {
|
|
|
|
key = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Key of taint.";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = name;
|
2022-09-09 14:08:57 +00:00
|
|
|
defaultText = literalMD "Name of this submodule.";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = str;
|
|
|
|
};
|
|
|
|
value = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Value of taint.";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = str;
|
|
|
|
};
|
|
|
|
effect = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Effect of taint.";
|
2020-04-24 23:36:52 +00:00
|
|
|
example = "NoSchedule";
|
|
|
|
type = enum ["NoSchedule" "PreferNoSchedule" "NoExecute"];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
taints = concatMapStringsSep "," (v: "${v.key}=${v.value}:${v.effect}") (mapAttrsToList (n: v: v) cfg.taints);
|
|
|
|
in
|
|
|
|
{
|
|
|
|
imports = [
|
|
|
|
(mkRemovedOptionModule [ "services" "kubernetes" "kubelet" "applyManifests" ] "")
|
|
|
|
(mkRemovedOptionModule [ "services" "kubernetes" "kubelet" "cadvisorPort" ] "")
|
|
|
|
(mkRemovedOptionModule [ "services" "kubernetes" "kubelet" "allowPrivileged" ] "")
|
|
|
|
];
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
options.services.kubernetes.kubelet = with lib.types; {
|
|
|
|
|
|
|
|
address = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Kubernetes kubelet info server listening address.";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "0.0.0.0";
|
|
|
|
type = str;
|
|
|
|
};
|
|
|
|
|
|
|
|
clusterDns = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Use alternative DNS.";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "10.1.0.1";
|
|
|
|
type = str;
|
|
|
|
};
|
|
|
|
|
|
|
|
clusterDomain = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Use alternative domain.";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = config.services.kubernetes.addons.dns.clusterDomain;
|
2021-12-19 01:06:50 +00:00
|
|
|
defaultText = literalExpression "config.${options.services.kubernetes.addons.dns.clusterDomain}";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = str;
|
|
|
|
};
|
|
|
|
|
|
|
|
clientCaFile = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Kubernetes apiserver CA file for client authentication.";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = top.caFile;
|
2021-12-19 01:06:50 +00:00
|
|
|
defaultText = literalExpression "config.${otop.caFile}";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = nullOr path;
|
|
|
|
};
|
|
|
|
|
|
|
|
cni = {
|
|
|
|
packages = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "List of network plugin packages to install.";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = listOf package;
|
|
|
|
default = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Kubernetes CNI configuration.";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = listOf attrs;
|
|
|
|
default = [];
|
2021-10-06 13:57:05 +00:00
|
|
|
example = literalExpression ''
|
2020-04-24 23:36:52 +00:00
|
|
|
[{
|
|
|
|
"cniVersion": "0.3.1",
|
|
|
|
"name": "mynet",
|
|
|
|
"type": "bridge",
|
|
|
|
"bridge": "cni0",
|
|
|
|
"isGateway": true,
|
|
|
|
"ipMasq": true,
|
|
|
|
"ipam": {
|
|
|
|
"type": "host-local",
|
|
|
|
"subnet": "10.22.0.0/16",
|
|
|
|
"routes": [
|
|
|
|
{ "dst": "0.0.0.0/0" }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
} {
|
|
|
|
"cniVersion": "0.3.1",
|
|
|
|
"type": "loopback"
|
|
|
|
}]
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
configDir = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Path to Kubernetes CNI configuration directory.";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = nullOr path;
|
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-03-09 03:18:52 +00:00
|
|
|
containerRuntime = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Which container runtime type to use";
|
2021-03-09 03:18:52 +00:00
|
|
|
type = enum ["docker" "remote"];
|
|
|
|
default = "remote";
|
|
|
|
};
|
|
|
|
|
|
|
|
containerRuntimeEndpoint = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Endpoint at which to find the container runtime api interface/socket";
|
2021-03-09 03:18:52 +00:00
|
|
|
type = str;
|
2021-05-20 23:08:51 +00:00
|
|
|
default = "unix:///run/containerd/containerd.sock";
|
2021-03-09 03:18:52 +00:00
|
|
|
};
|
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "Kubernetes kubelet.");
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
extraOpts = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Kubernetes kubelet extra command line options.";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "";
|
2021-05-03 20:48:10 +00:00
|
|
|
type = separatedString " ";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
featureGates = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "List set of feature gates";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = top.featureGates;
|
2021-12-19 01:06:50 +00:00
|
|
|
defaultText = literalExpression "config.${otop.featureGates}";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = listOf str;
|
|
|
|
};
|
|
|
|
|
|
|
|
healthz = {
|
|
|
|
bind = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Kubernetes kubelet healthz listening address.";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "127.0.0.1";
|
|
|
|
type = str;
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Kubernetes kubelet healthz port.";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = 10248;
|
|
|
|
type = int;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
hostname = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Kubernetes kubelet hostname override.";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = config.networking.hostName;
|
2021-12-06 16:07:01 +00:00
|
|
|
defaultText = literalExpression "config.networking.hostName";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = str;
|
|
|
|
};
|
|
|
|
|
2022-01-13 20:06:32 +00:00
|
|
|
kubeconfig = top.lib.mkKubeConfigOptions "Kubelet";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
manifests = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "List of manifests to bootstrap with kubelet (only pods can be created as manifest entry)";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = attrsOf attrs;
|
|
|
|
default = {};
|
|
|
|
};
|
|
|
|
|
|
|
|
networkPlugin = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Network plugin to use by Kubernetes.";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = nullOr (enum ["cni" "kubenet"]);
|
|
|
|
default = "kubenet";
|
|
|
|
};
|
|
|
|
|
|
|
|
nodeIp = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "IP address of the node. If set, kubelet will use this IP address for the node.";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = null;
|
|
|
|
type = nullOr str;
|
|
|
|
};
|
|
|
|
|
|
|
|
registerNode = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Whether to auto register kubelet with API server.";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = true;
|
|
|
|
type = bool;
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Kubernetes kubelet info server listening port.";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = 10250;
|
|
|
|
type = int;
|
|
|
|
};
|
|
|
|
|
|
|
|
seedDockerImages = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "List of docker images to preload on system";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = [];
|
|
|
|
type = listOf package;
|
|
|
|
};
|
|
|
|
|
|
|
|
taints = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Node taints (https://kubernetes.io/docs/concepts/configuration/assign-pod-node/).";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = {};
|
|
|
|
type = attrsOf (submodule [ taintOptions ]);
|
|
|
|
};
|
|
|
|
|
|
|
|
tlsCertFile = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "File containing x509 Certificate for HTTPS.";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = null;
|
|
|
|
type = nullOr path;
|
|
|
|
};
|
|
|
|
|
|
|
|
tlsKeyFile = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "File containing x509 private key matching tlsCertFile.";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = null;
|
|
|
|
type = nullOr path;
|
|
|
|
};
|
|
|
|
|
|
|
|
unschedulable = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Whether to set node taint to unschedulable=true as it is the case of node that has only master role.";
|
2020-04-24 23:36:52 +00:00
|
|
|
default = false;
|
|
|
|
type = bool;
|
|
|
|
};
|
|
|
|
|
|
|
|
verbosity = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Optional glog verbosity level for logging statements. See
|
2022-08-12 12:06:08 +00:00
|
|
|
<https://github.com/kubernetes/community/blob/master/contributors/devel/logging.md>
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
default = null;
|
|
|
|
type = nullOr int;
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
config = mkMerge [
|
|
|
|
(mkIf cfg.enable {
|
2021-03-09 03:18:52 +00:00
|
|
|
|
|
|
|
environment.etc."cni/net.d".source = cniConfig;
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
services.kubernetes.kubelet.seedDockerImages = [infraContainer];
|
|
|
|
|
2021-03-09 03:18:52 +00:00
|
|
|
boot.kernel.sysctl = {
|
|
|
|
"net.bridge.bridge-nf-call-iptables" = 1;
|
|
|
|
"net.ipv4.ip_forward" = 1;
|
|
|
|
"net.bridge.bridge-nf-call-ip6tables" = 1;
|
|
|
|
};
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
systemd.services.kubelet = {
|
|
|
|
description = "Kubernetes Kubelet Service";
|
|
|
|
wantedBy = [ "kubernetes.target" ];
|
2021-03-09 03:18:52 +00:00
|
|
|
after = [ "containerd.service" "network.target" "kube-apiserver.service" ];
|
2021-02-05 17:12:51 +00:00
|
|
|
path = with pkgs; [
|
|
|
|
gitMinimal
|
|
|
|
openssh
|
|
|
|
util-linux
|
2021-04-08 16:26:57 +00:00
|
|
|
iproute2
|
2021-02-05 17:12:51 +00:00
|
|
|
ethtool
|
|
|
|
thin-provisioning-tools
|
|
|
|
iptables
|
|
|
|
socat
|
|
|
|
] ++ lib.optional config.boot.zfs.enabled config.boot.zfs.package ++ top.path;
|
2020-04-24 23:36:52 +00:00
|
|
|
preStart = ''
|
|
|
|
${concatMapStrings (img: ''
|
2021-03-09 03:18:52 +00:00
|
|
|
echo "Seeding container image: ${img}"
|
|
|
|
${if (lib.hasSuffix "gz" img) then
|
2021-03-16 09:55:35 +00:00
|
|
|
''${pkgs.gzip}/bin/zcat "${img}" | ${pkgs.containerd}/bin/ctr -n k8s.io image import --all-platforms -''
|
2021-03-09 03:18:52 +00:00
|
|
|
else
|
2021-03-16 09:55:35 +00:00
|
|
|
''${pkgs.coreutils}/bin/cat "${img}" | ${pkgs.containerd}/bin/ctr -n k8s.io image import --all-platforms -''
|
2021-03-09 03:18:52 +00:00
|
|
|
}
|
2020-04-24 23:36:52 +00:00
|
|
|
'') cfg.seedDockerImages}
|
|
|
|
|
|
|
|
rm /opt/cni/bin/* || true
|
|
|
|
${concatMapStrings (package: ''
|
|
|
|
echo "Linking cni package: ${package}"
|
|
|
|
ln -fs ${package}/bin/* /opt/cni/bin
|
|
|
|
'') cfg.cni.packages}
|
|
|
|
'';
|
|
|
|
serviceConfig = {
|
|
|
|
Slice = "kubernetes.slice";
|
|
|
|
CPUAccounting = true;
|
|
|
|
MemoryAccounting = true;
|
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = "1000ms";
|
|
|
|
ExecStart = ''${top.package}/bin/kubelet \
|
|
|
|
--address=${cfg.address} \
|
|
|
|
--authentication-token-webhook \
|
|
|
|
--authentication-token-webhook-cache-ttl="10s" \
|
|
|
|
--authorization-mode=Webhook \
|
|
|
|
${optionalString (cfg.clientCaFile != null)
|
|
|
|
"--client-ca-file=${cfg.clientCaFile}"} \
|
|
|
|
${optionalString (cfg.clusterDns != "")
|
|
|
|
"--cluster-dns=${cfg.clusterDns}"} \
|
|
|
|
${optionalString (cfg.clusterDomain != "")
|
|
|
|
"--cluster-domain=${cfg.clusterDomain}"} \
|
|
|
|
--cni-conf-dir=${cniConfig} \
|
|
|
|
${optionalString (cfg.featureGates != [])
|
|
|
|
"--feature-gates=${concatMapStringsSep "," (feature: "${feature}=true") cfg.featureGates}"} \
|
|
|
|
--hairpin-mode=hairpin-veth \
|
|
|
|
--healthz-bind-address=${cfg.healthz.bind} \
|
|
|
|
--healthz-port=${toString cfg.healthz.port} \
|
|
|
|
--hostname-override=${cfg.hostname} \
|
|
|
|
--kubeconfig=${kubeconfig} \
|
|
|
|
${optionalString (cfg.networkPlugin != null)
|
|
|
|
"--network-plugin=${cfg.networkPlugin}"} \
|
|
|
|
${optionalString (cfg.nodeIp != null)
|
|
|
|
"--node-ip=${cfg.nodeIp}"} \
|
|
|
|
--pod-infra-container-image=pause \
|
|
|
|
${optionalString (cfg.manifests != {})
|
|
|
|
"--pod-manifest-path=/etc/${manifestPath}"} \
|
|
|
|
--port=${toString cfg.port} \
|
|
|
|
--register-node=${boolToString cfg.registerNode} \
|
|
|
|
${optionalString (taints != "")
|
|
|
|
"--register-with-taints=${taints}"} \
|
|
|
|
--root-dir=${top.dataDir} \
|
|
|
|
${optionalString (cfg.tlsCertFile != null)
|
|
|
|
"--tls-cert-file=${cfg.tlsCertFile}"} \
|
|
|
|
${optionalString (cfg.tlsKeyFile != null)
|
|
|
|
"--tls-private-key-file=${cfg.tlsKeyFile}"} \
|
|
|
|
${optionalString (cfg.verbosity != null) "--v=${toString cfg.verbosity}"} \
|
2021-03-09 03:18:52 +00:00
|
|
|
--container-runtime=${cfg.containerRuntime} \
|
|
|
|
--container-runtime-endpoint=${cfg.containerRuntimeEndpoint} \
|
|
|
|
--cgroup-driver=systemd \
|
2020-04-24 23:36:52 +00:00
|
|
|
${cfg.extraOpts}
|
|
|
|
'';
|
|
|
|
WorkingDirectory = top.dataDir;
|
|
|
|
};
|
2021-08-23 08:02:39 +00:00
|
|
|
unitConfig = {
|
|
|
|
StartLimitIntervalSec = 0;
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# Allways include cni plugins
|
2021-09-18 10:52:07 +00:00
|
|
|
services.kubernetes.kubelet.cni.packages = [pkgs.cni-plugins pkgs.cni-plugin-flannel];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-03-09 03:18:52 +00:00
|
|
|
boot.kernelModules = ["br_netfilter" "overlay"];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
services.kubernetes.kubelet.hostname = with config.networking;
|
|
|
|
mkDefault (hostName + optionalString (domain != null) ".${domain}");
|
|
|
|
|
2022-01-13 20:06:32 +00:00
|
|
|
services.kubernetes.pki.certs = with top.lib; {
|
2020-04-24 23:36:52 +00:00
|
|
|
kubelet = mkCert {
|
|
|
|
name = "kubelet";
|
|
|
|
CN = top.kubelet.hostname;
|
|
|
|
action = "systemctl restart kubelet.service";
|
|
|
|
|
|
|
|
};
|
|
|
|
kubeletClient = mkCert {
|
|
|
|
name = "kubelet-client";
|
|
|
|
CN = "system:node:${top.kubelet.hostname}";
|
|
|
|
fields = {
|
|
|
|
O = "system:nodes";
|
|
|
|
};
|
|
|
|
action = "systemctl restart kubelet.service";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
services.kubernetes.kubelet.kubeconfig.server = mkDefault top.apiserverAddress;
|
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf (cfg.enable && cfg.manifests != {}) {
|
|
|
|
environment.etc = mapAttrs' (name: manifest:
|
|
|
|
nameValuePair "${manifestPath}/${name}.json" {
|
|
|
|
text = builtins.toJSON manifest;
|
|
|
|
mode = "0755";
|
|
|
|
}
|
|
|
|
) cfg.manifests;
|
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf (cfg.unschedulable && cfg.enable) {
|
|
|
|
services.kubernetes.kubelet.taints.unschedulable = {
|
|
|
|
value = "true";
|
|
|
|
effect = "NoSchedule";
|
|
|
|
};
|
|
|
|
})
|
|
|
|
|
|
|
|
];
|
2022-01-13 20:06:32 +00:00
|
|
|
|
|
|
|
meta.buildDocsInSandbox = false;
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|