#!/bin/sh # Creates or updates Co-Lite's Kubernetes Secrets from the filled-in copy of deploy/k8s/secrets.example # (openspec spec cluster-deployment: secrets never enter the repository). Never prints a value. # deploy/create-secrets.sh [--dry-run] [folder] folder defaults to deploy/k8s/secrets # --dry-run checks the folder and lists the Secrets and key names without kubectl. # NAMESPACE (default co-lite) picks the namespace. set -eu cd "$(dirname "$0")/.." dry= [ "${1:-}" = "--dry-run" ] && { dry=1; shift; } src=${1:-deploy/k8s/secrets} tpl=deploy/k8s/secrets.example ns=${NAMESPACE:-co-lite} optional="co-lite-whatsapp.env" fail() { echo "create-secrets: $*" >&2; exit 1; } [ -d "$src" ] || fail "no folder $src; copy $tpl there and fill it in" keys() { sed -n 's/^\([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' "$1"; } value_of() { sed -n "s/^$2=//p" "$1" | head -n 1; } # Check everything before creating anything, so a half-filled folder changes nothing. for t in "$tpl"/*.env "$tpl"/*/; do base=$(basename "$t") f="$src/$base" if [ ! -e "$f" ]; then case " $optional " in *" $base "*) continue ;; esac fail "missing $f" fi if [ -d "$t" ]; then for k in "$t"*; do kf="$f/$(basename "$k")" [ -s "$kf" ] || fail "missing or empty $kf" ! grep -q '<[^<>]*>' "$kf" || fail "$kf still holds a <…> placeholder" done else for k in $(keys "$t"); do v=$(value_of "$f" "$k") [ -n "$v" ] || fail "$f: $k is missing or empty" case "$v" in *"<"*">"*) fail "$f: $k is still a placeholder" ;; esac done fi done apply() { # name, then kubectl create arguments name=$1; shift kubectl create secret "$@" "$name" -n "$ns" --dry-run=client -o yaml | kubectl apply -f - >/dev/null echo "applied Secret $name" } for f in "$src"/*.env "$src"/*/; do [ -e "$f" ] || continue name=$(basename "$f" .env); name=${name%/} [ -e "$tpl/$(basename "$f")" ] || fail "$f has no template in $tpl" if [ "$name" = co-lite-registry ]; then echo "Secret $name (image pull secret for gitea.containerverse.dev)" [ -n "$dry" ] && continue tmp=$(mktemp -d); trap 'rm -rf "$tmp"' EXIT auth=$(printf '%s:%s' "$(value_of "$f" REGISTRY_USER)" "$(value_of "$f" REGISTRY_PASSWORD)" | base64 | tr -d '\n') (umask 077; printf '{"auths":{"gitea.containerverse.dev":{"auth":"%s"}}}' "$auth" > "$tmp/config.json") apply "$name" generic --type=kubernetes.io/dockerconfigjson --from-file=.dockerconfigjson="$tmp/config.json" elif [ -d "$f" ]; then echo "Secret $name: $(ls "$f" | tr '\n' ' ')" [ -n "$dry" ] && continue apply "$name" generic --from-file="$f" else echo "Secret $name: $(keys "$f" | tr '\n' ' ')" [ -n "$dry" ] && continue apply "$name" generic --from-env-file="$f" fi done