www-jrtorres042-github-enterprise-org
/
git_microsoft-powershell_code-of-conduct_credential_achived-tracker_merge-gitpod_covid-19_diff-1
Public template
forked from gitpod-io/gitpod-microsoft-aks-guide
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·475 lines (403 loc) · 14.8 KB
/
setup.sh
File metadata and controls
executable file
·475 lines (403 loc) · 14.8 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#!/usr/bin/env bash
set -eo pipefail
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)
if [ ! -f "${DIR}/.env" ]; then
echo "Missing ${DIR}/.env configuration file."
exit 1;
fi
set -a
source "${DIR}/.env"
set -a
# Name of the node-pools for Gitpod services and workspaces
SERVICES_POOL="services"
WORKSPACES_POOL="workspaces"
AKS_VERSION=${AKS_VERSION:="1.20.7"}
K8S_NODE_VM_SIZE=${K8S_NODE_VM_SIZE:="Standard_DS3_v2"}
GITPOD_VERSION=${GITPOD_VERSION:="0.10.0"}
function auth() {
AUTHPROVIDERS_CONFIG=${1:="auth-providers-patch.yaml"}
if [ ! -f "${AUTHPROVIDERS_CONFIG}" ]; then
echo "The auth provider configuration file ${AUTHPROVIDERS_CONFIG} does not exist."
exit 1
fi
login
setup_kubectl
echo "Using the auth providers configuration file: ${AUTHPROVIDERS_CONFIG}"
# Patching the configuration with the user auth provider/s
kubectl patch configmap auth-providers-config --type merge --patch "$(cat ${AUTHPROVIDERS_CONFIG})"
# Restart the server component
kubectl rollout restart deployment/server
}
function check_prerequisites() {
if [ -z "${AZURE_SUBSCRIPTION_ID}" ]; then
echo "Missing AZURE_SUBSCRIPTION_ID environment variable."
exit 1;
fi
if [ -z "${AZURE_TENANT_ID}" ]; then
echo "Missing AZURE_TENANT_ID environment variable."
exit 1;
fi
if [ -z "${AZURE_CLIENT_ID}" ]; then
echo "Missing AZURE_CLIENT_ID environment variable."
exit 1;
fi
if [ -z "${AZURE_CLIENT_SECRET}" ]; then
echo "Missing AZURE_CLIENT_SECRET environment variable."
exit 1;
fi
if [ -z "${RESOURCE_GROUP}" ]; then
echo "Missing RESOURCE_GROUP environment variable."
exit 1;
fi
if [ -z "${CLUSTER_NAME}" ]; then
echo "Missing CLUSTER_NAME environment variable."
exit 1;
fi
if [ -z "${DOMAIN}" ]; then
echo "Missing DOMAIN environment variable."
exit 1;
fi
if [ -z "${LOCATION}" ]; then
echo "Missing LOCATION environment variable."
exit 1;
fi
if [ -z "${REGISTRY_NAME}" ]; then
echo "Missing REGISTRY_NAME environment variable."
exit 1;
fi
}
function install() {
check_prerequisites
echo "Updating helm repositories..."
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo add gitpod https://charts.gitpod.io
helm repo add jaegertracing https://jaegertracing.github.io/helm-charts
helm repo add jetstack https://charts.jetstack.io
helm repo update
echo "Installing..."
login
# Everything will be installed to this resource group
if [ "$(az group show --name ${RESOURCE_GROUP} --query "name == '${RESOURCE_GROUP}'" || echo "empty")" == "true" ]; then
echo "Resource group exists..."
else
az group create \
--location "${LOCATION}" \
--name "${RESOURCE_GROUP}"
fi
if [ "$(az aks show --name ${CLUSTER_NAME} --resource-group ${RESOURCE_GROUP} --query "name == '${CLUSTER_NAME}'" || echo "empty")" == "true" ]; then
echo "Kubernetes cluster exists..."
else
echo "Creating Kubernetes instance..."
az aks create \
--enable-cluster-autoscaler \
--enable-managed-identity \
--location "${LOCATION}" \
--kubernetes-version "${AKS_VERSION}" \
--max-count "50" \
--max-pods "110" \
--min-count "3" \
--name "${CLUSTER_NAME}" \
--node-osdisk-size "100" \
--node-vm-size "${K8S_NODE_VM_SIZE}" \
--nodepool-labels "gitpod.io/workload_services=true" \
--nodepool-name "${SERVICES_POOL}" \
--resource-group "${RESOURCE_GROUP}" \
--no-ssh-key \
--vm-set-type "VirtualMachineScaleSets"
fi
if [ "$(az aks nodepool show --cluster-name ${CLUSTER_NAME} --name ${WORKSPACES_POOL} --resource-group ${RESOURCE_GROUP} --query "name == '${WORKSPACES_POOL}'" || echo "empty")" == "true" ]; then
echo "Node pool ${WORKSPACES_POOL} exists..."
else
echo "Creating ${WORKSPACES_POOL} node pool..."
az aks nodepool add \
--cluster-name "${CLUSTER_NAME}" \
--enable-cluster-autoscaler \
--kubernetes-version "${AKS_VERSION}" \
--labels "gitpod.io/workload_workspaces=true" \
--max-count "50" \
--max-pods "110" \
--min-count "3" \
--name "${WORKSPACES_POOL}" \
--node-osdisk-size "100" \
--node-vm-size "${K8S_NODE_VM_SIZE}" \
--resource-group "${RESOURCE_GROUP}"
fi
setup_kubectl
# Create secret with container registry credentials
if [ -n "${IMAGE_PULL_SECRET_FILE}" ] && [ -f "${IMAGE_PULL_SECRET_FILE}" ]; then
if ! kubectl get secret gitpod-image-pull-secret; then
kubectl create secret generic gitpod-image-pull-secret \
--from-file=.dockerconfigjson="${IMAGE_PULL_SECRET_FILE}" \
--type=kubernetes.io/dockerconfigjson >/dev/null 2>&1 || true
fi
fi
install_cert_manager
setup_container_registry
setup_managed_dns
setup_mysql_database
setup_storage
install_jaeger_operator
install_gitpod
}
function install_cert_manager() {
echo "Installing cert-manager..."
helm upgrade \
--atomic \
--cleanup-on-fail \
--create-namespace \
--install \
--namespace='cert-manager' \
--reset-values \
--set installCRDs=true \
--set 'extraArgs={--dns01-recursive-nameservers-only=true,--dns01-recursive-nameservers=8.8.8.8:53\,1.1.1.1:53}' \
--wait \
cert-manager \
jetstack/cert-manager
# ensure cert-manager and CRDs are installed and running
kubectl wait --for=condition=available --timeout=300s deployment/cert-manager -n cert-manager
}
function install_gitpod() {
echo "Installing Gitpod..."
envsubst < "${DIR}/charts/assets/gitpod-values.yaml" | helm upgrade --install gitpod gitpod/gitpod -f -
echo "Create certificate..."
cat <<EOF > gitpod-certificate.yaml
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: gitpod-certificate
namespace: default
spec:
secretName: proxy-config-certificates
issuerRef:
name: azure-issuer
kind: ClusterIssuer
dnsNames:
- $DOMAIN
- '*.$DOMAIN'
- '*.ws.$DOMAIN'
EOF
kubectl apply -f gitpod-certificate.yaml
rm gitpod-certificate.yaml
kubectl rollout restart deployment/server
echo "Gitpod successfully installed to ${DOMAIN}..."
}
function install_jaeger_operator(){
echo "Installing Jaeger operator..."
kubectl apply -f https://raw.githubusercontent.com/jaegertracing/helm-charts/main/charts/jaeger-operator/crds/crd.yaml
helm upgrade \
--atomic \
--cleanup-on-fail \
--create-namespace \
--install \
--namespace='jaeger-operator' \
--reset-values \
--set installCRDs=true \
--set crd.install=false \
--values "${DIR}/charts/assets/jaeger-values.yaml" \
--wait \
jaegeroperator \
jaegertracing/jaeger-operator
kubectl apply -f "${DIR}/charts/assets/jaeger-gitpod.yaml"
}
function login() {
echo "Log into Azure with Service Principal..."
az login --service-principal -u "${AZURE_CLIENT_ID}" -p "${AZURE_CLIENT_SECRET}" --tenant "${AZURE_TENANT_ID}" > /dev/null 2>&1
echo "Set Azure subscription..."
az account set --subscription "${AZURE_SUBSCRIPTION_ID}"
}
function setup_container_registry() {
if [ "$(az acr show --name ${REGISTRY_NAME} --resource-group ${RESOURCE_GROUP} --query "name == '${REGISTRY_NAME}'" || echo "empty")" == "true" ]; then
echo "Registry exists..."
else
echo "Setup Azure Container Registry..."
az acr create \
--admin-enabled true \
--name "${REGISTRY_NAME}" \
--resource-group "${RESOURCE_GROUP}" \
--sku Premium
fi
DOCKER_USER=$(az acr credential show \
--name "${REGISTRY_NAME}" \
--output tsv \
--query username \
--resource-group "${RESOURCE_GROUP}")
export DOCKER_REGISTRY_SERVER=$(az acr show \
--name "${REGISTRY_NAME}" \
--output tsv \
--query loginServer \
--resource-group "${RESOURCE_GROUP}")
DOCKER_PASSWORD=$(az acr credential show \
--name "${REGISTRY_NAME}" \
--output tsv \
--query passwords[0].value \
--resource-group "${RESOURCE_GROUP}")
kubectl create secret docker-registry image-builder-registry-secret \
--docker-server="${DOCKER_REGISTRY_SERVER}" \
--docker-username="${DOCKER_USER}" \
--docker-password="${DOCKER_PASSWORD}" \
--dry-run=client -o yaml | \
kubectl replace -n "${namespace}" --force -f -
}
function setup_kubectl() {
echo "Get Kubernetes credentials..."
az aks get-credentials \
--name "${CLUSTER_NAME}" \
--resource-group "${RESOURCE_GROUP}" \
--overwrite-existing
}
function setup_managed_dns() {
if [ -n "${SETUP_MANAGED_DNS}" ] && [ "${SETUP_MANAGED_DNS}" == "true" ]; then
echo "Installing managed DNS..."
if [ "$(az network dns zone show --name ${DOMAIN} --resource-group ${RESOURCE_GROUP} --query "name == '${DOMAIN}'" || echo "empty")" == "true" ]; then
echo "Using existing managed DNS zone ${DOMAIN}..."
else
echo "Creating managed DNS zone for domain ${DOMAIN}..."
az network dns zone create \
--name "${DOMAIN}" \
--resource-group "${RESOURCE_GROUP}"
fi
echo "Allow Kubernetes managed identity to make DNS changes..."
PRINCIPAL_ID=$(az aks show --name "${CLUSTER_NAME}" --resource-group "${RESOURCE_GROUP}" --query "identityProfile.kubeletidentity.objectId" -o tsv)
ZONE_ID=$(az network dns zone show --name "${DOMAIN}" --resource-group "${RESOURCE_GROUP}" --query "id" -o tsv)
# The Service Principal needs "Group Administrator" role
# @link https://www.simonemms.com/blog/2021/01/10/setting-terraform-service-principal-to-work-with-azure-active-directory
az role assignment create \
--assignee "${PRINCIPAL_ID}" \
--role "DNS Zone Contributor" \
--scope "${ZONE_ID}"
helm upgrade \
--atomic \
--cleanup-on-fail \
--create-namespace \
--install \
--namespace external-dns \
--reset-values \
--set provider=azure \
--set azure.resourceGroup="${RESOURCE_GROUP}" \
--set azure.aadClientId="${AZURE_CLIENT_ID}" \
--set azure.aadClientSecret="${AZURE_CLIENT_SECRET}" \
--set azure.subscriptionId="${AZURE_SUBSCRIPTION_ID}" \
--set azure.tenantId="${AZURE_TENANT_ID}" \
--set logFormat=json \
--wait \
external-dns \
bitnami/external-dns
echo "Installing cert-manager certificate issuer..."
envsubst < "${DIR}/charts/assets/issuer.yaml" | kubectl apply -f -
fi
}
# @todo allow Gitpod to work with external Azure DB https://github.com/gitpod-io/gitpod/issues/5508
function setup_mysql_database() {
export MYSQL_GITPOD_PASSWORD=$(openssl rand -base64 20)
# if [ "$(az mysql server show --name ${MYSQL_INSTANCE_NAME} --resource-group ${RESOURCE_GROUP} --query "name == '${MYSQL_INSTANCE_NAME}'" || echo "empty")" == "true" ]; then
# echo "MySQL instance exists - updating password..."
# az mysql server update \
# --admin-password "${MYSQL_GITPOD_PASSWORD}" \
# --name "${MYSQL_INSTANCE_NAME}" \
# --resource-group "${RESOURCE_GROUP}"
# else
# echo "Creating MySQL instance..."
# az mysql server create \
# --admin-user gitpod \
# --admin-password "${MYSQL_GITPOD_PASSWORD}" \
# --auto-grow Enabled \
# --name "${MYSQL_INSTANCE_NAME}" \
# --public Enabled \
# --resource-group "${RESOURCE_GROUP}" \
# --sku-name GP_Gen5_2 \
# --ssl-enforcement Disabled \
# --storage-size 20480 \
# --version "5.7"
# echo "Creating MySQL replica..."
# az mysql server replica create \
# --name "${MYSQL_INSTANCE_NAME}-replica" \
# --source-server "${MYSQL_INSTANCE_NAME}" \
# --resource-group "${RESOURCE_GROUP}"
# fi
# DB_NAME=gitpod
# if [ "$(az mysql db show --name ${DB_NAME} --resource-group ${RESOURCE_GROUP} --server-name ${MYSQL_INSTANCE_NAME} --query "name == '${DB_NAME}'" || echo "empty")" == "true" ]; then
# echo "Gitpod MySQL database exists..."
# else
# echo "Creating Gitpod MySQL database..."
# az mysql db create \
# --name "${DB_NAME}" \
# --resource-group "${RESOURCE_GROUP}" \
# --server-name "${MYSQL_INSTANCE_NAME}"
# fi
# echo "Allow Azure resources to access MySQL database..."
# az mysql server firewall-rule create \
# --end-ip-address "0.0.0.0" \
# --name "Azure_Resources" \
# --resource-group "${RESOURCE_GROUP}" \
# --server-name "${MYSQL_INSTANCE_NAME}" \
# --start-ip-address "0.0.0.0"
}
function setup_storage() {
if [ "$(az storage account show --name ${STORAGE_ACCOUNT_NAME} --resource-group ${RESOURCE_GROUP} --query "name == '${STORAGE_ACCOUNT_NAME}'" || echo "empty")" == "true" ]; then
echo "Storage account exists..."
else
echo "Create storage account..."
az storage account create \
--access-tier Hot \
--kind StorageV2 \
--name "${1}" \
--resource-group "${RESOURCE_GROUP}" \
--sku Standard_LRS
fi
PRINCIPAL_ID=$(az aks show --name "${CLUSTER_NAME}" --resource-group "${RESOURCE_GROUP}" --query "identityProfile.kubeletidentity.objectId" -o tsv)
STORAGE_ACCOUNT_ID=$(az storage account show \
--name "${STORAGE_ACCOUNT_NAME}" \
--output tsv \
--query id \
--resource-group "${RESOURCE_GROUP}" )
echo "Allow Kubernetes managed identity to access the storage account..."
az role assignment create \
--assignee "${PRINCIPAL_ID}" \
--role "Storage Blob Data Contributor" \
--scope "${STORAGE_ACCOUNT_ID}"
export STORAGE_ACCOUNT_KEY=$(az storage account keys list \
--account-name "${STORAGE_ACCOUNT_NAME}" \
--resource-group "${RESOURCE_GROUP}" \
--output json \
| jq -r '.[] | select(.keyName == "key1") | .value')
}
function uninstall() {
check_prerequisites
read -p "Are you sure you want to delete: Gitpod (y/n)? " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
set +e
login
setup_kubectl
helm uninstall gitpod
kubectl delete secret gitpod-image-pull-secret
kubectl delete secret image-builder-registry-secret
# Ensure we remove the load balancer
kubectl delete service proxy
echo "Deleting Kubernetes cluster..."
az aks delete \
--name "${CLUSTER_NAME}" \
--resource-group "${RESOURCE_GROUP}" \
--yes
printf "\n%s\n" "Please make sure to delete the resource group ${RESOURCE_GROUP} and services:"
printf "%s\n" "- https://portal.azure.com/#resource/subscriptions/${AZURE_SUBSCRIPTION_ID}/resourceGroups/${RESOURCE_GROUP}/overview"
fi
}
function main() {
case $1 in
'--auth')
auth "auth-providers-patch.yaml"
;;
'--install')
install
;;
'--uninstall')
uninstall
;;
*)
echo "Unknown command: $1"
echo "Usage: $0 [--install|--uninstall|--auth]"
;;
esac
echo "Done"
}
main "$@"