diff --git a/.github/workflows/e2e-backend-switching-reuseable.yml b/.github/workflows/e2e-backend-switching-reuseable.yml index e81ea464..2a1caa63 100644 --- a/.github/workflows/e2e-backend-switching-reuseable.yml +++ b/.github/workflows/e2e-backend-switching-reuseable.yml @@ -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" @@ -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 diff --git a/.github/workflows/pull-target-e2e.yml b/.github/workflows/pull-target-e2e.yml index cf83ac74..c96f8935 100644 --- a/.github/workflows/pull-target-e2e.yml +++ b/.github/workflows/pull-target-e2e.yml @@ -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 diff --git a/scripts/gardener/aws/get_random_region.sh b/scripts/gardener/aws/get_random_region.sh new file mode 100755 index 00000000..934424e7 --- /dev/null +++ b/scripts/gardener/aws/get_random_region.sh @@ -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]} diff --git a/scripts/gardener/aws/provision.sh b/scripts/gardener/aws/provision.sh index c7f06ff8..412627fd 100755 --- a/scripts/gardener/aws/provision.sh +++ b/scripts/gardener/aws/provision.sh @@ -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 \ diff --git a/scripts/is_pr_approved.sh b/scripts/is_pr_approved.sh new file mode 100755 index 00000000..b62cc5e6 --- /dev/null +++ b/scripts/is_pr_approved.sh @@ -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 diff --git a/scripts/utils/utils.sh b/scripts/utils/utils.sh index d299bc5d..9ab17c26 100755 --- a/scripts/utils/utils.sh +++ b/scripts/utils/utils.sh @@ -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 @@ -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 +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 " + exit 1 + fi + + local min=$1 + local max=$2 + echo $(awk -v seed=$RANDOM "BEGIN{srand(seed); print int(rand()*($max-$min+1))+$min}") +}