obch/setup-cluster

147 lines
4.0 KiB
Plaintext
Raw Normal View History

2024-01-07 20:03:28 +00:00
#!/usr/bin/env bash
#
2024-01-12 06:19:55 +00:00
# Setup a local k8s minikube cluster
2024-01-07 20:03:28 +00:00
# with k8s dashboard and flux
#
2024-01-10 22:36:10 +00:00
set -o pipefail
2024-01-07 20:03:28 +00:00
2024-02-19 16:10:55 +00:00
PRJ="flux"
2024-01-07 20:03:28 +00:00
readonly PRJ
USE_TF=0
readonly USE_TF
GITEA_HOSTNAME="git.e2m.io"
readonly GITEA_HOSTNAME
GITEA_USER="obch-flux"
readonly GITEA_USER
FLUX_VERSION="2.2.1"
readonly FLUX_VERSION
FLUX_CHECKSUM="466756ca6b3437d30a6a5fb58e60f3e5a82d8291f3869cfc55b6f041962601b5"
readonly FLUX_CHECKSUM
FLUX_ARCHIVE_LINUX="flux_${FLUX_VERSION}_linux_amd64.tar.gz"
readonly FLUX_ARCHIVE_LINUX
FLUX_ARCHIVE_MACOS="flux_${FLUX_VERSION}_darwin_arm64.tar.gz"
readonly FLUX_ARCHIVE_MACOS
FLUX_URL_LINUX="https://github.com/fluxcd/flux2/releases/download/v${FLUX_VERSION}/${FLUX_ARCHIVE_LINUX}"
readonly FLUX_URL_LINUX
FLUX_URL_MACOS="https://github.com/fluxcd/flux2/releases/download/v${FLUX_VERSION}/${FLUX_ARCHIVE_MACOS}"
readonly FLUX_URL_MACOS
2024-01-07 20:03:28 +00:00
FLUX_FORCE_LOCAL=1
readonly FLUX_FORCE_LOCAL
TF_VERSION="1.6.6"
readonly TF_VERSION
TF_ARCHIVE_LINUX="terraform_${TF_VERSION}_linux_amd64.zip"
readonly TF_ARCHIVE_LINUX
TF_ARCHIVE_MACOS="terraform_${TF_VERSION}_darwin_arm64.zip"
readonly TF_ARCHIVE_MACOS
2024-01-07 20:03:28 +00:00
TF_CHECKSUM=""
readonly TF_CHECKSUM
TF_FORCE_LOCAL=1
readonly TF_FORCE_LOCAL
OS="Linux"
2024-01-07 20:03:28 +00:00
VERBOSE=0
readonly VERBOSE
2024-01-11 21:37:42 +00:00
minikube_driver="podman"
if [[ $(uname) == "Darwin" ]]; then
OS="macos"
elif [[ $(grep '^ID=' /etc/os-release | awk -F'=' '{print $2}') == "ubuntu" ]]; then
OS="ubuntu"
fi
printf "Running on ${OS}\n"
if [[ "$OS" = "macos" ]]; then
FLUX_URL="${FLUX_URL_MACOS}"
FLUX_ARCHIVE="${FLUX_ARCHIVE_MACOS}"
TF_ARCHIVE="${TF_ARCHIVE_MACOS}"
TF_URL="https://releases.hashicorp.com/terraform/${TF_VERSION}/${TF_ARCHIVE}"
else
FLUX_URL="${FLUX_URL_LINUX}"
FLUX_ARCHIVE="${FLUX_ARCHIVE_LINUX}"
TF_ARCHIVE="${TF_ARCHIVE_LINUX}"
TF_URL="https://releases.hashicorp.com/terraform/${TF_VERSION}/${TF_ARCHIVE}"
fi
2024-01-12 06:19:55 +00:00
# We prefer podman but Ubuntu's podman is too old
if [[ ${OS} = "ubuntu" ]]; then
2024-01-11 21:37:42 +00:00
minikube_driver="docker"
fi
2024-01-07 20:03:28 +00:00
# Start minikube
2024-01-12 06:19:55 +00:00
if ! $(minikube status | grep Nonexistent\|Stopped); then
printf 'minikube is not running\nStarting minikube..\n'
# Increase memory from default 2G
podman machine set --memory=4096
podman machine start
minikube config set memory 3800
2024-01-12 06:19:55 +00:00
if [[ 0 == "${VERBOSE}" ]]; then
2024-01-11 21:37:42 +00:00
minikube start --driver="${minikube_driver}"
2024-01-07 20:03:28 +00:00
else
2024-01-11 21:37:42 +00:00
minikube start --driver="${minikube_driver}" --alsologtostderr -v=7
2024-01-07 20:03:28 +00:00
fi
else
printf 'minikube is already running\n'
fi
# Check cluster availability
# TODO: Check for errors
kubectl cluster-info
# Deploy k8s dashboard
if [[ $(kubectl get pods -A -o wide | grep kubernetes-dashboard | grep Running) ]]; then
printf "Installing k8s dashboard\n"
2024-01-12 06:19:55 +00:00
# The metrics server collides with kube-prometheus-stack
#minikube addons enable metrics-server
2024-01-07 20:03:28 +00:00
minikube dashboard &
else
printf 'k8s dashboard is already running\n'
fi
# Install terraform if local version enforced
2024-01-12 06:19:55 +00:00
if [[ "$TF_FORCE_LOCAL" = 1 ]]; then
if [[ -e "$TF_ARCHIVE}" ]]; then
printf "terraform binary exists\n"
else
printf "Fetching terraform archive..\n"
curl -LO "${TF_URL}"
unzip -n "${TF_ARCHIVE}"
fi
2024-01-07 20:03:28 +00:00
TF_CMD="./terraform"
else
TF_CMD="terraform"
fi
2024-01-12 06:19:55 +00:00
# Install flux if local version enforced
if [[ "$FLUX_FORCE_LOCAL" = 1 ]]; then
2024-01-07 20:03:28 +00:00
printf "Fetching flux archive..\n"
curl -LO "${FLUX_URL}"
tar xf "${FLUX_ARCHIVE}"
FLUX_CMD="./flux"
else
FLUX_CMD="flux"
fi
# Deploy Flux Controllers
# Needs cluster admin privileges
if [[ $(${FLUX_CMD} get helmreleases --all-namespaces) ]]; then
printf 'Flux controllers are running\n'
fi
# 'flux bootstrap' is idempotent
2024-01-08 16:34:45 +00:00
# We use the Gitea integration with a PAT
# that needs to be supplied to create and write to
# Gitea fluxcd owned repositories
2024-01-07 20:03:28 +00:00
printf 'Installing Flux controller\n'
${FLUX_CMD} bootstrap gitea \
--hostname="$GITEA_HOSTNAME" \
--token-auth \
--owner="$GITEA_USER" \
--repository="$PRJ"-deploy \
--branch=main \
2024-02-19 16:10:55 +00:00
--path=clusters/dev \
2024-01-07 20:03:28 +00:00
--personal \
--read-write-key=true \
--private=false