2018-07-22 13:14:20 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
top = config.services.kubernetes;
|
|
|
|
cfg = top.flannel;
|
|
|
|
|
2019-02-12 16:48:23 +01:00
|
|
|
# we want flannel to use kubernetes itself as configuration backend, not direct etcd
|
|
|
|
storageBackend = "kubernetes";
|
|
|
|
|
2018-07-22 13:14:20 +02:00
|
|
|
# needed for flannel to pass options to docker
|
|
|
|
mkDockerOpts = pkgs.runCommand "mk-docker-opts" {
|
|
|
|
buildInputs = [ pkgs.makeWrapper ];
|
|
|
|
} ''
|
|
|
|
mkdir -p $out
|
|
|
|
cp ${pkgs.kubernetes.src}/cluster/centos/node/bin/mk-docker-opts.sh $out/mk-docker-opts.sh
|
|
|
|
|
|
|
|
# bashInteractive needed for `compgen`
|
|
|
|
makeWrapper ${pkgs.bashInteractive}/bin/bash $out/mk-docker-opts --add-flags "$out/mk-docker-opts.sh"
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
{
|
|
|
|
###### interface
|
|
|
|
options.services.kubernetes.flannel = {
|
2019-04-20 03:41:48 +02:00
|
|
|
enable = mkEnableOption "flannel networking";
|
2019-03-11 10:53:59 +01:00
|
|
|
kubeconfig = top.lib.mkKubeConfigOptions "Kubernetes flannel";
|
2018-07-22 13:14:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
2019-03-11 10:53:59 +01:00
|
|
|
config = let
|
|
|
|
|
|
|
|
flannelPaths = filter (a: a != null) [
|
|
|
|
cfg.kubeconfig.caFile
|
|
|
|
cfg.kubeconfig.certFile
|
|
|
|
cfg.kubeconfig.keyFile
|
|
|
|
];
|
|
|
|
kubeconfig = top.lib.mkKubeConfig "flannel" cfg.kubeconfig;
|
|
|
|
|
|
|
|
in mkIf cfg.enable {
|
2018-07-22 13:14:20 +02:00
|
|
|
services.flannel = {
|
|
|
|
|
|
|
|
enable = mkDefault true;
|
|
|
|
network = mkDefault top.clusterCidr;
|
2019-03-11 10:53:59 +01:00
|
|
|
inherit storageBackend kubeconfig;
|
|
|
|
nodeName = top.kubelet.hostname;
|
2018-07-22 13:14:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
services.kubernetes.kubelet = {
|
|
|
|
networkPlugin = mkDefault "cni";
|
|
|
|
cni.config = mkDefault [{
|
|
|
|
name = "mynet";
|
|
|
|
type = "flannel";
|
|
|
|
delegate = {
|
|
|
|
isDefaultGateway = true;
|
|
|
|
bridge = "docker0";
|
|
|
|
};
|
|
|
|
}];
|
|
|
|
};
|
|
|
|
|
2019-03-01 08:44:45 +01:00
|
|
|
systemd.services.mk-docker-opts = {
|
2018-07-22 13:14:20 +02:00
|
|
|
description = "Pre-Docker Actions";
|
2019-03-01 08:44:45 +01:00
|
|
|
wantedBy = [ "flannel.target" ];
|
|
|
|
before = [ "flannel.target" ];
|
2018-07-22 13:14:20 +02:00
|
|
|
path = with pkgs; [ gawk gnugrep ];
|
|
|
|
script = ''
|
|
|
|
${mkDockerOpts}/mk-docker-opts -d /run/flannel/docker
|
2019-02-14 10:28:51 +01:00
|
|
|
systemctl restart docker
|
2018-07-22 13:14:20 +02:00
|
|
|
'';
|
2019-03-01 07:56:59 +01:00
|
|
|
unitConfig.ConditionPathExists = [ "/run/flannel/subnet.env" ];
|
2018-07-22 13:14:20 +02:00
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
};
|
2019-02-14 10:28:51 +01:00
|
|
|
|
2019-03-01 07:56:59 +01:00
|
|
|
systemd.paths.flannel-subnet-env = {
|
|
|
|
wantedBy = [ "mk-docker-opts.service" ];
|
2019-02-14 10:28:51 +01:00
|
|
|
pathConfig = {
|
2019-03-01 07:56:59 +01:00
|
|
|
PathExists = [ "/run/flannel/subnet.env" ];
|
|
|
|
PathChanged = [ "/run/flannel/subnet.env" ];
|
2019-02-14 10:28:51 +01:00
|
|
|
Unit = "mk-docker-opts.service";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-03-01 08:44:45 +01:00
|
|
|
systemd.targets.flannel = {
|
2019-03-06 17:17:20 +01:00
|
|
|
wantedBy = [ "kube-node-online.target" ];
|
|
|
|
before = [ "kube-node-online.target" ];
|
2019-03-01 08:44:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.flannel = {
|
|
|
|
wantedBy = [ "flannel.target" ];
|
|
|
|
after = [ "kubelet.target" ];
|
|
|
|
before = [ "flannel.target" ];
|
2019-03-11 10:53:59 +01:00
|
|
|
path = with pkgs; [ iptables kubectl ];
|
|
|
|
environment.KUBECONFIG = kubeconfig;
|
|
|
|
preStart = let
|
|
|
|
args = [
|
|
|
|
"--selector=kubernetes.io/hostname=${top.kubelet.hostname}"
|
|
|
|
# flannel exits if node is not registered yet, before that there is no podCIDR
|
|
|
|
"--output=jsonpath={.items[0].spec.podCIDR}"
|
|
|
|
# if jsonpath cannot be resolved exit with status 1
|
|
|
|
"--allow-missing-template-keys=false"
|
|
|
|
];
|
|
|
|
in ''
|
|
|
|
until kubectl get nodes ${concatStringsSep " " args} 2>/dev/null; do
|
|
|
|
echo Waiting for ${top.kubelet.hostname} to be RegisteredNode
|
|
|
|
sleep 1
|
|
|
|
done
|
2019-03-06 16:52:27 +01:00
|
|
|
'';
|
2019-03-11 10:53:59 +01:00
|
|
|
unitConfig.ConditionPathExists = flannelPaths;
|
2019-03-01 08:44:45 +01:00
|
|
|
};
|
|
|
|
|
2019-03-11 10:53:59 +01:00
|
|
|
systemd.paths.flannel = {
|
|
|
|
wantedBy = [ "flannel.service" ];
|
|
|
|
pathConfig = {
|
|
|
|
PathExists = flannelPaths;
|
|
|
|
PathChanged = flannelPaths;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
services.kubernetes.flannel.kubeconfig.server = mkDefault top.apiserverAddress;
|
|
|
|
|
2019-02-20 20:52:36 +01:00
|
|
|
systemd.services.docker = {
|
|
|
|
environment.DOCKER_OPTS = "-b none";
|
2019-02-21 00:26:11 +01:00
|
|
|
serviceConfig.EnvironmentFile = "-/run/flannel/docker";
|
2019-02-20 20:52:36 +01:00
|
|
|
};
|
2018-07-22 13:14:20 +02: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 16:48:23 +01:00
|
|
|
flannelClient = top.lib.mkCert {
|
|
|
|
name = "flannel-client";
|
|
|
|
CN = "flannel-client";
|
2018-07-22 13:14:20 +02:00
|
|
|
action = "systemctl restart flannel.service";
|
|
|
|
};
|
|
|
|
};
|
2019-02-12 16:48:23 +01:00
|
|
|
|
|
|
|
# give flannel som kubernetes rbac permissions if applicable
|
2019-03-06 16:44:38 +01:00
|
|
|
services.kubernetes.addonManager.bootstrapAddons = mkIf ((storageBackend == "kubernetes") && (elem "RBAC" top.apiserver.authorizationMode)) {
|
|
|
|
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 16:48:23 +01:00
|
|
|
|
2019-03-06 16:44:38 +01: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 08:44:45 +01:00
|
|
|
};
|
2019-02-12 16:48:23 +01:00
|
|
|
};
|
2019-03-06 16:44:38 +01:00
|
|
|
};
|
2018-07-22 13:14:20 +02:00
|
|
|
}
|