feat: guard storage volume expansion

This commit is contained in:
juvenal.diaz 2026-10-22 15:34:37 -06:00
parent 022ed6e539
commit 44656ca29c
1 changed files with 42 additions and 1 deletions

View File

@ -3303,6 +3303,22 @@ storage_status() {
check_pimox_storage || printf '%s\n' 'Pimox storage check failed or is unavailable.'
}
storage_size_bytes() {
local value="${1:-}"
local number unit multiplier
if [[ ! "${value}" =~ ^([0-9]+)(Mi|Gi|Ti)$ ]]; then
return 1
fi
number="${BASH_REMATCH[1]}"
unit="${BASH_REMATCH[2]}"
case "${unit}" in
Mi) multiplier=$((1024 ** 2)) ;;
Gi) multiplier=$((1024 ** 3)) ;;
Ti) multiplier=$((1024 ** 4)) ;;
esac
printf '%s\n' $((number * multiplier))
}
storage_manage() {
local action="${1:-status}"
shift || true
@ -3431,6 +3447,8 @@ EOF
esac
;;
expand)
local storage_class_name
local allow_expansion
while (($# > 0)); do
case "$1" in
--pvc) pvc_ref="${2:-}"; shift 2 ;;
@ -3450,8 +3468,31 @@ EOF
return 2
fi
current="$(kubectl --kubeconfig "${kubeconfig}" -n "${namespace}" get pvc "${pvc}" -o jsonpath='{.spec.resources.requests.storage}')"
if [[ -z "${current}" ]]; then
echo "PVC ${namespace}/${pvc} was not found or has no storage request." >&2
return 1
fi
storage_class_name="$(kubectl --kubeconfig "${kubeconfig}" -n "${namespace}" get pvc "${pvc}" -o jsonpath='{.spec.storageClassName}')"
if [[ -z "${storage_class_name}" ]]; then
echo "PVC ${namespace}/${pvc} has no StorageClass; expansion is unsafe." >&2
return 1
fi
allow_expansion="$(kubectl --kubeconfig "${kubeconfig}" get storageclass "${storage_class_name}" -o jsonpath='{.allowVolumeExpansion}')"
if [[ "${allow_expansion}" != true ]]; then
echo "StorageClass ${storage_class_name} does not allow volume expansion." >&2
return 1
fi
current_bytes="$(storage_size_bytes "${current}")" || {
echo "Current PVC size ${current} is not supported for safe comparison." >&2
return 1
}
requested_bytes="$(storage_size_bytes "${size}")" || return 1
if (( requested_bytes <= current_bytes )); then
echo "Requested size ${size} must be larger than current size ${current}." >&2
return 1
fi
if [[ "${yes}" != true ]]; then
echo "Would request expansion of ${namespace}/${pvc}: ${current} -> ${size}. Re-run with --yes to apply."
echo "Would request expansion of ${namespace}/${pvc}: ${current} -> ${size} (StorageClass ${storage_class_name}, expansion enabled). Re-run with --yes to apply."
return 0
fi
kubectl --kubeconfig "${kubeconfig}" -n "${namespace}" patch pvc "${pvc}" --type merge -p "{\"spec\":{\"resources\":{\"requests\":{\"storage\":\"${size}\"}}}}"