summaryrefslogtreecommitdiffstats
path: root/nix
diff options
context:
space:
mode:
Diffstat (limited to 'nix')
-rw-r--r--nix/deploy-to-kind.nix51
-rw-r--r--nix/kubenix.nix8
-rw-r--r--nix/test-deployment.nix40
3 files changed, 99 insertions, 0 deletions
diff --git a/nix/deploy-to-kind.nix b/nix/deploy-to-kind.nix
new file mode 100644
index 0000000..d40fd06
--- /dev/null
+++ b/nix/deploy-to-kind.nix
@@ -0,0 +1,51 @@
+{ kind
+, helloImage
+, worldImage
+, joinerImage
+, clusterName ? "cluster"
+, config
+, pkgs }:
+
+let
+ kindConfig = pkgs.writeText "kind-config" ''
+ kind: Cluster
+ apiVersion: kind.x-k8s.io/v1alpha4
+ nodes:
+ - role: control-plane
+ kubeadmConfigPatches:
+ - |
+ kind: InitConfiguration
+ nodeRegistration:
+ kubeletExtraArgs:
+ node-labels: "ingress-ready=true"
+ extraPortMappings:
+ - containerPort: 80
+ hostPort: 8080
+ protocol: TCP
+ - containerPort: 443
+ hostPort: 8443
+ protocol: TCP
+ '';
+in
+
+pkgs.writeScriptBin "deploy-to-kind" ''
+ #! ${pkgs.runtimeShell}
+ set -euo pipefail
+
+ ${kind}/bin/kind delete ${clusterName} || true
+ ${kind}/bin/kind create ${clusterName} --config ${kindConfig}
+
+ echo "Loading the ${pkgs.docker}/bin/docker image inside the kind docker container ..."
+
+ kind load image-archive <(gzip --decompress --stdout ${helloImage})
+ kind load image-archive <(gzip --decompress --stdout ${worldImage})
+ kind load image-archive <(gzip --decompress --stdout ${joinerImage})
+
+ echo "Applying the configuration ..."
+ cat ${config} | ${pkgs.jq}/bin/jq "."
+ cat ${config} | ${pkgs.kubectl}/bin/kubectl apply -f -
+
+ echo "Applying nginx ingress ..."
+ kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/kind/deploy.yaml
+''
+
diff --git a/nix/kubenix.nix b/nix/kubenix.nix
new file mode 100644
index 0000000..c09161e
--- /dev/null
+++ b/nix/kubenix.nix
@@ -0,0 +1,8 @@
+{ pkgs }:
+
+import (pkgs.fetchFromGitHub {
+ owner = "xtruder";
+ repo = "kubenix";
+ rev = "473a18371d2c0bb22bf060f750589695fb7d3100";
+ sha256 = "sha256:0j3mzg81s8pd8gsa3lx7bmhawjpb3f5li2irh55kr7b9kyxr2nsy";
+ }) { inherit pkgs; }
diff --git a/nix/test-deployment.nix b/nix/test-deployment.nix
new file mode 100644
index 0000000..d10e5e6
--- /dev/null
+++ b/nix/test-deployment.nix
@@ -0,0 +1,40 @@
+{ kind, pkgs }:
+
+pkgs.writeScriptBin "test-deployment" ''
+ #! ${pkgs.runtimeShell}
+ set -euo pipefail
+
+ SERVICE_URL=http://localhost:8001/api/v1/namespaces/default/services/hello:3000/proxy/
+
+ KUBECONFIG=$(${kind}/bin/kind get kubeconfig-path --name="kind")
+ PROXY_PID=""
+ trap cleanup EXIT
+
+ function cleanup {
+ if ! [ -z $PROXY_PID ]; then
+ kill -9 $PROXY_PID
+ fi
+ }
+
+ CLUSTERS=$(${kind}/bin/kind get clusters)
+ if ! [ "$CLUSTERS" = "kind" ]; then
+ echo "Error: kind cluster not running"
+ exit 1
+ fi
+
+ echo "- Cluster seems to be up and running ✓"
+ ${pkgs.kubectl}/bin/kubectl proxy >/dev/null &
+
+ PROXY_PID=$!
+ sleep 3
+
+ RESPONSE=$(${pkgs.curl}/bin/curl --silent $SERVICE_URL)
+
+ if ! [ "$RESPONSE" == "Hello World" ]; then
+ echo "Error: did not get expected response from service:"
+ echo $RESPONSE
+ exit 1
+ fi
+ echo "- Service returns expected response ✓"
+''
+