Repository navigation
Expand file tree
/
Copy pathcreate-pod-in-k8s
More file actions
executable file
·216 lines (200 loc) · 6.19 KB
/
Copy pathcreate-pod-in-k8s
File metadata and controls
executable file
·216 lines (200 loc) · 6.19 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/bin/bash
set -e
# Default values
initiative="${CREATE_POD_IN_K8S_INITIATIVE:-standard}"
volume_size="${CREATE_POD_IN_K8S_VOLUME_SIZE:-}"
volume_name="${CREATE_POD_IN_K8S_VOLUME_NAME:-data}"
requests_cpu="${CREATE_POD_IN_K8S_REQUESTS_CPU:-}"
requests_memory="${CREATE_POD_IN_K8S_REQUESTS_MEMORY:-}"
namespace="${CREATE_POD_IN_K8S_NAMESPACE:-}"
image="${CREATE_POD_IN_K8S_IMAGE:-ubuntu}"
pod_name="${CREATE_POD_IN_K8S_POD_NAME:-troubleshooter}"
pod_running_timeout="${CREATE_POD_IN_K8S_POD_RUNNING_TIMEOUT:-5m}"
dry_run=false
# Function to print usage
print_usage() {
script_name=$(basename "$0")
echo "Usage: $script_name [options]"
echo
echo "Options:"
echo " -i, --initiative <value> Set the initiative (default: $initiative)"
echo " -v, --volume-size <value> Set the volume size"
echo " -c, --requests-cpu <value> Set the CPU requests"
echo " -m, --requests-memory <value> Set the memory requests"
echo " -n, --namespace <value> Set the namespace"
echo " --image <value> Set the image (default: $image)"
echo " --pod-name <value> Set the pod name (default: $pod_name)"
echo " -t, --pod-running-timeout <value> Set the length of time (like 5s, 2m, or 3h) to wait"
echo " until at least one pod is running (default: $pod_running_timeout)"
echo " --dry-run Print the command without executing it"
echo " -h, --help Show this help message and exit"
echo ""
echo "Many options can be supplied by env vars (example: CREATE_POD_IN_K8S_POD_NAME)."
echo "See script source for which ones can be supplied this way."
}
# Parse options
# TODO This is a lot of boilerplate. Consider converting this to a Kotlin script (kts) and using Clikt
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
print_usage
exit 0
;;
-i=*|--initiative=*)
initiative="${1#*=}"
shift
;;
-i|--initiative)
initiative="$2"
shift 2
;;
-i*)
initiative="${1#-i}"
shift
;;
-v=*|--volume-size=*)
volume_size="${1#*=}"
shift
;;
-v|--volume-size)
volume_size="$2"
shift 2
;;
-v*)
volume_size="${1#-v}"
shift
;;
-c=*|--requests-cpu=*)
requests_cpu="${1#*=}"
shift
;;
-c|--requests-cpu)
requests_cpu="$2"
shift 2
;;
-c*)
requests_cpu="${1#-c}"
shift
;;
-m=*|--requests-memory=*)
requests_memory="${1#*=}"
shift
;;
-m|--requests-memory)
requests_memory="$2"
shift 2
;;
-m*)
requests_memory="${1#-m}"
shift
;;
-n=*|--namespace=*)
namespace="${1#*=}"
shift
;;
-n|--namespace)
namespace="$2"
shift 2
;;
-n*)
namespace="${1#-n}"
shift
;;
--image=*)
image="${1#*=}"
shift
;;
--image)
image="$2"
shift 2
;;
--pod-name=*)
pod_name="${1#*=}"
shift
;;
--pod-name)
pod_name="$2"
shift 2
;;
-t=*|--pod-running-timeout=*)
pod_running_timeout="${1#*=}"
shift
;;
-t|--pod-running-timeout)
pod_running_timeout="$2"
shift 2
;;
-t*)
pod_running_timeout="${1#-t}"
shift
;;
--dry-run)
dry_run=true
shift
;;
*)
break
;;
esac
done
# Capture remaining arguments
args=("$@")
# Default to /bin/bash if no arguments are supplied
if [[ ${#args[@]} -eq 0 ]]; then
args=("/bin/bash")
fi
# Initialize an empty JSON object
json='{}'
# Add key-value pairs to the JSON object
json=$(jq --arg pod_name "$pod_name" '.spec.containers[0].name = $pod_name' <<< "$json")
json=$(jq --arg image "$image" '.spec.containers[0].image = $image' <<< "$json")
json=$(jq '.spec.containers[0].stdin = true' <<< "$json")
json=$(jq '.spec.containers[0].tty = true' <<< "$json")
json=$(jq '.spec.tolerations[0].key = "dedicated"' <<< "$json")
json=$(jq '.spec.tolerations[0].operator = "Equal"' <<< "$json")
json=$(jq --arg initiative "$initiative" '.spec.tolerations[0].value = $initiative' <<< "$json")
json=$(jq '.spec.tolerations[0].effect = "NoSchedule"' <<< "$json")
json=$(jq --arg initiative "$initiative" '.spec.nodeSelector.initiative = $initiative' <<< "$json")
# Add volumes section if --volume-size is supplied
if [[ -n "$volume_size" ]]; then
json=$(jq --arg volume_name "$volume_name" '.spec.volumes[0].name = $volume_name' <<< "$json")
json=$(jq '.spec.volumes[0].ephemeral.volumeClaimTemplate.spec.accessModes[0] = "ReadWriteOnce"' <<< "$json")
json=$(jq '.spec.volumes[0].ephemeral.volumeClaimTemplate.spec.storageClassName = "managed"' <<< "$json")
json=$(jq --arg size "$volume_size" '.spec.volumes[0].ephemeral.volumeClaimTemplate.spec.resources.requests.storage = $size' <<< "$json")
json=$(jq --arg volume_name "$volume_name" '.spec.containers[0].volumeMounts[0].name = $volume_name' <<< "$json")
json=$(jq '.spec.containers[0].volumeMounts[0].mountPath = "/data"' <<< "$json")
fi
# Add CPU and memory requests if supplied
if [[ -n "$requests_cpu" ]]; then
json=$(jq --arg cpu "$requests_cpu" '.spec.containers[0].resources.requests.cpu = $cpu' <<< "$json")
fi
if [[ -n "$requests_memory" ]]; then
json=$(jq --arg memory "$requests_memory" '.spec.containers[0].resources.requests.memory = $memory' <<< "$json")
fi
# Construct the kubectl command as an array
kubectl_command=(
kubectl run "$pod_name"
--image="$image"
--rm
--stdin --tty --restart=Never
--overrides="$json"
)
# Add namespace if supplied
if [[ -n "$namespace" ]]; then
kubectl_command+=(-n "$namespace")
fi
# Add pod running timeout to the kubectl command if supplied
if [[ -n "$pod_running_timeout" ]]; then
kubectl_command+=(--pod-running-timeout="$pod_running_timeout")
fi
# Add the remaining arguments
kubectl_command+=(-- "${args[@]}")
# Print command (and execute unless --dry-run was supplied)
if [[ "$dry_run" == true ]]; then
printf "Would execute command: %s\n" "${kubectl_command[*]}"
exit 0
else
printf "Executing command: %s\n" "${kubectl_command[*]}"
# Execute the kubectl command
"${kubectl_command[@]}"
fi