2018-07-22 12:14:20 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
top = config.services.kubernetes;
|
|
|
|
cfg = top.flannel;
|
|
|
|
|
2019-02-12 15:48:23 +00:00
|
|
|
# we want flannel to use kubernetes itself as configuration backend, not direct etcd
|
|
|
|
storageBackend = "kubernetes";
|
|
|
|
|
2018-07-22 12:14:20 +01:00
|
|
|
# needed for flannel to pass options to docker
|
|
|
|
mkDockerOpts = pkgs.runCommand "mk-docker-opts" {
|
|
|
|
buildInputs = [ pkgs.makeWrapper ];
|
|
|
|
} ''
|
|
|
|
mkdir -p $out
|
|
|
|
|
|
|
|
# bashInteractive needed for `compgen`
|
2019-08-24 12:22:47 +01:00
|
|
|
makeWrapper ${pkgs.bashInteractive}/bin/bash $out/mk-docker-opts --add-flags "${pkgs.kubernetes}/bin/mk-docker-opts.sh"
|
2018-07-22 12:14:20 +01:00
|
|
|
'';
|
|
|
|
in
|
|
|
|
{
|
|
|
|
###### interface
|
|
|
|
options.services.kubernetes.flannel = {
|
2019-08-24 11:52:32 +01:00
|
|
|
enable = mkEnableOption "enable flannel networking";
|
2018-07-22 12:14:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
2019-08-24 11:52:32 +01:00
|
|
|
config = mkIf cfg.enable {
|
2018-07-22 12:14:20 +01:00
|
|
|
services.flannel = {
|
|
|
|
|
|
|
|
enable = mkDefault true;
|
|
|
|
network = mkDefault top.clusterCidr;
|
2019-08-24 11:52:32 +01:00
|
|
|
inherit storageBackend;
|
|
|
|
nodeName = config.services.kubernetes.kubelet.hostname;
|
2018-07-22 12:14:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
services.kubernetes.kubelet = {
|
|
|
|
networkPlugin = mkDefault "cni";
|
|
|
|
cni.config = mkDefault [{
|
|
|
|
name = "mynet";
|
|
|
|
type = "flannel";
|
2019-11-15 04:58:35 +00:00
|
|
|
cniVersion = "0.3.1";
|
2018-07-22 12:14:20 +01:00
|
|
|
delegate = {
|
|
|
|
isDefaultGateway = true;
|
|
|
|
bridge = "docker0";
|
|
|
|
};
|
|
|
|
}];
|
|
|
|
};
|
|
|
|
|
2019-09-09 00:38:31 +01:00
|
|
|
systemd.services.mk-docker-opts = {
|
2018-07-22 12:14:20 +01:00
|
|
|
description = "Pre-Docker Actions";
|
|
|
|
path = with pkgs; [ gawk gnugrep ];
|
|
|
|
script = ''
|
|
|
|
${mkDockerOpts}/mk-docker-opts -d /run/flannel/docker
|
2019-02-14 09:28:51 +00:00
|
|
|
systemctl restart docker
|
2018-07-22 12:14:20 +01:00
|
|
|
'';
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
};
|
2019-02-14 09:28:51 +00:00
|
|
|
|
2019-09-09 00:38:31 +01:00
|
|
|
systemd.paths.flannel-subnet-env = {
|
2019-03-11 09:53:59 +00:00
|
|
|
wantedBy = [ "flannel.service" ];
|
|
|
|
pathConfig = {
|
2019-08-24 11:52:32 +01:00
|
|
|
PathModified = "/run/flannel/subnet.env";
|
|
|
|
Unit = "mk-docker-opts.service";
|
2019-03-11 09:53:59 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-02-20 19:52:36 +00:00
|
|
|
systemd.services.docker = {
|
|
|
|
environment.DOCKER_OPTS = "-b none";
|
2019-02-20 23:26:11 +00:00
|
|
|
serviceConfig.EnvironmentFile = "-/run/flannel/docker";
|
2019-02-20 19:52:36 +00:00
|
|
|
};
|
2018-07-22 12:14:20 +01:00
|
|
|
|
|
|
|
# read environment variables generated by mk-docker-opts
|
|
|
|
virtualisation.docker.extraOptions = "$DOCKER_OPTS";
|
|
|
|
|
|
|
|
networking = {
|
|
|
|
firewall.allowedUDPPorts = [
|
|
|
|
8285 # flannel udp
|
|
|
|
8472 # flannel vxlan
|
|
|
|
];
|
|
|
|
dhcpcd.denyInterfaces = [ "docker*" "flannel*" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
services.kubernetes.pki.certs = {
|
2019-02-12 15:48:23 +00:00
|
|
|
flannelClient = top.lib.mkCert {
|
|
|
|
name = "flannel-client";
|
|
|
|
CN = "flannel-client";
|
2018-07-22 12:14:20 +01:00
|
|
|
action = "systemctl restart flannel.service";
|
|
|
|
};
|
|
|
|
};
|
2019-02-12 15:48:23 +00:00
|
|
|
|
|
|
|
# give flannel som kubernetes rbac permissions if applicable
|
2019-03-06 15:44:38 +00:00
|
|
|
services.kubernetes.addonManager.bootstrapAddons = mkIf ((storageBackend == "kubernetes") && (elem "RBAC" top.apiserver.authorizationMode)) {
|
2019-08-24 11:52:32 +01:00
|
|
|
|
2019-03-06 15:44:38 +00:00
|
|
|
flannel-cr = {
|
|
|
|
apiVersion = "rbac.authorization.k8s.io/v1beta1";
|
|
|
|
kind = "ClusterRole";
|
|
|
|
metadata = { name = "flannel"; };
|
|
|
|
rules = [{
|
|
|
|
apiGroups = [ "" ];
|
|
|
|
resources = [ "pods" ];
|
|
|
|
verbs = [ "get" ];
|
|
|
|
}
|
|
|
|
{
|
|
|
|
apiGroups = [ "" ];
|
|
|
|
resources = [ "nodes" ];
|
|
|
|
verbs = [ "list" "watch" ];
|
|
|
|
}
|
|
|
|
{
|
|
|
|
apiGroups = [ "" ];
|
|
|
|
resources = [ "nodes/status" ];
|
|
|
|
verbs = [ "patch" ];
|
|
|
|
}];
|
|
|
|
};
|
2019-02-12 15:48:23 +00:00
|
|
|
|
2019-03-06 15:44:38 +00:00
|
|
|
flannel-crb = {
|
|
|
|
apiVersion = "rbac.authorization.k8s.io/v1beta1";
|
|
|
|
kind = "ClusterRoleBinding";
|
|
|
|
metadata = { name = "flannel"; };
|
|
|
|
roleRef = {
|
|
|
|
apiGroup = "rbac.authorization.k8s.io";
|
|
|
|
kind = "ClusterRole";
|
|
|
|
name = "flannel";
|
|
|
|
};
|
|
|
|
subjects = [{
|
|
|
|
kind = "User";
|
|
|
|
name = "flannel-client";
|
|
|
|
}];
|
2019-03-01 07:44:45 +00:00
|
|
|
};
|
2019-08-24 11:52:32 +01:00
|
|
|
|
2019-02-12 15:48:23 +00:00
|
|
|
};
|
2019-03-06 15:44:38 +00:00
|
|
|
};
|
2018-07-22 12:14:20 +01:00
|
|
|
}
|