-
Notifications
You must be signed in to change notification settings - Fork 0
/
launch-dashboard
executable file
·168 lines (135 loc) · 4.45 KB
/
launch-dashboard
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env bash
#
# (C) SpoddyCoder, 2022
# See DOC_URL for more info on what this is and how to use
#
#
# TODO:
# Make kubernetes pod scaling optional
# Add copy+paste support for other systems
#
# version
VER=0.9.7
DOC_URL="https://github.com/SpoddyCoder/scube-tools"
########################
# user conf
# these can be overridden, see README
#
# service name you want to generate a login token for
DASHBOARD_SERVICE_NAME="k8s-dashboard-admin"
# number of pods to deploy when bringing the dashboard back up
DEPLOYMENT_SCALE=1
# browser used to open the dashbaoard proxy url
BROWSER_PATH="/Applications/Google Chrome.app"
# proxy dashboard url
DASHBOARD_URI="http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/"
# default query to show when browser opens - this suitable for a cluster admin only...
LAUNCH_QUERY="workloads?all-namepsaces=&namespace=_all"
BROWSER_URL="${DASHBOARD_URI}#/${LAUNCH_QUERY}"
# use custom config if found
if [ -f /etc/scube-tools/launch-dashboard.conf ]; then
source /etc/scube-tools/launch-dashboard.conf
fi
if [ -f ~/.scube-tools/launch-dashboard.conf ]; then
source ~/.scube-tools/launch-dashboard.conf
fi
if [ -f ./.launch-dashboard.conf ]; then
source ./.launch-dashboard.conf
fi
if [ -f ./.scube-launch-dashboard.conf ]; then
source ./.scube-launch-dashboard.conf
fi
########################
# fixed conf
#
# kubernetes-dashboard component names
DASHBAORD_NAMESPACE="kubernetes-dashboard"
DASHBOARD_DEPLOYMENT_NAME="kubernetes-dashboard"
METRICS_DEPLOYMENT_NAME="dashboard-metrics-scraper"
# Mac pbcopy path - if this doesn't exist, will just output the token to STDOUT
PBCOPY_PATH="/usr/bin/pbcopy"
########################
# functions
#
# show help
usage() {
echo
echo "v${VER}"
echo "${DOC_URL}"
echo
}
# cleanup resources before quitting
# optionally exit with error code 1 by passing $1="error"
function cleanup()
{
if [ ! -z $BG_PID ]; then
echo
echo "Stopping kubectl proxy"
kill $BG_PID
fi
if (( $(kubectl get replicaset -n $DASHBAORD_NAMESPACE | grep $DASHBOARD_DEPLOYMENT_NAME | tr -s ' ' | cut -f2 -d' ') > 0 )); then
echo "Removing kubernetes-dashboard pods"
kubectl scale -n $DASHBAORD_NAMESPACE --replicas=0 deployment/$DASHBOARD_DEPLOYMENT_NAME
kubectl scale -n $DASHBAORD_NAMESPACE --replicas=0 deployment/$METRICS_DEPLOYMENT_NAME
fi
if [[ "$#" == 1 && "$1" == "error" ]]; then
exit 1
fi
exit 0
}
# generate new token and copy to clipboard / output to STDOUT
function genNewToken()
{
token=$(kubectl -n $DASHBAORD_NAMESPACE create token $DASHBOARD_SERVICE_NAME)
if [ "$?" -ne "0" ]; then
cleanup "error" # kill off resouces and exit 1
fi
if [ -f "$PBCOPY_PATH" ]; then
echo $token | pbcopy # mac only
echo "Generated $DASHBOARD_SERVICE_NAME token and copied to clipboard"
else
echo "Generated $DASHBOARD_SERVICE_NAME token..."
echo "$token"
fi
}
# open new browser, showing default query
function openBrowser()
{
echo "Opening $BROWSER_PATH"
echo "Navigating to $BROWSER_URL"
/usr/bin/open -a "$BROWSER_PATH" "$BROWSER_URL"
}
########################
# start
#
# display help (also respond to --help)
if [ "$#" -gt "0" ]; then
if [[ "$1" == "help" || "$1" == "--help" ]]; then
usage
exit 1
fi
fi
trap cleanup INT # handle CRTL+C quit
genNewToken
# redeploy dashboard pods if not already running
if (( $(kubectl get replicaset -n $DASHBAORD_NAMESPACE | grep $DASHBOARD_DEPLOYMENT_NAME | tr -s ' ' | cut -f2 -d' ') == 0 )); then
echo "Redeploying the dashboard pods (scale: $DEPLOYMENT_SCALE)"
kubectl scale -n $DASHBAORD_NAMESPACE --replicas=$DEPLOYMENT_SCALE deployment/$DASHBOARD_DEPLOYMENT_NAME
kubectl scale -n $DASHBAORD_NAMESPACE --replicas=$DEPLOYMENT_SCALE deployment/$METRICS_DEPLOYMENT_NAME
# wait for the kubernetes-dashboard deployment to complete
kubectl -n $DASHBAORD_NAMESPACE rollout status deployment $DASHBOARD_DEPLOYMENT_NAME
fi
# start the kubectl proxy if not already running & open the browser
if [ "$(ps aux | grep -w '[k]ubectl proxy' | wc -l)" -eq "0" ]; then
echo "Starting kubectl proxy"
# put the process in the background and get the pid
kubectl proxy &
BG_PID=$!
sleep 1 # allow a beat for the proxy to init
openBrowser
echo "kubectl proxy is running (CTRL+C to exit)"
wait
else
openBrowser
fi
exit 0