|
| 1 | +#!/bin/bash |
| 2 | +# Copyright 2025 The Kubernetes Authors. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +### Helper script for running EBS-backed PVC expansion and modification test |
| 17 | + |
| 18 | +# We expect this helper script is sourced from hack/ebs-scale-test |
| 19 | +path_to_resize_and_modify_test_dir="${BASE_DIR}/helpers/scale-test/expand-and-modify-test" |
| 20 | + |
| 21 | +export EXPAND_ONLY=${EXPAND_ONLY:="false"} |
| 22 | +export MODIFY_ONLY=${MODIFY_ONLY:="false"} |
| 23 | + |
| 24 | +expand_and_modify_test() { |
| 25 | + manifest_path="$path_to_resize_and_modify_test_dir/expand-and-modify.yaml" |
| 26 | + export_manifest_path="$EXPORT_DIR/expand-and-modify.yaml" |
| 27 | + |
| 28 | + echo "Applying $manifest_path. Exported to $export_manifest_path" |
| 29 | + gomplate -f "$manifest_path" -o "$export_manifest_path" |
| 30 | + kubectl apply -f "$export_manifest_path" |
| 31 | + |
| 32 | + # Cleanup K8s resources upon script interruption |
| 33 | + trap 'echo "Test interrupted! Deleting test resources to prevent leak"; kubectl delete -f $export_manifest_path' EXIT |
| 34 | + |
| 35 | + echo "Waiting for all PVCs to be dynamically provisioned" |
| 36 | + wait_for_pvcs_to_bind |
| 37 | + |
| 38 | + case "$EXPAND_ONLY-$MODIFY_ONLY" in |
| 39 | + *false-false*) patches='[{"op": "replace", "path": "/spec/volumeAttributesClassName", "value": "ebs-scale-test-expand-and-modify"},{"op": "replace", "path": "/spec/resources/requests/storage", "value": "2Gi"}]' ;; |
| 40 | + *false-true*) patches='[{"op": "replace", "path": "/spec/volumeAttributesClassName", "value": "ebs-scale-test-expand-and-modify"}]' ;; |
| 41 | + *true-false* | *delete*) patches='[{"op": "replace", "path": "/spec/resources/requests/storage", "value": "2Gi"}]' ;; |
| 42 | + *) echo "Environment variables EXPAND_ONLY ('$EXPAND_ONLY') and MODIFY_ONLY ('$MODIFY_ONLY') are not properly set to either 'true' or 'false'" ;; |
| 43 | + esac |
| 44 | + |
| 45 | + echo "Patching PVCs with $patches" |
| 46 | + kubectl get pvc -o name | sed -e 's/.*\///g' | xargs -P 5 -I {} kubectl patch pvc {} --type=json -p="$patches" |
| 47 | + |
| 48 | + echo "Waiting until volumes modified and/or expanded" |
| 49 | + ensure_volumes_modified |
| 50 | + |
| 51 | + echo "Deleting resources" |
| 52 | + kubectl delete -f "$export_manifest_path" |
| 53 | + |
| 54 | + echo "Waiting for all PVs to be deleted" |
| 55 | + wait_for_pvs_to_delete |
| 56 | + |
| 57 | + trap - EXIT |
| 58 | +} |
| 59 | + |
| 60 | +ensure_volumes_modified() { |
| 61 | + if [[ "$EXPAND_ONLY" == "false" ]]; then |
| 62 | + while true; do |
| 63 | + modified_volumes_count=$(kubectl get pvc -o json | jq '.items | map(select(.status.currentVolumeAttributesClassName == "ebs-scale-test-expand-and-modify")) | length') |
| 64 | + echo "$modified_volumes_count/$REPLICAS volumes modified" |
| 65 | + if [[ "$modified_volumes_count" == "$REPLICAS" ]]; then |
| 66 | + echo "All volumes modified" |
| 67 | + break |
| 68 | + fi |
| 69 | + sleep 5 |
| 70 | + done |
| 71 | + fi |
| 72 | + |
| 73 | + if [[ "$MODIFY_ONLY" == "false" ]]; then |
| 74 | + while true; do |
| 75 | + expanded_volumes_count=$(kubectl get pvc -o json | jq '.items | map(select(.status.capacity.storage == "2Gi")) | length') |
| 76 | + echo "$expanded_volumes_count/$REPLICAS volumes expanded" |
| 77 | + if [[ "$expanded_volumes_count" == "$REPLICAS" ]]; then |
| 78 | + echo "All volumes expanded" |
| 79 | + break |
| 80 | + fi |
| 81 | + sleep 5 |
| 82 | + done |
| 83 | + fi |
| 84 | +} |
| 85 | + |
| 86 | +wait_for_pvcs_to_bind() { |
| 87 | + while true; do |
| 88 | + bound_pvc_count=$(kubectl get pvc -o json | jq '.items | map(select(.status.phase == "Bound")) | length') |
| 89 | + if [[ "$bound_pvc_count" -ge "$REPLICAS" ]]; then |
| 90 | + echo "All PVCs bound, proceeding..." |
| 91 | + break |
| 92 | + else |
| 93 | + echo "Only $bound_pvc_count PVCs are bound, waiting for a total of $REPLICAS..." |
| 94 | + sleep 5 |
| 95 | + fi |
| 96 | + done |
| 97 | +} |
| 98 | + |
| 99 | +(return 0 2>/dev/null) || ( |
| 100 | + echo "This script is not meant to be run directly, only sourced as a helper!" |
| 101 | + exit 1 |
| 102 | +) |
0 commit comments