#!/usr/bin/env bash # # Setup a local k8s minikube cluster # with k8s dashboard and flux # set -o pipefail PRJ="flux" 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 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 TF_CHECKSUM="" readonly TF_CHECKSUM TF_FORCE_LOCAL=1 readonly TF_FORCE_LOCAL OS="Linux" VERBOSE=0 readonly VERBOSE 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 # We prefer podman but Ubuntu's podman is too old if [[ ${OS} = "ubuntu" ]]; then minikube_driver="docker" fi # Start minikube 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 if [[ 0 == "${VERBOSE}" ]]; then minikube start --driver="${minikube_driver}" else minikube start --driver="${minikube_driver}" --alsologtostderr -v=7 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" # The metrics server collides with kube-prometheus-stack #minikube addons enable metrics-server minikube dashboard & else printf 'k8s dashboard is already running\n' fi # Install terraform if local version enforced 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 TF_CMD="./terraform" else TF_CMD="terraform" fi # Install flux if local version enforced if [[ "$FLUX_FORCE_LOCAL" = 1 ]]; then 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 # We use the Gitea integration with a PAT # that needs to be supplied to create and write to # Gitea fluxcd owned repositories printf 'Installing Flux controller\n' ${FLUX_CMD} bootstrap gitea \ --hostname="$GITEA_HOSTNAME" \ --token-auth \ --owner="$GITEA_USER" \ --repository="$PRJ"-deploy \ --branch=main \ --path=clusters/dev \ --personal \ --read-write-key=true \ --private=false