| garciadeblas | 8d8cd99 | 2024-05-21 16:04:14 +0200 | [diff] [blame] | 1 | ####################################################################################### |
| 2 | # Copyright ETSI Contributors and Others. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 13 | # implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | ####################################################################################### |
| 17 | |
| 18 | # Retrieves Gitea connection info data |
| 19 | |
| 20 | # Helper function to monitor progress of a condition |
| 21 | function monitor_condition() { |
| 22 | local CONDITION="$1" # Function with the condition |
| 23 | local MESSAGE="${2:-}" # Message during each check |
| 24 | local TIMEOUT="${3:-300}" # Timeout, in seconds (default: 5 minutes) |
| 25 | local STEP="${4:-2}" # Polling period (default: 2 seconds) |
| 26 | |
| 27 | until "${CONDITION}" || [ ${TIMEOUT} -le 0 ] |
| 28 | do |
| 29 | echo -en "${MESSAGE}" |
| 30 | |
| 31 | ((TIMEOUT-=${STEP})) |
| 32 | |
| 33 | sleep "${STEP}" |
| 34 | done |
| 35 | |
| 36 | "${CONDITION}" |
| 37 | } |
| 38 | |
| 39 | # Check that the IP associated to the Ingress service is available |
| 40 | function ingress_service_ip_available() { |
| 41 | kubectl get svc/ingress-nginx-controller -n ingress-nginx -o jsonpath='{.status.loadBalancer.ingress[0].ip}' > /dev/null |
| 42 | } |
| 43 | |
| 44 | # Wait until the IP associated to the Ingress service is available |
| 45 | function wait_until_ingress_service_ip_available() { |
| 46 | monitor_condition ingress_service_ip_available "External IP address for Ingress not ready yet...\n" 300 5 |
| 47 | if [[ $? -ne 0 ]] |
| 48 | then |
| 49 | echo -e "\nFATAL: Timeout waiting for external IP address for Ingress to be ready. ABORTED.\n" |
| 50 | exit 1 |
| 51 | fi |
| 52 | } |
| 53 | |
| 54 | # Internal services and ports |
| 55 | export GITEA_INTERNAL_HTTP_IP=gitea-http.gitea |
| 56 | export GITEA_INTERNAL_SSH_IP=gitea-ssh.gitea |
| 57 | export GITEA_HTTP_PORT=$(kubectl get svc/gitea-http -n gitea -o jsonpath='{.spec.ports[0].port}') |
| 58 | export GITEA_SSH_PORT=$(kubectl get svc/gitea-ssh -n gitea -o jsonpath='{.spec.ports[0].port}') |
| 59 | |
| 60 | # If applicable, gets recommended service IP addresses |
| 61 | ## SSH service |
| 62 | if [[ -n $(kubectl get svc/gitea-ssh -n gitea -o jsonpath='{.status.loadBalancer.ingress[0].ip}') ]] |
| 63 | then |
| 64 | # Retrieves the external IP address |
| 65 | export GITEA_SSH_IP=$(kubectl get svc/gitea-ssh -n gitea -o jsonpath='{.status.loadBalancer.ingress[0].ip}') || true |
| 66 | else |
| 67 | # Otherwise just uses the internal service name |
| 68 | export GITEA_SSH_IP=${GITEA_INTERNAL_SSH_IP} |
| 69 | fi |
| 70 | ## HTTP service |
| 71 | if [[ -n $(kubectl get svc/gitea-http -n gitea -o jsonpath='{.status.loadBalancer.ingress[0].ip}') ]] |
| 72 | then |
| 73 | # Retrieves the external IP addresses (if it exists) |
| 74 | export GITEA_HTTP_IP=$(kubectl get svc/gitea-http -n gitea -o jsonpath='{.status.loadBalancer.ingress[0].ip}') || true |
| 75 | # In case it is behind an Ingress |
| 76 | elif [[ -n $(kubectl get ingress/gitea -n gitea 2> /dev/null) ]] |
| 77 | then |
| 78 | # Waits until the external IP address is available |
| 79 | echo "Waiting until the Ingress service IP address is available..." |
| 80 | wait_until_ingress_service_ip_available |
| 81 | |
| 82 | # Retrieves the external IP address of the Ingress service |
| 83 | export GITEA_HTTP_IP=$(kubectl get svc/ingress-nginx-controller -n ingress-nginx -o jsonpath='{.status.loadBalancer.ingress[0].ip}') || true |
| 84 | echo "Got it: ${GITEA_HTTP_IP}" |
| 85 | else |
| 86 | # Otherwise just uses the internal service name |
| 87 | export GITEA_HTTP_IP=${GITEA_INTERNAL_HTTP_IP} |
| 88 | fi |
| 89 | |
| 90 | # Applicable URLs |
| 91 | export GITEA_HTTP_HOST_DOMAIN="git.${GITEA_HTTP_IP}.nip.io" |
| 92 | export GITEA_HTTP_URL="http://git.${GITEA_HTTP_IP}.nip.io" |
| 93 | export GITEA_SSH_URL="git.${GITEA_SSH_IP}.nip.io" |
| 94 | export GITEA_INTERNAL_HTTP_URL="http://${GITEA_INTERNAL_HTTP_IP}" |
| 95 | export GITEA_INTERNAL_SSH_URL="${GITEA_INTERNAL_SSH_IP}" |
| 96 | |
| 97 | # Add explicit ports if required |
| 98 | if [[ "${GITEA_HTTP_PORT}" != 80 ]] |
| 99 | then |
| 100 | export GITEA_INTERNAL_HTTP_URL="${GITEA_INTERNAL_HTTP_URL}:${GITEA_HTTP_PORT}" |
| 101 | |
| 102 | # If it is not behind an Ingress, the port will be the original one, not necessarily 80 |
| 103 | if [[ -z $(kubectl get ingress/gitea -n gitea 2> /dev/null) ]] |
| 104 | then |
| 105 | export GITEA_HTTP_URL="${GITEA_HTTP_URL}:${GITEA_HTTP_PORT}" |
| 106 | fi |
| 107 | fi |
| 108 | |
| 109 | # Add port to SSH URL if needed |
| 110 | export GITEA_SSH_SERVER="${GITEA_SSH_URL}" |
| 111 | export GITEA_INTERNAL_SSH_SERVER="${GITEA_INTERNAL_SSH_URL}" |
| 112 | if [[ "${GITEA_SSH_PORT}" != 22 ]] |
| 113 | then |
| 114 | export GITEA_SSH_URL="${GITEA_SSH_URL}:${GITEA_SSH_PORT}" |
| 115 | export GITEA_INTERNAL_SSH_URL="${GITEA_INTERNAL_SSH_URL}:${GITEA_SSH_PORT}" |
| 116 | fi |
| 117 | |
| 118 | # Complete the SSH URLs to avoid ambiguity |
| 119 | export GITEA_SSH_URL="ssh://git@${GITEA_SSH_URL}" |
| 120 | export GITEA_INTERNAL_SSH_URL="ssh://git@${GITEA_INTERNAL_SSH_URL}" |