Skip to content

feat(backups): configure resource required for enabling backups #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
AZURE_SUBSCRIPTION_ID=""
AZURE_TENANT_ID=""

# Set if you want to configure KOTS backups
# https://docs.replicated.com/vendor/snapshots-overview
BACKUPS_ENABLED=false

# The name of the Kubernetes cluster
CLUSTER_NAME=gitpod

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.idea
gitpod.yaml
gitpod-config.yaml
credentials-velero
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mcr.microsoft.com/azure-cli:2.9.1
FROM mcr.microsoft.com/azure-cli:2.37.0

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

COPY --from=velero/velero:v1.8.1 /velero /usr/bin/velero

WORKDIR /gitpod

COPY . /gitpod
Expand Down
87 changes: 85 additions & 2 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ SERVICES_POOL="services"
WORKSPACES_POOL="workspaces"

K8S_NODE_VM_SIZE=${K8S_NODE_VM_SIZE:="Standard_D4_v3"}
BACKUP_SP_NAME="velero"

function check_prerequisites() {
if [ -z "${AZURE_SUBSCRIPTION_ID}" ]; then
Expand Down Expand Up @@ -123,7 +124,7 @@ function install() {
--node-osdisk-size "100" \
--node-vm-size "${K8S_NODE_VM_SIZE}" \
--resource-group "${RESOURCE_GROUP}"
fi
fi

setup_kubectl

Expand All @@ -141,6 +142,7 @@ function install() {
setup_managed_dns
setup_mysql_database
setup_storage
setup_backup
output_config
}

Expand Down Expand Up @@ -366,6 +368,82 @@ function setup_mysql_database() {
--start-ip-address "0.0.0.0"
}

function setup_backup() {
if [ -n "${BACKUPS_ENABLED}" ] && [ "${BACKUPS_ENABLED}" == "true" ]; then
BACKUP_RESOURCE_GROUP="$(az aks show --name gitpod -g gitpod --query "nodeResourceGroup" -o tsv)"

echo "Configuring backups in ${BACKUP_RESOURCE_GROUP}..."

# Based from https://github.com/vmware-tanzu/velero-plugin-for-microsoft-azure#setup
BACKUP_ACCOUNT="${STORAGE_ACCOUNT_NAME}backup"
if [ "$(az storage account show --name ${BACKUP_ACCOUNT} --resource-group ${BACKUP_RESOURCE_GROUP} --query "name == '${BACKUP_ACCOUNT}'" || echo "empty")" == "true" ]; then
echo "Backup storage account exists..."
else
echo "Create backup storage account..."
az storage account create \
--name "${STORAGE_ACCOUNT_NAME}backup" \
--resource-group "${BACKUP_RESOURCE_GROUP}" \
--location "${LOCATION}" \
--sku Standard_GRS \
--encryption-services blob \
--https-only true \
--kind BlobStorage \
--access-tier Hot
fi

ACCOUNT_KEY="$(az storage account keys list --resource-group "${BACKUP_RESOURCE_GROUP}" --account-name "${BACKUP_ACCOUNT}" --query "[0].value" -o tsv)"

BLOB_CONTAINER="velero"
if [ "$(az storage container show --account-name ${BACKUP_ACCOUNT} --name ${BLOB_CONTAINER} --account-key="${ACCOUNT_KEY}" --query "name == '${BLOB_CONTAINER}'" || echo "empty")" == "true" ]; then
echo "Backup storage container exists..."
else
echo "Create backup storage container..."
az storage container create \
-n "${BLOB_CONTAINER}" \
--account-key="${ACCOUNT_KEY}" \
--public-access off \
--account-name "${BACKUP_ACCOUNT}"
fi

echo "Create service principal for Velero"
AZURE_ROLE="Contributor"

# Delete each time
az ad sp delete --id $(az ad sp list --display-name "${BACKUP_SP_NAME}" --query "[].id" -o tsv) || true

AZURE_CLIENT_SECRET=$(az ad sp create-for-rbac \
--display-name "${BACKUP_SP_NAME}" \
--role "${AZURE_ROLE}" \
--scopes /subscriptions/27ef008d-9475-4fe2-ac63-d15da9362546 \
--query "password" \
-o tsv)

AZURE_CLIENT_ID=$(az ad sp list --display-name "${BACKUP_SP_NAME}" --query '[0].appId' -o tsv)

cat << EOF > ./credentials-velero
AZURE_SUBSCRIPTION_ID=${AZURE_SUBSCRIPTION_ID}
AZURE_TENANT_ID=${AZURE_TENANT_ID}
AZURE_CLIENT_ID=${AZURE_CLIENT_ID}
AZURE_CLIENT_SECRET=${AZURE_CLIENT_SECRET}
AZURE_RESOURCE_GROUP=${BACKUP_RESOURCE_GROUP}
AZURE_CLOUD_NAME=AzurePublicCloud
EOF

# Delete to force update to new values
velero uninstall --force

velero install \
--provider azure \
--plugins velero/velero-plugin-for-microsoft-azure:v1.4.0 \
--bucket "${BLOB_CONTAINER}" \
--secret-file ./credentials-velero \
--backup-location-config "resourceGroup=${BACKUP_RESOURCE_GROUP},storageAccount=${BACKUP_ACCOUNT},subscriptionId=${AZURE_SUBSCRIPTION_ID}" \
--snapshot-location-config "apiTimeout=2m" \
--use-restic \
--wait
fi
}

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..."
Expand Down Expand Up @@ -397,7 +475,7 @@ function setup_storage() {
function uninstall() {
check_prerequisites

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

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

if [ -n "${BACKUPS_ENABLED}" ] && [ "${BACKUPS_ENABLED}" == "true" ]; then
echo "Deleting backup service principal"
az ad sp delete --id $(az ad sp list --display-name "${BACKUP_SP_NAME}" --query "[].id" -o tsv) || true
fi

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
Expand Down