diff --git a/k8s/infrastructure/longhorn/expand-replicas-job.yaml b/k8s/infrastructure/longhorn/expand-replicas-job.yaml index b0609a5..f08d81f 100644 --- a/k8s/infrastructure/longhorn/expand-replicas-job.yaml +++ b/k8s/infrastructure/longhorn/expand-replicas-job.yaml @@ -1,6 +1,4 @@ # PostSync hook Job that expands all Longhorn volumes to 3 replicas. -# Runs after the longhorn-config Application syncs (which adds cp-2/cp-3 nodes -# + taint toleration), ensuring there are 3 nodes available before expanding. apiVersion: batch/v1 kind: Job metadata: @@ -28,9 +26,8 @@ spec: echo "Waiting for all 3 Longhorn nodes to be Ready..." for i in {1..30}; do - READY_COUNT=$(kubectl -n longhorn-system get nodes.longhorn.io \ - -o jsonpath='{range .items[?(@.status.conditions[?(@.type=="Ready")].status=="True")]}{.metadata.name}{"\n"}{end}' \ - | wc -l | tr -d ' ') + READY_COUNT=$(kubectl -n longhorn-system get nodes.longhorn.io -o json | \ + jq -r '[.items[] | select(.status.conditions[] | select(.type=="Ready" and .status=="True"))] | length') if [ "$READY_COUNT" -ge 3 ]; then echo "✓ All 3 nodes Ready" @@ -49,16 +46,19 @@ spec: echo echo "Expanding volumes with < 3 replicas..." - kubectl -n longhorn-system get volumes.longhorn.io \ - -o jsonpath='{range .items[?(@.spec.numberOfReplicas<3)]}{.metadata.name}{"\n"}{end}' | \ - while read -r vol; do - if [ -n "$vol" ]; then + VOLUMES=$(kubectl -n longhorn-system get volumes.longhorn.io -o json | \ + jq -r '.items[] | select(.spec.numberOfReplicas < 3) | .metadata.name') + + if [ -z "$VOLUMES" ]; then + echo " No volumes need expansion" + else + echo "$VOLUMES" | while read -r vol; do CURRENT=$(kubectl -n longhorn-system get volume "$vol" -o jsonpath='{.spec.numberOfReplicas}') echo " $vol: $CURRENT → 3 replicas" kubectl -n longhorn-system patch volume "$vol" --type merge \ -p '{"spec":{"numberOfReplicas":3}}' - fi - done + done + fi echo echo "Done. Final replica counts:"