prole/etc/init_k8s.sh

253 lines
5.9 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# etc/init_k8s.sh
# Purpose: Manage k3d/k8s environment
# Initialize SCRIPT_DIR
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# Load environment and config via knoe_cfg.sh
# shellcheck disable=SC1090
source "$SCRIPT_DIR/knoe_cfg.sh"
# Default values
VERBOSE=false
ENVIRONMENT="prod"
HOST=""
CLUSTER_NAME_DEFAULT="knoe-dev-cluster"
CLUSTER_PORT_DEFAULT="6443"
usage() {
cat <<EOF
Usage: $0 [initialize|start|stop|restart|status] [options]
Actions:
initialize Create a k3d cluster (dev) or fetch kubeconfig (service/prod)
start Start the k3d cluster
stop Stop the k3d cluster
restart Restart the k3d cluster
status Show status of Docker and k3d cluster
Options:
-v, --verbose Enable verbose output
-m, --mode k3d|k3s|k8s (maps to dev|service|prod)
-e, --environment dev|service|prod (default: $ENVIRONMENT)
-h, --host Remote host for service/prod environments
-n, --name k3d cluster name
EOF
exit 1
}
log() {
echo "[INFO] $*"
}
debug() {
if [[ "$VERBOSE" == "true" ]]; then
echo "[DEBUG] $*"
fi
}
# Parse options
POSITIONAL_ARGS=()
CLUSTER_NAME=""
while [[ $# -gt 0 ]]; do
case $1 in
-v|--verbose)
VERBOSE=true
shift
;;
-m|--mode)
knoe_set_mode "${2:-}"
case "${KNOE_MODE:-}" in
k3d) ENVIRONMENT="dev" ;;
k3s) ENVIRONMENT="service" ;;
k8s) ENVIRONMENT="prod" ;;
esac
shift 2
;;
--mode=*|-m=*)
knoe_set_mode "${1#*=}"
case "${KNOE_MODE:-}" in
k3d) ENVIRONMENT="dev" ;;
k3s) ENVIRONMENT="service" ;;
k8s) ENVIRONMENT="prod" ;;
esac
shift
;;
-e|--environment)
ENVIRONMENT="$2"
shift 2
;;
-h|--host)
HOST="$2"
shift 2
;;
-n|--name)
CLUSTER_NAME="$2"
shift 2
;;
*)
POSITIONAL_ARGS+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}"
ACTION=${1:-}
if [[ -z "$ACTION" ]]; then
usage
fi
ensure_tools() {
if [[ "$ENVIRONMENT" == "dev" ]]; then
for t in k3d docker; do
command -v "$t" >/dev/null || { echo "ERROR: Missing required tool: $t" >&2; exit 1; }
done
else
command -v kubectl >/dev/null || { echo "ERROR: Missing required tool: kubectl" >&2; exit 1; }
fi
}
ensure_docker() {
if [[ "$ENVIRONMENT" != "dev" ]]; then
return 0
fi
if ! docker info >/dev/null 2>&1; then
echo "ERROR: Docker is not running." >&2
exit 1
fi
}
initialize() {
ensure_tools
ensure_docker
if [[ "$ENVIRONMENT" == "dev" ]]; then
if [[ -z "$CLUSTER_NAME" ]]; then
if [[ -t 0 ]]; then
read -p "Enter k3d cluster name [$CLUSTER_NAME_DEFAULT]: " CLUSTER_NAME
CLUSTER_NAME=${CLUSTER_NAME:-$CLUSTER_NAME_DEFAULT}
else
CLUSTER_NAME=$CLUSTER_NAME_DEFAULT
fi
fi
if k3d cluster list "$CLUSTER_NAME" >/dev/null 2>&1; then
log "Cluster '$CLUSTER_NAME' already exists."
else
data_dir="${PROLE_DATA:-}"
if [[ -z "$data_dir" && -f "$SCRIPT_DIR/../env.sh" ]]; then
# shellcheck disable=SC1090
source "$SCRIPT_DIR/../env.sh"
data_dir="${PROLE_DATA:-}"
fi
if [[ -z "$data_dir" ]]; then
data_dir="$SCRIPT_DIR/../data"
fi
mkdir -p "$data_dir" >/dev/null 2>&1 || true
volume_args=()
if [[ -n "$data_dir" ]]; then
volume_args=(--volume "${data_dir}:/var/lib/rancher/k3s/storage@all")
fi
# Generate a registries.yaml so k3d nodes can pull from the local
# registry over plain HTTP (insecure). The file is written to a
# temp location and passed via --registry-config at creation time.
registry_config_args=()
local reg_cfg
reg_cfg=$(mktemp "${TMPDIR:-/tmp}/k3d-registries-XXXXXX.yaml")
cat > "$reg_cfg" <<'REGEOF'
mirrors:
"k3d-knoe-registry:5000":
endpoint:
- "http://k3d-knoe-registry:5000"
REGEOF
registry_config_args=(--registry-config "$reg_cfg")
log "Creating k3d cluster '$CLUSTER_NAME'..."
k3d cluster create "$CLUSTER_NAME" \
--api-port "0.0.0.0:${CLUSTER_PORT_DEFAULT}" \
--agents 2 \
-p "0.0.0.0:${CLUSTER_PORT_DEFAULT}:6443@server:0" \
"${volume_args[@]}" \
"${registry_config_args[@]}"
rm -f "$reg_cfg" 2>/dev/null || true
fi
else
if [[ -n "$HOST" ]]; then
log "Environment is $ENVIRONMENT. Host is $HOST."
log "TODO: Fetch kubeconfig from $HOST (placeholder)."
else
log "Environment is $ENVIRONMENT. Use -h|--host to specify the remote cluster host."
fi
fi
}
status() {
ensure_tools
log "Checking Docker status..."
if docker info >/dev/null 2>&1; then
echo "Docker is running."
else
echo "Docker is NOT running."
fi
log "Listing k3d clusters..."
k3d cluster list
}
get_cluster_name() {
# If a name was provided or exists in env, use it.
# Otherwise, if there is only one cluster, use it.
# Finally, use default.
if [[ -n "${CLUSTER_NAME:-}" ]]; then
echo "$CLUSTER_NAME"
return
fi
local clusters
clusters=$(k3d cluster list --no-headers | awk '{print $1}')
local count
count=$(echo "$clusters" | grep -c . || true)
if [[ "$count" -eq 1 ]]; then
echo "$clusters"
else
echo "$CLUSTER_NAME_DEFAULT"
fi
}
case "$ACTION" in
initialize)
initialize
;;
status)
status
;;
start)
ensure_tools
CLUSTER_NAME=$(get_cluster_name)
log "Starting k3d cluster '$CLUSTER_NAME'..."
k3d cluster start "$CLUSTER_NAME"
;;
stop)
ensure_tools
CLUSTER_NAME=$(get_cluster_name)
log "Stopping k3d cluster '$CLUSTER_NAME'..."
k3d cluster stop "$CLUSTER_NAME"
;;
restart)
ensure_tools
CLUSTER_NAME=$(get_cluster_name)
log "Restarting k3d cluster '$CLUSTER_NAME'..."
k3d cluster stop "$CLUSTER_NAME"
k3d cluster start "$CLUSTER_NAME"
;;
*)
usage
;;
esac