#!/bin/bash # Methods library for loki — sourced by install-loki.sh and generated run-*.sh scripts. # SCRIPT_DIR must be set before sourcing. _kubectl() { if command -v kubectl >/dev/null 2>&1; then command kubectl "$@" elif command -v k0s >/dev/null 2>&1; then k0s kubectl "$@" else echo "ERROR: no kubectl or k0s binary found" >&2; exit 127 fi } _require_env() { local var="$1" if [ -z "${!var:-}" ]; then echo "ERROR: required variable $var is not set" >&2; exit 1 fi } _pod_name() { _kubectl get pod \ --namespace "$LOKI_NAMESPACE" \ --selector "app=loki" \ -o jsonpath='{.items[0].metadata.name}' 2>/dev/null } _wait_for_pod() { local timeout="${1:-180}" _kubectl wait pod \ --namespace "$LOKI_NAMESPACE" \ --selector "app=loki" \ --for=condition=Ready \ --timeout="${timeout}s" } _plan_apply() { local file="$1" local path="$SCRIPT_DIR/manifests/${file}.yaml" [ -f "$path" ] || { echo " [skip] ${file}.yaml not in manifests/"; return 0; } _kubectl apply -f "$path" } _plan_apply_skip() { local file="$1" local path="$SCRIPT_DIR/manifests/${file}.yaml" [ -f "$path" ] || { echo " [skip] ${file}.yaml not in manifests/"; return 0; } if _kubectl get -f "$path" >/dev/null 2>&1; then echo " [skip] ${file} already exists in cluster" return 0 fi _kubectl apply -f "$path" } _plan_rollout_restart() { local file="$1" local path="$SCRIPT_DIR/manifests/${file}.yaml" [ -f "$path" ] || { echo " [skip] ${file}.yaml not in manifests/"; return 0; } _kubectl rollout restart -f "$path" } _plan_delete() { local file="$1" local path="$SCRIPT_DIR/manifests/${file}.yaml" [ -f "$path" ] || { echo " [skip] ${file}.yaml not in manifests/"; return 0; } _kubectl delete -f "$path" --ignore-not-found } _method_wait-ready() { _wait_for_pod 180 } _method_create-credentials() { [ -f "${SCRIPT_DIR}/_credentials.env" ] && set -a && source "${SCRIPT_DIR}/_credentials.env" && set +a [ -f "${SCRIPT_DIR}/env-loki" ] && set -a && source "${SCRIPT_DIR}/env-loki" && set +a if [ "${LOKI_STORAGE_BACKEND:-filesystem}" != "s3" ]; then echo " [skip] storage_backend=${LOKI_STORAGE_BACKEND:-filesystem} — no S3 credentials secret needed" return 0 fi _require_env LOKI_S3_ACCESS_KEY _require_env LOKI_S3_SECRET_KEY _kubectl create secret generic loki-s3-credentials \ --namespace "$LOKI_NAMESPACE" \ --from-literal=LOKI_S3_ACCESS_KEY="$LOKI_S3_ACCESS_KEY" \ --from-literal=LOKI_S3_SECRET_KEY="$LOKI_S3_SECRET_KEY" \ --dry-run=client -o yaml | _kubectl apply -f - echo " [ok] credentials secret 'loki-s3-credentials' created in ${LOKI_NAMESPACE}" }