sigs.k8s.io/cluster-api-provider-azure@v1.14.3/hack/util.sh (about) 1 #!/usr/bin/env bash 2 3 # Copyright 2021 The Kubernetes Authors. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 set -o errexit 18 set -o nounset 19 set -o pipefail 20 21 CURL_RETRIES=3 22 23 capz::util::get_latest_ci_version() { 24 release="${1}" 25 ci_version_url="https://dl.k8s.io/ci/latest-${release}.txt" 26 if ! curl --retry "${CURL_RETRIES}" -fL "${ci_version_url}" > /dev/null; then 27 ci_version_url="https://dl.k8s.io/ci/latest.txt" 28 fi 29 curl --retry "${CURL_RETRIES}" -sSL "${ci_version_url}" 30 } 31 32 capz::util::should_build_kubernetes() { 33 if [[ -n "${TEST_K8S:-}" ]]; then 34 echo "true" && return 35 fi 36 # JOB_TYPE, REPO_OWNER, and REPO_NAME are environment variables set by a prow job - 37 # https://github.com/kubernetes/test-infra/blob/master/prow/jobs.md#job-environment-variables 38 if [[ "${JOB_TYPE:-}" == "presubmit" ]] && [[ "${REPO_OWNER:-}/${REPO_NAME:-}" == "kubernetes/kubernetes" ]]; then 39 echo "true" && return 40 fi 41 echo "false" 42 } 43 44 capz::util::should_build_ccm() { 45 # TEST_CCM is an environment variable set by a prow job to indicate that the CCM should be built and tested. 46 if [[ -n "${TEST_CCM:-}" ]]; then 47 echo "true" && return 48 fi 49 # If testing a custom Kubernetes version, CCM should be built. 50 if [[ "$(capz::util::should_build_kubernetes)" == "true" ]]; then 51 echo "true" && return 52 fi 53 # If using Kubernetes CI artifacts, CCM should be built. 54 if [[ "${E2E_ARGS:-}" == "-kubetest.use-ci-artifacts" ]]; then 55 echo "true" && return 56 fi 57 # If the Kubernetes version contains "latest", CCM should be built. 58 if [[ "${KUBERNETES_VERSION:-}" =~ "latest" ]]; then 59 echo "true" && return 60 fi 61 echo "false" 62 } 63 64 # all test regions must support AvailabilityZones 65 capz::util::get_random_region() { 66 local REGIONS=("canadacentral" "eastus" "eastus2" "northeurope" "uksouth" "westeurope" "westus2" "westus3") 67 echo "${REGIONS[${RANDOM} % ${#REGIONS[@]}]}" 68 } 69 # all regions below must have GPU availability for the chosen GPU VM SKU 70 capz::util::get_random_region_gpu() { 71 local REGIONS=("eastus" "eastus2" "northeurope" "uksouth" "westeurope" "westus2") 72 echo "${REGIONS[${RANDOM} % ${#REGIONS[@]}]}" 73 } 74 # all regions below must support ExtendedLocation 75 capz::util::get_random_region_edgezone() { 76 local REGIONS=("canadacentral") 77 echo "${REGIONS[${RANDOM} % ${#REGIONS[@]}]}" 78 } 79 80 capz::util::generate_ssh_key() { 81 # Generate SSH key. 82 AZURE_SSH_PUBLIC_KEY_FILE=${AZURE_SSH_PUBLIC_KEY_FILE:-""} 83 if [ -z "${AZURE_SSH_PUBLIC_KEY_FILE}" ]; then 84 echo "generating sshkey for e2e" 85 AZURE_SSH_KEY="${PWD}"/.sshkey 86 # This is required if e2e.test is run with --provider=skeleton. This option is used when 87 # running Windows e2e tests against a CAPZ cluster to ensure no extra Azure resources are 88 # created by e2e.test. 89 # Ref: https://github.com/kubernetes-sigs/windows-testing/blob/be73dd57a551be4f9527e7a3a3e6491e86cae6d2/capz/run-capz-e2e.sh#L300 90 # Further more, --provider is what decides which SSH environment variable is used to create 91 # the signer needed to SSH into nodes. 92 # Ref: https://github.com/kubernetes/kubernetes/blob/41890534532931742770a7dc98f78bcdc59b1a6f/test/e2e/framework/ssh/ssh.go#L58 93 KUBE_SSH_KEY="${AZURE_SSH_KEY}" 94 rm -f "${AZURE_SSH_KEY}" 2>/dev/null 95 ssh-keygen -t rsa -b 2048 -f "${AZURE_SSH_KEY}" -N '' 1>/dev/null 96 AZURE_SSH_PUBLIC_KEY_FILE="${AZURE_SSH_KEY}.pub" 97 # This is needed to run tests that required SSH access to nodes 98 export AZURE_SSH_KEY 99 export KUBE_SSH_KEY 100 fi 101 AZURE_SSH_PUBLIC_KEY_B64=$(base64 < "${AZURE_SSH_PUBLIC_KEY_FILE}" | tr -d '\r\n') 102 export AZURE_SSH_PUBLIC_KEY_B64 103 # Windows sets the public key via cloudbase-init which take the raw text as input 104 AZURE_SSH_PUBLIC_KEY=$(tr -d '\r\n' < "${AZURE_SSH_PUBLIC_KEY_FILE}") 105 export AZURE_SSH_PUBLIC_KEY 106 } 107 108 capz::util::ensure_azure_envs() { 109 : "${AZURE_SUBSCRIPTION_ID:?Environment variable empty or not defined.}" 110 : "${AZURE_TENANT_ID:?Environment variable empty or not defined.}" 111 : "${AZURE_CLIENT_ID:?Environment variable empty or not defined.}" 112 : "${AZURE_CLIENT_SECRET:?Environment variable empty or not defined.}" 113 }