-
Notifications
You must be signed in to change notification settings - Fork 1
/
run-k8s-job.sh
executable file
·59 lines (48 loc) · 1.49 KB
/
run-k8s-job.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash
# This is a helper script for running k8s jobs.
# If you have a long-running job you'll want to decompose this script and
# wait on the job itself. You can use add "set -x" to this script to do so.
set -e
JOB_FILE=$1
# Check if a yaml file is provided
if [ -z "$JOB_FILE" ]; then
echo "Usage: $0 <job.yaml>"
exit 1
fi
NAMESPACE="dev"
# Apply the job
echo "Deploying the Job in namespace '$NAMESPACE'..."
output=$(kubectl apply -f "$JOB_FILE" -n "$NAMESPACE")
if [ $? -ne 0 ]; then
echo "Error deploying the Job."
exit 1
fi
# Extract the job name from the output
JOB_NAME=$(echo "$output" | grep -oP '(?<=job.batch/)[^ ]+')
if [ -z "$JOB_NAME" ]; then
echo "Error: Could not extract Job name from the apply output."
exit 1
fi
echo "Job name: $JOB_NAME"
# Wait for the job to complete
echo "Waiting for the Job to complete in namespace '$NAMESPACE'..."
kubectl wait job/$JOB_NAME -n "$NAMESPACE" --for condition=complete --timeout=900s
if [ $? -ne 0 ]; then
echo "Error: Job did not complete successfully."
exit 1
fi
# Fetch logs from the job
echo "Fetching logs from namespace '$NAMESPACE'..."
kubectl logs job/$JOB_NAME -n "$NAMESPACE"
if [ $? -ne 0 ]; then
echo "Error: Failed to fetch logs from the Job."
exit 1
fi
# Delete the job
echo "Deleting the Job in namespace '$NAMESPACE'..."
kubectl delete job/$JOB_NAME -n "$NAMESPACE"
if [ $? -ne 0 ]; then
echo "Error: Failed to delete the Job."
exit 1
fi
echo "Job execution completed successfully in '$NAMESPACE' namespace."