From b7ac30704e57fa47475cdd806a9eb094e6358023 Mon Sep 17 00:00:00 2001
From: ArthurSens <arthursens2005@gmail.com>
Date: Mon, 14 Jun 2021 23:57:56 +0000
Subject: [PATCH] Run k3s inside gitpod and deploy kube-prometheus.

Signed-off-by: ArthurSens <arthursens2005@gmail.com>
---
 .gitignore                        |  2 ++
 .gitpod.yml                       | 23 ++++++++++++++-
 .gitpod/deploy-kube-prometheus.sh | 16 ++++++++++
 .gitpod/prepare-k3s.sh            | 49 +++++++++++++++++++++++++++++++
 .gitpod/prepare-rootfs.sh         | 48 ++++++++++++++++++++++++++++++
 .gitpod/qemu.sh                   | 14 +++++++++
 .gitpod/scp.sh                    |  3 ++
 .gitpod/ssh.sh                    |  3 ++
 8 files changed, 157 insertions(+), 1 deletion(-)
 create mode 100755 .gitpod/deploy-kube-prometheus.sh
 create mode 100755 .gitpod/prepare-k3s.sh
 create mode 100755 .gitpod/prepare-rootfs.sh
 create mode 100755 .gitpod/qemu.sh
 create mode 100755 .gitpod/scp.sh
 create mode 100755 .gitpod/ssh.sh

diff --git a/.gitignore b/.gitignore
index 42cf6651..a82ceced 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,3 +4,5 @@ vendor/
 ./auth
 .swp
 crdschemas/
+
+.gitpod/_output/
\ No newline at end of file
diff --git a/.gitpod.yml b/.gitpod.yml
index 1ec86098..936bc53a 100644
--- a/.gitpod.yml
+++ b/.gitpod.yml
@@ -1,4 +1,5 @@
-
+image: gitpod/workspace-full
+checkoutLocation: gitpod-k3s
 tasks: 
   - init: |
       make --always-make
@@ -21,6 +22,26 @@ tasks:
       fi
       EOF
       chmod +x ${PWD}/.git/hooks/pre-commit
+  - name: run kube-prometheus
+    command: |
+      .gitpod/prepare-k3s.sh
+      .gitpod/deploy-kube-prometheus.sh
+  - name: kernel dev environment
+    init: |
+      sudo apt update -y
+      sudo apt install qemu qemu-system-x86 linux-image-$(uname -r) libguestfs-tools sshpass netcat -y
+      sudo curl -o /usr/bin/kubectl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
+      sudo chmod +x /usr/bin/kubectl
+      .gitpod/prepare-rootfs.sh
+    command: |
+      .gitpod/qemu.sh
+ports:
+  - port: 3000
+    onOpen: open-browser
+  - port: 9090
+    onOpen: open-browser
+  - port: 9093
+    onOpen: open-browser
 vscode:
   extensions:
     - heptio.jsonnet@0.1.0:woEDU5N62LRdgdz0g/I6sQ==
\ No newline at end of file
diff --git a/.gitpod/deploy-kube-prometheus.sh b/.gitpod/deploy-kube-prometheus.sh
new file mode 100755
index 00000000..fdd9c1d2
--- /dev/null
+++ b/.gitpod/deploy-kube-prometheus.sh
@@ -0,0 +1,16 @@
+kubectl apply -f manifests/setup
+
+# Safety wait for CRDs to be working
+sleep 30
+
+kubectl apply -f manifests/
+
+kubectl rollout status -n monitoring daemonset node-exporter
+kubectl rollout status -n monitoring statefulset alertmanager-main
+kubectl rollout status -n monitoring statefulset prometheus-k8s
+kubectl rollout status -n monitoring deployment grafana
+kubectl rollout status -n monitoring deployment kube-state-metrics
+
+kubectl port-forward -n monitoring svc/grafana 3000 > /dev/null 2>&1 &
+kubectl port-forward -n monitoring svc/alertmanager-main 9093 > /dev/null 2>&1 &
+kubectl port-forward -n monitoring svc/prometheus-k8s 9090 > /dev/null 2>&1 &
\ No newline at end of file
diff --git a/.gitpod/prepare-k3s.sh b/.gitpod/prepare-k3s.sh
new file mode 100755
index 00000000..ccfd658a
--- /dev/null
+++ b/.gitpod/prepare-k3s.sh
@@ -0,0 +1,49 @@
+#!/bin/bash
+
+script_dirname="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
+rootfslock="${script_dirname}/_output/rootfs/rootfs-ready.lock"
+k3sreadylock="${script_dirname}/_output/rootfs/k3s-ready.lock"
+
+if test -f "${k3sreadylock}"; then
+    exit 0
+fi
+
+cd $script_dirname
+
+function waitssh() {
+  while ! nc -z 127.0.0.1 2222; do   
+    sleep 0.1
+  done
+  ./ssh.sh "whoami" &>/dev/null
+  if [ $? -ne 0 ]; then
+    sleep 1
+    waitssh
+  fi
+}
+
+function waitrootfs() {
+  while ! test -f "${rootfslock}"; do
+    sleep 0.1
+  done
+}
+
+echo "🔥 Installing everything, this will be done only one time per workspace."
+
+echo "Waiting for the rootfs to become available, it can take a while, open the terminal #2 for progress"
+waitrootfs
+echo "✅ rootfs available"
+
+echo "Waiting for the ssh server to become available, it can take a while, after this k3s is getting installed"
+waitssh
+echo "✅ ssh server available"
+
+./ssh.sh "curl -sfL https://get.k3s.io | sh -"
+
+mkdir -p ~/.kube
+./scp.sh root@127.0.0.1:/etc/rancher/k3s/k3s.yaml ~/.kube/config
+
+echo "✅ k3s server is ready"
+touch "${k3sreadylock}"
+
+# safety wait for cluster availability
+sleep 30s
\ No newline at end of file
diff --git a/.gitpod/prepare-rootfs.sh b/.gitpod/prepare-rootfs.sh
new file mode 100755
index 00000000..c67e9a77
--- /dev/null
+++ b/.gitpod/prepare-rootfs.sh
@@ -0,0 +1,48 @@
+#!/bin/bash
+
+set -euo pipefail
+
+img_url="https://cloud-images.ubuntu.com/hirsute/current/hirsute-server-cloudimg-amd64.tar.gz"
+
+script_dirname="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
+outdir="${script_dirname}/_output/rootfs"
+
+rm -Rf $outdir
+mkdir -p $outdir
+
+curl -L -o "${outdir}/rootfs.tar.gz" $img_url
+
+cd $outdir
+
+tar -xvf rootfs.tar.gz
+
+qemu-img resize hirsute-server-cloudimg-amd64.img +20G
+
+sudo virt-customize -a hirsute-server-cloudimg-amd64.img --run-command 'resize2fs /dev/sda'
+
+sudo virt-customize -a hirsute-server-cloudimg-amd64.img --root-password password:root
+
+netconf="
+network:
+  version: 2
+  renderer: networkd
+  ethernets:
+    enp0s3:
+      dhcp4: yes
+"
+
+# networking setup
+sudo virt-customize -a hirsute-server-cloudimg-amd64.img --run-command "echo '${netconf}' > /etc/netplan/01-net.yaml"
+
+# copy kernel modules
+sudo virt-customize -a hirsute-server-cloudimg-amd64.img --copy-in /lib/modules/$(uname -r):/lib/modules
+
+# ssh
+sudo virt-customize -a hirsute-server-cloudimg-amd64.img --run-command 'apt remove openssh-server -y && apt install openssh-server -y'
+sudo virt-customize -a hirsute-server-cloudimg-amd64.img --run-command "sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config"
+sudo virt-customize -a hirsute-server-cloudimg-amd64.img --run-command "sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config"
+
+# mark as ready
+touch rootfs-ready.lock
+
+echo "k3s development environment is ready"
diff --git a/.gitpod/qemu.sh b/.gitpod/qemu.sh
new file mode 100755
index 00000000..f4256439
--- /dev/null
+++ b/.gitpod/qemu.sh
@@ -0,0 +1,14 @@
+#!/bin/bash
+
+set -xeuo pipefail
+
+script_dirname="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
+outdir="${script_dirname}/_output"
+
+sudo qemu-system-x86_64 -kernel "/boot/vmlinuz" \
+-boot c -m 3073M -hda "${outdir}/rootfs/hirsute-server-cloudimg-amd64.img" \
+-net user \
+-smp 8 \
+-append "root=/dev/sda rw console=ttyS0,115200 acpi=off nokaslr" \
+-nic user,hostfwd=tcp::2222-:22,hostfwd=tcp::6443-:6443 \
+-serial mon:stdio -display none
\ No newline at end of file
diff --git a/.gitpod/scp.sh b/.gitpod/scp.sh
new file mode 100755
index 00000000..2295c3c0
--- /dev/null
+++ b/.gitpod/scp.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+sshpass -p 'root' scp -o StrictHostKeychecking=no -P 2222 $@
\ No newline at end of file
diff --git a/.gitpod/ssh.sh b/.gitpod/ssh.sh
new file mode 100755
index 00000000..b4d2ca8c
--- /dev/null
+++ b/.gitpod/ssh.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+sshpass  -p 'root' ssh -o StrictHostKeychecking=no -p 2222 root@127.0.0.1 "$@"
\ No newline at end of file
-- 
GitLab