Skip to content

Commit 6ea919c

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

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-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: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ set -a
1616
SERVICES_POOL="services"
1717
WORKSPACES_POOL="workspaces"
1818

19-
K8S_NODE_VM_SIZE=${K8S_NODE_VM_SIZE:="Standard_D4_v3"}
19+
K8S_NODE_VM_SIZE=${K8S_NODE_VM_SIZE:="Standard_DS3_v2"}
2020

2121
function check_prerequisites() {
2222
if [ -z "${AZURE_SUBSCRIPTION_ID}" ]; then
@@ -123,7 +123,7 @@ function install() {
123123
--node-osdisk-size "100" \
124124
--node-vm-size "${K8S_NODE_VM_SIZE}" \
125125
--resource-group "${RESOURCE_GROUP}"
126-
fi
126+
fi
127127

128128
setup_kubectl
129129

@@ -141,6 +141,7 @@ function install() {
141141
setup_managed_dns
142142
setup_mysql_database
143143
setup_storage
144+
setup_backup
144145
output_config
145146
}
146147

@@ -366,6 +367,83 @@ function setup_mysql_database() {
366367
--start-ip-address "0.0.0.0"
367368
}
368369

370+
function setup_backup() {
371+
if [ -n "${BACKUPS_ENABLED}" ] && [ "${BACKUPS_ENABLED}" == "true" ]; then
372+
BACKUP_RESOURCE_GROUP="$(az aks show --name gitpod -g gitpod --query "nodeResourceGroup" -o tsv)"
373+
374+
echo "Configuring backups in ${BACKUP_RESOURCE_GROUP}..."
375+
376+
# Based from https://github.com/vmware-tanzu/velero-plugin-for-microsoft-azure#setup
377+
BACKUP_ACCOUNT="${STORAGE_ACCOUNT_NAME}backup"
378+
if [ "$(az storage account show --name ${BACKUP_ACCOUNT} --resource-group ${BACKUP_RESOURCE_GROUP} --query "name == '${BACKUP_ACCOUNT}'" || echo "empty")" == "true" ]; then
379+
echo "Backup storage account exists..."
380+
else
381+
echo "Create backup storage account..."
382+
az storage account create \
383+
--name "${STORAGE_ACCOUNT_NAME}backup" \
384+
--resource-group "${BACKUP_RESOURCE_GROUP}" \
385+
--location "${LOCATION}" \
386+
--sku Standard_GRS \
387+
--encryption-services blob \
388+
--https-only true \
389+
--kind BlobStorage \
390+
--access-tier Hot
391+
fi
392+
393+
ACCOUNT_KEY="$(az storage account keys list --resource-group "${BACKUP_RESOURCE_GROUP}" --account-name "${BACKUP_ACCOUNT}" --query "[0].value" -o tsv)"
394+
395+
BLOB_CONTAINER="velero"
396+
if [ "$(az storage container show --account-name ${BACKUP_ACCOUNT} --name ${BLOB_CONTAINER} --account-key="${ACCOUNT_KEY}" --query "name == '${BLOB_CONTAINER}'" || echo "empty")" == "true" ]; then
397+
echo "Backup storage container exists..."
398+
else
399+
echo "Create backup storage container..."
400+
az storage container create \
401+
-n "${BLOB_CONTAINER}" \
402+
--account-key="${ACCOUNT_KEY}" \
403+
--public-access off \
404+
--account-name "${BACKUP_ACCOUNT}"
405+
fi
406+
407+
echo "Create service principal for Velero"
408+
AZURE_ROLE="Contributor"
409+
SP_NAME="velero"
410+
411+
# Delete each time
412+
az ad sp delete --id $(az ad sp list --display-name "${SP_NAME}" --query "[].id" -o tsv) || true
413+
414+
AZURE_CLIENT_SECRET=$(az ad sp create-for-rbac \
415+
--display-name "${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 "${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..."

0 commit comments

Comments
 (0)