2021-12-14 22:45:23 +01:00
|
|
|
{ system ? builtins.currentSystem, pkgs ? import ../../.. { inherit system; } }:
|
2017-09-09 02:00:35 +02:00
|
|
|
with import ./base.nix { inherit system; };
|
2017-05-03 01:20:32 +02:00
|
|
|
let
|
|
|
|
|
|
|
|
roServiceAccount = pkgs.writeText "ro-service-account.json" (builtins.toJSON {
|
|
|
|
kind = "ServiceAccount";
|
|
|
|
apiVersion = "v1";
|
|
|
|
metadata = {
|
|
|
|
name = "read-only";
|
|
|
|
namespace = "default";
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
roRoleBinding = pkgs.writeText "ro-role-binding.json" (builtins.toJSON {
|
2018-03-17 06:35:35 +01:00
|
|
|
apiVersion = "rbac.authorization.k8s.io/v1";
|
2017-09-09 02:00:35 +02:00
|
|
|
kind = "RoleBinding";
|
|
|
|
metadata = {
|
|
|
|
name = "read-pods";
|
|
|
|
namespace = "default";
|
2017-05-03 01:20:32 +02:00
|
|
|
};
|
2017-09-09 02:00:35 +02:00
|
|
|
roleRef = {
|
|
|
|
apiGroup = "rbac.authorization.k8s.io";
|
|
|
|
kind = "Role";
|
|
|
|
name = "pod-reader";
|
2017-05-03 01:20:32 +02:00
|
|
|
};
|
2017-09-09 02:00:35 +02:00
|
|
|
subjects = [{
|
|
|
|
kind = "ServiceAccount";
|
|
|
|
name = "read-only";
|
|
|
|
namespace = "default";
|
2017-05-03 01:20:32 +02:00
|
|
|
}];
|
|
|
|
});
|
|
|
|
|
|
|
|
roRole = pkgs.writeText "ro-role.json" (builtins.toJSON {
|
2018-03-17 06:35:35 +01:00
|
|
|
apiVersion = "rbac.authorization.k8s.io/v1";
|
2017-05-24 19:05:54 +02:00
|
|
|
kind = "Role";
|
|
|
|
metadata = {
|
|
|
|
name = "pod-reader";
|
|
|
|
namespace = "default";
|
2017-05-03 01:20:32 +02:00
|
|
|
};
|
2017-05-24 19:05:54 +02:00
|
|
|
rules = [{
|
|
|
|
apiGroups = [""];
|
|
|
|
resources = ["pods"];
|
|
|
|
verbs = ["get" "list" "watch"];
|
2017-05-03 01:20:32 +02:00
|
|
|
}];
|
|
|
|
});
|
|
|
|
|
|
|
|
kubectlPod = pkgs.writeText "kubectl-pod.json" (builtins.toJSON {
|
|
|
|
kind = "Pod";
|
|
|
|
apiVersion = "v1";
|
|
|
|
metadata.name = "kubectl";
|
|
|
|
metadata.namespace = "default";
|
|
|
|
metadata.labels.name = "kubectl";
|
|
|
|
spec.serviceAccountName = "read-only";
|
|
|
|
spec.containers = [{
|
|
|
|
name = "kubectl";
|
|
|
|
image = "kubectl:latest";
|
2017-09-09 02:00:35 +02:00
|
|
|
command = ["/bin/tail" "-f"];
|
2017-05-03 01:20:32 +02:00
|
|
|
imagePullPolicy = "Never";
|
|
|
|
tty = true;
|
|
|
|
}];
|
|
|
|
});
|
|
|
|
|
|
|
|
kubectlPod2 = pkgs.writeTextDir "kubectl-pod-2.json" (builtins.toJSON {
|
|
|
|
kind = "Pod";
|
|
|
|
apiVersion = "v1";
|
|
|
|
metadata.name = "kubectl-2";
|
|
|
|
metadata.namespace = "default";
|
|
|
|
metadata.labels.name = "kubectl-2";
|
|
|
|
spec.serviceAccountName = "read-only";
|
|
|
|
spec.containers = [{
|
|
|
|
name = "kubectl-2";
|
|
|
|
image = "kubectl:latest";
|
2017-09-09 02:00:35 +02:00
|
|
|
command = ["/bin/tail" "-f"];
|
2017-05-03 01:20:32 +02:00
|
|
|
imagePullPolicy = "Never";
|
|
|
|
tty = true;
|
|
|
|
}];
|
|
|
|
});
|
|
|
|
|
2022-02-16 01:05:14 +01:00
|
|
|
copyKubectl = pkgs.runCommand "copy-kubectl" { } ''
|
2017-09-09 02:00:35 +02:00
|
|
|
mkdir -p $out/bin
|
|
|
|
cp ${pkgs.kubernetes}/bin/kubectl $out/bin/kubectl
|
|
|
|
'';
|
|
|
|
|
2017-05-03 01:20:32 +02:00
|
|
|
kubectlImage = pkgs.dockerTools.buildImage {
|
|
|
|
name = "kubectl";
|
|
|
|
tag = "latest";
|
2022-02-16 01:05:14 +01:00
|
|
|
contents = [ copyKubectl pkgs.busybox kubectlPod2 ];
|
2021-02-25 16:00:59 +01:00
|
|
|
config.Entrypoint = ["/bin/sh"];
|
2017-05-03 01:20:32 +02:00
|
|
|
};
|
|
|
|
|
2017-09-09 02:00:35 +02:00
|
|
|
base = {
|
|
|
|
name = "rbac";
|
|
|
|
};
|
2017-05-03 01:20:32 +02:00
|
|
|
|
2017-09-09 02:00:35 +02:00
|
|
|
singlenode = base // {
|
|
|
|
test = ''
|
2020-05-14 15:14:02 +02:00
|
|
|
machine1.wait_until_succeeds("kubectl get node machine1.my.zyx | grep -w Ready")
|
|
|
|
|
|
|
|
machine1.wait_until_succeeds(
|
2021-02-25 16:00:59 +01:00
|
|
|
"${pkgs.gzip}/bin/zcat ${kubectlImage} | ${pkgs.containerd}/bin/ctr -n k8s.io image import -"
|
2020-05-14 15:14:02 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
machine1.wait_until_succeeds(
|
|
|
|
"kubectl apply -f ${roServiceAccount}"
|
|
|
|
)
|
|
|
|
machine1.wait_until_succeeds(
|
|
|
|
"kubectl apply -f ${roRole}"
|
|
|
|
)
|
|
|
|
machine1.wait_until_succeeds(
|
|
|
|
"kubectl apply -f ${roRoleBinding}"
|
|
|
|
)
|
|
|
|
machine1.wait_until_succeeds(
|
|
|
|
"kubectl create -f ${kubectlPod}"
|
|
|
|
)
|
|
|
|
|
|
|
|
machine1.wait_until_succeeds("kubectl get pod kubectl | grep Running")
|
|
|
|
|
2021-12-13 15:35:22 +01:00
|
|
|
machine1.wait_until_succeeds("kubectl exec kubectl -- kubectl get pods")
|
|
|
|
machine1.fail("kubectl exec kubectl -- kubectl create -f /kubectl-pod-2.json")
|
|
|
|
machine1.fail("kubectl exec kubectl -- kubectl delete pods -l name=kubectl")
|
2017-09-09 02:00:35 +02:00
|
|
|
'';
|
2017-05-03 01:20:32 +02:00
|
|
|
};
|
|
|
|
|
2017-09-09 02:00:35 +02:00
|
|
|
multinode = base // {
|
|
|
|
test = ''
|
2018-07-22 13:14:20 +02:00
|
|
|
# Node token exchange
|
2020-05-14 15:14:02 +02:00
|
|
|
machine1.wait_until_succeeds(
|
|
|
|
"cp -f /var/lib/cfssl/apitoken.secret /tmp/shared/apitoken.secret"
|
|
|
|
)
|
|
|
|
machine2.wait_until_succeeds(
|
|
|
|
"cat /tmp/shared/apitoken.secret | nixos-kubernetes-node-join"
|
|
|
|
)
|
|
|
|
|
|
|
|
machine1.wait_until_succeeds("kubectl get node machine2.my.zyx | grep -w Ready")
|
|
|
|
|
|
|
|
machine2.wait_until_succeeds(
|
2021-02-25 16:00:59 +01:00
|
|
|
"${pkgs.gzip}/bin/zcat ${kubectlImage} | ${pkgs.containerd}/bin/ctr -n k8s.io image import -"
|
2020-05-14 15:14:02 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
machine1.wait_until_succeeds(
|
|
|
|
"kubectl apply -f ${roServiceAccount}"
|
|
|
|
)
|
|
|
|
machine1.wait_until_succeeds(
|
|
|
|
"kubectl apply -f ${roRole}"
|
|
|
|
)
|
|
|
|
machine1.wait_until_succeeds(
|
|
|
|
"kubectl apply -f ${roRoleBinding}"
|
|
|
|
)
|
|
|
|
machine1.wait_until_succeeds(
|
|
|
|
"kubectl create -f ${kubectlPod}"
|
|
|
|
)
|
|
|
|
|
|
|
|
machine1.wait_until_succeeds("kubectl get pod kubectl | grep Running")
|
|
|
|
|
2021-12-13 15:35:22 +01:00
|
|
|
machine1.wait_until_succeeds("kubectl exec kubectl -- kubectl get pods")
|
|
|
|
machine1.fail("kubectl exec kubectl -- kubectl create -f /kubectl-pod-2.json")
|
|
|
|
machine1.fail("kubectl exec kubectl -- kubectl delete pods -l name=kubectl")
|
2017-09-09 02:00:35 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
in {
|
|
|
|
singlenode = mkKubernetesSingleNodeTest singlenode;
|
|
|
|
multinode = mkKubernetesMultiNodeTest multinode;
|
2017-05-03 01:20:32 +02:00
|
|
|
}
|