From 6242ec3a25fbb623328de61b29ad364611ca321f Mon Sep 17 00:00:00 2001
From: Sheogorath <sheogorath@shivering-isles.com>
Date: Mon, 11 Oct 2021 00:54:49 +0200
Subject: [PATCH] Makefile: Add all commands to deploy the kubernetes
 controlplane

This patch adds a few util functions to deploy the entire kubernetes
cluster using `make deploy`. Currently it's still very iterative and not
ideal, but it works.
---
 Makefile                 |  3 +++
 utils/flux.mk            | 22 ++++++++++++++++++++++
 utils/kubernetes-init.mk | 24 ++++++++++++++++++++++++
 3 files changed, 49 insertions(+)
 create mode 100644 utils/flux.mk
 create mode 100644 utils/kubernetes-init.mk

diff --git a/Makefile b/Makefile
index 473fbf17f..c57479cc1 100644
--- a/Makefile
+++ b/Makefile
@@ -21,6 +21,9 @@ cli-config: ## Configure koolbox CLI (To setup terraform values as well was toke
 .PHONY: deploy
 deploy: check-machine ## Deploy infrastructure on Hetzner Cloud
 	cd ./terraform && make apply
+	date
+	sleep 600
+	make kubernetes-install
 
 .PHONY: destroy
 destroy: check-machine ## Destroy infrastructure on Hetzner Cloud
diff --git a/utils/flux.mk b/utils/flux.mk
new file mode 100644
index 000000000..a2b8f69e1
--- /dev/null
+++ b/utils/flux.mk
@@ -0,0 +1,22 @@
+.PHONY: flux-preflight
+flux-preflight:
+	# Checking for required variables
+	env | grep -Pe '^GITLAB_TOKEN' > /dev/null
+
+.PHONY: flux-watch
+flux-watch: ## flux: Show currently deployed resources an their status in all namespaces
+	koolbox flux get all --all-namespaces
+
+.PHONY: flux-update-git
+flux-update-git: ## flux: Reload flux-system repository
+	koolbox flux reconcile source git flux-system
+
+.PHONY: flux-bootstrap
+flux-bootstrap: flux-preflight
+	flux bootstrap gitlab \
+	  --hostname=git.shivering-isles.com \
+	  --ssh-hostname=git.shivering-isles.com:2222 \
+	  --ssh-key-algorithm ed25519 \
+	  --owner=shivering-isles \
+	  --repository=infrastructure-gitops \
+	  --path=clusters/k8s01
diff --git a/utils/kubernetes-init.mk b/utils/kubernetes-init.mk
new file mode 100644
index 000000000..c43edcf21
--- /dev/null
+++ b/utils/kubernetes-init.mk
@@ -0,0 +1,24 @@
+.PHONY: kubeadm-init
+kubeadm-init:
+	ssh node01.$(TF_VAR_dns_domain) kubeadm init --control-plane-endpoint "api.$(TF_VAR_dns_domain):6443" --upload-certs --pod-network-cidr "192.168.0.0/16"
+	ssh node01.$(TF_VAR_dns_domain) systemctl enable kubelet.service
+
+kubeadm-copy-config: ## Copy Kubernetes admin config from node01 to the local machine
+	scp node01.$(TF_VAR_dns_domain):/etc/kubernetes/admin.conf /root/.kube/config
+
+kubeadm-join-masters:
+	ssh node02.$(TF_VAR_dns_domain) $$(ssh node01.$(TF_VAR_dns_domain) kubeadm token create --ttl 1h --print-join-command --certificate-key "$$(ssh node01.$(TF_VAR_dns_domain) kubeadm init phase upload-certs --upload-certs | tail -1)" | tail -1)
+	ssh node02.$(TF_VAR_dns_domain) systemctl enable kubelet.service
+	ssh node03.$(TF_VAR_dns_domain) $$(ssh node01.$(TF_VAR_dns_domain) kubeadm token create --ttl 1h --print-join-command --certificate-key "$$(ssh node01.$(TF_VAR_dns_domain) kubeadm init phase upload-certs --upload-certs | tail -1)" | tail -1)
+	ssh node03.$(TF_VAR_dns_domain) systemctl enable kubelet.service
+
+kubectl-remove-first-master-taints:
+	kubectl taint nodes node01.$(TF_VAR_dns_domain) node-role.kubernetes.io/master-
+
+kubectl-remove-all-master-taints:
+	kubectl taint nodes --all node-role.kubernetes.io/master- || true
+
+kubectl-delete-wrong-subnet:
+	kubectl get pods --all-namespaces -o wide | grep 10.85. | awk '{print "-n " $$1 " " $$2}' | xargs -L 1 kubectl delete pod
+
+kubernetes-install: kubeadm-init kubeadm-copy-config kubectl-remove-first-master-taints flux-bootstrap kubeadm-join-masters kubectl-remove-all-master-taints kubectl-delete-wrong-subnet
-- 
GitLab