1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.flannel;
7
8 networkConfig = filterAttrs (n: v: v != null) {
9 Network = cfg.network;
10 SubnetLen = cfg.subnetLen;
11 SubnetMin = cfg.subnetMin;
12 SubnetMax = cfg.subnetMax;
13 Backend = cfg.backend;
14 };
15in {
16 options.services.flannel = {
17 enable = mkEnableOption "flannel";
18
19 package = mkOption {
20 description = "Package to use for flannel";
21 type = types.package;
22 default = pkgs.flannel.bin;
23 defaultText = "pkgs.flannel.bin";
24 };
25
26 publicIp = mkOption {
27 description = ''
28 IP accessible by other nodes for inter-host communication.
29 Defaults to the IP of the interface being used for communication.
30 '';
31 type = types.nullOr types.str;
32 default = null;
33 };
34
35 iface = mkOption {
36 description = ''
37 Interface to use (IP or name) for inter-host communication.
38 Defaults to the interface for the default route on the machine.
39 '';
40 type = types.nullOr types.str;
41 default = null;
42 };
43
44 etcd = {
45 endpoints = mkOption {
46 description = "Etcd endpoints";
47 type = types.listOf types.str;
48 default = ["http://127.0.0.1:2379"];
49 };
50
51 prefix = mkOption {
52 description = "Etcd key prefix";
53 type = types.str;
54 default = "/coreos.com/network";
55 };
56
57 caFile = mkOption {
58 description = "Etcd certificate authority file";
59 type = types.nullOr types.path;
60 default = null;
61 };
62
63 certFile = mkOption {
64 description = "Etcd cert file";
65 type = types.nullOr types.path;
66 default = null;
67 };
68
69 keyFile = mkOption {
70 description = "Etcd key file";
71 type = types.nullOr types.path;
72 default = null;
73 };
74 };
75
76 network = mkOption {
77 description = " IPv4 network in CIDR format to use for the entire flannel network.";
78 type = types.str;
79 };
80
81 subnetLen = mkOption {
82 description = ''
83 The size of the subnet allocated to each host. Defaults to 24 (i.e. /24)
84 unless the Network was configured to be smaller than a /24 in which case
85 it is one less than the network.
86 '';
87 type = types.int;
88 default = 24;
89 };
90
91 subnetMin = mkOption {
92 description = ''
93 The beginning of IP range which the subnet allocation should start with.
94 Defaults to the first subnet of Network.
95 '';
96 type = types.nullOr types.str;
97 default = null;
98 };
99
100 subnetMax = mkOption {
101 description = ''
102 The end of IP range which the subnet allocation should start with.
103 Defaults to the last subnet of Network.
104 '';
105 type = types.nullOr types.str;
106 default = null;
107 };
108
109 backend = mkOption {
110 description = "Type of backend to use and specific configurations for that backend.";
111 type = types.attrs;
112 default = {
113 Type = "vxlan";
114 };
115 };
116 };
117
118 config = mkIf cfg.enable {
119 systemd.services.flannel = {
120 description = "Flannel Service";
121 wantedBy = [ "multi-user.target" ];
122 after = [ "network.target" ];
123 environment = {
124 FLANNELD_PUBLIC_IP = cfg.publicIp;
125 FLANNELD_ETCD_ENDPOINTS = concatStringsSep "," cfg.etcd.endpoints;
126 FLANNELD_ETCD_KEYFILE = cfg.etcd.keyFile;
127 FLANNELD_ETCD_CERTFILE = cfg.etcd.certFile;
128 FLANNELD_ETCD_CAFILE = cfg.etcd.caFile;
129 FLANNELD_IFACE = cfg.iface;
130 ETCDCTL_CERT_FILE = cfg.etcd.certFile;
131 ETCDCTL_KEY_FILE = cfg.etcd.keyFile;
132 ETCDCTL_CA_FILE = cfg.etcd.caFile;
133 ETCDCTL_PEERS = concatStringsSep "," cfg.etcd.endpoints;
134 };
135 preStart = ''
136 echo "setting network configuration"
137 until ${pkgs.etcdctl.bin}/bin/etcdctl set /coreos.com/network/config '${builtins.toJSON networkConfig}'
138 do
139 echo "setting network configuration, retry"
140 sleep 1
141 done
142 '';
143 postStart = ''
144 while [ ! -f /run/flannel/subnet.env ]
145 do
146 sleep 1
147 done
148 '';
149 serviceConfig.ExecStart = "${cfg.package}/bin/flannel";
150 };
151
152 services.etcd.enable = mkDefault (cfg.etcd.endpoints == ["http://127.0.0.1:2379"]);
153 };
154}