Skip to content

Commit

Permalink
[controller] Improve migration hook with checks for existence of PVCs…
Browse files Browse the repository at this point in the history
… and terminating PVs (#161)

Signed-off-by: Aleksandr Zimin <[email protected]>
  • Loading branch information
AleksZimin authored Aug 13, 2024
1 parent a73e664 commit 94c9ba6
Showing 1 changed file with 38 additions and 10 deletions.
48 changes: 38 additions & 10 deletions hooks/migrate_csi_endpoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ run_trigger() {
values::set sdsReplicatedVolume.internal.csiMigrationHook.completed "true"
exit 0
fi



termintating_pvs=$(kubectl get pv -o json | jq -r --arg oldDriverName "$OLD_DRIVER_NAME" '.items[] | select(.spec.csi.driver == $oldDriverName and .metadata.deletionTimestamp != null) | .metadata.name')
if [[ -n "$termintating_pvs" ]]; then
echo "There are PVs with driver $OLD_DRIVER_NAME in Terminating state. Fail the hook to restart the migration process"
values::set sdsReplicatedVolume.internal.csiMigrationHook.completed "false"
exit 1
fi

delete_resource ${NAMESPACE} daemonset linstor-csi-node
scale_down_pods ${NAMESPACE} linstor-csi-controller
scale_down_pods ${NAMESPACE} linstor-affinity-controller
Expand Down Expand Up @@ -193,12 +201,15 @@ migrate_pv_pvc() {
pvc=${array[1]}
pv=${array[2]}

kubectl get pvc $pvc -n $namespace -o yaml > pvc-${pvc}.yaml
kubectl get pv $pv -o yaml > pv-${pv}.yaml
if kubectl get pvc "$pvc" -n "$namespace" -o yaml > "pvc-${pvc}.yaml" 2>/dev/null; then
echo "PVC $pvc retrieved successfully and saved to pvc-${pvc}.yaml."
cp pvc-${pvc}.yaml old.pvc-${pvc}.yaml.$TIMESTAMP
else
echo "PVC $pvc does not exist or couldn't be retrieved."
fi

cp pvc-${pvc}.yaml old.pvc-${pvc}.yaml.$TIMESTAMP
kubectl get pv $pv -o yaml > pv-${pv}.yaml
cp pv-${pv}.yaml old.pv-${pv}.yaml.$TIMESTAMP

sed -i "s/$escaped_OLD_DRIVER_NAME/$NEW_DRIVER_NAME/g" pv-${pv}.yaml
sed -i "s/$OLD_ATTACHER/$NEW_ATTACHER/g" pv-${pv}.yaml
sed -i '/resourceVersion: /d' pv-${pv}.yaml
Expand All @@ -217,17 +228,34 @@ migrate_pv_pvc() {
namespace=${array[0]}
pvc=${array[1]}
pv=${array[2]}

echo "Checking if PVC $pvc exists in namespace $namespace"
if kubectl get pvc "$pvc" -n "$namespace" &>/dev/null; then
echo "PVC $pvc exists in namespace $namespace"
pvc_exists=true
else
echo "PVC $pvc does not exist in namespace $namespace. Skipping migration of PVC $pvc."
pvc_exists=false
fi

echo "Deleting pv: $pv"
kubectl delete pv $pv --wait=false # the pv will stuck in Terminating state
echo "Deleting finalizer from pv: $pv"
kubectl patch pv $pv --type json -p '[{"op": "remove", "path": "/metadata/finalizers"}]'
echo "Patching annotations in pvc: $pvc"
kubectl patch pvc ${pvc} -n $namespace --type=json -p='[{"op": "replace", "path": "/metadata/annotations/volume.beta.kubernetes.io~1storage-provisioner", "value":"'$NEW_DRIVER_NAME'"}]'
kubectl patch pvc ${pvc} -n $namespace --type=json -p='[{"op": "replace", "path": "/metadata/annotations/volume.kubernetes.io~1storage-provisioner", "value":"'$NEW_DRIVER_NAME'"}]'

if [[ "$pvc_exists" == "true" ]]; then
echo "Patching annotations in pvc: $pvc"
kubectl patch pvc ${pvc} -n $namespace --type=json -p='[{"op": "replace", "path": "/metadata/annotations/volume.beta.kubernetes.io~1storage-provisioner", "value":"'$NEW_DRIVER_NAME'"}]'
kubectl patch pvc ${pvc} -n $namespace --type=json -p='[{"op": "replace", "path": "/metadata/annotations/volume.kubernetes.io~1storage-provisioner", "value":"'$NEW_DRIVER_NAME'"}]'
fi

echo "Recreating pv: $pv"
kubectl create -f pv-${pv}.yaml
echo "Deleting annotation from pvc $pvc to trigger pvc/pv binding"
kubectl patch pvc ${pvc} -n $namespace --type=json -p='[{"op": "remove", "path": "/metadata/annotations/pv.kubernetes.io~1bind-completed"}]'

if [[ "$pvc_exists" == "true" ]]; then
echo "Deleting annotation from pvc $pvc to trigger pvc/pv binding"
kubectl patch pvc ${pvc} -n $namespace --type=json -p='[{"op": "remove", "path": "/metadata/annotations/pv.kubernetes.io~1bind-completed"}]'
fi

IFS=$old_ifs
done
Expand Down

0 comments on commit 94c9ba6

Please sign in to comment.