Created
December 19, 2022 06:18
-
-
Save itudoben/b50b7bc4012b0428ccfa1263d49b28d3 to your computer and use it in GitHub Desktop.
gke client-2.0
This file contains hidden or 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
#!/usr/bin/env bash | |
# Allow pipeline errors to bubble up. | |
set -o pipefail | |
# Set the working directories. | |
declare -r base_dir=$(dirname $0) | |
pushd "$base_dir/../../.." &>/dev/null | |
declare -r project_dir="$(pwd)" | |
declare -r src_lib_dir="$project_dir/src/main/bash/lib" | |
popd &>/dev/null | |
source "$src_lib_dir/init.sh" | |
source "$src_lib_dir/errors.sh" | |
source "$src_lib_dir/gcloud_utils.sh" | |
source "$src_lib_dir/logging.sh" | |
source "$src_lib_dir/utils.sh" | |
### | |
usage() { | |
cat <<EOT | |
SYNOPSIS | |
usage: $(basename $0) [-a pool] [-c cluster] [-d pool] [-m mode] [-p type] [-t machine] [-h] | |
DESCRIPTION | |
In its first form, this utility list existing node pools for the specified cluster. | |
Node pools can be created a certain way always on preemptible machines. | |
-a node_pool, --create node_pool | |
Creates a node pool in the specified running cluster. | |
-c name, --cluster name | |
The name of your existing cluster. Defaults to 'testing'. | |
-d node_pool, --delete node_pool | |
Deletes a node pool in the specified running cluster. | |
-m mode, --mode-execution mode | |
One of the modes of execution: custom, development or production. Defaults to development. | |
-p type, --pod-type type | |
One of the pod types that will be deployed on the node pool: federate, client, client-2.0 or default. | |
Defaults to 'default' using default or specified machine type for the nodes of the node pool. | |
-t machine_type, --machine-type machine_type | |
The type of machine to use for nodes. Defaults to e2-medium. | |
https://cloud.google.com/compute/docs/machine-resource | |
-h, --help | |
Print this help. | |
EOT | |
local -r error_msg=$1 | |
if [[ -n "$error_msg" ]]; then | |
cat <<EOT | |
error: $error_msg | |
EOT | |
fi | |
} | |
declare job_type='list' | |
declare mode_execution='development' | |
declare cluster | |
declare node_pool | |
declare pod_type='default' | |
declare machine_type='none' | |
declare disk_size_gb='20' | |
configure() { | |
while [[ "$#" -gt 0 ]]; do | |
case $1 in | |
-c | --cluster) | |
shift | |
if [[ "$1" =~ ^-|^-- ]] || [[ -z "$1" ]]; then | |
log 'ERROR' "Argument '$1' must be a cluster name." '' 'Use --help to get supported arguments.' | |
exit 1 | |
fi | |
mode_execution='custom' | |
cluster=$1 | |
;; | |
-a | --create) | |
shift | |
if [[ "$1" =~ ^-|^-- ]] || [[ -z "$1" ]]; then | |
log 'ERROR' "Argument '$1' must be a node pool name." '' 'Use --help to get supported arguments.' | |
exit 1 | |
fi | |
node_pool=$1 | |
job_type='create' | |
;; | |
-d | --delete) | |
shift | |
if [[ "$1" =~ ^-|^-- ]] || [[ -z "$1" ]]; then | |
log 'ERROR' "Argument '$1' must be a node pool name." '' 'Use --help to get supported arguments.' | |
exit 1 | |
fi | |
node_pool=$1 | |
job_type='delete' | |
;; | |
-m | --mode-execution) | |
shift | |
if [[ "$1" =~ ^-|^-- ]] || [[ -z "$1" ]]; then | |
log 'ERROR' "Argument '$1' must be a mode of execution." '' 'Use --help to get supported arguments.' | |
exit 1 | |
fi | |
mode_execution=$1 | |
;; | |
-p | --pod-type) | |
shift | |
if [[ "$1" =~ ^-|^-- ]] || [[ -z "$1" ]]; then | |
log 'ERROR' "Argument '$1' must be a component type from 'federate' or 'client'" '' 'Use --help to get supported arguments.' | |
exit 1 | |
fi | |
pod_type=$1 | |
;; | |
-t | --machine-type) | |
shift | |
if [[ "$1" =~ ^-|^-- ]] || [[ -z "$1" ]]; then | |
log 'ERROR' "Argument '$1' must be a node pool name." '' 'Use --help to get supported arguments.' | |
exit 1 | |
fi | |
machine_type=$1 | |
;; | |
-h | --help) | |
usage | |
exit 0 | |
;; | |
*) | |
if [[ "$1" =~ ^-|^-- ]]; then | |
log 'ERROR' "Argument '$1' is not supported." '' 'Use --help to get supported arguments.' | |
else | |
usage "cannot process argument '$1'." | |
fi | |
exit 1 | |
;; | |
esac | |
shift | |
done | |
if [[ -z "$node_pool" ]]; then | |
node_pool=='node-pool-to-be-changed' | |
fi | |
case $mode_execution in | |
production) | |
cluster='cicd' | |
;; | |
development) | |
cluster='testing' | |
;; | |
custom) | |
if [[ -z "$cluster" ]]; then | |
log 'ERROR' "$mode_execution requires a running cluster name to be provided" '' 'Use --help to get supported arguments.' | |
exit 1 | |
fi | |
;; | |
*) | |
log 'ERROR' "Execution mode '$1' is not supported." '' 'Use --help to get supported arguments.' | |
exit 1 | |
;; | |
esac | |
case $pod_type in | |
federate) | |
machine_type='n1-highmem-8' | |
disk_size_gb='64' | |
;; | |
client) | |
machine_type='n2-standard-8' | |
disk_size_gb='16' | |
;; | |
client-2.0) | |
machine_type='n2-standard-2' | |
disk_size_gb='16' | |
;; | |
default) ;; | |
*) | |
log 'ERROR' "node pool type $pod_type is not supported set to use machine type '$machine_type'." '' 'Use --help to get supported arguments.' | |
exit 1 | |
;; | |
esac | |
if [[ machine_type == 'none' ]]; then | |
log 'ERROR' "Machine type '$machine_type' is not supported." '' 'Use --help to get supported arguments.' | |
exit 1 | |
fi | |
check_google_account_is_authorized | |
} | |
list_node_pools() { | |
echo "$(get_date) List all node pools for cluster '$cluster'." | |
eval "gcloud container node-pools list --cluster $cluster" | |
} | |
create_node_pool() { | |
local -r node_labels="benchmark.siren.io/environment=${mode_execution},benchmark.siren.io/component=$pod_type" | |
local -r node_taints="benchmark.siren.io/job=${pod_type}:NoSchedule" | |
echo "$(get_date) Create node pool '$node_pool' in the running cluster '$cluster'." | |
eval "gcloud container node-pools create $node_pool --cluster=$cluster \ | |
--preemptible --num-nodes=0 --enable-autoscaling --min-nodes=0 --max-nodes=10 \ | |
--disk-size=$disk_size_gb --disk-type=pd-balanced --machine-type=$machine_type \ | |
--node-labels='$node_labels' --node-taints='$node_taints'" | |
} | |
delete_node_pool() { | |
echo "$(get_date) Delete node pool $node_pool in the running cluster '$cluster'." | |
eval "gcloud container node-pools delete $node_pool --cluster=$cluster" | |
} | |
main() { | |
start_session | |
configure "$@" | |
case $job_type in | |
list) | |
list_node_pools | |
;; | |
create) | |
create_node_pool | |
;; | |
delete) | |
delete_node_pool | |
;; | |
*) | |
CAT <<EOT | |
### ERROR | |
# | |
# '$1' command is not supported. | |
# Use --help to get supported commands. | |
# | |
### | |
EOT | |
exit 1 | |
;; | |
esac | |
stop_session | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment