github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/config/scripts/create_oke_multi_cluster.sh (about)

     1  #!/bin/bash
     2  #
     3  # Copyright (c) 2020, 2023, Oracle and/or its affiliates.
     4  # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     5  #
     6  
     7  SCRIPT_DIR=$(cd $(dirname "$0"); pwd -P)
     8  CLUSTER_COUNT=${1:-1}
     9  KUBECONFIG_DIR=$2
    10  INSTALL_CALICO=${3:-true}
    11  REQUIRED_VNC_COUNT=0
    12  REQUIRED_LB_COUNT=0
    13  CLUSTER_NAME_PREFIX="oke"
    14  MODULE_NAME_PREFIX="oke"
    15  
    16  check_for_resources() {
    17    local resource_type=$1
    18    local service_name=$2
    19    local limit_name=$3
    20    local min_required=$4
    21  
    22    local count=$(${SCRIPT_DIR}/get_resource_availability.sh $service_name $limit_name)
    23    local status_code=$?
    24    if [ ${status_code:-1} -eq 0 ]; then
    25      # OCI query succeeded, proceed with value evaluation
    26      if [ $count -lt $min_required ]; then
    27        echo "ERROR: Not enough ${resource_type}s available to create the OKE cluster : ${count}"
    28        exit 1
    29      elif [ $count -lt 5 ]; then
    30        echo "WARNING: Critically low number of ${resource_type}s available in tenancy: ${count}. Proceeding with creating the OKE cluster ..."
    31      else
    32        echo "Sufficient number of ${resource_type}s available for creating the OKE cluster: ${count}"
    33      fi
    34    else
    35      echo "ERROR: Query for available number of ${resource_type}s in tenancy failed."
    36      exit 1
    37    fi
    38  }
    39  
    40  if [ -z "$TF_VAR_compartment_id" ] ; then
    41      echo "TF_VAR_compartment_id env var must be set!"
    42      exit 1
    43  fi
    44  
    45  echo "Check OCI CLI is working..."
    46  # If OCI CLI is not configured correctly, the following command will have a non-zero return code
    47  # which will cause the job to fail at this point
    48  oci ce cluster list --compartment-id=${TF_VAR_compartment_id} --region=${TF_VAR_region} > /dev/null
    49  
    50  if [ -f /tmp/main.tf ]; then
    51    rm /tmp/main.tf
    52  fi
    53  
    54  # Create main.tf files for each cluster, and evaluate the VNC and LB counts
    55  for i in $(seq 1 $CLUSTER_COUNT)
    56  do
    57    let REQUIRED_VNC_COUNT=$REQUIRED_VNC_COUNT+1
    58    let REQUIRED_LB_COUNT=$REQUIRED_LB_COUNT+2
    59    cp ${SCRIPT_DIR}/terraform/cluster/multi_cluster_main_tf_template /tmp/main.tf.$i
    60    sed -i "s/MODULE_NAME/$MODULE_NAME_PREFIX-$i/g" /tmp/main.tf.$i
    61    sed -i "s/CLUSTER_NAME/$CLUSTER_NAME_PREFIX-$i/g" /tmp/main.tf.$i
    62  done
    63  
    64  echo "Minimum required VNC count: ${REQUIRED_VNC_COUNT}"
    65  echo "Minimum required Load Balancer count: ${REQUIRED_LB_COUNT}"
    66  
    67  rm -rf ${KUBECONFIG_DIR}/*
    68  
    69  # check available resources
    70  check_for_resources VCN vcn vcn-count $REQUIRED_VNC_COUNT
    71  check_for_resources LB load-balancer lb-flexible-count $REQUIRED_LB_COUNT
    72  
    73  cd ${SCRIPT_DIR}/terraform/cluster
    74  
    75  # Set whether Calico is to be installed or not by the OCI OKE TF provider
    76  export TF_VAR_calico_enabled="${INSTALL_CALICO}"
    77  export TF_VAR_calico_version="$(grep 'calico-version=' ${SCRIPT_DIR}/../../../../.third-party-test-versions | sed 's/calico-version=//g')"
    78  
    79  for i in $(seq 1 $CLUSTER_COUNT)
    80  do
    81    echo 'Create OKE cluster...'
    82    # Copy the temporary file as main.tf
    83    cat /tmp/main.tf.$i
    84    cp /tmp/main.tf.$i ${SCRIPT_DIR}/terraform/cluster/main.tf
    85  
    86    ./create-multi-cluster.sh $i $CLUSTER_NAME_PREFIX
    87    status_code=$?
    88  
    89    if [ ${status_code:-1} -eq 0 ]; then
    90      echo "Create kube config for cluster ${TF_VAR_label_prefix}-$CLUSTER_NAME_PREFIX-$i ..."
    91      CLUSTER_OCID=$(oci ce cluster list --compartment-id "${TF_VAR_compartment_id}" --name "${TF_VAR_label_prefix}-${CLUSTER_NAME_PREFIX}-$i" --lifecycle-state "ACTIVE" | jq -r '.data[0]."id"')
    92      echo "OCID of the cluster ${TF_VAR_label_prefix}-$CLUSTER_NAME_PREFIX-$i : ${CLUSTER_OCID}."
    93      mkdir -p "${KUBECONFIG_DIR}/$i"
    94      oci ce cluster create-kubeconfig --cluster-id ${CLUSTER_OCID} --file "${KUBECONFIG_DIR}/$i/kube_config" --region "${TF_VAR_region}" --token-version 2.0.0
    95      export KUBECONFIG="${KUBECONFIG_DIR}/$i/kube_config"
    96      # Adding a Service Account Authentication Token to kubeconfig
    97      # https://docs.cloud.oracle.com/en-us/iaas/Content/ContEng/Tasks/contengaddingserviceaccttoken.htm
    98      ${SCRIPT_DIR}/update_oke_kubeconfig.sh
    99    else
   100      echo "OKE Cluster creation request failed!"
   101      exit 1
   102    fi
   103  done
   104  
   105  # Wait for all of the OKE clusters to be ready
   106  for i in $(seq 1 $CLUSTER_COUNT)
   107  do
   108    export KUBECONFIG="${KUBECONFIG_DIR}/$i/kube_config"
   109    echo "Waiting for nodes to be added to cluster..."
   110    timeout 15m bash -c 'until kubectl get nodes | grep NAME; do sleep 10; done'
   111    echo "Waiting for nodes to transition to 'READY'..."
   112    kubectl wait --for=condition=ready nodes --timeout=5m --all
   113  done