PVC 이름변경 스크립트

1 개요[ | ]

PVC 이름변경 스크립트
#!/bin/bash

set -euo pipefail

if [[ $# -ne 3 ]]; then
  echo "Usage: $0 <namespace> <old-pvc-name> <new-pvc-name>"
  exit 1
fi

NAMESPACE="$1"
OLD_PVC="$2"
NEW_PVC="$3"

TMP_DIR=$(mktemp -d)

function cleanup {
  rm -rf "$TMP_DIR"
}
trap cleanup EXIT

echo "Namespace: $NAMESPACE"
echo "Old PVC:   $OLD_PVC"
echo "New PVC:   $NEW_PVC"
echo ""

# 1. 기존 PVC 가져오기
echo "[1/6] Exporting old PVC..."
kubectl get pvc "$OLD_PVC" -n "$NAMESPACE" -o json > "$TMP_DIR/old-pvc.json"

PV_NAME=$(jq -r '.spec.volumeName' "$TMP_DIR/old-pvc.json")
if [[ -z "$PV_NAME" || "$PV_NAME" == "null" ]]; then
  echo "ERROR: No PersistentVolume bound to PVC $OLD_PVC"
  exit 1
fi
echo "Bound PV: $PV_NAME"

# 2. 새 PVC 생성
echo "[2/6] Creating new PVC..."
jq "
  .metadata.name = \"$NEW_PVC\" |
  .metadata.resourceVersion = null |
  .metadata.uid = null |
  .metadata.creationTimestamp = null |
  .spec.volumeName = \"$PV_NAME\"
" "$TMP_DIR/old-pvc.json" | kubectl apply -n "$NAMESPACE" -f -

# 3. 새 PVC의 UID 가져오기
echo "[3/6] Patching PV to point to new PVC..."
NEW_UID=$(kubectl get pvc "$NEW_PVC" -n "$NAMESPACE" -o jsonpath='{.metadata.uid}')

kubectl get pv "$PV_NAME" -o json | jq "
  .spec.claimRef.name = \"$NEW_PVC\" |
  .spec.claimRef.namespace = \"$NAMESPACE\" |
  .spec.claimRef.uid = \"$NEW_UID\"
" | kubectl apply -f -

# 4. PVC 상태 확인
echo "[4/6] Waiting for new PVC to be Bound..."
for i in {1..10}; do
  STATUS=$(kubectl get pvc "$NEW_PVC" -n "$NAMESPACE" -o jsonpath='{.status.phase}')
  echo "PVC Status: $STATUS"
  if [[ "$STATUS" == "Bound" ]]; then
    break
  fi
  sleep 2
done

if [[ "$STATUS" != "Bound" ]]; then
  echo "ERROR: New PVC did not reach Bound status."
  exit 1
fi

# 5. 기존 PVC 삭제
echo "[5/6] Deleting old PVC..."
kubectl delete pvc "$OLD_PVC" -n "$NAMESPACE"

echo "[6/6] PVC rename completed successfully!"

2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}