From 27d0fbfa76d4a8168141fbeaf0ce47788648a333 Mon Sep 17 00:00:00 2001 From: Tope Emmanuel Date: Fri, 6 Oct 2023 13:23:14 +0100 Subject: [PATCH] added dfi sign in to review --- Makefile | 8 +- script/get_next_mapping_aks.sh | 211 ++++++++------------------- script/get_next_mapping_aks_debug.sh | 152 ------------------- terraform/aks/application.tf | 3 + terraform/aks/ingress.tf | 24 --- terraform/aks/variables.tf | 9 ++ 6 files changed, 79 insertions(+), 328 deletions(-) delete mode 100755 script/get_next_mapping_aks_debug.sh delete mode 100644 terraform/aks/ingress.tf diff --git a/Makefile b/Makefile index 3b6d7c5ebd..a60c73d34a 100644 --- a/Makefile +++ b/Makefile @@ -58,17 +58,19 @@ review: $(eval export TF_VAR_paas_application_name=${PR_NAME}) cf target -s ${SPACE_NAME} cf delete-orphaned-routes -f - $(eval export TF_VAR_static_route=$(shell script/get_next_mapping.sh ${PR_NAME})) + $(eval export static_route=$(shell script/get_next_mapping.sh ${PR_NAME})) + $(error TF_VAR_static_route ${static_route}) $(eval BACKEND_KEY=-backend-config=key=${PR_NAME}.tfstate) .PHONY: review_aks review_aks: $(eval include global_config/review.sh) $(if $(PR_NUMBER), , $(error Missing environment variable "PR_NUMBER")) - $(eval export PR_NAME=review-school-experience-${PR_NUMBER}) - $(eval export TF_VAR_static_route=$(shell script/get_next_mapping_aks.sh ${PR_NAME})) + $(eval export PR_NAME=get-school-experience-review-pr-${PR_NUMBER}.test.teacherservices.cloud) + $(eval export TF_VAR_dsi_hostname=$(shell script/get_next_mapping_aks.sh ${PR_NUMBER} ${PR_NAME} VERBOSE=1)) $(eval export TF_VAR_environment=review-pr-$(PR_NUMBER)) + .PHONY: staging staging: $(eval export DEPLOY_ENV=staging) diff --git a/script/get_next_mapping_aks.sh b/script/get_next_mapping_aks.sh index bf3bec2af5..a2606e67c8 100755 --- a/script/get_next_mapping_aks.sh +++ b/script/get_next_mapping_aks.sh @@ -1,166 +1,79 @@ -# Input variables pr_number=${1} +pr_name=${2} +minimun_ing_num=1 +maximun_ing_num=20 -##########################new code starts###################################### - -# check_existing_ingress "${numbers_from_ings1[@]}" -check_existing_ingress() { - local current="get-school-experience-review-pr-2973.test.teacherservices.cloud" - +get_all_relevant_ingresses() { # Find if it is already in the list of ingresses - INGS=($(kubectl get ing -n git-development -o json | \ + ings=($(kubectl get ing -n git-development -o json | \ jq -r '.items[] | select(.metadata.name | startswith("get-school-experience-review-pr")) | .metadata.name')) - - ings1_list=() - - for ((i = 0; i < ${#INGS[@]}; i += 1)); do - itemname="${INGS[i]}" - ings1_list+=("$itemname") # Add itemname to the list - if [ "$itemname" == "$current" ]; then - echo "Found existing ${itemname} ${backend_service} " - fi + ings_list=() + for ((i = 0; i < ${#ings[@]}; i += 1)); do + itemname="${ings[i]}" + ings_list+=("$itemname") # Add itemname to the list done - - echo "ings1_list from the function is: ${ings1_list[@]}" + echo "${ings_list[@]}" } -extract_numbers_from_list() { - local input_strings=("$@") - local prefix="review-get-into-teaching-app-" - local suffix="-internal" - local numbers=() - - for input_string in "${input_strings[@]}"; do - if [[ "$input_string" =~ "${prefix}([0-9]+)${suffix}" ]]; then - numbers+=("${BASH_REMATCH[1]}") - else - numbers+=("Number not found") - fi - done - - echo "${numbers[@]}" +check_existing_dsi_ingress() { + # Find if it is already in the list of existing linked DSI ingresses + servicename="get-school-experience-review-pr-${pr_number}" + ings=($(kubectl get ing -o=custom-columns='NAME:.metadata.name,SVCs:..service.name' -n git-development | grep "${servicename}" | grep -v "${pr_name}")) + echo "$ings" } -get_ings1_list() { - # Find if it is already in the list of ingresses - INGS=($(kubectl get ing -n git-development -o json | \ - jq -r '.items[] | select(.metadata.name | startswith("get-school-experience-review-pr")) | .metadata.name, .spec.rules[0].http.paths[0].backend.service.name')) - - ings1_list=() - - for ((i = 1; i < ${#INGS[@]}; i += 1)); do - ings1_list+=("${INGS[i]}") - done - - echo "${ings1_list[@]}" -} - -number_exists_in_list() { - local number_to_check="$1" - shift - local number_list=("$@") - - for number in "${number_list[@]}"; do - if [ "$number_to_check" -eq "$number" ]; then - return 0 # Number found - fi - done - - return 1 # Number not found -} - -# Example usage: extract_numbers_from_list() { - local input_strings=("$@") - local prefix="get-school-experience-review-pr-" - local suffix=".test.teacherservices.cloud" - local numbers=() - - for input_string in "${input_strings[@]}"; do - if [[ "$input_string" =~ "${prefix}([0-9]+)${suffix}" ]]; then - numbers+=("${BASH_REMATCH[1]}") - else - numbers+=("Number not found") - fi - done - - echo "${numbers[@]}" -} - -number_exists_in_list() { - local number_to_check="$1" - shift - local number_list=("$@") - - for number in "${number_list[@]}"; do - if [ "$number_to_check" -eq "$number" ]; then - return 0 # Number found - fi - done - - return 1 # Number not found -} - -# Example usage with extract_numbers_from_list and number_exists_in_list -# string_list=("review-get-into-teaching-app-2619-internal" "review-get-into-teaching-app-2685-internal" "review-get-into-teaching-app-2632-internal") - -# # Extract numbers from the string list -# numbers_from_list=($(extract_numbers_from_list "${string_list[@]}")) - -# Number to find -number_to_find=$pr_number - -# Check if the number exists in the extracted list -# if number_exists_in_list "$number_to_find" "${numbers_from_list[@]}"; then -# echo "$number_to_find exists in the list." -# else -# echo "$number_to_find does not exist in the list." -# fi - - -# Get the INGS[1] values -# ings1_list=$(check_existing_ingress) - #echo "ings1_list is $ings1_list" -# # Use ings1_list as input to extract_numbers_from_list - #numbers_from_ings1=$(extract_numbers_from_list "${ings1_list[@]}") -# echo "=============================================================" -# echo "numbers_from_ings1 is $numbers_from_ings1" - -# # Call the check_existing_ingress function with numbers_from_ings1 as input - - -# Call the function -#check_existing_ingress -extract_numbers_from_list() { - local input_strings=("$@") + local all_existing_ings=("$@") local pattern="get-school-experience-review-pr-([0-9]+)\.test\.teacherservices\.cloud" - local numbers=() - - for input_string in "${input_strings[@]}"; do + local all_existing_review_ings=() + for input_string in "${all_existing_ings[@]}"; do if [[ "$input_string" =~ $pattern ]]; then - numbers+=("${BASH_REMATCH[1]}") + all_existing_review_ings+=("${BASH_REMATCH[1]}") else - numbers+=("Number not found") + all_existing_review_ings+=("Number not found") fi done - - echo "${numbers[@]}" + echo "${all_existing_review_ings[@]}" } -# Example usage with the input strings -input_strings=( - "get-school-experience-review-pr-2972.test.teacherservices.cloud" - "get-school-experience-review-pr-2973.test.teacherservices.cloud" - "get-school-experience-review-pr-777.test.teacherservices.cloud" -) - -# Extract numbers from the input strings -numbers_from_list=($(extract_numbers_from_list "${input_strings[@]}")) - -# Print the extracted numbers -for number in "${numbers_from_list[@]}"; do - echo "$number" -done - - -# ############################END NEW CODE#################################### +# check if there is an existing linked ingress +existing_ing=($(check_existing_dsi_ingress)) + +next_ingress="" +if [ -z "$existing_ing" ]; then + # get all existing ingresses + ings_list_result=($(get_all_relevant_ingresses)) + + # extract the number part of the ingresses from ingress lists + numbers_from_ings=($(extract_numbers_from_list "${ings_list_result[@]}")) + available_numbers=() + unavailable_numbers=() + all_possible_ings_pr_num=() + for ((i = $minimun_ing_num; i <= $maximun_ing_num; i++)); do + all_possible_ings_pr_num+=("$i") + done + for possible_ings_pr_num in "${all_possible_ings_pr_num[@]}"; do + found=false + for ((i = 0; i < ${#numbers_from_ings[@]}; i += 1)); do + itemname="${numbers_from_ings[i]}" + if [ "$itemname" == "$possible_ings_pr_num" ]; then + unavailable_numbers+=("$possible_ings_pr_num") + found=true + break + fi + done + if [ "$found" = false ]; then + available_numbers+=("$possible_ings_pr_num") + fi + done + if [ ${#available_numbers[@]} -gt 0 ]; then + sorted_numbers=($(printf "%s\n" "${available_numbers[@]}" | sort -n)) + selectednumber=${sorted_numbers[0]} + next_ingress="get-school-experience-review-pr-$selectednumber.test.teacherservices.cloud" + else + next_ingress="" + fi + else + next_ingress="$existing_ing" + fi +echo "$next_ingress" diff --git a/script/get_next_mapping_aks_debug.sh b/script/get_next_mapping_aks_debug.sh deleted file mode 100755 index c5de0435a4..0000000000 --- a/script/get_next_mapping_aks_debug.sh +++ /dev/null @@ -1,152 +0,0 @@ -# Input variables -pr_number=${1} - -##########################new code starts###################################### -check_existing_ingress() { - local current="get-school-experience-review-pr-2973.test.teacherservices.cloud" - - # Find if it is already in the list of ingresses - INGS=($(kubectl get ing -n git-development -o json | \ - jq -r '.items[] | select(.metadata.name | startswith("get-school-experience-review-pr")) | .metadata.name')) - - ings1_list=() - - for ((i = 0; i < ${#INGS[@]}; i += 1)); do - itemname="${INGS[i]}" - ings1_list+=("$itemname") # Add itemname to the list - # if [ "$itemname" == "$current" ]; then - # echo "Found existing ${itemname} ${backend_service} " - # fi - done - echo "${ings1_list[@]}" -} - -extract_numbers_from_list() { - local input_strings=("$@") - local pattern="get-school-experience-review-pr-([0-9]+)\.test\.teacherservices\.cloud" - local numbers=() - - for input_string in "${input_strings[@]}"; do - echo "input_string is $input_string" - if [[ "$input_string" =~ $pattern ]]; then - numbers+=("${BASH_REMATCH[1]}") - else - numbers+=("Number not found") - fi - done - - echo "${numbers[@]}" -} - -find_largest_not_in_result() { - local input_list=("$@") - local available_numbers=($(seq 1 20)) - echo "input_list is $input_list" - new_numbers=() - numbers=() - for ((i = 1; i <= 20; i++)); do - numbers+=("$i") - done - - - for number in "${numbers[@]}"; do - # if [ "$number" -ne "$number_to_remove" ]; then - # new_numbers+=("$number") - # fi - #inner loop - - found=false - for n in $input_list; do - echo "n is $n is in list" - if [ "$n" -eq "$number" ]; then - found=true - break - fi - done - - if [ "$found" = false ]; then - echo "$number number not in list" - new_numbers+=("$number") - fi - - # for input_list_number in "${input_list[@]}"; do - # # Remove any number found in the input list from the available_numbers array - # if [ "$number" -ne "$number_to_remove" ]; then - # new_numbers+=("$number") - # fi - done - - if [ ${#new_numbers[@]} -gt 0 ]; then - # Sort the remaining available numbers and return the minimum - sorted_numbers=($(printf "%s\n" "${new_numbers[@]}" | sort -n)) - echo "${sorted_numbers[0]}" - else - # All numbers from 1 to 20 are in the input list, so return 0 - echo "0" - fi - # done - # new_numbers=() - # echo "available numbers beginning $available_numbers" - # for input_list_number in "${input_list[@]}"; do - # # Remove any number found in the input list from the available_numbers array - # available_numbers=(${available_numbers[@]/$number}) - # if [ "$number" -ne "$number_to_remove" ]; then - # new_numbers+=("$number") - # fi - # done - - # if [ ${#available_numbers[@]} -gt 0 ]; then - # # Sort the remaining available numbers and return the minimum - # sorted_numbers=($(printf "%s\n" "${available_numbers[@]}" | sort -n)) - # echo "${sorted_numbers[0]}" - # else - # # All numbers from 1 to 20 are in the input list, so return 0 - # echo "0" - # fi -} - -calculate_return_value() { - local numbers_from_ings1=("$@") - local largest_not_in_result=$(find_largest_not_in_result "${numbers_from_ings1[@]}") - - if [ -z "$largest_not_in_result" ]; then - echo "" - else - echo "get-school-experience-review-pr-$largest_not_in_result.test.teacherservices.cloud" - fi -} - -# Call the function -ings1_list_result=($(check_existing_ingress)) - -echo "ings1_list_result is $ings1_list_result" -echo "======" -# Pass ings1_list_result as input to extract_numbers_from_list -numbers_from_ings1=($(extract_numbers_from_list "${ings1_list_result[@]}")) -numbers_from_ings1+=(1 2 3 5) -# find out if pr number is in extracted number -existing="" -returnval="" -for number in "${numbers_from_ings1[@]}"; do - echo "$number" - if [ "$number" == "$pr_number" ]; then - echo "Found existing ${number}" - existing=$number - fi -done - -if [ -z "$existing" ]; then - - echo "Existing is still empty after the loop." - largest_not_in_result=$(find_largest_not_in_result "${numbers_from_ings1[@]}") - echo "largest_not_in_result is $largest_not_in_result" - if [ -z "$largest_not_in_result" ]; then - returnval="" - else - returnval="get-school-experience-review-pr-$largest_not_in_result.test.teacherservices.cloud" - fi -else - echo "Existing is not empty so we have an existing" - returnval="get-school-experience-review-pr-$pr_number.test.teacherservices.cloud" -fi -echo "$returnval" diff --git a/terraform/aks/application.tf b/terraform/aks/application.tf index a9cf990aeb..102ddf974e 100644 --- a/terraform/aks/application.tf +++ b/terraform/aks/application.tf @@ -1,3 +1,5 @@ + + module "application_configuration" { source = "./vendor/modules/aks//aks/application_configuration" @@ -39,6 +41,7 @@ module "web_application" { docker_image = var.docker_image command = ["/app/docker-entrypoint.sh", "-m", "-f"] probe_path = null + web_external_hostnames = local.web_external_hostnames } module "worker_application" { diff --git a/terraform/aks/ingress.tf b/terraform/aks/ingress.tf deleted file mode 100644 index 696e5f7e07..0000000000 --- a/terraform/aks/ingress.tf +++ /dev/null @@ -1,24 +0,0 @@ -resource "kubernetes_ingress_v1" "example" { - wait_for_load_balancer = true - metadata { - name = "example" - } - spec { - ingress_class_name = "nginx" - rule { - http { - path { - path = "/*" - backend { - service { - name = kubernetes_service_v1.example.metadata.0.name - port { - number = 80 - } - } - } - } - } - } - } -} diff --git a/terraform/aks/variables.tf b/terraform/aks/variables.tf index 3020b94778..271c5f1acb 100644 --- a/terraform/aks/variables.tf +++ b/terraform/aks/variables.tf @@ -48,6 +48,13 @@ variable "sidekiq_replicas" { description = "number of replicas of the sidekiq" } +variable "dsi_hostname" { + description = "the static hostname for dsi " + default = "" +} + + + variable "external_url" { default = null @@ -144,4 +151,6 @@ locals { postgres_ssl_mode = var.enable_postgres_ssl ? "require" : "disable" app_name_suffix = var.app_name == null ? var.environment : var.app_name + + web_external_hostnames = var.dsi_hostname =="" ? [] : [var.dsi_hostname] }