2018-11-11 09:41:11 +01:00
|
|
|
{ system ? builtins.currentSystem,
|
|
|
|
config ? {},
|
2018-12-12 21:30:32 +01:00
|
|
|
pkgs ? import ../../.. { inherit system config; }
|
2018-11-11 09:41:11 +01:00
|
|
|
}:
|
2017-09-09 02:00:35 +02:00
|
|
|
|
2020-05-14 15:14:02 +02:00
|
|
|
with import ../../lib/testing-python.nix { inherit system pkgs; };
|
2017-09-09 02:00:35 +02:00
|
|
|
with pkgs.lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
mkKubernetesBaseTest =
|
|
|
|
{ name, domain ? "my.zyx", test, machines
|
|
|
|
, extraConfiguration ? null }:
|
|
|
|
let
|
|
|
|
masterName = head (filter (machineName: any (role: role == "master") machines.${machineName}.roles) (attrNames machines));
|
|
|
|
master = machines.${masterName};
|
|
|
|
extraHosts = ''
|
|
|
|
${master.ip} etcd.${domain}
|
|
|
|
${master.ip} api.${domain}
|
|
|
|
${concatMapStringsSep "\n" (machineName: "${machines.${machineName}.ip} ${machineName}.${domain}") (attrNames machines)}
|
|
|
|
'';
|
2018-07-22 13:14:20 +02:00
|
|
|
kubectl = with pkgs; runCommand "wrap-kubectl" { buildInputs = [ makeWrapper ]; } ''
|
|
|
|
mkdir -p $out/bin
|
|
|
|
makeWrapper ${pkgs.kubernetes}/bin/kubectl $out/bin/kubectl --set KUBECONFIG "/etc/kubernetes/cluster-admin.kubeconfig"
|
|
|
|
'';
|
2017-09-09 02:00:35 +02:00
|
|
|
in makeTest {
|
|
|
|
inherit name;
|
|
|
|
|
|
|
|
nodes = mapAttrs (machineName: machine:
|
|
|
|
{ config, pkgs, lib, nodes, ... }:
|
|
|
|
mkMerge [
|
|
|
|
{
|
2019-08-24 12:52:32 +02:00
|
|
|
boot.postBootCommands = "rm -fr /var/lib/kubernetes/secrets /tmp/shared/*";
|
2018-05-23 16:37:09 +02:00
|
|
|
virtualisation.memorySize = mkDefault 1536;
|
2017-09-09 02:00:35 +02:00
|
|
|
virtualisation.diskSize = mkDefault 4096;
|
|
|
|
networking = {
|
|
|
|
inherit domain extraHosts;
|
|
|
|
primaryIPAddress = mkForce machine.ip;
|
|
|
|
|
|
|
|
firewall = {
|
|
|
|
allowedTCPPorts = [
|
|
|
|
10250 # kubelet
|
|
|
|
];
|
2021-06-28 21:33:17 +02:00
|
|
|
trustedInterfaces = ["mynet"];
|
2017-09-09 02:00:35 +02:00
|
|
|
|
|
|
|
extraCommands = concatMapStrings (node: ''
|
|
|
|
iptables -A INPUT -s ${node.config.networking.primaryIPAddress} -j ACCEPT
|
|
|
|
'') (attrValues nodes);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
programs.bash.enableCompletion = true;
|
2018-07-22 13:14:20 +02:00
|
|
|
environment.systemPackages = [ kubectl ];
|
2017-09-09 02:00:35 +02:00
|
|
|
services.flannel.iface = "eth1";
|
2018-07-22 13:14:20 +02:00
|
|
|
services.kubernetes = {
|
|
|
|
addons.dashboard.enable = true;
|
2019-11-15 05:58:35 +01:00
|
|
|
proxy.hostname = "${masterName}.${domain}";
|
2018-07-22 13:14:20 +02:00
|
|
|
|
|
|
|
easyCerts = true;
|
|
|
|
inherit (machine) roles;
|
|
|
|
apiserver = {
|
|
|
|
securePort = 443;
|
|
|
|
advertiseAddress = master.ip;
|
|
|
|
};
|
|
|
|
masterAddress = "${masterName}.${config.networking.domain}";
|
2021-06-28 21:33:17 +02:00
|
|
|
# workaround for:
|
|
|
|
# https://github.com/kubernetes/kubernetes/issues/102676
|
|
|
|
# (workaround from) https://github.com/kubernetes/kubernetes/issues/95488
|
|
|
|
kubelet.extraOpts = ''\
|
|
|
|
--cgroups-per-qos=false \
|
|
|
|
--enforce-node-allocatable="" \
|
|
|
|
'';
|
2018-07-22 13:14:20 +02:00
|
|
|
};
|
2017-09-09 02:00:35 +02:00
|
|
|
}
|
|
|
|
(optionalAttrs (any (role: role == "master") machine.roles) {
|
|
|
|
networking.firewall.allowedTCPPorts = [
|
|
|
|
443 # kubernetes apiserver
|
|
|
|
];
|
|
|
|
})
|
2019-08-13 23:52:01 +02:00
|
|
|
(optionalAttrs (machine ? extraConfiguration) (machine.extraConfiguration { inherit config pkgs lib nodes; }))
|
2017-09-09 02:00:35 +02:00
|
|
|
(optionalAttrs (extraConfiguration != null) (extraConfiguration { inherit config pkgs lib nodes; }))
|
|
|
|
]
|
|
|
|
) machines;
|
|
|
|
|
|
|
|
testScript = ''
|
2020-05-14 15:14:02 +02:00
|
|
|
start_all()
|
|
|
|
'' + test;
|
2017-09-09 02:00:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
mkKubernetesMultiNodeTest = attrs: mkKubernetesBaseTest ({
|
|
|
|
machines = {
|
|
|
|
machine1 = {
|
|
|
|
roles = ["master"];
|
|
|
|
ip = "192.168.1.1";
|
|
|
|
};
|
|
|
|
machine2 = {
|
|
|
|
roles = ["node"];
|
|
|
|
ip = "192.168.1.2";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
} // attrs // {
|
|
|
|
name = "kubernetes-${attrs.name}-multinode";
|
|
|
|
});
|
|
|
|
|
|
|
|
mkKubernetesSingleNodeTest = attrs: mkKubernetesBaseTest ({
|
|
|
|
machines = {
|
|
|
|
machine1 = {
|
|
|
|
roles = ["master" "node"];
|
|
|
|
ip = "192.168.1.1";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
} // attrs // {
|
|
|
|
name = "kubernetes-${attrs.name}-singlenode";
|
|
|
|
});
|
|
|
|
in {
|
|
|
|
inherit mkKubernetesBaseTest mkKubernetesSingleNodeTest mkKubernetesMultiNodeTest;
|
|
|
|
}
|