-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhf.kube.curl.sh
More file actions
executable file
·160 lines (134 loc) · 3.65 KB
/
Copy pathhf.kube.curl.sh
File metadata and controls
executable file
·160 lines (134 loc) · 3.65 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/bash
# Curl from inside a Kubernetes pod in the current cluster
source "$(dirname "$(realpath "$0")")/hf.lib.sh"
hf_require_kubectl
set -euo pipefail
POD_NAME="hf-kcurl"
usage() {
hf_usage "[options] <url>"
cat <<'EOF'
Spin up a pod in the current Kubernetes cluster and execute a curl command from
inside it. The pod is reused across invocations and auto-terminates after 5
minutes of inactivity.
Options:
-X <method> HTTP method (GET, POST, PUT, DELETE, etc.) — default: GET
-H <header> HTTP header (repeatable)
-d <data> Inline request body data
-f <file> File to send as request body (uploaded to pod via kubectl cp)
-o <file> Write response to local file instead of stdout
-i Include response headers in output
-v Verbose curl output
-k Allow insecure TLS connections
--image <img> Override container image (default: curlimages/curl)
--help Show this help message
-- Pass remaining args directly to curl
EOF
exit 0
}
# Defaults
METHOD=""
HEADERS=()
DATA=""
FILE=""
OUTPUT=""
INCLUDE_HEADERS=false
VERBOSE=false
INSECURE=false
IMAGE="curlimages/curl"
EXTRA_CURL_ARGS=()
URL=""
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-X)
METHOD="$2"; shift 2 ;;
-H)
HEADERS+=("$2"); shift 2 ;;
-d)
DATA="$2"; shift 2 ;;
-f)
FILE="$2"; shift 2 ;;
-o)
OUTPUT="$2"; shift 2 ;;
-i)
INCLUDE_HEADERS=true; shift ;;
-v)
VERBOSE=true; shift ;;
-k)
INSECURE=true; shift ;;
--image)
IMAGE="$2"; shift 2 ;;
--help)
usage ;;
--)
shift; EXTRA_CURL_ARGS+=("$@"); break ;;
-*)
hf_die "Unknown option: $1" ;;
*)
URL="$1"; shift ;;
esac
done
if [[ -z "$URL" ]]; then
hf_die "Missing required <url> argument. Run '$(basename "$0") --help' for usage."
fi
# Ensure the shared curl pod is running, creating or replacing as needed.
ensure_pod() {
local phase
phase="$(hf_kubectl get pod "$POD_NAME" -o jsonpath='{.status.phase}' 2>/dev/null || true)"
if [[ "$phase" == "Running" ]]; then
return
fi
if [[ -n "$phase" ]]; then
# Pod exists but is not Running (Succeeded, Failed, Pending, Unknown)
hf_kubectl delete pod "$POD_NAME" --grace-period=0 --force --wait=false >/dev/null 2>&1 || true
fi
hf_kubectl run "$POD_NAME" \
--image="$IMAGE" \
--restart=Never \
--quiet \
--command -- sleep 300 >/dev/null 2>&1
hf_kubectl wait --for=condition=Ready "pod/$POD_NAME" --timeout=30s >/dev/null 2>&1
: # pod created, expires in 5m
}
ensure_pod
# Build curl arguments
CURL_ARGS=(-sS)
if [[ -n "$METHOD" ]]; then
CURL_ARGS+=(-X "$METHOD")
fi
for h in "${HEADERS[@]+"${HEADERS[@]}"}"; do
CURL_ARGS+=(-H "$h")
done
if $INCLUDE_HEADERS; then
CURL_ARGS+=(-i)
fi
if $VERBOSE; then
CURL_ARGS+=(-v)
fi
if $INSECURE; then
CURL_ARGS+=(-k)
fi
# File upload path: copy file into pod, then exec curl
if [[ -n "$FILE" ]]; then
if [[ ! -f "$FILE" ]]; then
hf_die "File not found: $FILE"
fi
BASENAME="$(basename "$FILE")"
hf_kubectl cp "$FILE" "$POD_NAME:/tmp/$BASENAME"
CURL_ARGS+=(--data-binary "@/tmp/$BASENAME")
CURL_ARGS+=("${EXTRA_CURL_ARGS[@]+"${EXTRA_CURL_ARGS[@]}"}")
CURL_ARGS+=("$URL")
elif [[ -n "$DATA" ]]; then
CURL_ARGS+=(-d "$DATA")
CURL_ARGS+=("${EXTRA_CURL_ARGS[@]+"${EXTRA_CURL_ARGS[@]}"}")
CURL_ARGS+=("$URL")
else
CURL_ARGS+=("${EXTRA_CURL_ARGS[@]+"${EXTRA_CURL_ARGS[@]}"}")
CURL_ARGS+=("$URL")
fi
# Execute curl on the shared pod
if [[ -n "$OUTPUT" ]]; then
hf_kubectl exec "$POD_NAME" -- curl "${CURL_ARGS[@]}" > "$OUTPUT"
else
hf_kubectl exec "$POD_NAME" -- curl "${CURL_ARGS[@]}"
fi