feat: add storage snapshot lifecycle commands
This commit is contained in:
parent
b5b7bcb80c
commit
022ed6e539
|
|
@ -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 - <<EOF
|
||||
case "${snapshot_action}" in
|
||||
create)
|
||||
if [[ ! "${pvc_ref}" =~ ^([a-z0-9-]+)/([a-z0-9-]+)$ || ! "${snapshot_name}" =~ ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$ ]]; then
|
||||
echo "Usage: ./jeannie storage snapshot create --pvc NAMESPACE/PVC --name SNAPSHOT [--yes]" >&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 - <<EOF
|
||||
apiVersion: snapshot.storage.k8s.io/v1
|
||||
kind: VolumeSnapshot
|
||||
metadata:
|
||||
|
|
@ -3359,6 +3373,62 @@ spec:
|
|||
source:
|
||||
persistentVolumeClaimName: ${pvc}
|
||||
EOF
|
||||
;;
|
||||
delete)
|
||||
if [[ ! "${snapshot_ref}" =~ ^([a-z0-9-]+)/([a-z0-9-]+)$ ]]; then
|
||||
echo "Usage: ./jeannie storage snapshot delete --snapshot NAMESPACE/SNAPSHOT [--yes]" >&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 - <<EOF
|
||||
apiVersion: v1
|
||||
kind: PersistentVolumeClaim
|
||||
metadata:
|
||||
name: ${pvc}
|
||||
spec:
|
||||
accessModes:
|
||||
- ReadWriteOnce
|
||||
resources:
|
||||
requests:
|
||||
storage: ${restore_size}
|
||||
dataSource:
|
||||
name: ${snapshot_resource}
|
||||
kind: VolumeSnapshot
|
||||
apiGroup: snapshot.storage.k8s.io
|
||||
$(if [[ -n "${storage_class}" ]]; then printf ' storageClassName: %s\n' "${storage_class}"; fi)
|
||||
EOF
|
||||
;;
|
||||
esac
|
||||
;;
|
||||
expand)
|
||||
while (($# > 0)); do
|
||||
|
|
|
|||
Loading…
Reference in New Issue