forked from openshift/sandboxed-containers-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpodvm-builder.sh
executable file
·263 lines (226 loc) · 8.23 KB
/
podvm-builder.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/bin/bash
#
# Function to install Azure deps
function install_azure_deps() {
echo "Installing Azure deps"
# Install the required packages
/scripts/azure-podvm-image-handler.sh -- install_cli
/scripts/azure-podvm-image-handler.sh -- install_binaries
}
# Function to install AWS deps
function install_aws_deps() {
echo "Installing AWS deps"
# Install the required packages
/scripts/aws-podvm-image-handler.sh -- install_cli
/scripts/aws-podvm-image-handler.sh -- install_binaries
}
# Function to check if peer-pods-cm configmap exists
function check_peer_pods_cm_exists() {
if kubectl get configmap peer-pods-cm -n openshift-sandboxed-containers-operator >/dev/null 2>&1; then
return 0
else
return 1
fi
}
# Function to create podvm image
function create_podvm_image() {
case "${CLOUD_PROVIDER}" in
azure)
echo "Creating Azure image"
/scripts/azure-podvm-image-handler.sh -c
if [ "${UPDATE_PEERPODS_CM}" == "yes" ]; then
# Check if peer-pods-cm configmap exists
if ! check_peer_pods_cm_exists; then
echo "peer-pods-cm configmap does not exist. Skipping the update of peer-pods-cm"
exit 0
fi
# Get the IMAGE_ID from the LATEST_IMAGE_ID annotation key in peer-pods-cm configmap
IMAGE_ID=$(kubectl get configmap peer-pods-cm -n openshift-sandboxed-containers-operator -o jsonpath='{.metadata.annotations.LATEST_IMAGE_ID}')
# if IMAGE_ID is not set, then exit
if [ -z "${IMAGE_ID}" ]; then
echo "IMAGE_ID is not set in peer-pods-cm. Skipping the update of peer-pods-cm"
exit 1
fi
# Update peer-pods-cm configmap with the IMAGE_ID value
echo "Updating peer-pods-cm configmap with IMAGE_ID=${IMAGE_ID}"
kubectl patch configmap peer-pods-cm -n openshift-sandboxed-containers-operator --type merge -p "{\"data\":{\"AZURE_IMAGE_ID\":\"${IMAGE_ID}\"}}"
fi
;;
aws)
echo "Creating AWS AMI"
/scripts/aws-podvm-image-handler.sh -c
if [ "${UPDATE_PEERPODS_CM}" == "yes" ]; then
# Check if peer-pods-cm configmap exists
if ! check_peer_pods_cm_exists; then
echo "peer-pods-cm configmap does not exist. Skipping the update of peer-pods-cm"
exit 0
fi
# Get the AMI_ID from the LATEST_AMI_ID annotation key in peer-pods-cm configmap
AMI_ID=$(kubectl get configmap peer-pods-cm -n openshift-sandboxed-containers-operator -o jsonpath='{.metadata.annotations.LATEST_AMI_ID}')
# if AMI_ID is not set, then exit
if [ -z "${AMI_ID}" ]; then
echo "AMI_ID is not set in peer-pods-cm. Skipping the update of peer-pods-cm"
exit 1
fi
# Update peer-pods-cm configmap with the AMI_ID value
echo "Updating peer-pods-cm configmap with AMI_ID=${AMI_ID}"
kubectl patch configmap peer-pods-cm -n openshift-sandboxed-containers-operator --type merge -p "{\"data\":{\"PODVM_AMI_ID\":\"${AMI_ID}\"}}"
fi
;;
*)
echo "CLOUD_PROVIDER is not set to azure or aws"
exit 1
;;
esac
}
# Function to delete podvm image
# IMAGE_ID or AMI_ID is the input and expected to be set
# These are checked in individual cloud provider scripts and if not set, the script will exit
# Accepts two optional arguments
# -f : force delete the image
# -g : delete the image gallery
function delete_podvm_image() {
local args=("$@")
local force=false
local delete_gallery=false
for ((i = 0; i < ${#args[@]}; i++)); do
case "${args[$i]}" in
-f) force=true ;;
-g) delete_gallery=true ;;
esac
done
# Check for the existence of peer-pods-cm configmap. If not present, then exit
if ! check_peer_pods_cm_exists; then
echo "peer-pods-cm configmap does not exist. Skipping image deletion"
exit 0
fi
case "${CLOUD_PROVIDER}" in
azure)
# If IMAGE_ID is not set, then exit
if [ -z "${IMAGE_ID}" ]; then
echo "IMAGE_ID is not set. Skipping the deletion of Azure image"
exit 1
fi
AZURE_IMAGE_ID=$(kubectl get configmap peer-pods-cm -n openshift-sandboxed-containers-operator -o jsonpath='{.data.AZURE_IMAGE_ID}')
# If AZURE_IMAGE_ID is not set, then exit
if [ -z "${AZURE_IMAGE_ID}" ]; then
echo "AZURE_IMAGE_ID is not set in peer-pods-cm. Skipping the deletion of Azure image"
exit 1
fi
# check if the AZURE_IMAGE_ID value in peer-pods-cm is same as the input IMAGE_ID
# If yes, then don't delete the image unless force option is provided
if [ "${AZURE_IMAGE_ID}" == "${IMAGE_ID}" ]; then
if ! ${force}; then
echo "AZURE_IMAGE_ID in peer-pods-cm is same as the input image to be deleted. Skipping the deletion of Azure image"
exit 0
fi
fi
echo "Deleting Azure image $IMAGE_ID"
/scripts/azure-podvm-image-handler.sh -C
# Update the peer-pods-cm configmap and remove the AZURE_IMAGE_ID value
if [ "${UPDATE_PEERPODS_CM}" == "yes" ]; then
kubectl patch configmap peer-pods-cm -n openshift-sandboxed-containers-operator --type merge -p "{\"data\":{\"AZURE_IMAGE_ID\":\"\"}}"
fi
# If delete_gallery is set, then delete the image gallery
if ${delete_gallery}; then
echo "Deleting Azure image gallery (by force) since -g option is set"
delete_podvm_image_gallery -f
fi
;;
aws)
# If AMI_ID is not set, then exit
if [ -z "${AMI_ID}" ]; then
echo "AMI_ID is not set. Skipping the deletion of AWS AMI"
exit 1
fi
PODVM_AMI_ID=$(kubectl get configmap peer-pods-cm -n openshift-sandboxed-containers-operator -o jsonpath='{.data.PODVM_AMI_ID}')
# If PODVM_AMI_ID is not set, then exit
if [ -z "${PODVM_AMI_ID}" ]; then
echo "PODVM_AMI_ID is not set in peer-pods-cm. Skipping the deletion of AWS AMI"
exit 1
fi
# check if the PODVM_AMI_ID value in peer-pods-cm is same as the input AMI_ID
# If yes, then don't delete the image unless force option is provided
if [ "${PODVM_AMI_ID}" == "${AMI_ID}" ]; then
if [ "$1" != "-f" ]; then
echo "PODVM_AMI_ID in peer-pods-cm is same as the input image to be deleted. Skipping the deletion of AWS AMI"
exit 0
fi
fi
echo "Deleting AWS AMI"
/scripts/aws-podvm-image-handler.sh -C
# Update the peer-pods-cm configmap and remove the PODVM_AMI_ID value
if [ "${UPDATE_PEERPODS_CM}" == "yes" ]; then
kubectl patch configmap peer-pods-cm -n openshift-sandboxed-containers-operator --type merge -p "{\"data\":{\"PODVM_AMI_ID\":\"\"}}"
fi
;;
*)
echo "CLOUD_PROVIDER is not set to azure or aws"
exit 1
;;
esac
}
# Delete the podvm image gallery in Azure
# It accepts an optional argument
# -f : force delete the image gallery
function delete_podvm_image_gallery() {
echo "Deleting Azure image gallery"
# Check if CLOUD_PROVIDER is set to azure, otherwise return
if [ "${CLOUD_PROVIDER}" != "azure" ]; then
echo "CLOUD_PROVIDER is not Azure"
return
fi
# Check if peer-pods-cm configmap exists
if ! check_peer_pods_cm_exists; then
echo "peer-pods-cm configmap does not exist. Skipping image gallery deletion"
exit 0
fi
# Get the IMAGE_GALLERY_NAME from the IMAGE_GALLERY_NAME annotation key in peer-pods-cm configmap
IMAGE_GALLERY_NAME=$(kubectl get configmap peer-pods-cm -n openshift-sandboxed-containers-operator -o jsonpath='{.metadata.annotations.IMAGE_GALLERY_NAME}')
# If IMAGE_GALLERY_NAME is not set, then exit
if [ -z "${IMAGE_GALLERY_NAME}" ]; then
echo "IMAGE_GALLERY_NAME is not set in peer-pods-cm. Skipping image gallery deletion"
exit 0
fi
if [ "$1" == "-f" ]; then
/scripts/azure-podvm-image-handler.sh -G force
else
/scripts/azure-podvm-image-handler.sh -G
fi
}
function display_usage() {
echo "Usage: $0 {create|delete [-f] [-g]|delete-gallery [-f]}"
}
# Check if CLOUD_PROVIDER is set to azure or aws
# Install the required dependencies
case "${CLOUD_PROVIDER}" in
azure)
install_azure_deps
;;
aws)
install_aws_deps
;;
*)
echo "CLOUD_PROVIDER is not set to azure or aws"
display_usage
exit 1
;;
esac
# Call the function to create or delete podvm image based on argument
case "$1" in
create)
create_podvm_image
;;
delete)
# Pass the arguments to delete_podvm_image function except the first argument
shift
delete_podvm_image "$@"
;;
delete-gallery)
delete_podvm_image_gallery "$2"
;;
*)
display_usage
exit 1
;;
esac