1 { config, lib, options, pkgs, ... }:
6 top = config.services.kubernetes;
7 otop = options.services.kubernetes;
11 if cfg.cni.config != [] && cfg.cni.configDir != null then
12 throw "Verbatim CNI-config and CNI configDir cannot both be set."
13 else if cfg.cni.configDir != null then
17 name = "kubernetes-cni-config";
18 paths = imap (i: entry:
19 pkgs.writeTextDir "${toString (10+i)}-${entry.type}.conf" (builtins.toJSON entry)
23 infraContainer = pkgs.dockerTools.buildImage {
26 copyToRoot = pkgs.buildEnv {
28 pathsToLink = [ "/bin" ];
29 paths = [ top.package.pause ];
31 config.Cmd = ["/bin/pause"];
34 kubeconfig = top.lib.mkKubeConfig "kubelet" cfg.kubeconfig;
36 manifestPath = "kubernetes/manifests";
38 taintOptions = with lib.types; { name, ... }: {
41 description = lib.mdDoc "Key of taint.";
43 defaultText = literalMD "Name of this submodule.";
47 description = lib.mdDoc "Value of taint.";
51 description = lib.mdDoc "Effect of taint.";
52 example = "NoSchedule";
53 type = enum ["NoSchedule" "PreferNoSchedule" "NoExecute"];
58 taints = concatMapStringsSep "," (v: "${v.key}=${v.value}:${v.effect}") (mapAttrsToList (n: v: v) cfg.taints);
62 (mkRemovedOptionModule [ "services" "kubernetes" "kubelet" "applyManifests" ] "")
63 (mkRemovedOptionModule [ "services" "kubernetes" "kubelet" "cadvisorPort" ] "")
64 (mkRemovedOptionModule [ "services" "kubernetes" "kubelet" "allowPrivileged" ] "")
65 (mkRemovedOptionModule [ "services" "kubernetes" "kubelet" "networkPlugin" ] "")
69 options.services.kubernetes.kubelet = with lib.types; {
72 description = lib.mdDoc "Kubernetes kubelet info server listening address.";
77 clusterDns = mkOption {
78 description = lib.mdDoc "Use alternative DNS.";
83 clusterDomain = mkOption {
84 description = lib.mdDoc "Use alternative domain.";
85 default = config.services.kubernetes.addons.dns.clusterDomain;
86 defaultText = literalExpression "config.${options.services.kubernetes.addons.dns.clusterDomain}";
90 clientCaFile = mkOption {
91 description = lib.mdDoc "Kubernetes apiserver CA file for client authentication.";
93 defaultText = literalExpression "config.${otop.caFile}";
99 description = lib.mdDoc "List of network plugin packages to install.";
100 type = listOf package;
105 description = lib.mdDoc "Kubernetes CNI configuration.";
108 example = literalExpression ''
110 "cniVersion": "0.3.1",
117 "type": "host-local",
118 "subnet": "10.22.0.0/16",
120 { "dst": "0.0.0.0/0" }
124 "cniVersion": "0.3.1",
130 configDir = mkOption {
131 description = lib.mdDoc "Path to Kubernetes CNI configuration directory.";
137 containerRuntime = mkOption {
138 description = lib.mdDoc "Which container runtime type to use";
139 type = enum ["docker" "remote"];
143 containerRuntimeEndpoint = mkOption {
144 description = lib.mdDoc "Endpoint at which to find the container runtime api interface/socket";
146 default = "unix:///run/containerd/containerd.sock";
149 enable = mkEnableOption (lib.mdDoc "Kubernetes kubelet.");
151 extraOpts = mkOption {
152 description = lib.mdDoc "Kubernetes kubelet extra command line options.";
154 type = separatedString " ";
157 featureGates = mkOption {
158 description = lib.mdDoc "List set of feature gates";
159 default = top.featureGates;
160 defaultText = literalExpression "config.${otop.featureGates}";
166 description = lib.mdDoc "Kubernetes kubelet healthz listening address.";
167 default = "127.0.0.1";
172 description = lib.mdDoc "Kubernetes kubelet healthz port.";
178 hostname = mkOption {
179 description = lib.mdDoc "Kubernetes kubelet hostname override.";
180 defaultText = literalExpression "config.networking.fqdnOrHostName";
184 kubeconfig = top.lib.mkKubeConfigOptions "Kubelet";
186 manifests = mkOption {
187 description = lib.mdDoc "List of manifests to bootstrap with kubelet (only pods can be created as manifest entry)";
188 type = attrsOf attrs;
193 description = lib.mdDoc "IP address of the node. If set, kubelet will use this IP address for the node.";
198 registerNode = mkOption {
199 description = lib.mdDoc "Whether to auto register kubelet with API server.";
205 description = lib.mdDoc "Kubernetes kubelet info server listening port.";
210 seedDockerImages = mkOption {
211 description = lib.mdDoc "List of docker images to preload on system";
213 type = listOf package;
217 description = lib.mdDoc "Node taints (https://kubernetes.io/docs/concepts/configuration/assign-pod-node/).";
219 type = attrsOf (submodule [ taintOptions ]);
222 tlsCertFile = mkOption {
223 description = lib.mdDoc "File containing x509 Certificate for HTTPS.";
228 tlsKeyFile = mkOption {
229 description = lib.mdDoc "File containing x509 private key matching tlsCertFile.";
234 unschedulable = mkOption {
235 description = lib.mdDoc "Whether to set node taint to unschedulable=true as it is the case of node that has only master role.";
240 verbosity = mkOption {
241 description = lib.mdDoc ''
242 Optional glog verbosity level for logging statements. See
243 <https://github.com/kubernetes/community/blob/master/contributors/devel/logging.md>
251 ###### implementation
255 environment.etc."cni/net.d".source = cniConfig;
257 services.kubernetes.kubelet.seedDockerImages = [infraContainer];
259 boot.kernel.sysctl = {
260 "net.bridge.bridge-nf-call-iptables" = 1;
261 "net.ipv4.ip_forward" = 1;
262 "net.bridge.bridge-nf-call-ip6tables" = 1;
265 systemd.services.kubelet = {
266 description = "Kubernetes Kubelet Service";
267 wantedBy = [ "kubernetes.target" ];
268 after = [ "containerd.service" "network.target" "kube-apiserver.service" ];
275 thin-provisioning-tools
278 ] ++ lib.optional config.boot.zfs.enabled config.boot.zfs.package ++ top.path;
280 ${concatMapStrings (img: ''
281 echo "Seeding container image: ${img}"
282 ${if (lib.hasSuffix "gz" img) then
283 ''${pkgs.gzip}/bin/zcat "${img}" | ${pkgs.containerd}/bin/ctr -n k8s.io image import --all-platforms -''
285 ''${pkgs.coreutils}/bin/cat "${img}" | ${pkgs.containerd}/bin/ctr -n k8s.io image import --all-platforms -''
287 '') cfg.seedDockerImages}
289 rm /opt/cni/bin/* || true
290 ${concatMapStrings (package: ''
291 echo "Linking cni package: ${package}"
292 ln -fs ${package}/bin/* /opt/cni/bin
293 '') cfg.cni.packages}
296 Slice = "kubernetes.slice";
297 CPUAccounting = true;
298 MemoryAccounting = true;
299 Restart = "on-failure";
300 RestartSec = "1000ms";
301 ExecStart = ''${top.package}/bin/kubelet \
302 --address=${cfg.address} \
303 --authentication-token-webhook \
304 --authentication-token-webhook-cache-ttl="10s" \
305 --authorization-mode=Webhook \
306 ${optionalString (cfg.clientCaFile != null)
307 "--client-ca-file=${cfg.clientCaFile}"} \
308 ${optionalString (cfg.clusterDns != "")
309 "--cluster-dns=${cfg.clusterDns}"} \
310 ${optionalString (cfg.clusterDomain != "")
311 "--cluster-domain=${cfg.clusterDomain}"} \
312 ${optionalString (cfg.featureGates != [])
313 "--feature-gates=${concatMapStringsSep "," (feature: "${feature}=true") cfg.featureGates}"} \
314 --hairpin-mode=hairpin-veth \
315 --healthz-bind-address=${cfg.healthz.bind} \
316 --healthz-port=${toString cfg.healthz.port} \
317 --hostname-override=${cfg.hostname} \
318 --kubeconfig=${kubeconfig} \
319 ${optionalString (cfg.nodeIp != null)
320 "--node-ip=${cfg.nodeIp}"} \
321 --pod-infra-container-image=pause \
322 ${optionalString (cfg.manifests != {})
323 "--pod-manifest-path=/etc/${manifestPath}"} \
324 --port=${toString cfg.port} \
325 --register-node=${boolToString cfg.registerNode} \
326 ${optionalString (taints != "")
327 "--register-with-taints=${taints}"} \
328 --root-dir=${top.dataDir} \
329 ${optionalString (cfg.tlsCertFile != null)
330 "--tls-cert-file=${cfg.tlsCertFile}"} \
331 ${optionalString (cfg.tlsKeyFile != null)
332 "--tls-private-key-file=${cfg.tlsKeyFile}"} \
333 ${optionalString (cfg.verbosity != null) "--v=${toString cfg.verbosity}"} \
334 --container-runtime=${cfg.containerRuntime} \
335 --container-runtime-endpoint=${cfg.containerRuntimeEndpoint} \
336 --cgroup-driver=systemd \
339 WorkingDirectory = top.dataDir;
342 StartLimitIntervalSec = 0;
346 # Allways include cni plugins
347 services.kubernetes.kubelet.cni.packages = [pkgs.cni-plugins pkgs.cni-plugin-flannel];
349 boot.kernelModules = ["br_netfilter" "overlay"];
351 services.kubernetes.kubelet.hostname =
352 mkDefault config.networking.fqdnOrHostName;
354 services.kubernetes.pki.certs = with top.lib; {
357 CN = top.kubelet.hostname;
358 action = "systemctl restart kubelet.service";
361 kubeletClient = mkCert {
362 name = "kubelet-client";
363 CN = "system:node:${top.kubelet.hostname}";
367 action = "systemctl restart kubelet.service";
371 services.kubernetes.kubelet.kubeconfig.server = mkDefault top.apiserverAddress;
374 (mkIf (cfg.enable && cfg.manifests != {}) {
375 environment.etc = mapAttrs' (name: manifest:
376 nameValuePair "${manifestPath}/${name}.json" {
377 text = builtins.toJSON manifest;
383 (mkIf (cfg.unschedulable && cfg.enable) {
384 services.kubernetes.kubelet.taints.unschedulable = {
386 effect = "NoSchedule";
392 meta.buildDocsInSandbox = false;