Skip to content

Commit

Permalink
Get random region for Gardener cluster in backend switching job and c…
Browse files Browse the repository at this point in the history
…heck for is-PR-approved (#420)

* get random region for Gardener cluster in backend switching job

* added check for is PR approved for backend switching job
  • Loading branch information
mfaizanse authored Jan 19, 2024
1 parent 3c84f14 commit 342ee27
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 5 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/e2e-backend-switching-reuseable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ jobs:
env:
GARDENER_CLUSTER_VERSION: ${{ inputs.kube-version }}
CLUSTER_PREFIX: ${{ inputs.cluster-name-prefix }}
GARDENER_REGION: "eu-west-1"
GARDENER_ZONES: "eu-west-1a"
GARDENER_PROJECT_NAME: ${{ vars.GARDENER_PROJECT_NAME }}
GARDENER_PROVIDER_SECRET_NAME: ${{ vars.GARDENER_PROVIDER_SECRET_NAME_AWS }}
MACHINE_TYPE: "c4.xlarge"
Expand All @@ -60,6 +58,9 @@ jobs:
# generate cluster name and export it to Github env for cleanup step to access it.
export CLUSTER_NAME="${CLUSTER_PREFIX}$(openssl rand -hex 2)"
echo "CLUSTER_NAME=${CLUSTER_NAME}" >> $GITHUB_ENV
# set random region for AWS.
export GARDENER_REGION=$(./scripts/gardener/aws/get_random_region.sh)
export GARDENER_ZONES="${GARDENER_REGION}a"
# provision gardener cluster.
make -C hack/ci/ provision-gardener-cluster
kubectl version
Expand Down
11 changes: 11 additions & 0 deletions .github/workflows/pull-target-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,19 @@ on:
- "sec-scanners-config.yaml"

jobs:
is-pr-approved:
runs-on: ubuntu-latest
steps:
- name: Check if PR approved
env:
GH_TOKEN: ${{ github.token }}
PR_URL: https://github.com/kyma-project/eventing-manager/pull/${{ github.event.number }}
run: |
./scripts/is_pr_approved.sh
wait-until-build-succeeds:
runs-on: ubuntu-latest
needs: is-pr-approved
steps:
- name: Wait for the 'pull-eventing-manager-build' job to succeed
id: pull-wait-build
Expand Down
28 changes: 28 additions & 0 deletions scripts/gardener/aws/get_random_region.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash

# This script returns a random AWS region name for Gardener cluster.
# Usage: No parameters are required to run this script. Simply call this script to get the region name.

source "$(pwd)/scripts/utils/utils.sh"

# standard bash error handling
set -o nounset # treat unset variables as an error and exit immediately.
set -E # needs to be set if we want the ERR trap.
set -o pipefail # prevents errors in a pipeline from being masked.
set -o errexit # exit immediately when a command fails.

# only lists the regions where machine type: `c1.xlarge` is available.
AWS_REGIONS=(
"eu-west-1"
"us-east-1"
"us-west-1"
"us-west-2"
"sa-east-1"
"ap-northeast-1"
)

## MAIN Logic
# NOTE: This script should only echo the result.
RAND_INDEX=$(utils::generate_random_number 0 $(( ${#AWS_REGIONS[@]} - 1 )))
# print the random region name
echo ${AWS_REGIONS[$RAND_INDEX]}
14 changes: 12 additions & 2 deletions scripts/gardener/aws/provision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,22 @@ gardener::validate_and_default() {
if [ -z "$GARDENER_CLUSTER_VERSION" ]; then
# grep the default kube-version defined in kyma CLI.
export GARDENER_CLUSTER_VERSION="$(${KYMA_CLI} provision gardener aws --help | grep "kube-version string" | awk -F "\"" '{print $2}')"
log::info "Using GARDENER_CLUSTER_VERSION=${GARDENER_CLUSTER_VERSION}"
fi

# print configurations for debugging purposes:
log::banner "Configurations:"
echo "CLUSTER_NAME: ${CLUSTER_NAME}"
echo "GARDENER_REGION: ${GARDENER_REGION}"
echo "GARDENER_ZONES: ${GARDENER_ZONES}"
echo "MACHINE_TYPE: ${MACHINE_TYPE}"
echo "SCALER_MIN: ${SCALER_MIN}"
echo "SCALER_MAX: ${SCALER_MAX}"
echo "GARDENER_CLUSTER_VERSION: ${GARDENER_CLUSTER_VERSION}"
echo "RETRY_ATTEMPTS ${RETRY_ATTEMPTS}"
}

gardener::provision_cluster() {
log::info "Provision cluster: \"${CLUSTER_NAME}\""
log::banner "Provision cluster: \"${CLUSTER_NAME}\""

# decreasing attempts to 2 because we will try to create new cluster from scratch on exit code other than 0
${KYMA_CLI} provision gardener aws \
Expand Down
24 changes: 24 additions & 0 deletions scripts/is_pr_approved.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

# This script will check if a PR is approved or not.
# Usage: To run this script, provide the following environment variable(s) and run this script.
# - PR_URL - (example: https://github.com/kyma-project/eventing-manager/pull/123)

# standard bash error handling
set -o nounset # treat unset variables as an error and exit immediately.
set -E # needs to be set if we want the ERR trap.
set -o pipefail # prevents errors in a pipeline from being masked.
set -o errexit # exit immediately when a command fails.

## MAIN Logic
echo "Checking approval status of : ${PR_URL}"
REVIEW_DECISION=$(gh pr view ${PR_URL} --json=reviewDecision | jq -r '.reviewDecision')

echo "REVIEW_DECISION: ${REVIEW_DECISION}"
if [[ ${REVIEW_DECISION} == "APPROVED" ]]; then
echo "Pull request is approved!"
exit 0
fi

echo "Error: Pull request is not approved!"
exit 1
20 changes: 19 additions & 1 deletion scripts/utils/utils.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

source "${PROJECT_ROOT}/scripts/utils/log.sh"
source "$(pwd)/scripts/utils/log.sh"

# utils::check_required_vars checks if all provided variables are initialized
# Arguments
Expand All @@ -18,3 +18,21 @@ function utils::check_required_vars() {
exit 1
fi
}

# utils::generate_random_number generates a random number between provided range.
# Arguments
# $1 - minimum
# $2 - maximum
# Example: utils::generate_random_number <minimum> <maximum>
function utils::generate_random_number() {
# NOTE: This method should only echo the result.
if [ "$#" -ne 2 ]
then
log::error "Invalid arguments. Usage: utils::generate_random_number <minimum> <maximum>"
exit 1
fi

local min=$1
local max=$2
echo $(awk -v seed=$RANDOM "BEGIN{srand(seed); print int(rand()*($max-$min+1))+$min}")
}

0 comments on commit 342ee27

Please sign in to comment.