Skip to content

Commit 6063668

Browse files
author
Simon Emms
committed
feat(backups): configure resources required for enabling backups
1 parent f8e1981 commit 6063668

File tree

4 files changed

+93
-3
lines changed

4 files changed

+93
-3
lines changed

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
AZURE_SUBSCRIPTION_ID=""
44
AZURE_TENANT_ID=""
55

6+
# Set if you want to configure KOTS backups
7+
# https://docs.replicated.com/vendor/snapshots-overview
8+
BACKUPS_ENABLED=false
9+
610
# The name of the Kubernetes cluster
711
CLUSTER_NAME=gitpod
812

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
.idea
44
gitpod.yaml
55
gitpod-config.yaml
6+
credentials-velero

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM mcr.microsoft.com/azure-cli:2.9.1
1+
FROM mcr.microsoft.com/azure-cli:2.37.0
22

33
RUN apk add --no-cache \
44
gettext \
@@ -17,6 +17,8 @@ RUN mkdir -p /tmp/helm/ \
1717
RUN curl -fsSL https://github.com/mikefarah/yq/releases/download/v4.12.2/yq_linux_amd64 -o /usr/local/bin/yq \
1818
&& chmod +x /usr/local/bin/yq
1919

20+
COPY --from=velero/velero:v1.8.1 /velero /usr/bin/velero
21+
2022
WORKDIR /gitpod
2123

2224
COPY . /gitpod

setup.sh

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ SERVICES_POOL="services"
1717
WORKSPACES_POOL="workspaces"
1818

1919
K8S_NODE_VM_SIZE=${K8S_NODE_VM_SIZE:="Standard_D4_v3"}
20+
BACKUP_SP_NAME="velero"
2021

2122
function check_prerequisites() {
2223
if [ -z "${AZURE_SUBSCRIPTION_ID}" ]; then
@@ -123,7 +124,7 @@ function install() {
123124
--node-osdisk-size "100" \
124125
--node-vm-size "${K8S_NODE_VM_SIZE}" \
125126
--resource-group "${RESOURCE_GROUP}"
126-
fi
127+
fi
127128

128129
setup_kubectl
129130

@@ -141,6 +142,7 @@ function install() {
141142
setup_managed_dns
142143
setup_mysql_database
143144
setup_storage
145+
setup_backup
144146
output_config
145147
}
146148

@@ -366,6 +368,82 @@ function setup_mysql_database() {
366368
--start-ip-address "0.0.0.0"
367369
}
368370

371+
function setup_backup() {
372+
if [ -n "${BACKUPS_ENABLED}" ] && [ "${BACKUPS_ENABLED}" == "true" ]; then
373+
BACKUP_RESOURCE_GROUP="$(az aks show --name gitpod -g gitpod --query "nodeResourceGroup" -o tsv)"
374+
375+
echo "Configuring backups in ${BACKUP_RESOURCE_GROUP}..."
376+
377+
# Based from https://github.com/vmware-tanzu/velero-plugin-for-microsoft-azure#setup
378+
BACKUP_ACCOUNT="${STORAGE_ACCOUNT_NAME}backup"
379+
if [ "$(az storage account show --name ${BACKUP_ACCOUNT} --resource-group ${BACKUP_RESOURCE_GROUP} --query "name == '${BACKUP_ACCOUNT}'" || echo "empty")" == "true" ]; then
380+
echo "Backup storage account exists..."
381+
else
382+
echo "Create backup storage account..."
383+
az storage account create \
384+
--name "${STORAGE_ACCOUNT_NAME}backup" \
385+
--resource-group "${BACKUP_RESOURCE_GROUP}" \
386+
--location "${LOCATION}" \
387+
--sku Standard_GRS \
388+
--encryption-services blob \
389+
--https-only true \
390+
--kind BlobStorage \
391+
--access-tier Hot
392+
fi
393+
394+
ACCOUNT_KEY="$(az storage account keys list --resource-group "${BACKUP_RESOURCE_GROUP}" --account-name "${BACKUP_ACCOUNT}" --query "[0].value" -o tsv)"
395+
396+
BLOB_CONTAINER="velero"
397+
if [ "$(az storage container show --account-name ${BACKUP_ACCOUNT} --name ${BLOB_CONTAINER} --account-key="${ACCOUNT_KEY}" --query "name == '${BLOB_CONTAINER}'" || echo "empty")" == "true" ]; then
398+
echo "Backup storage container exists..."
399+
else
400+
echo "Create backup storage container..."
401+
az storage container create \
402+
-n "${BLOB_CONTAINER}" \
403+
--account-key="${ACCOUNT_KEY}" \
404+
--public-access off \
405+
--account-name "${BACKUP_ACCOUNT}"
406+
fi
407+
408+
echo "Create service principal for Velero"
409+
AZURE_ROLE="Contributor"
410+
411+
# Delete each time
412+
az ad sp delete --id $(az ad sp list --display-name "${BACKUP_SP_NAME}" --query "[].id" -o tsv) || true
413+
414+
AZURE_CLIENT_SECRET=$(az ad sp create-for-rbac \
415+
--display-name "${BACKUP_SP_NAME}" \
416+
--role "${AZURE_ROLE}" \
417+
--scopes /subscriptions/27ef008d-9475-4fe2-ac63-d15da9362546 \
418+
--query "password" \
419+
-o tsv)
420+
421+
AZURE_CLIENT_ID=$(az ad sp list --display-name "${BACKUP_SP_NAME}" --query '[0].appId' -o tsv)
422+
423+
cat << EOF > ./credentials-velero
424+
AZURE_SUBSCRIPTION_ID=${AZURE_SUBSCRIPTION_ID}
425+
AZURE_TENANT_ID=${AZURE_TENANT_ID}
426+
AZURE_CLIENT_ID=${AZURE_CLIENT_ID}
427+
AZURE_CLIENT_SECRET=${AZURE_CLIENT_SECRET}
428+
AZURE_RESOURCE_GROUP=${BACKUP_RESOURCE_GROUP}
429+
AZURE_CLOUD_NAME=AzurePublicCloud
430+
EOF
431+
432+
# Delete to force update to new values
433+
velero uninstall --force
434+
435+
velero install \
436+
--provider azure \
437+
--plugins velero/velero-plugin-for-microsoft-azure:v1.4.0 \
438+
--bucket "${BLOB_CONTAINER}" \
439+
--secret-file ./credentials-velero \
440+
--backup-location-config "resourceGroup=${BACKUP_RESOURCE_GROUP},storageAccount=${BACKUP_ACCOUNT},subscriptionId=${AZURE_SUBSCRIPTION_ID}" \
441+
--snapshot-location-config "apiTimeout=2m" \
442+
--use-restic \
443+
--wait
444+
fi
445+
}
446+
369447
function setup_storage() {
370448
if [ "$(az storage account show --name ${STORAGE_ACCOUNT_NAME} --resource-group ${RESOURCE_GROUP} --query "name == '${STORAGE_ACCOUNT_NAME}'" || echo "empty")" == "true" ]; then
371449
echo "Storage account exists..."
@@ -397,7 +475,7 @@ function setup_storage() {
397475
function uninstall() {
398476
check_prerequisites
399477

400-
read -p "Are you sure you want to delete: Gitpod (y/n)? " -n 1 -r
478+
read -p "Are you sure you want to delete: Gitpod (y/N)? " -n 1 -r
401479
if [[ $REPLY =~ ^[Yy]$ ]]; then
402480
set +e
403481

@@ -418,6 +496,11 @@ function uninstall() {
418496
--resource-group "${RESOURCE_GROUP}" \
419497
--yes
420498

499+
if [ -n "${BACKUPS_ENABLED}" ] && [ "${BACKUPS_ENABLED}" == "true" ]; then
500+
echo "Deleting backup service principal"
501+
az ad sp delete --id $(az ad sp list --display-name "${BACKUP_SP_NAME}" --query "[].id" -o tsv) || true
502+
fi
503+
421504
printf "\n%s\n" "Please make sure to delete the resource group ${RESOURCE_GROUP} and services:"
422505
printf "%s\n" "- https://portal.azure.com/#resource/subscriptions/${AZURE_SUBSCRIPTION_ID}/resourceGroups/${RESOURCE_GROUP}/overview"
423506
fi

0 commit comments

Comments
 (0)