|
42 | 42 | kubectl apply -f $POD_CONFIG |
43 | 43 |
|
44 | 44 | # Delete POD on exit and describe it before deletion if exit was unsuccessful |
45 | | -trap '[[ $? != 0 ]] && kubectl describe "pod/${POD_NAME}"; kubectl delete pods "${POD_NAME}"' EXIT |
| 45 | +trap 'exit_code=$? |
| 46 | +if [[ ${exit_code} != 0 ]]; then |
| 47 | + echo "Presubmit failed for ${POD_NAME}. Describing pod..." |
| 48 | + kubectl describe "pod/${POD_NAME}" || echo "Failed to describe pod." |
| 49 | +
|
| 50 | + PROJECT_ID=$(gcloud config get-value project 2>/dev/null || echo "unknown-project") |
| 51 | + BUCKET="dataproc-init-actions-test-${PROJECT_ID}" |
| 52 | + LOG_GCS_PATH="gs://${BUCKET}/${BUILD_ID}/logs/${POD_NAME}.log" |
| 53 | +
|
| 54 | + echo "Attempting to upload logs to ${LOG_GCS_PATH}" |
| 55 | + if kubectl logs "${POD_NAME}" | gsutil cp - "${LOG_GCS_PATH}"; then |
| 56 | + echo "Logs for failed pod ${POD_NAME} uploaded to: ${LOG_GCS_PATH}" |
| 57 | + else |
| 58 | + echo "Log upload to ${LOG_GCS_PATH} failed." |
| 59 | + fi |
| 60 | +fi |
| 61 | +echo "Deleting pod ${POD_NAME}..." |
| 62 | +kubectl delete pods "${POD_NAME}" --ignore-not-found=true |
| 63 | +exit ${exit_code}' EXIT |
46 | 64 |
|
47 | 65 | kubectl wait --for=condition=Ready "pod/${POD_NAME}" --timeout=15m |
48 | 66 |
|
| 67 | +# To mitigate problems with early test failure, retry kubectl logs |
| 68 | +sleep 10s |
49 | 69 | while ! kubectl describe "pod/${POD_NAME}" | grep -q Terminated; do |
50 | | - kubectl logs -f "${POD_NAME}" --since-time="${LOGS_SINCE_TIME}" --timestamps=true |
| 70 | + # Try to stream logs, but primary log capture is now in the trap |
| 71 | + kubectl logs -f "${POD_NAME}" --since-time="${LOGS_SINCE_TIME}" --timestamps=true || true |
51 | 72 | LOGS_SINCE_TIME=$(date --iso-8601=seconds) |
| 73 | + sleep 2 # Short sleep to avoid busy waiting if logs -f exits |
52 | 74 | done |
53 | 75 |
|
54 | | -EXIT_CODE=$(kubectl get pod "${POD_NAME}" \ |
55 | | - -o go-template="{{range .status.containerStatuses}}{{.state.terminated.exitCode}}{{end}}") |
| 76 | +# Final check on the pod exit code |
| 77 | +EXIT_CODE=$(kubectl get pod "${POD_NAME}" -o go-template="{{range .status.containerStatuses}}{{.state.terminated.exitCode}}{{end}}" || echo "1") |
56 | 78 |
|
57 | 79 | if [[ ${EXIT_CODE} != 0 ]]; then |
58 | | - echo "Presubmit failed!" |
| 80 | + echo "Presubmit final state for ${POD_NAME} indicates failure (Exit Code: ${EXIT_CODE})." |
| 81 | + # The trap will handle the log upload and cleanup |
59 | 82 | exit 1 |
60 | 83 | fi |
| 84 | + |
| 85 | +echo "Presubmit for ${POD_NAME} successful." |
| 86 | +# Explicitly exit 0 to clear the trap's exit code |
| 87 | +exit 0 |
0 commit comments