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

Fix scaledobject #342

Closed
wants to merge 13 commits into from
Closed
98 changes: 98 additions & 0 deletions .github/scripts/create_version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/bin/bash

# Ensure script exits on first error
set -e

SCRIPT_DIR=$(dirname "$0")
cd "${SCRIPT_DIR}/../../application"

name=$(cat Chart.yaml | yq '.name')
version=$(cat Chart.yaml | yq '.version')

# Prompt user to specify if the version is internal
echo "Is the version internal? (yes/no)"
read answer
internal=false
if [ "${answer}" == "yes" ]; then
internal=true
fi

# Backup the original Chart.yaml
cp Chart.yaml Chart.yaml.backup

# The version in Chart.yaml remains unchanged
new_version=${version}

if $internal; then
# Fetch all versions from ECR starting with the specified version prefix and ending with -sb.x
# Note: Adjust the following command to match your AWS CLI version and query capabilities
versions=$(aws --profile shared ecr-public describe-images --region us-east-1 --repository-name ${name} --query 'sort_by(imageDetails,& imagePushedAt)[*].imageTags' --output text | grep "^${version}-sb" | sort -V)
if [ -z "${versions}" ]; then
# If no versions found, start with -sb.1
ecr_version="${version}-sb.1"
else
# If versions found, pick the last one and increment
last_version=$(echo "${versions}" | tail -n 1)
num=$(echo "$last_version" | grep -o -E '[0-9]+$')
new_num=$((num + 1))
ecr_version="${version}-sb.${new_num}"
fi
# For internal use, we will use ecr_version to tag the ECR image
yq eval -i ".version = \"${ecr_version}\"" Chart.yaml
else
# For non-internal use, we keep the ECR tag same as the Chart version
ecr_version=$version
fi

# Proceed with packaging using the original version in Chart.yaml
output=$(helm package .)
chart_tgz_path=$(echo "$output" | awk '{print $NF}')

# Restore the original Chart.yaml from the backup
mv Chart.yaml.backup Chart.yaml

# Temporarily disable exit on error for AWS CLI operations
set +e

create_output=$(aws --profile shared ecr-public create-repository --region us-east-1 --repository-name ${name} 2>&1)
create_status=$?

# Re-enable exit on error
set -e

if [[ $create_status -ne 0 && $create_output == *"RepositoryAlreadyExistsException"* ]]; then
echo "Repository '${name}' already exists in AWS ECR. Proceeding with existing repository."
elif [[ $create_status -eq 0 ]]; then
echo "Repository '${name}' successfully created in AWS ECR."
else
echo "Failed to create repository '${name}' due to an unexpected error: $create_output"
exit 1
fi

# Temporarily disable exit on error for AWS CLI operations
set +e

registry_uri=$(aws --profile shared ecr-public describe-registries --region us-east-1 --query "registries[0].registryUri" --output text)
# Check if the image version (for internal use, the ecr_version) already exists in the repository
image_exists=$(aws --profile shared ecr-public describe-images --region us-east-1 --repository-name ${name} --image-ids imageTag=${ecr_version} 2>&1)
image_exists_status=$?

# Re-enable exit on error
set -e

if [[ $image_exists_status -eq 0 ]]; then
echo "Error: The version ${ecr_version} already exists in the repository."
rm ${chart_tgz_path}
exit 1
fi

# Login to ECR and push the image
aws --profile shared ecr-public get-login-password --region us-east-1 | helm registry login --username AWS --password-stdin public.ecr.aws
echo "chart tgz path: ${chart_tgz_path}"
echo "registry: oci://${registry_uri}"
echo "version: ${ecr_version}"
helm push ${chart_tgz_path} oci://${registry_uri}

rm ${chart_tgz_path}

echo "Completed successfully"
7 changes: 7 additions & 0 deletions application/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
{{- define "renderedValues" -}}
{{- $originalValues := .Values | toYaml }}
{{- $newValuesYaml := tpl $originalValues . }}
{{- $newValues := $newValuesYaml | fromYaml }}
{{- $_ := set $ "Values" $newValues -}}
{{- end }}

{{/* vim: set filetype=mustache: */}}

{{/*
Expand Down
39 changes: 39 additions & 0 deletions application/templates/scaledobject-sb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{{- if and .Values.deployment.enabled (and .Values.scaledObject (default false .Values.scaledObject.enabled)) }}
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: {{ template "application.name" . }}-scaled-object
namespace: {{ include "application.namespace" . }}
labels:
{{- include "application.labels" . | nindent 4 }}
spec:
scaleTargetRef:
name: {{ template "application.name" . }}
pollingInterval: {{ required "Undefined pollingInterval for ScaledObject" .Values.scaledObject.pollingInterval }}
cooldownPeriod: {{ required "Undefined cooldownPeriod for ScaledObject" .Values.scaledObject.cooldownPeriod }}
minReplicaCount: {{ required "Undefined minReplicaCount for ScaledObject" .Values.scaledObject.minReplicaCount }}
maxReplicaCount: {{ required "Undefined maxReplicaCount for ScaledObject" .Values.scaledObject.maxReplicaCount }}
{{- if (not (eq .Values.scaledObject.idleReplicaCount nil)) }}
idleReplicaCount: {{ .Values.scaledObject.idleReplicaCount }}
{{- end }}
{{- $minReplicaCount := .Values.scaledObject.minReplicaCount }}
{{- with .Values.scaledObject.fallback }}
fallback:
failureThreshold: {{ default 3 .failureThreshold }}
replicas: {{ default $minReplicaCount .replicas }}
{{- end }}
{{- if .Values.scaledObject.advanced }}
{{- $scaleDown := required "Undefined scaleDown configuration in advanced settings" .Values.scaledObject.advanced.scaleDown }}
{{- $scaleUp := required "Undefined scaleUp configuration in advanced settings" .Values.scaledObject.advanced.scaleUp }}
advanced:
horizontalPodAutoscalerConfig:
behavior:
scaleDown:
{{- toYaml $scaleDown | nindent 10 }}
scaleUp:
{{- toYaml $scaleUp | nindent 10 }}
{{- end }}
{{- $triggers := required "Undefined triggers for ScaledObject" .Values.scaledObject.triggers }}
triggers:
{{- tpl (toYaml $triggers) $ | nindent 4 }}
{{- end }}
1 change: 1 addition & 0 deletions application/templates/valuesTpl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{- include "renderedValues" . }}
48 changes: 48 additions & 0 deletions application/values-sb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Deployment settings
deployment:
enabled: true

# scaledObjectAdvancedSb settings
scaledObject:
enabled: true
pollingInterval: 30
cooldownPeriod: 300
minReplicaCount: 1
maxReplicaCount: 5
idleReplicaCount: 0
fallback:
failureThreshold: 3
replicas: 2

# Optional - use advanced scaling
advanced:
# ScaleDown configuration
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 100
periodSeconds: 15
selectPolicy: Max

# ScaleUp configuration
scaleUp:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 100
periodSeconds: 15
selectPolicy: Max

# Triggers for scaling
triggers:
- type: rabbitmq
metadata:
protocol: amqp
queueName: sepolia-identifiers
mode: QueueLength
value: "20"
authenticationRef:
kind: ClusterTriggerAuthentication
name: rabbitmq-trigger-auth
# Define additional triggers as needed
Loading