prole/k3s/create_k3d_knoe_data_cluster.sh

72 lines
2.1 KiB
Bash
Executable File

#!/usr/bin/env bash
set -o pipefail
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck disable=SC1090
source "$SCRIPT_DIR/../etc/knoe_cfg.sh"
set -euo pipefail
KNOE_HOME="${KNOE_HOME:-$(cd "$SCRIPT_DIR/.." && pwd)}"
export KNOE_HOME
KNOE_SERVICE="${KNOE_SERVICE:-$KNOE_HOME/etc}"
export KNOE_SERVICE
openbao_url() {
if [[ -n "${PROLE_OPENBAO_URL:-}" ]]; then
echo "$PROLE_OPENBAO_URL"
return 0
fi
echo "http://127.0.0.1:18200"
}
openbao_token() {
if [[ -f "$KNOE_SERVICE/secrets/openbao-root-token" ]]; then
cat "$KNOE_SERVICE/secrets/openbao-root-token"
else
echo "${OPENBAO_ROOT_TOKEN:-}"
fi
}
fetch_openbao_secret() {
local path="$1"
local key="$2"
local token url
token=$(openbao_token)
url=$(openbao_url)
if [[ -z "$token" ]]; then
echo ""
return 0
fi
curl -sS -H "X-Vault-Token: $token" "$url/v1/kv/data/$path" | jq -r ".data.data.\"$key\"" || echo ""
}
PROLE_PASSWD="${PROLE_PASSWD:-}"
if [[ -z "$PROLE_PASSWD" || "$PROLE_PASSWD" == '${OPENBAO:'* || "$PROLE_PASSWD" == '${KNOE_SECRET:'* ]]; then
command -v curl >/dev/null 2>&1 || { echo "ERROR: curl is required to read OpenBao secrets." >&2; exit 1; }
command -v jq >/dev/null 2>&1 || { echo "ERROR: jq is required to read OpenBao secrets." >&2; exit 1; }
path="knoe/${NAMESPACE:-default}/db"
fetched=$(fetch_openbao_secret "$path" "password")
if [[ -n "$fetched" && "$fetched" != "null" ]]; then
PROLE_PASSWD="$fetched"
fi
fi
if [[ -z "$PROLE_PASSWD" ]]; then
echo "ERROR: PROLE_PASSWD is required. Store it in OpenBao (kv/knoe/<namespace>/db#password) or export it." >&2
exit 1
fi
HOST_IP="${HOST_IP:-}"
if [[ -z "$HOST_IP" ]]; then
HOST_IP=$(ifconfig en0 2>/dev/null | grep 'inet ' | awk '{print $2}' | head -n1 || true)
fi
if [[ -z "$HOST_IP" ]]; then
echo "ERROR: Unable to detect HOST_IP. Set HOST_IP to the host interface address." >&2
exit 1
fi
k3d cluster create knoe-data-cluster \
--agents 2 \
--registry-use k3d-knoe-data-registry:5000 \
--registry-config ${KNOE_HOME}/k3s/registries.yaml \
--k3s-arg "--datastore-endpoint=mysql://knoe:$PROLE_PASSWD\@tcp($HOST_IP:3306)/k3s@server:*"