-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path01-deploy-resources.sh
More file actions
executable file
·308 lines (272 loc) · 11.6 KB
/
Copy path01-deploy-resources.sh
File metadata and controls
executable file
·308 lines (272 loc) · 11.6 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/bin/bash
# Variables
source ./00-variables.sh
# Change the current directory to the script's directory
cd "$CURRENT_DIR" || exit
# Ask how the Azure file share holding the activities should be provisioned.
# Both answers are persisted below, so 05-deploy-app.sh deploys the app for the same combination
# without asking again. Export PROVISIONING_MODE and FILE_SHARE_PROTOCOL beforehand to skip the
# menus altogether, for example in an unattended run:
#
# PROVISIONING_MODE=dynamic FILE_SHARE_PROTOCOL=nfs ./01-deploy-resources.sh
if [[ -z "$PROVISIONING_MODE" ]]; then
# Print the menu
echo "===================================="
echo "Provisioning Mode (1-3): "
echo "===================================="
options=(
"Static: use a file share created by this script"
"Dynamic: let the CSI driver create the file share"
"Quit"
)
# Select an option
COLUMNS=0
select option in "${options[@]}"; do
case $option in
"Static: use a file share created by this script")
PROVISIONING_MODE='static'
break
;;
"Dynamic: let the CSI driver create the file share")
PROVISIONING_MODE='dynamic'
break
;;
"Quit")
exit
;;
*) echo "invalid option $REPLY" ;;
esac
done
fi
PROVISIONING_MODE="${PROVISIONING_MODE,,}"
if [[ "$PROVISIONING_MODE" != 'static' && "$PROVISIONING_MODE" != 'dynamic' ]]; then
echo "Invalid provisioning mode [$PROVISIONING_MODE]: expected [static] or [dynamic]"
exit 1
fi
# Ask which protocol the Azure file share should use.
if [[ -z "$FILE_SHARE_PROTOCOL" ]]; then
# Print the menu
echo "===================================="
echo "File Share Protocol (1-3): "
echo "===================================="
options=(
"SMB: mounted with the storage account key"
"NFS: mounted from a virtual network, no key"
"Quit"
)
# Select an option
COLUMNS=0
select option in "${options[@]}"; do
case $option in
"SMB: mounted with the storage account key")
FILE_SHARE_PROTOCOL='smb'
break
;;
"NFS: mounted from a virtual network, no key")
FILE_SHARE_PROTOCOL='nfs'
break
;;
"Quit")
exit
;;
*) echo "invalid option $REPLY" ;;
esac
done
fi
FILE_SHARE_PROTOCOL="${FILE_SHARE_PROTOCOL,,}"
if [[ "$FILE_SHARE_PROTOCOL" != 'smb' && "$FILE_SHARE_PROTOCOL" != 'nfs' ]]; then
echo "Invalid file share protocol [$FILE_SHARE_PROTOCOL]: expected [smb] or [nfs]"
exit 1
fi
# Persist both answers so 05-deploy-app.sh deploys the app for the same combination.
# Each value is written with ${VAR:-value} syntax, so an exported environment variable still takes
# precedence when 00-variables.sh sources this file.
cat <<EOF >"$DEPLOY_OPTIONS_FILE"
# Generated by 01-deploy-resources.sh - do not edit.
# An exported environment variable takes precedence over the value stored here.
PROVISIONING_MODE="\${PROVISIONING_MODE:-$PROVISIONING_MODE}"
FILE_SHARE_PROTOCOL="\${FILE_SHARE_PROTOCOL:-$FILE_SHARE_PROTOCOL}"
EOF
if [[ $? == 0 ]]; then
echo "Deployment options saved to [$DEPLOY_OPTIONS_FILE]"
else
echo "Failed to save the deployment options to [$DEPLOY_OPTIONS_FILE]"
exit 1
fi
echo "Deploying the resources for [$PROVISIONING_MODE] provisioning over [${FILE_SHARE_PROTOCOL^^}]..."
# Create a resource group
echo "Checking if resource group [$RESOURCE_GROUP_NAME] exists in the subscription [$SUBSCRIPTION_NAME]..."
az group show --name $RESOURCE_GROUP_NAME &>/dev/null
if [[ $? != 0 ]]; then
echo "No resource group [$RESOURCE_GROUP_NAME] exists in the subscription [$SUBSCRIPTION_NAME]"
echo "Creating resource group [$RESOURCE_GROUP_NAME] in the subscription [$SUBSCRIPTION_NAME]..."
az group create \
--name $RESOURCE_GROUP_NAME \
--location "$LOCATION" \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "Resource group [$RESOURCE_GROUP_NAME] successfully created in the subscription [$SUBSCRIPTION_NAME]"
else
echo "Failed to create resource group [$RESOURCE_GROUP_NAME] in the subscription [$SUBSCRIPTION_NAME]"
exit 1
fi
else
echo "Resource group [$RESOURCE_GROUP_NAME] already exists in the subscription [$SUBSCRIPTION_NAME]"
fi
# Create the Azure Container Registry
echo "Checking if [$ACR_NAME] Azure Container Registry already exists in the [$RESOURCE_GROUP_NAME] resource group..."
az acr show \
--name "$ACR_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$ACR_NAME] Azure Container Registry exists in the [$RESOURCE_GROUP_NAME] resource group"
echo "Creating Azure Container Registry [$ACR_NAME]..."
az acr create \
--name "$ACR_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--location "$LOCATION" \
--sku "$ACR_SKU" \
--admin-enabled "true" \
--only-show-errors 1>/dev/null
if [ $? -eq 0 ]; then
echo "Azure Container Registry [$ACR_NAME] created successfully."
else
echo "Failed to create Azure Container Registry [$ACR_NAME]."
exit 1
fi
else
echo "[$ACR_NAME] Azure Container Registry already exists in the [$RESOURCE_GROUP_NAME] resource group"
fi
# In dynamic mode there is nothing else to create: the Azure Files CSI driver creates the storage
# account and the file share in the node resource group when the PersistentVolumeClaim is bound.
if [[ "$PROVISIONING_MODE" == 'dynamic' ]]; then
echo "Dynamic provisioning selected: the Azure Files CSI driver creates the storage account and the [$FILE_SHARE_NAME] file share when the [$PERSISTENT_VOLUME_CLAIM_NAME] persistent volume claim is bound by 05-deploy-app.sh."
exit 0
fi
# Static provisioning: create the storage account and the file share the PersistentVolume points at.
if [[ "$FILE_SHARE_PROTOCOL" == 'smb' ]]; then
STORAGE_ACCOUNT_NAME="$SMB_STORAGE_ACCOUNT_NAME"
STORAGE_ACCOUNT_KIND="$SMB_STORAGE_ACCOUNT_KIND"
STORAGE_ACCOUNT_SKU="$SMB_STORAGE_ACCOUNT_SKU"
ENABLED_PROTOCOLS='SMB'
# An SMB share is mounted with the account key over an encrypted connection, so secure transfer
# stays on and the account keeps its default network access.
STORAGE_ACCOUNT_EXTRA_ARGS=(--https-only true)
FILE_SHARE_EXTRA_ARGS=()
else
STORAGE_ACCOUNT_NAME="$NFS_STORAGE_ACCOUNT_NAME"
STORAGE_ACCOUNT_KIND="$NFS_STORAGE_ACCOUNT_KIND"
STORAGE_ACCOUNT_SKU="$NFS_STORAGE_ACCOUNT_SKU"
ENABLED_PROTOCOLS='NFS'
# An NFSv4.1 mount is not encrypted in transit unless the client uses the AZNFS TLS helper, so
# secure transfer has to be off for the CSI driver to mount the share
# (https://learn.microsoft.com/en-us/azure/storage/files/files-nfs-protocol).
STORAGE_ACCOUNT_EXTRA_ARGS=(--https-only false)
# NoRootSquash lets the init container of the deployment chown the mount point as root, which is
# how the NFS variant makes the share writable by the non-root user of the app.
FILE_SHARE_EXTRA_ARGS=(--root-squash NoRootSquash)
fi
echo "Checking if storage account [$STORAGE_ACCOUNT_NAME] exists in the resource group [$RESOURCE_GROUP_NAME]..."
az storage account show \
--name $STORAGE_ACCOUNT_NAME \
--resource-group $RESOURCE_GROUP_NAME &>/dev/null
if [[ $? != 0 ]]; then
echo "Creating [$STORAGE_ACCOUNT_KIND] storage account [$STORAGE_ACCOUNT_NAME] with the [$STORAGE_ACCOUNT_SKU] sku..."
az storage account create \
--name $STORAGE_ACCOUNT_NAME \
--location "$LOCATION" \
--resource-group $RESOURCE_GROUP_NAME \
--kind $STORAGE_ACCOUNT_KIND \
--sku $STORAGE_ACCOUNT_SKU \
"${STORAGE_ACCOUNT_EXTRA_ARGS[@]}" \
--only-show-errors 1>/dev/null
if [ $? -eq 0 ]; then
echo "Storage account [$STORAGE_ACCOUNT_NAME] created successfully."
else
echo "Failed to create storage account [$STORAGE_ACCOUNT_NAME]."
exit 1
fi
else
echo "Storage account [$STORAGE_ACCOUNT_NAME] already exists in the [$RESOURCE_GROUP_NAME] resource group"
fi
# An NFS share has no user-based authentication: access is granted by network rules only, so the
# account must be restricted to the subnets of the AKS nodes that mount the share. The subnets are
# read from the cluster itself, so this works with either of the two cluster provisioning scripts.
# The Microsoft.Storage service endpoint on those subnets is enabled by the cluster script.
if [[ "$FILE_SHARE_PROTOCOL" == 'nfs' ]]; then
echo "Retrieving the node subnets of the [$AKS_CLUSTER_NAME] AKS cluster..."
node_subnet_ids=$(az aks show \
--name $AKS_CLUSTER_NAME \
--resource-group $RESOURCE_GROUP_NAME \
--query "agentPoolProfiles[?vnetSubnetId!=null].vnetSubnetId" \
--output tsv \
--only-show-errors 2>/dev/null | sort --unique)
if [[ -z "$node_subnet_ids" ]]; then
echo "WARNING: no node subnet found for the [$AKS_CLUSTER_NAME] AKS cluster: the [$STORAGE_ACCOUNT_NAME] storage account is left open to all networks."
echo "WARNING: on Azure the NFS mount will fail, because an NFS share can only be reached from a restricted network."
else
network_rules_added='true'
for node_subnet_id in $node_subnet_ids; do
echo "Allowing access to the [$STORAGE_ACCOUNT_NAME] storage account from the [$(basename "$node_subnet_id")] subnet..."
az storage account network-rule add \
--account-name $STORAGE_ACCOUNT_NAME \
--resource-group $RESOURCE_GROUP_NAME \
--subnet "$node_subnet_id" \
--only-show-errors 1>/dev/null
if [[ $? != 0 ]]; then
network_rules_added='false'
echo "WARNING: failed to allow access from the [$(basename "$node_subnet_id")] subnet to the [$STORAGE_ACCOUNT_NAME] storage account."
fi
done
# The account is closed to every other network only once every subnet rule is in place:
# denying by default while a rule is missing would lock the nodes out of their own share.
if [[ "$network_rules_added" == 'true' ]]; then
echo "Restricting the [$STORAGE_ACCOUNT_NAME] storage account to the node subnets..."
az storage account update \
--name $STORAGE_ACCOUNT_NAME \
--resource-group $RESOURCE_GROUP_NAME \
--default-action Deny \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "The [$STORAGE_ACCOUNT_NAME] storage account is now reachable from the node subnets only."
else
echo "WARNING: failed to restrict the [$STORAGE_ACCOUNT_NAME] storage account to the node subnets."
echo "WARNING: on Azure the NFS mount will fail, because an NFS share can only be reached from a restricted network."
fi
else
echo "WARNING: the [$STORAGE_ACCOUNT_NAME] storage account is left open to all networks, because at least one subnet rule could not be created."
echo "WARNING: on Azure the NFS mount will fail, because an NFS share can only be reached from a restricted network."
fi
fi
fi
# Create the Azure file share.
# az storage share-rm talks to the storage resource provider (the control plane) instead of the file
# data plane, which is what makes --enabled-protocols and --root-squash available and lets the share
# be created without an account key.
echo "Checking if file share [$FILE_SHARE_NAME] exists in storage account [$STORAGE_ACCOUNT_NAME]..."
az storage share-rm show \
--name $FILE_SHARE_NAME \
--storage-account $STORAGE_ACCOUNT_NAME \
--resource-group $RESOURCE_GROUP_NAME \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "Creating [$ENABLED_PROTOCOLS] file share [$FILE_SHARE_NAME] with a quota of [$FILE_SHARE_QUOTA_GB] GiB..."
az storage share-rm create \
--name $FILE_SHARE_NAME \
--storage-account $STORAGE_ACCOUNT_NAME \
--resource-group $RESOURCE_GROUP_NAME \
--enabled-protocols $ENABLED_PROTOCOLS \
--quota $FILE_SHARE_QUOTA_GB \
"${FILE_SHARE_EXTRA_ARGS[@]}" \
--only-show-errors 1>/dev/null
if [ $? -eq 0 ]; then
echo "File share [$FILE_SHARE_NAME] created successfully."
else
echo "Failed to create file share [$FILE_SHARE_NAME]."
exit 1
fi
else
echo "File share [$FILE_SHARE_NAME] already exists in the [$STORAGE_ACCOUNT_NAME] storage account"
fi
echo "The [$FILE_SHARE_NAME] $ENABLED_PROTOCOLS file share of the [$STORAGE_ACCOUNT_NAME] storage account is ready to be mounted by the app."