prole/etc/init_k3s_registry.sh
chrisfu 4ee2b259c9 Checkpoint: rename installer to knoe + harden db build context
- Add build-context helper to copy Docker context safely (ignore runtime data, keep symlinks)

- Update UI and core actions to use ~/.prole/build and shared copy helper

- Add/adjust tests and scripts; introduce knoe ops helpers and update manifests

Co-authored-by: Junie <junie@jetbrains.com>
2026-03-22 01:45:21 -07:00

109 lines
2.8 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
# init_k3s_registry.sh
# Purpose:
# - Configure k3s/containerd to allow HTTPS access to the Prole registry
# - Writes /etc/rancher/k3s/registries.yaml on the k3s node
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck disable=SC1090
source "$SCRIPT_DIR/prole_cfg.sh"
ACTION=${1:-apply}
registry_host_from_url() {
local value="${1:-}"
value="${value#http://}"
value="${value#https://}"
value="${value%%/*}"
value="${value%%:*}"
printf '%s' "$value"
}
K3S_REGISTRY_HOST=${K3S_REGISTRY_HOST:-$(registry_host_from_url "${PROLE_K3S_SERVER:-${K3S_SERVER_URL:-}}")}
K3S_REGISTRY_PORT=${K3S_REGISTRY_PORT:-5000}
K3S_REGISTRY_NAMESPACE=${K3S_REGISTRY_NAMESPACE:-${REGISTRY_NAMESPACE:-${SERVICE_NAMESPACE:-${PROLE_NAMESPACE:-default}}}}
K3S_REGISTRY_FILE=${K3S_REGISTRY_FILE:-/etc/rancher/k3s/registries.yaml}
K3S_REGISTRY_SCHEME=${K3S_REGISTRY_SCHEME:-}
if [[ -z "${K3S_REGISTRY_SCHEME}" ]]; then
# k3d's local registry is plain HTTP by default.
if [[ "${K3S_REGISTRY_HOST}" == k3d-* ]]; then
K3S_REGISTRY_SCHEME="http"
else
K3S_REGISTRY_SCHEME="https"
fi
fi
ensure_root() {
if [[ "$(id -u)" -ne 0 ]]; then
echo "ERROR: must run as root to write $K3S_REGISTRY_FILE" >&2
exit 1
fi
}
render_registries_yaml() {
local host="$1"
local port="$2"
local ns="$3"
local scheme="$4"
if [[ "$scheme" == "http" ]]; then
cat <<EOF
mirrors:
"${host}:${port}":
endpoint:
- "http://${host}:${port}"
"registry.${ns}.svc.cluster.local:${port}":
endpoint:
- "http://${host}:${port}"
EOF
return 0
fi
cat <<EOF
mirrors:
"${host}:${port}":
endpoint:
- "${scheme}://${host}:${port}"
"registry.${ns}.svc.cluster.local:${port}":
endpoint:
- "${scheme}://${host}:${port}"
configs:
"${host}:${port}":
tls:
insecure_skip_verify: true
"registry.${ns}.svc.cluster.local:${port}":
tls:
insecure_skip_verify: true
EOF
}
case "$ACTION" in
apply|update)
if [[ -z "${K3S_REGISTRY_HOST:-}" ]]; then
echo "ERROR: K3S_REGISTRY_HOST is empty (set PROLE_K3S_SERVER or K3S_REGISTRY_HOST)." >&2
exit 2
fi
ensure_root
mkdir -p "$(dirname "$K3S_REGISTRY_FILE")"
render_registries_yaml "$K3S_REGISTRY_HOST" "$K3S_REGISTRY_PORT" "$K3S_REGISTRY_NAMESPACE" "$K3S_REGISTRY_SCHEME" >"$K3S_REGISTRY_FILE"
echo "Wrote $K3S_REGISTRY_FILE for ${K3S_REGISTRY_SCHEME}://${K3S_REGISTRY_HOST}:${K3S_REGISTRY_PORT}"
echo "Restart k3s to apply: sudo systemctl restart k3s"
;;
status)
if [[ -f "$K3S_REGISTRY_FILE" ]]; then
echo "Found $K3S_REGISTRY_FILE"
cat "$K3S_REGISTRY_FILE"
else
echo "No registries.yaml at $K3S_REGISTRY_FILE"
exit 1
fi
;;
*)
echo "Usage: $0 {apply|status}" >&2
exit 2
;;
esac