Skip to content
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

Add task for setting up an autoscaler for a site #302

Merged
merged 1 commit into from
May 3, 2024
Merged
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
12 changes: 12 additions & 0 deletions infrastructure/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,18 @@ tasks:
preconditions:
- *require_site

site:autoscaler:
desc: Runs the a dpladm setup of horizontal autoscaler, making sure the site has an autoscaler configured
deps: [cluster:auth]
summary: Run the task without additional variables to see required arguments
env:
SITES_CONFIG: "{{.dir_env}}/sites.yaml"
SITE: "{{.SITE}}"
cmds:
- dpladm/bin/set-up-horizontal-autoscaler.sh
preconditions:
- *require_site

sites:list-keys:
desc: List keys for sites in sites.yaml config
dir: "{{.dir_env}}"
Expand Down
25 changes: 25 additions & 0 deletions infrastructure/dpladm/autoscaler.template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: php-nginx-autoscaler
namespace: ${SITE_NAME}-${SITE_ENV}
spec:
maxReplicas: 5
metrics:
- resource:
name: memory
target:
averageUtilization: 500
type: Utilization
type: Resource
- resource:
name: cpu
target:
averageUtilization: 70
type: Utilization
type: Resource
minReplicas: 1
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx
68 changes: 68 additions & 0 deletions infrastructure/dpladm/bin/dpladm-shared.source
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,71 @@ function syncEnvRepo {
git push origin "${branchName}"
}

function print_usage {
# Start with a more specific error-message if we're passed the name of a
# variable that was missing.
if [[ -n "${1:-}" ]]; then
echo "Could not find the variable ${1}"
fi
echo
echo "Set the following environment variables before running the script."
echo " SITES_CONFIG: path to the sites.yaml that should be used for site configuration"
echo " SITE: the sites key in sites.yaml"
echo ""
echo " FORCE: Push even if there is no diff in which case an empty"
echo " commit will be pushed."

exit 1
}

function getSitePlan {
local plan
plan=$(yq eval ".sites.${1}.plan" "${2}")
if [[ "${plan}" == "null" ]]; then
echo "standard"
return
fi

echo "${plan}"
return
}

function setUpHorizontalAutoscaler {
local siteName=$1
local SITES_CONFIG=$2
local plan=$(getSitePlan "${siteName}" "${SITES_CONFIG}")

local workspace
workspace="$(getCleanWorkspacePath)"
cd "${workspace}"

renderAutoscalers "${siteName}" "${plan}"
kubectl apply --recursive -f autoscalers
}

function renderAutoscalers {
local siteName=$1
local plan=$2

local scriptDir
scriptDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

mkdir autoscalers

local envs=(
"main"
)

if [ plan = "webmaster" ]; then
local envs=(
"main"
"moduletest"
)
fi

for env in "${envs[@]}"; do
export SITE_NAME="$siteName";
export SITE_ENV="$env";
envsubst '$SITE_NAME $SITE_ENV' < "${scriptDir}/../autoscaler.template.yaml" > "autoscalers/$env.yaml";
done
}
21 changes: 21 additions & 0 deletions infrastructure/dpladm/bin/set-up-horizontal-autoscaler.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash
#
# Reads in a sites.yaml and updates the sites environment repository to bring it
# in sync with the configuration with regards to eg. the deployed release.
# This will typically trigger a deployment of the site.
#
set -euo pipefail
IFS=$'\n\t'

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "${SCRIPT_DIR}/dpladm-shared.source"

if [[ -z "${SITES_CONFIG:-}" ]]; then
print_usage "SITES_CONFIG"
fi

if [[ -z "${SITE:-}" ]]; then
print_usage "SITE"
fi

setUpHorizontalAutoscaler "$SITE" "$SITES_CONFIG"
29 changes: 0 additions & 29 deletions infrastructure/dpladm/bin/sync-site.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,6 @@ IFS=$'\n\t'
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "${SCRIPT_DIR}/dpladm-shared.source"

function print_usage {
# Start with a more specific error-message if we're passed the name of a
# variable that was missing.
if [[ -n "${1:-}" ]]; then
echo "Could not find the variable ${1}"
fi
echo
echo "Set the following environment variables before running the script."
echo " SITES_CONFIG: path to the sites.yaml that should be used for site configuration"
echo " SITE: the sites key in sites.yaml"
echo ""
echo " FORCE: Push even if there is no diff in which case an empty"
echo " commit will be pushed."

exit 1
}

function getSiteConfig {
local config
config=$(yq eval ".sites.${1}" "${2}")
Expand Down Expand Up @@ -77,18 +60,6 @@ function getSiteReleaseImageName {
return
}

function getSitePlan {
local plan
plan=$(yq eval ".sites.${1}.plan" "${2}")
if [[ "${plan}" == "null" ]]; then
echo "standard"
return
fi

echo "${plan}"
return
}

function getSitePrimaryDomain {
local domain
domain=$(yq eval ".sites.${1}.primary-domain" "${2}")
Expand Down