#!/bin/sh # Create lab credentials once, outside Helm values and release history. set -eu : "${KUBE_CONTEXT:=dev}" "${NAMESPACE:=highload-applications}" : "${REGISTRY:=asisti.registry.twcstorage.ru}" "${REGISTRY_USER:=token}" : "${REGISTRY_TOKEN_FILE:=$HOME/.secrets/twc-registry-token}" test -s "$REGISTRY_TOKEN_FILE" || { echo 'Registry token file is missing or empty' >&2; exit 1; } umask 077 work=$(mktemp -d) trap 'rm -rf "$work"' EXIT kubectl --context "$KUBE_CONTEXT" create namespace "$NAMESPACE" --dry-run=client -o yaml | kubectl --context "$KUBE_CONTEXT" apply -f - if ! kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" get secret messenger-secrets >/dev/null 2>&1; then openssl rand -hex 32 | tr -d '\n' > "$work/postgres-password" printf 'postgres://messenger:%s@postgres:5432/messenger?sslmode=disable' "$(cat "$work/postgres-password")" > "$work/database-url" openssl rand -base64 32 | tr -d '\n' > "$work/cursor-key" kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" create secret generic messenger-secrets --from-file="$work/postgres-password" --from-file="$work/database-url" --from-file="$work/cursor-key" fi # Add the shared cursor key to an existing lab2 secret without replacing database credentials. if ! kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" get secret messenger-secrets -o json | jq -e '.data["cursor-key"] | length > 0' >/dev/null; then openssl rand -base64 32 | tr -d '\n' > "$work/cursor-key" jq -n --rawfile key "$work/cursor-key" '{stringData:{"cursor-key":$key}}' > "$work/patch.json" kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" patch secret messenger-secrets --type merge --patch-file "$work/patch.json" fi # Build a portable Docker config; macOS credential-helper references cannot be used by kubelet. jq -n --arg registry "$REGISTRY" --arg user "$REGISTRY_USER" --rawfile token "$REGISTRY_TOKEN_FILE" \ '{auths:{($registry):{auth:(($user+":"+($token | sub("[\\r\\n]+$"; "")))|@base64)}}}' > "$work/config.json" kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" create secret generic registry-pull \ --type=kubernetes.io/dockerconfigjson --from-file=".dockerconfigjson=$work/config.json" \ --dry-run=client -o yaml | kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" apply -f -