generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
293 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#!/usr/bin/env bash | ||
|
||
#Permissions: In order to run this script you need to use a service account with permissions equivalent to the following GCP roles: | ||
# - Compute Admin | ||
# - Service Account User | ||
# - Service Account Admin | ||
# - Service Account Token Creator | ||
# - Make sure the service account is enabled for the Google Identity and Access Management API. | ||
|
||
source "${PROJECT_ROOT}/scripts/utils/log.sh" | ||
source "${PROJECT_ROOT}/scripts/utils/utils.sh" | ||
|
||
gardener::init() { | ||
requiredVars=( | ||
GARDENER_REGION | ||
GARDENER_ZONES | ||
GARDENER_KUBECONFIG | ||
GARDENER_PROJECT_NAME | ||
GARDENER_PROVIDER_SECRET_NAME | ||
GARDENER_CLUSTER_VERSION | ||
) | ||
utils::check_required_vars "${requiredVars[@]}" | ||
|
||
# set default values | ||
if [ -z "$MACHINE_TYPE" ]; then | ||
export MACHINE_TYPE="m5.xlarge" | ||
fi | ||
} | ||
|
||
gardener::provision_cluster() { | ||
log::info "Provision cluster: \"${CLUSTER_NAME}\"" | ||
if [ "${#CLUSTER_NAME}" -gt 9 ]; then | ||
log::error "Provided cluster name is too long" | ||
return 1 | ||
fi | ||
|
||
# decreasing attempts to 2 because we will try to create new cluster from scratch on exit code other than 0 | ||
kyma provision gardener aws \ | ||
--secret "${GARDENER_PROVIDER_SECRET_NAME}" \ | ||
--name "${CLUSTER_NAME}" \ | ||
--project "${GARDENER_PROJECT_NAME}" \ | ||
--credentials "${GARDENER_KUBECONFIG}" \ | ||
--region "${GARDENER_REGION}" \ | ||
--zones "${GARDENER_ZONES}" \ | ||
--type "${MACHINE_TYPE}" \ | ||
--scaler-max 4 \ | ||
--scaler-min 2 \ | ||
--kube-version="${GARDENER_CLUSTER_VERSION}" \ | ||
--attempts 1 \ | ||
--verbose \ | ||
--hibernation-start "" | ||
} | ||
|
||
gardener::cleanup() { | ||
log::info "Deprovision cluster: \"${CLUSTER_NAME}\"" | ||
gardener::deprovision_cluster \ | ||
-p "${GARDENER_PROJECT_NAME}" \ | ||
-c "${CLUSTER_NAME}" \ | ||
-f "${GARDENER_KUBECONFIG}" | ||
} | ||
|
||
# gardener::deprovision_cluster removes a Gardener cluster | ||
# | ||
# Arguments: | ||
# | ||
# required: | ||
# p - project name | ||
# c - cluster name | ||
# f - kubeconfig file path | ||
function gardener::deprovision_cluster() { | ||
local OPTIND | ||
local projectName | ||
local clusterName | ||
local kubeconfigFile | ||
local namespace | ||
local wait="false" | ||
|
||
while getopts ":p:c:f:w:" opt; do | ||
case $opt in | ||
p) | ||
projectName="$OPTARG" ;; | ||
c) | ||
clusterName="$OPTARG" ;; | ||
f) | ||
kubeconfigFile="$OPTARG" ;; | ||
w) | ||
wait=${OPTARG:-$wait} ;; | ||
\?) | ||
echo "Invalid option: -$OPTARG" >&2; exit 1 ;; | ||
:) | ||
echo "Option -$OPTARG argument not provided" >&2 ;; | ||
esac | ||
done | ||
|
||
|
||
utils::check_empty_arg "$projectName" "Project name is empty. Exiting..." | ||
utils::check_empty_arg "$clusterName" "Cluster name is empty. Exiting..." | ||
utils::check_empty_arg "$kubeconfigFile" "Kubeconfig file path is empty. Exiting..." | ||
|
||
log::info "Deprovision cluster: ${clusterName}" | ||
|
||
namespace="garden-${projectName}" | ||
|
||
kubectl annotate shoot "${clusterName}" confirmation.gardener.cloud/deletion=true \ | ||
--overwrite \ | ||
-n "${namespace}" \ | ||
--kubeconfig "${kubeconfigFile}" | ||
kubectl delete shoot "${clusterName}" \ | ||
--wait="${wait}" \ | ||
--kubeconfig "${kubeconfigFile}" \ | ||
-n "${namespace}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/bash | ||
|
||
source "${PROJECT_ROOT}/scripts/gardener/aws.sh" | ||
|
||
gardener::init | ||
|
||
gardener::provision_cluster |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#!/usr/bin/env bash | ||
|
||
# log::date returns the current date in format expected by logs | ||
function log::date { | ||
date +"%Y-%m-%d %T %Z" | ||
} | ||
|
||
# log::dump_trace prints stacktrace when an error occurs. | ||
# | ||
log::dump_trace() { | ||
local frame=1 line func source n=0 | ||
while caller "$frame"; do | ||
((frame++)) | ||
done | while read -r line func source; do | ||
((n++ == 0)) && { | ||
printf 'Encountered an error\n' | ||
} | ||
printf '%4s at %s\n' " " "$func ($source:$line)" | ||
done | ||
} | ||
|
||
# log::banner prints message with INFO level in banner form for easier spotting in log files | ||
# | ||
# Arguments: | ||
# $* - Message | ||
function log::banner { | ||
local logdate | ||
logdate=$(log::date) | ||
local scriptname | ||
scriptname=${BASH_SOURCE[1]:-$1} | ||
echo -e "${logdate} [INFO] *************************************************************************************" | ||
echo -e "${logdate} [INFO] [$scriptname] * $*" | ||
echo -e "${logdate} [INFO] *************************************************************************************" | ||
} | ||
|
||
# log::info prints message with info level | ||
# | ||
# Arguments: | ||
# $* - Message | ||
function log::info { | ||
local funcname # get function that called this | ||
local scriptname | ||
funcname=${FUNCNAME[1]} | ||
scriptname=${BASH_SOURCE[1]:-$1} | ||
echo -e "$(log::date) [INFO] PID:$$ --- [$scriptname] $funcname:${BASH_LINENO[1]} $*" | ||
} | ||
|
||
# log::success prints a message with info level | ||
# | ||
# Arguments: | ||
# $* - Message | ||
function log::success { | ||
local logdate | ||
logdate=$(log::date) | ||
echo -e "${logdate} [INFO] =====================================================================================" | ||
echo -e "${logdate} [INFO] = SUCCESS =" | ||
echo -e "${logdate} [INFO] =====================================================================================" | ||
echo -e "${logdate} [INFO] = $*" | ||
echo -e "${logdate} [INFO] =====================================================================================" | ||
} | ||
|
||
# log::warn prints a message with warning level | ||
# | ||
# Arguments: | ||
# $* - Message | ||
function log::warn { | ||
local funcname # get function that called this | ||
local scriptname | ||
funcname=${FUNCNAME[1]} | ||
scriptname=${BASH_SOURCE[1]:-$1} | ||
echo -e "$(log::date) [WARN] PID:$$ --- [$scriptname] $funcname:${BASH_LINENO[1]} $*" | ||
} | ||
|
||
# log::error prints a message with error level | ||
# | ||
# Arguments: | ||
# $* - Message | ||
function log::error { | ||
local funcname # get function that called this | ||
local scriptname | ||
funcname=${FUNCNAME[1]} | ||
scriptname=${BASH_SOURCE[1]:-$1} | ||
>&2 echo -e "$(log::date) [ERROR] PID:$$ --- [$scriptname] $funcname:${BASH_LINENO[1]} $*" | ||
>&2 log::dump_trace | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
source "${PROJECT_ROOT}/scripts/utils/log.sh" | ||
|
||
# utils::check_required_vars checks if all provided variables are initialized | ||
# Arguments | ||
# $1 - list of variables | ||
function utils::check_required_vars() { | ||
log::info "Checks if all provided variables are initialized" | ||
local discoverUnsetVar=false | ||
for var in "$@"; do | ||
if [ -z "${!var}" ] ; then | ||
log::warn "ERROR: $var is not set" | ||
discoverUnsetVar=true | ||
fi | ||
done | ||
if [ "${discoverUnsetVar}" = true ] ; then | ||
exit 1 | ||
fi | ||
} |