2020-04-24 23:36:52 +00:00
|
|
|
{ system ? builtins.currentSystem,
|
|
|
|
config ? {},
|
|
|
|
pkgs ? import ../../.. { inherit system config; }
|
|
|
|
}:
|
|
|
|
|
2020-05-15 21:57:56 +00:00
|
|
|
with import ../../lib/testing-python.nix { inherit system pkgs; };
|
2020-04-24 23:36:52 +00: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)}
|
|
|
|
'';
|
2022-10-21 18:38:19 +00:00
|
|
|
wrapKubectl = with pkgs; runCommand "wrap-kubectl" { nativeBuildInputs = [ makeWrapper ]; } ''
|
2020-04-24 23:36:52 +00:00
|
|
|
mkdir -p $out/bin
|
|
|
|
makeWrapper ${pkgs.kubernetes}/bin/kubectl $out/bin/kubectl --set KUBECONFIG "/etc/kubernetes/cluster-admin.kubeconfig"
|
|
|
|
'';
|
|
|
|
in makeTest {
|
|
|
|
inherit name;
|
|
|
|
|
|
|
|
nodes = mapAttrs (machineName: machine:
|
|
|
|
{ config, pkgs, lib, nodes, ... }:
|
|
|
|
mkMerge [
|
|
|
|
{
|
|
|
|
boot.postBootCommands = "rm -fr /var/lib/kubernetes/secrets /tmp/shared/*";
|
|
|
|
virtualisation.memorySize = mkDefault 1536;
|
|
|
|
virtualisation.diskSize = mkDefault 4096;
|
|
|
|
networking = {
|
|
|
|
inherit domain extraHosts;
|
|
|
|
primaryIPAddress = mkForce machine.ip;
|
|
|
|
|
|
|
|
firewall = {
|
|
|
|
allowedTCPPorts = [
|
|
|
|
10250 # kubelet
|
|
|
|
];
|
2021-07-03 03:11:41 +00:00
|
|
|
trustedInterfaces = ["mynet"];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
extraCommands = concatMapStrings (node: ''
|
2022-10-21 18:38:19 +00:00
|
|
|
iptables -A INPUT -s ${node.networking.primaryIPAddress} -j ACCEPT
|
2020-04-24 23:36:52 +00:00
|
|
|
'') (attrValues nodes);
|
|
|
|
};
|
|
|
|
};
|
2024-06-20 14:57:18 +00:00
|
|
|
programs.bash.completion.enable = true;
|
2022-03-05 16:20:37 +00:00
|
|
|
environment.systemPackages = [ wrapKubectl ];
|
2020-04-24 23:36:52 +00:00
|
|
|
services.flannel.iface = "eth1";
|
|
|
|
services.kubernetes = {
|
|
|
|
proxy.hostname = "${masterName}.${domain}";
|
|
|
|
|
|
|
|
easyCerts = true;
|
|
|
|
inherit (machine) roles;
|
|
|
|
apiserver = {
|
|
|
|
securePort = 443;
|
|
|
|
advertiseAddress = master.ip;
|
|
|
|
};
|
|
|
|
masterAddress = "${masterName}.${config.networking.domain}";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
(optionalAttrs (any (role: role == "master") machine.roles) {
|
|
|
|
networking.firewall.allowedTCPPorts = [
|
|
|
|
443 # kubernetes apiserver
|
|
|
|
];
|
|
|
|
})
|
|
|
|
(optionalAttrs (machine ? extraConfiguration) (machine.extraConfiguration { inherit config pkgs lib nodes; }))
|
|
|
|
(optionalAttrs (extraConfiguration != null) (extraConfiguration { inherit config pkgs lib nodes; }))
|
|
|
|
]
|
|
|
|
) machines;
|
|
|
|
|
|
|
|
testScript = ''
|
2020-05-15 21:57:56 +00:00
|
|
|
start_all()
|
|
|
|
'' + test;
|
2020-04-24 23:36:52 +00: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;
|
|
|
|
}
|