<commit_before>#!/bin/bash

# Copyright Istio Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# set -x
# Creates a standard GKE cluster for testing.

set -euo pipefail

# shellcheck disable=SC2086
WD=$(dirname $0)
# shellcheck disable=SC2086
WD=$(cd $WD || exit; pwd)

# get default GKE cluster version for zone
function default_gke_version() {
  local zone=${1:?"zone is required"}
  # shellcheck disable=SC2155
  local temp_fname=$(mktemp)

  # shellcheck disable=SC2086
  gcloud container get-server-config --zone "${zone}"  > ${temp_fname} 2>&1
  # shellcheck disable=SC2181
  if [[ $? -ne 0 ]];then
    cat "${temp_fname}"
    exit 1
  fi

  # shellcheck disable=SC2002
  gke_ver=$(cat "${temp_fname}" | grep defaultClusterVersion | awk '{print $2}')
  echo "${gke_ver}"
  rm -rf "${temp_fname}"
}

# Required params
PROJECT_ID=${PROJECT_ID:?"project id is required"}

set +u # Allow referencing unbound variable $CLUSTER
if [[ -z ${CLUSTER} ]]; then
  CLUSTER_NAME=${1:?"cluster name is required"}
else
  CLUSTER_NAME=${CLUSTER}
fi
set -u

# Optional params
ZONE=${ZONE:-us-central1-a}
# specify REGION to create a regional cluster
REGION=${REGION:-}

# Check if zone or region is valid
if [[ -n "${REGION}" ]]; then
  if [[ -z "$(gcloud compute regions list --filter="name=('${REGION:-}')" --format="csv[no-heading](name)")" ]]; then
    echo "No such region: ${REGION:-}. Exiting."
    exit 1
  fi
  ZONE="${REGION}"
else
  if [[ -z "$(gcloud compute zones list --filter="name=('${ZONE}')" --format="csv[no-heading](name)")"  ]]; then
    echo "No such zone: ${ZONE}. Exiting."
    exit 1
  fi
fi

# Specify GCP_SA to create and use a specific service account.
# Default is to use same name as the cluster - each cluster can have different
# IAM permissions. This also enables workloadIdentity, which is recommended for GCP
GCP_SA=${GCP_SA:-$CLUSTER_NAME}
GCP_CTL_SA=${GCP_CTL_SA:-${CLUSTER_NAME}-control}

# Sizing
DISK_SIZE=${DISK_SIZE:-64}
MACHINE_TYPE=${MACHINE_TYPE:-n1-standard-32}
MIN_NODES=${MIN_NODES:-"4"}
MAX_NODES=${MAX_NODES:-"70"}
IMAGE=${IMAGE:-"COS"}
MAXPODS_PER_NODE=100

# Labels and version
ISTIO_VERSION=${ISTIO_VERSION:-master}

DEFAULT_GKE_VERSION=$(default_gke_version "${ZONE}")
# shellcheck disable=SC2181
if [[ $? -ne 0 ]];then
  echo "${DEFAULT_GKE_VERSION}"
  exit 1
fi

GKE_VERSION=${GKE_VERSION-${DEFAULT_GKE_VERSION}}

# default scope for reference
# shellcheck disable=SC2034
SCOPES_DEFAULT="https://www.googleapis.com/auth/devstorage.read_only,https://www.googleapis.com/auth/logging.write,https://www.googleapis.com/auth/monitoring,https://www.googleapis.com/auth/servicecontrol,https://www.googleapis.com/auth/service.management.readonly,https://www.googleapis.com/auth/trace.append"

# Full scope is needed for the context graph API and NEG integration
SCOPES_FULL="https://www.googleapis.com/auth/cloud-platform"

SCOPES="${SCOPES_FULL}"

# A label cannot have "." in it, replace "." with "_"
# shellcheck disable=SC2001
ISTIO_VERSION=$(echo "${ISTIO_VERSION}" | sed 's/\./_/g')

function gc() {
  # shellcheck disable=SC2236
  if [[ -n "${REGION:-}" ]];then
    ZZ="--region=${REGION}"
  else
    ZZ="--zone=${ZONE}"
  fi

  SA=""
  # shellcheck disable=SC2236
  if [[ -n "${GCP_SA:-}" ]];then
    SA=("--identity-namespace=${PROJECT_ID}.svc.id.goog" "--service-account=${GCP_SA}@${PROJECT_ID}.iam.gserviceaccount.com" "--workload-metadata-from-node=EXPOSED")
  fi

  # shellcheck disable=SC2048
  # shellcheck disable=SC2086
  echo gcloud $* "${ZZ}" "${SA[@]}"

  # shellcheck disable=SC2236
  set +u
  if [[ -n "${DRY_RUN:-}" ]];then
    return
  fi
  set -u

  # shellcheck disable=SC2086
  # shellcheck disable=SC2048<commit_msg>create_cluster.sh update with correct env var. (#1663)<commit_after>#!/bin/bash

# Copyright Istio Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# set -x
# Creates a standard GKE cluster for testing.

set -euo pipefail

# shellcheck disable=SC2086
WD=$(dirname $0)
# shellcheck disable=SC2086
WD=$(cd $WD || exit; pwd)

# get default GKE cluster version for zone
function default_gke_version() {
  local zone=${1:?"zone is required"}
  # shellcheck disable=SC2155
  local temp_fname=$(mktemp)

  # shellcheck disable=SC2086
  gcloud container get-server-config --zone "${zone}"  > ${temp_fname} 2>&1
  # shellcheck disable=SC2181
  if [[ $? -ne 0 ]];then
    cat "${temp_fname}"
    exit 1
  fi

  # shellcheck disable=SC2002
  gke_ver=$(cat "${temp_fname}" | grep defaultClusterVersion | awk '{print $2}')
  echo "${gke_ver}"
  rm -rf "${temp_fname}"
}

# Required params
PROJECT_ID=${PROJECT_ID:?"project id is required"}

set +u # Allow referencing unbound variable $CLUSTER
if [[ -z ${CLUSTER_NAME} ]]; then
  CLUSTER_NAME=${1:?"cluster name is required"}
else
  CLUSTER_NAME=${CLUSTER_NAME}
fi
set -u

# Optional params
ZONE=${ZONE:-us-central1-a}
# specify REGION to create a regional cluster
REGION=${REGION:-}

# Check if zone or region is valid
if [[ -n "${REGION}" ]]; then
  if [[ -z "$(gcloud compute regions list --filter="name=('${REGION:-}')" --format="csv[no-heading](name)")" ]]; then
    echo "No such region: ${REGION:-}. Exiting."
    exit 1
  fi
  ZONE="${REGION}"
else
  if [[ -z "$(gcloud compute zones list --filter="name=('${ZONE}')" --format="csv[no-heading](name)")"  ]]; then
    echo "No such zone: ${ZONE}. Exiting."
    exit 1
  fi
fi

# Specify GCP_SA to create and use a specific service account.
# Default is to use same name as the cluster - each cluster can have different
# IAM permissions. This also enables workloadIdentity, which is recommended for GCP
GCP_SA=${GCP_SA:-$CLUSTER_NAME}
GCP_CTL_SA=${GCP_CTL_SA:-${CLUSTER_NAME}-control}

# Sizing
DISK_SIZE=${DISK_SIZE:-64}
MACHINE_TYPE=${MACHINE_TYPE:-n1-standard-32}
MIN_NODES=${MIN_NODES:-"4"}
MAX_NODES=${MAX_NODES:-"70"}
IMAGE=${IMAGE:-"COS"}
MAXPODS_PER_NODE=100

# Labels and version
ISTIO_VERSION=${ISTIO_VERSION:-master}

DEFAULT_GKE_VERSION=$(default_gke_version "${ZONE}")
# shellcheck disable=SC2181
if [[ $? -ne 0 ]];then
  echo "${DEFAULT_GKE_VERSION}"
  exit 1
fi

GKE_VERSION=${GKE_VERSION-${DEFAULT_GKE_VERSION}}

# default scope for reference
# shellcheck disable=SC2034
SCOPES_DEFAULT="https://www.googleapis.com/auth/devstorage.read_only,https://www.googleapis.com/auth/logging.write,https://www.googleapis.com/auth/monitoring,https://www.googleapis.com/auth/servicecontrol,https://www.googleapis.com/auth/service.management.readonly,https://www.googleapis.com/auth/trace.append"

# Full scope is needed for the context graph API and NEG integration
SCOPES_FULL="https://www.googleapis.com/auth/cloud-platform"

SCOPES="${SCOPES_FULL}"

# A label cannot have "." in it, replace "." with "_"
# shellcheck disable=SC2001
ISTIO_VERSION=$(echo "${ISTIO_VERSION}" | sed 's/\./_/g')

function gc() {
  # shellcheck disable=SC2236
  if [[ -n "${REGION:-}" ]];then
    ZZ="--region=${REGION}"
  else
    ZZ="--zone=${ZONE}"
  fi

  SA=""
  # shellcheck disable=SC2236
  if [[ -n "${GCP_SA:-}" ]];then
    SA=("--identity-provider=${PROJECT_ID}.svc.id.goog" "--service-account=${GCP_SA}@${PROJECT_ID}.iam.gserviceaccount.com" "--workload-metadata-from-node=EXPOSED")
  fi

  # shellcheck disable=SC2048
  # shellcheck disable=SC2086
  echo gcloud $* "${ZZ}" "${SA[@]}"

  # shellcheck disable=SC2236
  set +u
  if [[ -n "${DRY_RUN:-}" ]];then
    return
  fi
  set -u

  # shellcheck disable=SC2086
  # shellcheck disable=SC2048
{'fraction_non_alphanumeric': 0.15332541567695962, 'fraction_numerical': 0.022327790973871733, 'mean_word_length': 4.558415841584158, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 14, 'lorem ipsum': 0, 'www.': 16, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3501934', 'n_tokens_mistral': 2994, 'n_tokens_neox': 2857, 'n_words': 779}