|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -o errexit -o nounset -o pipefail |
| 4 | + |
| 5 | +ACCEPTABLE_CPU_CORE_USAGE=2 |
| 6 | +DOCKER_IMAGE=pytorch/torchserve-nightly:latest-gpu |
| 7 | + |
| 8 | +# Get relative path of example dir with respect to root |
| 9 | +# Ex: if ROOT_DIR is ~/serve , EXAMPLE_DIR is ./kubernetes/tests/scripts |
| 10 | +EXAMPLE_DIR=$(dirname "$(readlink -f "$0")") |
| 11 | +ROOT_DIR=${EXAMPLE_DIR}/../../../ |
| 12 | +ROOT_DIR=$(realpath "$ROOT_DIR") |
| 13 | +EXAMPLE_DIR=$(echo "$EXAMPLE_DIR" | sed "s|$ROOT_DIR|./|") |
| 14 | + |
| 15 | +function start_minikube_cluster() { |
| 16 | + echo "Removing any previous Kubernetes cluster" |
| 17 | + minikube delete |
| 18 | + echo "Starting Kubernetes cluster" |
| 19 | + minikube start --gpus all --mount-string="$GITHUB_WORKSPACE:/host" --mount |
| 20 | + minikube addons enable metrics-server |
| 21 | +} |
| 22 | + |
| 23 | +function build_docker_image() { |
| 24 | + eval $(minikube docker-env) |
| 25 | + docker system prune -f |
| 26 | + docker build -t $DOCKER_IMAGE --file $ROOT_DIR/$EXAMPLE_DIR/../docker/Dockerfile --build-arg EXAMPLE_DIR="${EXAMPLE_DIR}" . |
| 27 | + eval $(minikube docker-env -u) |
| 28 | + |
| 29 | +} |
| 30 | + |
| 31 | +function get_model_archive() { |
| 32 | + echo "Downloading archive for $2" |
| 33 | + mkdir model_store -p |
| 34 | + wget $1 -O model_store/"$2".mar |
| 35 | + pwd |
| 36 | + echo $GITHUB_WORKSPACE |
| 37 | +} |
| 38 | + |
| 39 | +function deploy_cluster() { |
| 40 | + echo "Deploying the cluster" |
| 41 | + kubectl apply -f "$1" |
| 42 | + echo "Waiting for pod to come up..." |
| 43 | + wait_for_pod_running "$2" 300 |
| 44 | + echo "Check status of the pod" |
| 45 | + kubectl describe pod "$2" |
| 46 | +} |
| 47 | + |
| 48 | +function wait_for_pod_running() { |
| 49 | + pod_name="$1" |
| 50 | + max_wait_time="$2" |
| 51 | + interval=5 |
| 52 | + start_time=$(date +%s) |
| 53 | + while true; do |
| 54 | + sleep "$interval" |
| 55 | + pod_description=$(kubectl describe pod "$pod_name") |
| 56 | + status_line=$(echo "$pod_description" | grep -E "Status:") |
| 57 | + pod_status=$(echo "$status_line" | awk '{print $2}') |
| 58 | + if [[ "$pod_status" == "Running" ]]; then |
| 59 | + break |
| 60 | + fi |
| 61 | + current_time=$(date +%s) |
| 62 | + if (( current_time - start_time >= max_wait_time )); then |
| 63 | + echo "Timeout waiting for pod $pod_name to become Running." |
| 64 | + delete_minikube_cluster |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | + done |
| 68 | +} |
| 69 | + |
| 70 | +function delete_minikube_cluster() { |
| 71 | + echo "Delete cluster" |
| 72 | + minikube delete |
| 73 | +} |
| 74 | + |
| 75 | +function check_cpu_cores { |
| 76 | + |
| 77 | + start_time=$(date +%s) |
| 78 | + interval=10 |
| 79 | + while true; do |
| 80 | + # Check if the Metrics API error message is present |
| 81 | + if ! kubectl top pod -l app=$1 | grep -q $1 ; then |
| 82 | + sleep "$interval" |
| 83 | + else |
| 84 | + echo "Wait for metrics output to stabilize" |
| 85 | + sleep 60 |
| 86 | + break |
| 87 | + fi |
| 88 | + current_time=$(date +%s) |
| 89 | + if (( current_time - start_time >= $2 )); then |
| 90 | + echo "Timeout waiting for metrics information to be available" |
| 91 | + delete_minikube_cluster |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | + done |
| 95 | + # Get the CPU cores used by the pod |
| 96 | + pod_name=$(kubectl get pods -l app=$1 -o json | jq -r '.items[].metadata.name') |
| 97 | + cpu=$(kubectl top pod -l app=$1 | awk "/${pod_name}/{print \$2}") |
| 98 | + |
| 99 | + # Check if the CPU cores exceed 2 |
| 100 | + if [ $(echo "$cpu" | sed 's/m$//') -gt $ACCEPTABLE_CPU_CORE_USAGE ]; then |
| 101 | + echo "✘ Test failed: CPU cores $(echo "$cpu" | sed 's/m$//') for $pod_name exceeded $ACCEPTABLE_CPU_CORE_USAGE" >&2 |
| 102 | + exit 1 |
| 103 | + else |
| 104 | + echo "✓ SUCCESS" |
| 105 | + fi |
| 106 | +} |
| 107 | + |
| 108 | +function make_cluster_accessible() { |
| 109 | +kubectl apply -f $1 |
| 110 | +kubectl port-forward svc/ts-def 8080:8080 8081:8081 & |
| 111 | +sleep "$2" |
| 112 | +} |
| 113 | + |
| 114 | +function cleanup_port_forwarding() { |
| 115 | + echo "Clean up port forwarding" |
| 116 | + pkill kubectl |
| 117 | +} |
| 118 | + |
| 119 | +function make_prediction() { |
| 120 | +curl -X POST "localhost:8081/models?model_name=$1&url=$1.mar&initial_workers=1" |
| 121 | +PREDICTION=$(curl http://127.0.0.1:8080/predictions/$1 -T $2) |
| 122 | +EXPECTED="$3" |
| 123 | +if [ "${PREDICTION}" = "${EXPECTED}" ]; then |
| 124 | + echo "✓ SUCCESS" |
| 125 | + cleanup_port_forwarding |
| 126 | +else |
| 127 | + echo "✘ Test failed: Prediction: ${PREDICTION}, expected ${EXPECTED}." |
| 128 | + delete_minikube_cluster |
| 129 | + exit 1 |
| 130 | +fi |
| 131 | + |
| 132 | +} |
| 133 | + |
| 134 | +# Setup |
| 135 | +start_minikube_cluster |
| 136 | +build_docker_image |
| 137 | +get_model_archive "https://torchserve.pytorch.org/mar_files/mnist_v2.mar" "mnist" |
| 138 | +deploy_cluster "./kubernetes/tests/configs/deployment.yaml" "ts-def" |
| 139 | + |
| 140 | +echo "No model loaded CPU usage test" |
| 141 | +check_cpu_cores "ts-def" 180 |
| 142 | + |
| 143 | +echo "MNIST test inference" |
| 144 | +make_cluster_accessible "kubernetes/examples/mnist/service.yaml" 5 |
| 145 | +make_prediction "mnist" "examples/image_classifier/mnist/test_data/0.png" "0" |
| 146 | + |
| 147 | +# Clean up |
| 148 | +delete_minikube_cluster |
0 commit comments