2019-02-12 16:22:18 +01:00
|
|
|
{ config, pkgs, lib, kubenix, ... }:
|
|
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
cfg = config.testing;
|
2019-02-16 14:02:13 +01:00
|
|
|
parentConfig = config;
|
2019-02-20 09:34:15 +01:00
|
|
|
|
|
|
|
|
nixosTesting = import <nixpkgs/nixos/lib/testing.nix> {
|
|
|
|
|
inherit pkgs;
|
|
|
|
|
system = "x86_64-linux";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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)}
|
|
|
|
|
'';
|
|
|
|
|
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"
|
|
|
|
|
'';
|
|
|
|
|
in nixosTesting.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
|
|
|
|
|
];
|
|
|
|
|
trustedInterfaces = ["docker0"];
|
|
|
|
|
|
|
|
|
|
extraCommands = concatMapStrings (node: ''
|
|
|
|
|
iptables -A INPUT -s ${node.config.networking.primaryIPAddress} -j ACCEPT
|
|
|
|
|
'') (attrValues nodes);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
environment.systemPackages = [ kubectl ];
|
|
|
|
|
services.flannel.iface = "eth1";
|
|
|
|
|
services.kubernetes = {
|
|
|
|
|
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 = ''
|
|
|
|
|
startAll;
|
|
|
|
|
|
|
|
|
|
${test}
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mkKubernetesSingleNodeTest = attrs: mkKubernetesBaseTest ({
|
|
|
|
|
machines = {
|
|
|
|
|
kube = {
|
|
|
|
|
roles = ["master" "node"];
|
|
|
|
|
ip = "192.168.1.1";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
} // attrs // {
|
|
|
|
|
name = "kubernetes-${attrs.name}-singlenode";
|
|
|
|
|
});
|
2019-02-12 16:22:18 +01:00
|
|
|
in {
|
|
|
|
|
options = {
|
|
|
|
|
testing.throwError = mkOption {
|
|
|
|
|
description = "Whether to throw error";
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = true;
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-16 14:02:13 +01:00
|
|
|
testing.defaults = mkOption {
|
|
|
|
|
description = "Testing defaults";
|
|
|
|
|
type = types.coercedTo types.unspecified (value: [value]) (types.listOf types.unspecified);
|
|
|
|
|
example = literalExample ''{config, ...}: {
|
|
|
|
|
kubernetes.version = config.kubernetes.version;
|
|
|
|
|
}'';
|
|
|
|
|
default = [];
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-12 16:22:18 +01:00
|
|
|
testing.tests = mkOption {
|
|
|
|
|
description = "Attribute set of test cases";
|
|
|
|
|
default = [];
|
|
|
|
|
type = types.listOf (types.coercedTo types.path (module: {inherit module;}) (types.submodule ({config, ...}: let
|
|
|
|
|
modules = [config.module ./test.nix {
|
|
|
|
|
config._module.args.test = config;
|
2019-02-16 14:02:13 +01:00
|
|
|
}] ++ cfg.defaults;
|
2019-02-12 16:22:18 +01:00
|
|
|
|
|
|
|
|
test = (kubenix.evalKubernetesModules {
|
|
|
|
|
check = false;
|
|
|
|
|
inherit modules;
|
|
|
|
|
}).config.test;
|
|
|
|
|
|
2019-02-13 17:03:27 +01:00
|
|
|
evaled =
|
|
|
|
|
if test.enable
|
|
|
|
|
then builtins.trace "testing ${test.name}" (kubenix.evalKubernetesModules {
|
|
|
|
|
inherit modules;
|
|
|
|
|
})
|
|
|
|
|
else {success = false;};
|
2019-02-12 16:22:18 +01:00
|
|
|
in {
|
|
|
|
|
options = {
|
|
|
|
|
name = mkOption {
|
|
|
|
|
description = "test name";
|
|
|
|
|
type = types.str;
|
|
|
|
|
internal = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
description = mkOption {
|
|
|
|
|
description = "test description";
|
|
|
|
|
type = types.str;
|
|
|
|
|
internal = true;
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-13 17:03:27 +01:00
|
|
|
enable = mkOption {
|
|
|
|
|
description = "Whether to enable test";
|
|
|
|
|
type = types.bool;
|
|
|
|
|
internal = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module = mkOption {
|
|
|
|
|
description = "Module defining submodule";
|
|
|
|
|
type = types.unspecified;
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-12 16:22:18 +01:00
|
|
|
evaled = mkOption {
|
|
|
|
|
description = "Wheter test was evaled";
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default =
|
|
|
|
|
if cfg.throwError
|
|
|
|
|
then if evaled.config.test.assertions != [] then true else true
|
|
|
|
|
else (builtins.tryEval evaled.config.test.assertions).success;
|
|
|
|
|
internal = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
success = mkOption {
|
|
|
|
|
description = "Whether test was success";
|
|
|
|
|
type = types.bool;
|
|
|
|
|
internal = true;
|
|
|
|
|
default = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
assertions = mkOption {
|
|
|
|
|
description = "Test result";
|
|
|
|
|
type = types.unspecified;
|
|
|
|
|
internal = true;
|
|
|
|
|
default = [];
|
|
|
|
|
};
|
2019-02-20 09:34:15 +01:00
|
|
|
|
|
|
|
|
script = mkOption {
|
|
|
|
|
description = "Test script";
|
|
|
|
|
type = types.nullOr types.package;
|
|
|
|
|
default = null;
|
|
|
|
|
};
|
2019-02-12 16:22:18 +01:00
|
|
|
};
|
|
|
|
|
|
2019-02-20 09:34:15 +01:00
|
|
|
config = mkMerge [{
|
2019-02-13 17:03:27 +01:00
|
|
|
inherit (test) name description enable;
|
2019-02-20 09:34:15 +01:00
|
|
|
} (mkIf config.evaled {
|
|
|
|
|
inherit (evaled.config.test) assertions;
|
|
|
|
|
success = all (el: el.assertion) config.assertions;
|
|
|
|
|
script = if evaled.config.test.check != null then mkKubernetesSingleNodeTest {
|
|
|
|
|
name = config.name;
|
|
|
|
|
test = ''
|
|
|
|
|
$kube->waitUntilSucceeds("kubectl get node machine1.my.zyx | grep -w Ready");
|
|
|
|
|
${evaled.config.test.check}
|
|
|
|
|
'';
|
|
|
|
|
} else null;
|
|
|
|
|
})];
|
2019-02-12 16:22:18 +01:00
|
|
|
})));
|
2019-02-13 17:03:27 +01:00
|
|
|
apply = tests: filter (test: test.enable) tests;
|
2019-02-12 16:22:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
testing.success = mkOption {
|
|
|
|
|
description = "Whether testing was a success";
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = all (test: test.success) cfg.tests;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
testing.result = mkOption {
|
|
|
|
|
description = "Testing result";
|
|
|
|
|
type = types.package;
|
|
|
|
|
default = pkgs.writeText "testing-report.json" (builtins.toJSON {
|
|
|
|
|
success = cfg.success;
|
|
|
|
|
tests = map (test: {
|
|
|
|
|
inherit (test) name description evaled success;
|
|
|
|
|
assertions = moduleToAttrs test.assertions;
|
2019-02-20 09:34:15 +01:00
|
|
|
script = test.script;
|
2019-02-13 17:03:27 +01:00
|
|
|
}) (filter (test: test.enable) cfg.tests);
|
2019-02-12 16:22:18 +01:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}
|