-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathgke.sh
executable file
·147 lines (128 loc) · 4.02 KB
/
gke.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
# ===== User Configurable Variables =====
gke_project="elastic-support-k8s-dev"
gke_region="us-central1"
gke_machine_type="e2-standard-4"
label="division=support,org=support,team=support,project=gkelab"
gke_cluster_nodes="1"
# =======================================
gke_cluster_name="$(whoami | sed $'s/[^[:alnum:]\t]//g')-gkelab"
kubectl_url_base="https://dl.k8s.io/release/$(curl -s https://dl.k8s.io/release/stable.txt)/bin"
kubectl_install_path="/usr/local/bin/kubectl"
### colors
red=$(tput setaf 1)
green=$(tput setaf 2)
blue=$(tput setaf 14)
reset=$(tput sgr0)
# Function to display help
help() {
cat << EOF
This script is to stand up a GKE environment in ${gcp_project} Project
${green}Usage:${reset} ./$(basename "$0") COMMAND
${blue}COMMANDS${reset}
${green}create|start|deploy${reset} - Creates your GCP environment & runs post-scripts (Linux)
${green}find|check|info|status${reset} - Finds info about your GCP environment
${green}delete|cleanup|stop${reset} - Deletes your GCP environment
EOF
}
# Helper function to display debug messages
debug() {
echo "${green}[DEBUG]${reset} $1"
}
debugr() {
echo "${red}[DEBUG]${reset} $1"
}
checkkubectl() {
if ! command -v kubectl &>/dev/null; then
debugr "kubectl not found. Installing."
case ${OS} in
"linux") os_path="linux/amd64" ;;
"macos-x86_64") os_path="darwin/amd64" ;;
"macos-arm64") os_path="darwin/arm64" ;;
*) echo "${red}[ERROR]${reset} Unsupported OS: ${OS}"; return 1 ;;
esac
# Download kubectl
curl -LO "${kubectl_url_base}/${os_path}/kubectl"
# Install kubectl and clean up
sudo install kubectl "${kubectl_install_path}" && rm -f kubectl
else
debug "kubectl found."
fi
}
find_cluster() {
if gcloud container clusters list --project "${gke_project}" 2> /dev/null| grep -q "${gke_cluster_name}"; then
debug "Cluster ${gke_cluster_name} exists."
echo ""
gcloud container clusters list --project "${gke_project}" 2> /dev/null| grep -E "STATUS|${gke_cluster_name}"
exit
else
debugr "Cluster ${gke_cluster_name} not found."
fi
}
start_cluster() {
find_cluster
debug "Creating cluster ${gke_cluster_name}"
echo ""
gcloud container clusters create "${gke_cluster_name}" \
--labels="${label}" \
--project="${gke_project}" \
--region="${gke_region}" \
--num-nodes="${gke_cluster_nodes}" \
--machine-type="${gke_machine_type}" \
--disk-type="pd-ssd" \
--disk-size="100" \
--image-type="COS_CONTAINERD" \
--release-channel="stable" \
--max-pods-per-node="110" \
--cluster-ipv4-cidr="/17" \
--services-ipv4-cidr="/22" \
--enable-ip-alias \
--enable-autorepair
debug "Configuring kubectl context for ${gke_cluster_name}"
gcloud container clusters get-credentials "${gke_cluster_name}" --region="${gke_region}" --project="${gke_project}"
debug "Adding gcloud RBAC for cluster admin role"
kubectl create clusterrolebinding cluster-admin-binding \
--clusterrole=cluster-admin \
--user="$(gcloud auth list --filter=status:ACTIVE --format="value(account)")"
}
delete_cluster() {
if gcloud container clusters list --project "${gke_project}" 2> /dev/null| grep -q "${gke_cluster_name}"; then
debug "Removing kubectl context"
kubectl config unset current-context
kubectl config delete-context "gke_${gke_project}_${gke_region}_${gke_cluster_name}"
debug "Deleting ${gke_cluster_name}"
gcloud container clusters delete "${gke_cluster_name}" --project="${gke_project}" --region="${gke_region}" --quiet
else
debugr "Cluster ${gke_cluster_name} not found"
fi
}
# ===== Main Script =====
OS=$(uname -s)
case ${OS} in
"Linux")
OS="linux"
;;
"Darwin")
OS="macos-$(uname -m)"
;;
*)
debugr "This script only supports macOS and Linux"
exit 1
;;
esac
case ${1} in
start|deploy|create)
checkkubectl
start_cluster
;;
find|check|info|status)
find_cluster
;;
delete|cleanup|stop)
delete_cluster
;;
*)
help
exit 1
;;
esac