Commit 90e1700d7afc

Vincent Demeester <vincent@sbr.pm>
2020-11-25 17:14:51
tools: add bekind…
a tool to manage kind cluster on remote machines Signed-off-by: Vincent Demeester <vincent@sbr.pm>
1 parent 6115b48
Changed files (8)
overlays/sbr.nix
@@ -18,6 +18,7 @@ rec {
   vde-thinkpad = import ../pkgs/vde-thinkpad {
     inherit (self) stdenv lib;
   };
+  bekind = super.callPackage ../tools/bekind { };
 
   my = import ../pkgs {
     inherit (self) pkgs;
pkgs/default.nix
@@ -37,6 +37,7 @@ rec {
   vrsync = pkgs.callPackage ./vrsync { };
   vde-thinkpad = pkgs.callPackage ./vde-thinkpad { };
   bus = pkgs.callPackage ../tools/bus { };
+  bekind = pkgs.callPackage ../tools/bekind { };
 
   # Mine
   ape = pkgs.callPackage ./ape { };
tools/bekind/profiles/1n.yaml
@@ -0,0 +1,11 @@
+kind: Cluster
+apiVersion: kind.x-k8s.io/v1alpha4
+networking:
+  apiServerAddress: HOST
+  apiServerPort: PORT
+containerdConfigPatches:
+- |-
+  [plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:5000"]
+    endpoint = ["http://sakhalin.home:5000"]
+nodes:
+- role: control-plane
tools/bekind/profiles/3n.yaml
@@ -0,0 +1,26 @@
+kind: Cluster
+apiVersion: kind.x-k8s.io/v1alpha4
+networking:
+  apiServerAddress: HOST
+  apiServerPort: 8443
+containerdConfigPatches:
+- |-
+  [plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:5000"]
+    endpoint = ["http://sakhalin.home:5000"]
+nodes:
+- role: control-plane
+  kubeadmConfigPatches:
+  - |
+    kind: InitConfiguration
+    nodeRegistration:
+      kubeletExtraArgs:
+        node-labels: "ingress-ready=true"
+  extraPortMappings:
+  - containerPort: 80
+    hostPort: 80
+    protocol: TCP
+  - containerPort: 443
+    hostPort: 443
+    protocol: TCP
+- role: worker
+- role: worker
tools/bekind/bekind
@@ -0,0 +1,155 @@
+#!/usr/bin/env bash
+bekind=$(basename $0)
+bekind_dir=$(dirname "$(readlink -f "$0")")
+profile_dir="./profiles"
+
+bekind_help(){
+    echo "Usage: $ProgName <subcommand> [options]\n"
+    echo "Subcommands:"
+    echo "    clean    clean all kind clusters"
+    echo "    create   create a kind cluster"
+    echo "    delete   delete a kind cluster"
+    echo "    list     list kind clusters"
+    echo ""
+    echo "For help with each subcommand run:"
+    echo "$ProgName <subcommand> -h|--help"
+    echo ""
+}
+
+bekind_create(){
+    profile=""
+    host=""
+    port="8443"
+    while [[ $# -gt 1 ]]
+    do
+        case $1 in
+            "--profile" | "-p")
+                shift
+                profile=$1
+                shift
+                ;;
+            "--port")
+                shift
+                port=$1
+                shift
+                ;;
+            "--host")
+                shift
+                host=$1
+                shift
+                ;;
+        esac
+    done
+    name="$1"
+    shift
+    if [[ -z "${name}" ]]; then
+        name="kind"
+    fi
+    env=""
+    configfile="config.${name}"
+    if [[ -n "${host}" ]]; then
+        env="DOCKER_HOST=ssh://${host}"
+        configfile="${configfile}.${host}"
+    fi
+    args=""
+    if [[ -n "${profile}" ]]; then
+        if [[ -z "${host}" ]]; then
+            echo "Cannot use profile ${profile} without an host" 1>&2
+            exit 1
+        fi
+        # does the profile exists
+        profile_file="${profile_dir}/${profile}.yaml"
+        if [[ -f "${profile_file}" ]]; then
+            tmpfile=$(mktemp /tmp/bekind.XXXXXX)
+            hostip=$(getent hosts ${host} | awk '{ print $1 }')
+            sed "s/HOST/${hostip}/g" "${profile_file}" > ${tmpfile}
+            sed -i "s/PORT/${port}/g" ${tmpfile}
+            args="${args} --config ${tmpfile}"
+        else
+            echo "Profile ${profile} doesn't exists" 1>&2
+            exit 1
+        fi
+        # replace hosts in there
+    fi
+    echo "> Create the cluster (name: ${name}, args: ${args})"
+    env $env kind create cluster --name ${name} ${args}
+    echo "> Write the kubeconfig in ~/.kube/${configfile}"
+    env $env kind get kubeconfig --name ${name} > ~/.kube/${configfile}
+}
+
+bekind_delete() {
+    while [[ $# -gt 1 ]]
+    do
+        case $1 in
+            "--host")
+                shift
+                host=$1
+                shift
+                ;;
+        esac
+    done
+    name="$1"
+    shift
+    env=""
+    if [[ -z "${name}" ]]; then
+        name="kind"
+    fi
+    if [[ -n "${host}" ]]; then
+        env="DOCKER_HOST=ssh://${host}"
+    fi
+    echo "> Delete kind cluster ${name} from ${host}"
+    env $env kind delete cluster --name ${name}
+}
+
+bekind_list() {
+    while [[ $# -gt 0 ]]
+    do
+        case $1 in
+            "--host")
+                shift
+                host=$1
+                shift
+                ;;
+        esac
+    done
+    env=""
+    if [[ -n "$host" ]]; then
+        env="DOCKER_HOST=ssh://${host}"
+    fi
+    env $env kind get clusters
+}
+
+bekind_clean() {
+    while [[ $# -gt 1 ]]
+    do
+        case $1 in
+            "--host")
+                shift
+                host=$1
+                shift
+                ;;
+        esac
+    done
+    env=""
+    if [[ -n "$host" ]]; then
+        env="DOCKER_HOST=ssh://${host}"
+    fi
+    echo "> Clean kind clusters from ${host}"
+    env $env kind delete clusters --all
+}
+
+subcommand=$1
+case $subcommand in
+    "" | "-h" | "--help")
+        bekind_help
+        ;;
+    *)
+        shift
+        bekind_${subcommand} $@
+        if [ $? = 127 ]; then
+            echo "Error: '$subcommand' is not a known subcommand." >&2
+            echo "       Run '$ProgName --help' for a list of known subcommands." >&2
+            exit 1
+        fi
+        ;;
+esac
tools/bekind/default.nix
@@ -0,0 +1,13 @@
+{ stdenv }:
+
+stdenv.mkDerivation {
+  name = "bekind";
+  src = ./.;
+  phases = [ "installPhase" "fixupPhase" ];
+  installPhase = ''
+    mkdir -p $out $out/bin $out/share/bekind
+    sed "s%profile_dir=\"./profiles\"%profile_dir=$out/share/bekind/profiles%g" $src/bekind > $out/bin/bekind
+    chmod +x $out/bin/bekind
+    cp -r $src/profiles $out/share/bekind/
+  '';
+}
tools/bekind/README.org
@@ -0,0 +1,9 @@
+# bekind
+
+=bekind= is my personnal command line to manage kind cluster in my developement
+environment. Here is a list of features:
+
+- create a kind cluster on a remote machine (using =ssh=)
+- create a kind cluster based on a /profile/ (1 node, 3 nodes, with ingress, …)
+- list kind cluster on a given remote machine
+- remove/re-create a kind cluster
users/vincent/containers/kubernetes.nix
@@ -21,5 +21,6 @@ in
     my.kss
     # our own scripts
     knd
+    bekind
   ];
 }