Created
February 7, 2025 20:14
-
-
Save mattmattox/920dc50a773c2fff6d0d9f82066f883e to your computer and use it in GitHub Desktop.
harvester-lh-vol-1.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
volume_names=$(kubectl -n longhorn-system get volumes.longhorn.io -o json | jq -r '.items[] | .metadata.name') | |
for volume_name in $volume_names; | |
do | |
echo "############################################################################################################################" | |
echo -n "numberOfReplicas " | |
kubectl -n longhorn-system patch volumes.longhorn.io $volume_name --type='merge' -p '{"spec":{"numberOfReplicas":1}}'; | |
echo -n "dataLocality " | |
kubectl -n longhorn-system patch volumes.longhorn.io $volume_name --type='merge' -p '{"spec":{"dataLocality":"disabled"}}' | |
echo -n "replicaAutoBalance " | |
kubectl -n longhorn-system patch volumes.longhorn.io $volume_name --type='merge' -p '{"spec":{"replicaAutoBalance":"ignored"}}' | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
CLEANUP_SNAPSHOTS=false | |
volume_names=$(kubectl -n longhorn-system get volumes.longhorn.io -o json | jq -r '.items[] | .metadata.name') | |
for volume_name in $volume_names; | |
do | |
echo "############################################################################################################################" | |
# Patch the volume with the desired changes | |
echo -n "numberOfReplicas " | |
kubectl -n longhorn-system patch volumes.longhorn.io $volume_name --type='merge' -p '{"spec":{"numberOfReplicas":2}}'; | |
echo -n "dataLocality " | |
kubectl -n longhorn-system patch volumes.longhorn.io $volume_name --type='merge' -p '{"spec":{"dataLocality":"best-effort"}}' | |
echo -n "replicaAutoBalance " | |
kubectl -n longhorn-system patch volumes.longhorn.io $volume_name --type='merge' -p '{"spec":{"replicaAutoBalance":"best-effort"}}' | |
# Check if the volume is detached and skip if it is | |
state=$(kubectl -n longhorn-system get volumes.longhorn.io $volume_name -o jsonpath='{.status.state}' 2>/dev/null) | |
if [ "$state" != "attached" ]; then | |
echo "Volume $volume_name is in state: $state. Skipping..." | |
continue | |
fi | |
if [ "$CLEANUP_SNAPSHOTS" = true ]; then | |
echo "Deleting snapshots for volume $volume_name..." | |
# Get all snapshot names for the volume | |
snapshot_names=$(kubectl -n longhorn-system get snapshots.longhorn.io -o json | jq -r --arg VOLUME_NAME "$volume_name" '.items[] | select(.spec.volume == $VOLUME_NAME) | .metadata.name') | |
# Remove each snapshot | |
for snapshot_name in $snapshot_names; | |
do | |
echo "Deleting snapshot $snapshot_name for volume $volume_name" | |
kubectl -n longhorn-system delete snapshots.longhorn.io $snapshot_name | |
done | |
fi | |
# Wait for the rebuild to complete (ensure STATE is 'attached' and ROBUSTNESS is 'healthy') | |
echo "Waiting for volume $volume_name to become healthy..." | |
while true; | |
do | |
# Check if the volume still exists | |
if ! kubectl -n longhorn-system get volumes.longhorn.io $volume_name &> /dev/null; then | |
echo "Volume $volume_name was deleted. Moving on to the next volume." | |
break | |
fi | |
state=$(kubectl -n longhorn-system get volumes.longhorn.io $volume_name -o jsonpath='{.status.state}' 2>/dev/null) | |
robustness=$(kubectl -n longhorn-system get volumes.longhorn.io $volume_name -o jsonpath='{.status.robustness}' 2>/dev/null) | |
if [[ "$state" == "attached" && "$robustness" == "healthy" ]]; then | |
echo "Volume $volume_name is healthy and rebuild is complete." | |
break | |
else | |
echo "Volume $volume_name is still rebuilding. Current state: $state, robustness: $robustness" | |
sleep 10 # Wait for 10 seconds before checking again | |
fi | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment