2016-09-27 22:56:58 +02:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
let
|
|
|
|
cfg = config.services.flannel;
|
|
|
|
|
2024-12-15 09:15:44 +01:00
|
|
|
networkConfig =
|
|
|
|
(lib.filterAttrs (n: v: v != null) {
|
|
|
|
Network = cfg.network;
|
|
|
|
SubnetLen = cfg.subnetLen;
|
|
|
|
SubnetMin = cfg.subnetMin;
|
|
|
|
SubnetMax = cfg.subnetMax;
|
|
|
|
Backend = cfg.backend;
|
|
|
|
})
|
|
|
|
// cfg.extraNetworkConfig;
|
2016-09-27 22:56:58 +02:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.flannel = {
|
2024-08-28 21:19:07 +02:00
|
|
|
enable = lib.mkEnableOption "flannel";
|
2016-09-27 22:56:58 +02:00
|
|
|
|
2024-08-28 21:19:07 +02:00
|
|
|
package = lib.mkPackageOption pkgs "flannel" { };
|
2016-09-27 22:56:58 +02:00
|
|
|
|
2024-08-28 21:19:07 +02:00
|
|
|
publicIp = lib.mkOption {
|
2016-09-27 22:56:58 +02:00
|
|
|
description = ''
|
|
|
|
IP accessible by other nodes for inter-host communication.
|
|
|
|
Defaults to the IP of the interface being used for communication.
|
|
|
|
'';
|
2024-08-28 21:19:07 +02:00
|
|
|
type = lib.types.nullOr lib.types.str;
|
2016-09-27 22:56:58 +02:00
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
|
2024-08-28 21:19:07 +02:00
|
|
|
iface = lib.mkOption {
|
2016-09-27 22:56:58 +02:00
|
|
|
description = ''
|
|
|
|
Interface to use (IP or name) for inter-host communication.
|
|
|
|
Defaults to the interface for the default route on the machine.
|
|
|
|
'';
|
2024-08-28 21:19:07 +02:00
|
|
|
type = lib.types.nullOr lib.types.str;
|
2016-09-27 22:56:58 +02:00
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
etcd = {
|
2024-08-28 21:19:07 +02:00
|
|
|
endpoints = lib.mkOption {
|
2016-09-27 22:56:58 +02:00
|
|
|
description = "Etcd endpoints";
|
2024-08-28 21:19:07 +02:00
|
|
|
type = lib.types.listOf lib.types.str;
|
2016-09-27 22:56:58 +02:00
|
|
|
default = [ "http://127.0.0.1:2379" ];
|
|
|
|
};
|
|
|
|
|
2024-08-28 21:19:07 +02:00
|
|
|
prefix = lib.mkOption {
|
2016-09-27 22:56:58 +02:00
|
|
|
description = "Etcd key prefix";
|
2024-08-28 21:19:07 +02:00
|
|
|
type = lib.types.str;
|
2016-09-27 22:56:58 +02:00
|
|
|
default = "/coreos.com/network";
|
|
|
|
};
|
|
|
|
|
2024-08-28 21:19:07 +02:00
|
|
|
caFile = lib.mkOption {
|
2016-09-27 22:56:58 +02:00
|
|
|
description = "Etcd certificate authority file";
|
2024-08-28 21:19:07 +02:00
|
|
|
type = lib.types.nullOr lib.types.path;
|
2016-09-27 22:56:58 +02:00
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
|
2024-08-28 21:19:07 +02:00
|
|
|
certFile = lib.mkOption {
|
2016-09-27 22:56:58 +02:00
|
|
|
description = "Etcd cert file";
|
2024-08-28 21:19:07 +02:00
|
|
|
type = lib.types.nullOr lib.types.path;
|
2016-09-27 22:56:58 +02:00
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
|
2024-08-28 21:19:07 +02:00
|
|
|
keyFile = lib.mkOption {
|
2016-09-27 22:56:58 +02:00
|
|
|
description = "Etcd key file";
|
2024-08-28 21:19:07 +02:00
|
|
|
type = lib.types.nullOr lib.types.path;
|
2016-09-27 22:56:58 +02:00
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-08-28 21:19:07 +02:00
|
|
|
kubeconfig = lib.mkOption {
|
2019-02-11 13:47:45 +01:00
|
|
|
description = ''
|
|
|
|
Path to kubeconfig to use for storing flannel config using the
|
|
|
|
Kubernetes API
|
|
|
|
'';
|
2024-08-28 21:19:07 +02:00
|
|
|
type = lib.types.nullOr lib.types.path;
|
2019-02-11 13:47:45 +01:00
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
|
2024-08-28 21:19:07 +02:00
|
|
|
network = lib.mkOption {
|
2022-08-29 19:33:50 +02:00
|
|
|
description = " IPv4 network in CIDR format to use for the entire flannel network.";
|
2024-08-28 21:19:07 +02:00
|
|
|
type = lib.types.str;
|
2016-09-27 22:56:58 +02:00
|
|
|
};
|
|
|
|
|
2024-08-28 21:19:07 +02:00
|
|
|
nodeName = lib.mkOption {
|
2019-02-13 17:17:52 +01:00
|
|
|
description = ''
|
|
|
|
Needed when running with Kubernetes as backend as this cannot be auto-detected";
|
|
|
|
'';
|
2024-08-28 21:19:07 +02:00
|
|
|
type = lib.types.nullOr lib.types.str;
|
2022-10-06 16:22:27 +02:00
|
|
|
default = config.networking.fqdnOrHostName;
|
2024-08-28 21:19:07 +02:00
|
|
|
defaultText = lib.literalExpression "config.networking.fqdnOrHostName";
|
2019-02-13 17:17:52 +01:00
|
|
|
example = "node1.example.com";
|
|
|
|
};
|
|
|
|
|
2024-08-28 21:19:07 +02:00
|
|
|
storageBackend = lib.mkOption {
|
2019-02-11 13:47:45 +01:00
|
|
|
description = "Determines where flannel stores its configuration at runtime";
|
2024-08-28 21:19:07 +02:00
|
|
|
type = lib.types.enum [
|
|
|
|
"etcd"
|
|
|
|
"kubernetes"
|
|
|
|
];
|
2019-02-11 13:47:45 +01:00
|
|
|
default = "etcd";
|
|
|
|
};
|
|
|
|
|
2024-08-28 21:19:07 +02:00
|
|
|
subnetLen = lib.mkOption {
|
2016-09-27 22:56:58 +02:00
|
|
|
description = ''
|
|
|
|
The size of the subnet allocated to each host. Defaults to 24 (i.e. /24)
|
|
|
|
unless the Network was configured to be smaller than a /24 in which case
|
|
|
|
it is one less than the network.
|
|
|
|
'';
|
2024-08-28 21:19:07 +02:00
|
|
|
type = lib.types.int;
|
2016-09-27 22:56:58 +02:00
|
|
|
default = 24;
|
|
|
|
};
|
|
|
|
|
2024-08-28 21:19:07 +02:00
|
|
|
subnetMin = lib.mkOption {
|
2016-09-27 22:56:58 +02:00
|
|
|
description = ''
|
|
|
|
The beginning of IP range which the subnet allocation should start with.
|
|
|
|
Defaults to the first subnet of Network.
|
|
|
|
'';
|
2024-08-28 21:19:07 +02:00
|
|
|
type = lib.types.nullOr lib.types.str;
|
2016-09-27 22:56:58 +02:00
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
|
2024-08-28 21:19:07 +02:00
|
|
|
subnetMax = lib.mkOption {
|
2016-09-27 22:56:58 +02:00
|
|
|
description = ''
|
|
|
|
The end of IP range which the subnet allocation should start with.
|
|
|
|
Defaults to the last subnet of Network.
|
|
|
|
'';
|
2024-08-28 21:19:07 +02:00
|
|
|
type = lib.types.nullOr lib.types.str;
|
2016-09-27 22:56:58 +02:00
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
|
2024-08-28 21:19:07 +02:00
|
|
|
backend = lib.mkOption {
|
2016-09-27 22:56:58 +02:00
|
|
|
description = "Type of backend to use and specific configurations for that backend.";
|
2024-08-28 21:19:07 +02:00
|
|
|
type = lib.types.attrs;
|
2016-09-27 22:56:58 +02:00
|
|
|
default = {
|
|
|
|
Type = "vxlan";
|
|
|
|
};
|
|
|
|
};
|
2024-12-15 09:15:44 +01:00
|
|
|
|
|
|
|
extraNetworkConfig = lib.mkOption {
|
|
|
|
description = "Extra configuration to be added to the net-conf.json/etcd-backed network configuration.";
|
|
|
|
type = (pkgs.formats.json { }).type;
|
|
|
|
default = { };
|
|
|
|
example = {
|
|
|
|
EnableIPv6 = true;
|
|
|
|
};
|
|
|
|
};
|
2016-09-27 22:56:58 +02:00
|
|
|
};
|
|
|
|
|
2024-08-28 21:19:07 +02:00
|
|
|
config = lib.mkIf cfg.enable {
|
2016-09-27 22:56:58 +02:00
|
|
|
systemd.services.flannel = {
|
|
|
|
description = "Flannel Service";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
|
|
|
environment =
|
|
|
|
{
|
|
|
|
FLANNELD_PUBLIC_IP = cfg.publicIp;
|
2019-02-11 13:47:45 +01:00
|
|
|
FLANNELD_IFACE = cfg.iface;
|
2024-08-28 21:19:07 +02:00
|
|
|
}
|
|
|
|
// lib.optionalAttrs (cfg.storageBackend == "etcd") {
|
|
|
|
FLANNELD_ETCD_ENDPOINTS = lib.concatStringsSep "," cfg.etcd.endpoints;
|
2016-09-27 22:56:58 +02:00
|
|
|
FLANNELD_ETCD_KEYFILE = cfg.etcd.keyFile;
|
|
|
|
FLANNELD_ETCD_CERTFILE = cfg.etcd.certFile;
|
|
|
|
FLANNELD_ETCD_CAFILE = cfg.etcd.caFile;
|
2022-07-19 08:09:42 +02:00
|
|
|
ETCDCTL_CERT = cfg.etcd.certFile;
|
|
|
|
ETCDCTL_KEY = cfg.etcd.keyFile;
|
|
|
|
ETCDCTL_CACERT = cfg.etcd.caFile;
|
2024-08-28 21:19:07 +02:00
|
|
|
ETCDCTL_ENDPOINTS = lib.concatStringsSep "," cfg.etcd.endpoints;
|
2022-07-19 08:09:42 +02:00
|
|
|
ETCDCTL_API = "3";
|
2024-08-28 21:19:07 +02:00
|
|
|
}
|
|
|
|
// lib.optionalAttrs (cfg.storageBackend == "kubernetes") {
|
2019-02-11 13:47:45 +01:00
|
|
|
FLANNELD_KUBE_SUBNET_MGR = "true";
|
|
|
|
FLANNELD_KUBECONFIG_FILE = cfg.kubeconfig;
|
2019-02-13 17:17:52 +01:00
|
|
|
NODE_NAME = cfg.nodeName;
|
2016-09-27 22:56:58 +02:00
|
|
|
};
|
2019-03-12 14:58:01 +00:00
|
|
|
path = [ pkgs.iptables ];
|
2024-08-28 21:19:07 +02:00
|
|
|
preStart = lib.optionalString (cfg.storageBackend == "etcd") ''
|
2016-09-27 22:56:58 +02:00
|
|
|
echo "setting network configuration"
|
2022-07-19 08:09:42 +02:00
|
|
|
until ${pkgs.etcd}/bin/etcdctl put /coreos.com/network/config '${builtins.toJSON networkConfig}'
|
2016-09-27 22:56:58 +02:00
|
|
|
do
|
|
|
|
echo "setting network configuration, retry"
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
'';
|
2019-02-14 10:28:51 +01:00
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = "${cfg.package}/bin/flannel";
|
|
|
|
Restart = "always";
|
|
|
|
RestartSec = "10s";
|
2021-02-25 16:00:59 +01:00
|
|
|
RuntimeDirectory = "flannel";
|
2019-02-14 10:28:51 +01:00
|
|
|
};
|
2016-09-27 22:56:58 +02:00
|
|
|
};
|
|
|
|
|
2024-12-15 09:15:44 +01:00
|
|
|
boot.kernelModules = [ "br_netfilter" ];
|
|
|
|
|
2024-08-28 21:19:07 +02:00
|
|
|
services.etcd.enable = lib.mkDefault (
|
|
|
|
cfg.storageBackend == "etcd" && cfg.etcd.endpoints == [ "http://127.0.0.1:2379" ]
|
|
|
|
);
|
2019-02-11 13:47:45 +01:00
|
|
|
|
|
|
|
# for some reason, flannel doesn't let you configure this path
|
|
|
|
# see: https://github.com/coreos/flannel/blob/master/Documentation/configuration.md#configuration
|
2024-08-28 21:19:07 +02:00
|
|
|
environment.etc."kube-flannel/net-conf.json" = lib.mkIf (cfg.storageBackend == "kubernetes") {
|
2019-02-11 13:47:45 +01:00
|
|
|
source = pkgs.writeText "net-conf.json" (builtins.toJSON networkConfig);
|
|
|
|
};
|
2016-09-27 22:56:58 +02:00
|
|
|
};
|
|
|
|
}
|