at 21.11-pre 5.7 kB view raw
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; 23 defaultText = "pkgs.flannel"; 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 kubeconfig = mkOption { 77 description = '' 78 Path to kubeconfig to use for storing flannel config using the 79 Kubernetes API 80 ''; 81 type = types.nullOr types.path; 82 default = null; 83 }; 84 85 network = mkOption { 86 description = " IPv4 network in CIDR format to use for the entire flannel network."; 87 type = types.str; 88 }; 89 90 nodeName = mkOption { 91 description = '' 92 Needed when running with Kubernetes as backend as this cannot be auto-detected"; 93 ''; 94 type = types.nullOr types.str; 95 default = with config.networking; (hostName + optionalString (domain != null) ".${domain}"); 96 example = "node1.example.com"; 97 }; 98 99 storageBackend = mkOption { 100 description = "Determines where flannel stores its configuration at runtime"; 101 type = types.enum ["etcd" "kubernetes"]; 102 default = "etcd"; 103 }; 104 105 subnetLen = mkOption { 106 description = '' 107 The size of the subnet allocated to each host. Defaults to 24 (i.e. /24) 108 unless the Network was configured to be smaller than a /24 in which case 109 it is one less than the network. 110 ''; 111 type = types.int; 112 default = 24; 113 }; 114 115 subnetMin = mkOption { 116 description = '' 117 The beginning of IP range which the subnet allocation should start with. 118 Defaults to the first subnet of Network. 119 ''; 120 type = types.nullOr types.str; 121 default = null; 122 }; 123 124 subnetMax = mkOption { 125 description = '' 126 The end of IP range which the subnet allocation should start with. 127 Defaults to the last subnet of Network. 128 ''; 129 type = types.nullOr types.str; 130 default = null; 131 }; 132 133 backend = mkOption { 134 description = "Type of backend to use and specific configurations for that backend."; 135 type = types.attrs; 136 default = { 137 Type = "vxlan"; 138 }; 139 }; 140 }; 141 142 config = mkIf cfg.enable { 143 systemd.services.flannel = { 144 description = "Flannel Service"; 145 wantedBy = [ "multi-user.target" ]; 146 after = [ "network.target" ]; 147 environment = { 148 FLANNELD_PUBLIC_IP = cfg.publicIp; 149 FLANNELD_IFACE = cfg.iface; 150 } // optionalAttrs (cfg.storageBackend == "etcd") { 151 FLANNELD_ETCD_ENDPOINTS = concatStringsSep "," cfg.etcd.endpoints; 152 FLANNELD_ETCD_KEYFILE = cfg.etcd.keyFile; 153 FLANNELD_ETCD_CERTFILE = cfg.etcd.certFile; 154 FLANNELD_ETCD_CAFILE = cfg.etcd.caFile; 155 ETCDCTL_CERT_FILE = cfg.etcd.certFile; 156 ETCDCTL_KEY_FILE = cfg.etcd.keyFile; 157 ETCDCTL_CA_FILE = cfg.etcd.caFile; 158 ETCDCTL_PEERS = concatStringsSep "," cfg.etcd.endpoints; 159 } // optionalAttrs (cfg.storageBackend == "kubernetes") { 160 FLANNELD_KUBE_SUBNET_MGR = "true"; 161 FLANNELD_KUBECONFIG_FILE = cfg.kubeconfig; 162 NODE_NAME = cfg.nodeName; 163 }; 164 path = [ pkgs.iptables ]; 165 preStart = optionalString (cfg.storageBackend == "etcd") '' 166 echo "setting network configuration" 167 until ${pkgs.etcdctl}/bin/etcdctl set /coreos.com/network/config '${builtins.toJSON networkConfig}' 168 do 169 echo "setting network configuration, retry" 170 sleep 1 171 done 172 ''; 173 serviceConfig = { 174 ExecStart = "${cfg.package}/bin/flannel"; 175 Restart = "always"; 176 RestartSec = "10s"; 177 RuntimeDirectory = "flannel"; 178 }; 179 }; 180 181 services.etcd.enable = mkDefault (cfg.storageBackend == "etcd" && cfg.etcd.endpoints == ["http://127.0.0.1:2379"]); 182 183 # for some reason, flannel doesn't let you configure this path 184 # see: https://github.com/coreos/flannel/blob/master/Documentation/configuration.md#configuration 185 environment.etc."kube-flannel/net-conf.json" = mkIf (cfg.storageBackend == "kubernetes") { 186 source = pkgs.writeText "net-conf.json" (builtins.toJSON networkConfig); 187 }; 188 }; 189}