/ docker / bin / kubectl-wait-job
kubectl-wait-job
  1  #!/bin/bash
  2  #
  3  # This is copied from: https://github.com/brianpursley/kubectl-wait-job
  4  #
  5  # This code is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License.
  6  # To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/4.0/
  7  #
  8  # Attribution: This code was inspired by an answer on Stack Overflow licensed under CC BY-SA 4.0.
  9  # Original answer: https://stackoverflow.com/a/60286538/5074828 by Sebastian N (https://stackoverflow.com/users/3745474/sebastian-n)
 10  #
 11  
 12  # Check if --help is specified in the arguments and display help text
 13  for arg in "$@"; do
 14      if [[ "$arg" == "--help" ]]; then
 15          echo "Usage: kubectl wait-job [ARGS] [OPTIONS]"
 16          echo ""
 17          echo "This plugin waits for a Kubernetes job to either complete or fail."
 18          echo ""
 19          echo "Arguments:"
 20          echo "  [kubectl args]  Any args will be passed to kubectl wait."
 21          echo ""
 22          echo "Options:"
 23          echo "  [kubectl options]  Any options will be passed to kubectl wait."
 24          echo ""
 25          echo "Example:"
 26          echo "  kubectl wait-job job-name"
 27          echo ""
 28          exit 0
 29      fi
 30  done
 31  
 32  # Make sure there is no --for flag
 33  for arg in "$@"; do
 34      if [[ "$arg" == "--for" || "$arg" == --for=* ]]; then
 35          echo "Error: The '--for' flag cannot be used with this plugin."
 36          exit 2
 37      fi
 38  done
 39  
 40  # Cleanup
 41  cleanup() {
 42      if [[ -n $COMPLETE_STDERR ]]; then
 43          rm -f "$COMPLETE_STDERR" 2> /dev/null
 44      fi
 45      if [[ -n $FAILED_STDERR ]]; then
 46          rm -f "$FAILED_STDERR" 2> /dev/null
 47      fi
 48      if [[ -n $COMPLETE_PID ]]; then
 49          kill "$COMPLETE_PID" 2> /dev/null
 50      fi
 51      if [[ -n $FAILED_PID ]]; then
 52          kill "$FAILED_PID" 2> /dev/null
 53      fi
 54  }
 55  trap cleanup EXIT
 56  
 57  # Create temporary files to store stderr output
 58  COMPLETE_STDERR=$(mktemp -t kubectl-wait-job-stderr.XXXXXXXXXX) || { echo "error: failed to create temp file"; exit 3; }
 59  FAILED_STDERR=$(mktemp -t kubectl-wait-job-stderr.XXXXXXXXXX) || { echo "error: failed to create temp file"; exit 3; }
 60  
 61  # Wait for complete and failed conditions in parallel
 62  kubectl wait job "$@" --for=condition=complete > /dev/null 2> "$COMPLETE_STDERR" &
 63  COMPLETE_PID=$!
 64  kubectl wait job "$@" --for=condition=failed > /dev/null 2> "$FAILED_STDERR" &
 65  FAILED_PID=$!
 66  
 67  # Wait for one of the processes to exit (using loop instead of wait -n for compatibility)
 68  while true; do
 69      # Check if the process waiting for the job to complete has exited
 70      unset COMPLETE_RESULT
 71      if ! kill -0 "$COMPLETE_PID" 2>/dev/null; then
 72          wait $COMPLETE_PID;
 73          COMPLETE_RESULT=$?
 74          if [[ $COMPLETE_RESULT -eq 0 ]]; then
 75              echo "Job completed successfully"
 76              exit 0
 77          fi
 78      fi
 79  
 80      # Check if the process waiting for the job to fail has exited
 81      unset FAILED_RESULT
 82      if ! kill -0 "$FAILED_PID" 2>/dev/null; then
 83          wait $FAILED_PID
 84          FAILED_RESULT=$?
 85          if [[ $FAILED_RESULT -eq 0 ]]; then
 86              echo "Job failed"
 87              exit 1
 88          fi
 89      fi
 90  
 91      # If either process failed, print the stderr output and exit
 92      if [[ -n $COMPLETE_RESULT || -n $FAILED_RESULT ]]; then
 93          cat "$COMPLETE_STDERR" 2> /dev/null
 94          cat "$FAILED_STDERR" 2> /dev/null
 95          echo "error: kubectl wait failed"
 96          exit 3
 97      fi
 98  
 99      # Sleep for a short time before checking again
100      sleep 0.1
101  done