From 022ed6e539f48efa21c78fef74ee1672cab45e75 Mon Sep 17 00:00:00 2001 From: "juvenal.diaz" Date: Thu, 22 Oct 2026 15:34:07 -0600 Subject: [PATCH] feat: add storage snapshot lifecycle commands --- lib/jeannie/commands.sh | 96 +++++++++++++++++++++++++++++++++++------ 1 file changed, 83 insertions(+), 13 deletions(-) diff --git a/lib/jeannie/commands.sh b/lib/jeannie/commands.sh index c7628bb..7141243 100644 --- a/lib/jeannie/commands.sh +++ b/lib/jeannie/commands.sh @@ -3327,30 +3327,44 @@ storage_manage() { "${REPO_ROOT}/scripts/workers" disk-cleanup ;; snapshot) + local snapshot_action="create" + local snapshot_ref="" + local restore_pvc_ref="" + local storage_class="" + local restore_size="" + if [[ "${1:-}" == "create" || "${1:-}" == "delete" || "${1:-}" == "restore" ]]; then + snapshot_action="$1" + shift + fi while (($# > 0)); do case "$1" in --pvc) pvc_ref="${2:-}"; shift 2 ;; --name) snapshot_name="${2:-}"; shift 2 ;; + --snapshot) snapshot_ref="${2:-}"; shift 2 ;; + --size) restore_size="${2:-}"; shift 2 ;; + --storage-class) storage_class="${2:-}"; shift 2 ;; --yes) yes=true; shift ;; - *) echo "Usage: ./jeannie storage snapshot --pvc NAMESPACE/PVC --name SNAPSHOT [--yes]" >&2; return 2 ;; + *) echo "Usage: ./jeannie storage snapshot {create|delete|restore} ..." >&2; return 2 ;; esac done - if [[ ! "${pvc_ref}" =~ ^([^/]+)/([^/]+)$ || -z "${snapshot_name}" ]]; then - echo "Usage: ./jeannie storage snapshot --pvc NAMESPACE/PVC --name SNAPSHOT [--yes]" >&2 - return 2 - fi - namespace="${BASH_REMATCH[1]}" - pvc="${BASH_REMATCH[2]}" if ! kubectl --kubeconfig "${kubeconfig}" get crd volumesnapshots.snapshot.storage.k8s.io >/dev/null 2>&1; then echo "VolumeSnapshot CRD is not installed; no snapshot was created." >&2 return 1 fi - if [[ "${yes}" != true ]]; then - echo "Would snapshot ${namespace}/${pvc} as ${snapshot_name}. Re-run with --yes to apply." - return 0 - fi - kubectl --kubeconfig "${kubeconfig}" -n "${namespace}" get pvc "${pvc}" >/dev/null - kubectl --kubeconfig "${kubeconfig}" -n "${namespace}" apply -f - <&2 + return 2 + fi + namespace="${pvc_ref%%/*}" + pvc="${pvc_ref#*/}" + kubectl --kubeconfig "${kubeconfig}" -n "${namespace}" get pvc "${pvc}" >/dev/null + if [[ "${yes}" != true ]]; then + echo "Would snapshot ${namespace}/${pvc} as ${snapshot_name}. Re-run with --yes to apply." + return 0 + fi + kubectl --kubeconfig "${kubeconfig}" -n "${namespace}" apply -f - <&2 + return 2 + fi + namespace="${snapshot_ref%%/*}" + snapshot_name="${snapshot_ref#*/}" + kubectl --kubeconfig "${kubeconfig}" -n "${namespace}" get volumesnapshot "${snapshot_name}" >/dev/null + if [[ "${yes}" != true ]]; then + echo "Would delete VolumeSnapshot ${namespace}/${snapshot_name}. Re-run with --yes to apply." + return 0 + fi + kubectl --kubeconfig "${kubeconfig}" -n "${namespace}" delete volumesnapshot "${snapshot_name}" + ;; + restore) + if [[ ! "${snapshot_ref}" =~ ^([a-z0-9-]+)/([a-z0-9-]+)$ || ! "${pvc_ref}" =~ ^([a-z0-9-]+)/([a-z0-9-]+)$ || ! "${restore_size}" =~ ^[0-9]+(Mi|Gi|Ti)$ ]]; then + echo "Usage: ./jeannie storage snapshot restore --snapshot NAMESPACE/SNAPSHOT --pvc NAMESPACE/PVC --size SIZE [--storage-class CLASS] [--yes]" >&2 + return 2 + fi + local snapshot_namespace="${snapshot_ref%%/*}" + local snapshot_resource="${snapshot_ref#*/}" + namespace="${pvc_ref%%/*}" + pvc="${pvc_ref#*/}" + kubectl --kubeconfig "${kubeconfig}" -n "${snapshot_namespace}" get volumesnapshot "${snapshot_resource}" >/dev/null + if [[ "$(kubectl --kubeconfig "${kubeconfig}" -n "${snapshot_namespace}" get volumesnapshot "${snapshot_resource}" -o jsonpath='{.status.readyToUse}' 2>/dev/null)" != "true" ]]; then + echo "VolumeSnapshot ${snapshot_namespace}/${snapshot_resource} is not ready to use." >&2 + return 1 + fi + if kubectl --kubeconfig "${kubeconfig}" -n "${namespace}" get pvc "${pvc}" >/dev/null 2>&1; then + echo "Target PVC ${namespace}/${pvc} already exists." >&2 + return 1 + fi + if [[ "${yes}" != true ]]; then + echo "Would restore ${snapshot_namespace}/${snapshot_resource} to PVC ${namespace}/${pvc} (${restore_size}). Re-run with --yes to apply." + return 0 + fi + kubectl --kubeconfig "${kubeconfig}" -n "${namespace}" apply -f - < 0)); do