Files
homelab-gitops/apps/immich/scripts/storage-offline.sh
T

47 lines
1.6 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
readonly expected_context="kubernetes-admin@kubernetes"
readonly node="arschrock"
readonly ready_label="immich.brunner.ninja/library-ready"
readonly selector='app.kubernetes.io/instance=immich,app.kubernetes.io/name in (server,machine-learning)'
current_context="$(kubectl config current-context)"
if [[ "${current_context}" != "${expected_context}" ]]; then
echo "Refusing to modify ${node}: context is ${current_context}, expected ${expected_context}." >&2
exit 1
fi
kubectl get node "${node}" >/dev/null
if ! kubectl label node "${node}" "${ready_label}-" 2>/dev/null; then
labels="$(kubectl get node "${node}" --output=jsonpath='{.metadata.labels}')"
if [[ "${labels}" == *"${ready_label}"* ]]; then
echo "Could not remove the Immich library-ready label from ${node}; do not unmount /mnt." >&2
exit 1
fi
fi
mapfile -t pods < <(
kubectl --namespace immich get pods \
--selector="${selector}" \
--field-selector="spec.nodeName=${node}" \
--output=name 2>/dev/null || true
)
if ((${#pods[@]})); then
kubectl --namespace immich delete "${pods[@]}" --wait=false
for pod in "${pods[@]}"; do
kubectl --namespace immich wait --for=delete "${pod}" --timeout=5m
done
fi
remaining_pods="$(kubectl --namespace immich get pods \
--selector="${selector}" \
--field-selector="spec.nodeName=${node}" \
--output=name 2>/dev/null || true)"
if [[ -n "${remaining_pods}" ]]; then
echo "Immich Pods remain on ${node}; do not unmount /mnt." >&2
exit 1
fi
echo "Immich server and machine-learning Pods are stopped on ${node}; /mnt can be unmounted safely."