istio.io/istio@v0.0.0-20240520182934-d79c90f27776/tests/integration/create_cluster_gke.sh (about)

     1  #!/bin/bash
     2  
     3  # Copyright Istio 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  #
    18  # Creates and configures a GKE cluster for running the Istio e2e tests.
    19  # Notes:
    20  # * See README.md
    21  #
    22  
    23  PROJECT=${PROJECT:-$(gcloud config list --format 'value(core.project)' 2>/dev/null)}
    24  ZONE=${ZONE:-us-central1-f}
    25  CLUSTER_NAME=${CLUSTER_NAME:-istio-e2e}
    26  MACHINE_TYPE=${MACHINE_TYPE:-n1-standard-4}
    27  NUM_NODES=${NUM_NODES:-3}
    28  # Store the previous value (which may have been unset) so we can restore it on cleanup
    29  OLD_USE_CLIENT_CERT=$(gcloud config list 2>/dev/null | grep use_client_certificate | cut -d' ' -f3)
    30  
    31  function usage() {
    32    echo "${0} -p PROJECT [-z ZONE] [-c CLUSTER_NAME] [-v CLUSTER_VERSION] [-m MACHINE_TYPE] [-n NUM_NODES]"
    33    echo ''
    34    # shellcheck disable=SC2016
    35    echo '  -p: Specifies the GCP Project name. (defaults to $PROJECT_NAME, or current GCP project if unspecified).'
    36    # shellcheck disable=SC2016
    37    echo '  -z: Specifies the zone. (defaults to $ZONE, or "us-central1-f").'
    38    # shellcheck disable=SC2016
    39    echo '  -c: Specifies the cluster name. (defaults to $CLUSTER_NAME, or "istio-e2e").'
    40    # shellcheck disable=SC2016
    41    echo '  -v: Specifies the cluster version. (defaults to $CLUSTER_VERSION, or GCP default if unspecified ).'
    42    # shellcheck disable=SC2016
    43    echo '  -m: Specifies the machine type. (defaults to $MACHINE_TYPE, or "n1-standard-4").'
    44    # shellcheck disable=SC2016
    45    echo '  -n: Specifies the number of nodes. (defaults to $NUM_NODES, or "3").'
    46    echo ''
    47  }
    48  
    49  # Allow command-line args to override the defaults.
    50  while getopts ":p:z:c:v:m:n:h" opt; do
    51    case ${opt} in
    52      p)
    53        PROJECT=${OPTARG}
    54        ;;
    55      z)
    56        ZONE=${OPTARG}
    57        ;;
    58      c)
    59        CLUSTER_NAME=${OPTARG}
    60        ;;
    61      v)
    62        CLUSTER_VERSION=${OPTARG}
    63        ;;
    64      m)
    65        MACHINE_TYPE=${OPTARG}
    66        ;;
    67      n)
    68        NUM_NODES=${OPTARG}
    69        ;;
    70      h)
    71        usage
    72        exit 0
    73        ;;
    74      \?)
    75        echo "Invalid option: -$OPTARG" >&2
    76        usage
    77        exit 1
    78        ;;
    79    esac
    80  done
    81  
    82  if [[ -z "${PROJECT}" ]]; then
    83    echo "Error: PROJECT (-p) must be specified!"
    84    usage
    85    exit 1
    86  fi
    87  
    88  
    89  
    90  set -o errexit
    91  set -o nounset
    92  set -o pipefail
    93  set -x # echo on
    94  
    95  function cleanup {
    96    # Reset certificate config.
    97    if [ -z "$OLD_USE_CLIENT_CERT" ]; then
    98      gcloud config unset container/use_client_certificate
    99    else
   100      gcloud config set container/use_client_certificate "$OLD_USE_CLIENT_CERT"
   101    fi
   102  }
   103  
   104  # Run cleanup before we exit.
   105  trap cleanup EXIT
   106  
   107  # Create the cluster
   108  gcloud container clusters create "$CLUSTER_NAME" \
   109    --project="$PROJECT" \
   110    --cluster-version="$CLUSTER_VERSION" \
   111    --zone="$ZONE" \
   112    --machine-type="$MACHINE_TYPE" \
   113    --num-nodes="$NUM_NODES" \
   114    --no-enable-legacy-authorization
   115  
   116  # This is a hack to handle the case where clusterrolebinding creation returns:
   117  #
   118  # Error from server (Forbidden): clusterrolebindings.rbac.authorization.k8s.io is forbidden: User "client" cannot
   119  # create clusterrolebindings.rbac.authorization.k8s.io at the cluster scope
   120  gcloud config set container/use_client_certificate False
   121  
   122  # Download the credentials for the cluster.
   123  gcloud container clusters get-credentials "$CLUSTER_NAME" --project="$PROJECT" --zone="$ZONE"
   124  
   125  # Grant the current user admin privileges on the cluster.
   126  kubectl create clusterrolebinding cluster-admin-binding --clusterrole=cluster-admin --user="$(gcloud config get-value core/account)"