#!/bin/bash # Methods library for external_dns — sourced by run-*.sh and install-external_dns.sh. # SCRIPT_DIR must be set by the caller 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 } _resolve_kubeconfig() { if [ -n "${KUBECONFIG:-}" ] && [ -f "$KUBECONFIG" ]; then return fi for candidate in \ /var/lib/k0s/pki/admin.conf \ /etc/kubernetes/admin.conf \ /root/.kube/config; do if [ -f "$candidate" ]; then export KUBECONFIG="$candidate" return fi done if command -v k0s >/dev/null 2>&1; then local tmp tmp="$(mktemp)" k0s kubeconfig admin > "$tmp" 2>/dev/null && export KUBECONFIG="$tmp" && return rm -f "$tmp" fi echo "ERROR: kubeconfig not found — set KUBECONFIG explicitly" >&2; exit 1 } _require_env() { local var="$1" if [ -z "${!var:-}" ]; then echo "ERROR: required variable $var is not set" >&2; exit 1 fi } _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_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_apply_bundle() { local fname="${PLAN_PARAM_FILE:-}" [ -n "$fname" ] || { echo "ERROR: PLAN_PARAM_FILE not set for apply_bundle" >&2; exit 1; } local bundle="${SCRIPT_DIR}/manifests/${fname}" if [ ! -f "$bundle" ]; then local version="${EXTERNAL_DNS_VERSION:-v0.21.0}" local url="https://raw.githubusercontent.com/kubernetes-sigs/external-dns/${version}/config/crd/bases/externaldns.k8s.io_dnsendpoints.yaml" echo " [apply_bundle] downloading DNSEndpoint CRD ${version} from raw.githubusercontent.com" mkdir -p "${SCRIPT_DIR}/manifests" curl -fsSL "$url" -o "$bundle" fi _kubectl apply --server-side -f "$bundle" }