github.com/verrazzano/verrazzano@v1.7.1/ci/scripts/create_kind_clusters.sh (about)

     1  #!/bin/bash
     2  #
     3  # Copyright (c) 2022, 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_NAME=$1
     9  PLATFORM_OPERATOR_DIR=$2
    10  KUBECONFIG=$3
    11  K8S_VERSION=$4
    12  CLEANUP_KIND_CONTAINERS=${5:-true}
    13  CONNECT_JENKINS_RUNNER_TO_NETWORK=${6:-false}
    14  KIND_AT_CACHE=${7:-false}
    15  SETUP_CALICO=${8:-false}
    16  KIND_AT_CACHE_NAME=${9:-"NONE"}
    17  NODE_COUNT=${10:-1}
    18  CALICO_SUFFIX=""
    19  K8S_VERSION=${K8S_VERSION:-1.24}
    20  
    21  KIND_API_SERVER_ADDRESS=${KIND_API_SERVER_ADDRESS:-127.0.0.1}
    22  
    23  if [ -z "$TEST_SCRIPTS_DIR" ]; then
    24    echo "TEST_SCRIPTS_DIR must be set to the E2E test script directory location"
    25    exit 1
    26  fi
    27  if [ -z "${KUBECONFIG}" ]; then
    28    echo "KUBECONFIG must be set"
    29    exit 1
    30  fi
    31  if [ -z "$WORKSPACE" ]; then
    32    echo "WORKSPACE must be set"
    33    exit 1
    34  fi
    35  
    36  create_kind_cluster() {
    37  
    38    clusterNames=$(kind get clusters)
    39    if [[ $clusterNames == *"${CLUSTER_NAME}"* ]]; then
    40      echo "${CLUSTER_NAME} already exists"
    41      return 0
    42    fi
    43  
    44     if [ "${K8S_VERSION}" == "1.21" ]; then
    45        KIND_IMAGE="ghcr.io/verrazzano/kind:v1.21.14-20230510140100-7d934451"
    46      elif [ "${K8S_VERSION}" == "1.22" ]; then
    47        KIND_IMAGE="ghcr.io/verrazzano/kind:v1.22.15-20230510133529-7d934451"
    48      elif [ "${K8S_VERSION}" == "1.23" ]; then
    49        KIND_IMAGE="ghcr.io/verrazzano/kind:v1.23.13-20230510133509-7d934451"
    50      elif [ "${K8S_VERSION}" == "1.24" ]; then
    51        KIND_IMAGE="ghcr.io/verrazzano/kind:v1.24.10-20230510133459-7d934451"
    52      elif [ "${K8S_VERSION}" == "1.25" ]; then
    53        KIND_IMAGE="ghcr.io/verrazzano/kind:v1.25.8-20230510133540-7d934451"
    54      elif [ "${K8S_VERSION}" == "1.26" ]; then
    55        KIND_IMAGE="ghcr.io/verrazzano/kind:v1.26.3-20230512053749-7d934451"
    56      elif [ "${K8S_VERSION}" == "1.27" ]; then
    57        KIND_IMAGE="ghcr.io/verrazzano/kind:v1.27.2-20230823164336-7d934451"
    58      else
    59        echo "ERROR: Invalid value for Kubernetes Version ${K8S_VERSION}."
    60        exit 1
    61     fi
    62  
    63    if [ $SETUP_CALICO == true ] ; then
    64      CALICO_SUFFIX="-calico"
    65    fi
    66  
    67    export KUBECONFIG=$KUBECONFIG
    68    echo "Kubeconfig ${KUBECONFIG}"
    69    touch -f $KUBECONFIG
    70  
    71    echo "KIND Image : ${KIND_IMAGE}"
    72  
    73    # Set up a copy of the desired KIND config in the WORKSPACE before massaging it to avoid
    74    # doing a local edit in the repo branch
    75    KIND_CONFIG_FILE_NAME=kind-config${CALICO_SUFFIX}.yaml
    76    SOURCE_KIND_CONFIG_FILE=${TEST_SCRIPTS_DIR}/${KIND_CONFIG_FILE_NAME}
    77    KIND_CONFIG_FILE=${WORKSPACE}/${KIND_CONFIG_FILE_NAME}
    78    if [ ${KIND_AT_CACHE} == true ]; then
    79      if [ ${KIND_AT_CACHE_NAME} != "NONE" ]; then
    80        # If a cache name was specified, replace the at_test cache name with the one specified (this is used only
    81        # for multi-cluster tests at the moment)
    82        KIND_CONFIG_FILE_NAME=kind-config-ci${CALICO_SUFFIX}_${KIND_AT_CACHE_NAME}.yaml
    83        KIND_CONFIG_FILE=${WORKSPACE}/${KIND_CONFIG_FILE_NAME}
    84        SOURCE_KIND_CONFIG_FILE=${TEST_SCRIPTS_DIR}/${KIND_CONFIG_FILE_NAME}
    85      else
    86        # If no cache name specified use at_tests cache
    87        SOURCE_KIND_CONFIG_FILE=${TEST_SCRIPTS_DIR}/kind-config-ci${CALICO_SUFFIX}.yaml
    88      fi
    89    fi
    90    cp -v ${SOURCE_KIND_CONFIG_FILE} ${KIND_CONFIG_FILE}
    91  
    92    # Update the caching configuration if necessary
    93    if [ ${KIND_AT_CACHE} == true ]; then
    94      if [ ${KIND_AT_CACHE_NAME} != "NONE" ]; then
    95        sed -i "s;v8o_cache/at_tests;v8o_cache/${KIND_AT_CACHE_NAME};g" ${KIND_CONFIG_FILE}
    96      fi
    97    fi
    98  
    99    # List the permissions of /dev/null.  We have seen a failure where `docker ps` gets an operation not permitted error.
   100    # Listing the permissions will help to analyze what is wrong, if we see the failure again.
   101    echo "Listing permissions for /dev/null"
   102    ls -l /dev/null
   103    echo "Using ${KIND_CONFIG_FILE}"
   104    for (( n=2; n<=${NODE_COUNT}; n++ ))
   105    do
   106      echo "  - role: worker" >> ${KIND_CONFIG_FILE}
   107      echo "    image: KIND_IMAGE" >> ${KIND_CONFIG_FILE}
   108    done
   109    sed -i -e "s|KIND_IMAGE|${KIND_IMAGE}|g" ${KIND_CONFIG_FILE}
   110  
   111    # Use a custom API server address if specified; defaults to 127.0.0.1
   112    echo "Setting the cluster API server address to ${KIND_API_SERVER_ADDRESS}"
   113    yq -i eval '.networking.apiServerAddress=strenv(KIND_API_SERVER_ADDRESS)' ${KIND_CONFIG_FILE}
   114  
   115    cat ${KIND_CONFIG_FILE}
   116    HTTP_PROXY="" HTTPS_PROXY="" http_proxy="" https_proxy="" time kind create cluster --retain -v 9 --name ${CLUSTER_NAME} \
   117      --config=${KIND_CONFIG_FILE}
   118  }
   119  
   120  set -e
   121  if [ -n "${VZ_TEST_DEBUG}" ]; then
   122    set -xv
   123  fi
   124  
   125  create_kind_cluster
   126  
   127  kubectl config set-context kind-${CLUSTER_NAME}
   128  
   129  if [ "${CONNECT_JENKINS_RUNNER_TO_NETWORK}" == "true" ]; then
   130    dockerIP=$(docker inspect ${CLUSTER_NAME}-control-plane | jq -r '.[].NetworkSettings.Networks[].IPAddress')
   131    sed -i -e "s|127.0.0.1.*|${dockerIP}:6443|g" ${KUBECONFIG}
   132    cat ${KUBECONFIG} | grep server
   133  
   134    jenkinsRunnerNetwork=$(docker ps | grep "jenkins-runner" | awk '{ print $1 }')
   135    if [ -n "${jenkinsRunnerNetwork}" ]; then
   136      echo "Jenkins runner network detected, connecting to ${jenkinsRunnerNetwork}"
   137      docker network connect kind ${jenkinsRunnerNetwork}
   138    else
   139      echo "Ignore connecting jenkins-runner to a network."
   140    fi
   141  fi
   142  
   143  echo "KIND cluster ${CLUSTER_NAME} setup complete"