-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkube-functions.sh
executable file
·230 lines (195 loc) · 7.45 KB
/
kube-functions.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/bin/bash
export k8sdir=$(realpath $(dirname ${BASH_SOURCE[0]}))
if [ "$0" == "${BASH_SOURCE[0]}" ]
then
echo "you must source this script for it to work correctly"
exit 1
fi
if [[ -z "$K8S_HELM_REGISTRY" ]] ; then
echo please set the environment variables K8S_IMAGE_REGISTRY
echo to point to the URL of the HELM registry in which IOC charts are held
exit 1
fi
export HELM_EXPERIMENTAL_OCI=1
source <(helm completion bash)
source <(kubectl completion bash)
alias k=kubectl
complete -F __start_kubectl k
###########################################################################
# some helper functions for ioc management
###########################################################################
function kube-ioc-deploy()
{
(
set -e
IOC_NAME=${1}
VERSION=${2}
if [ -z "${VERSION}" ]; then VERSION=latest; fi
BL_PREFIX=${IOC_NAME%%-*}
IOC_HELM=${K8S_HELM_REGISTRY}/${IOC_NAME}
# pull the requested ioc helm chart from the registry
echo getting ${IOC_HELM}:${VERSION}
helm chart pull ${IOC_HELM}:${VERSION}
# export it to a folder
helm chart export ${IOC_HELM}:${VERSION} -d /tmp
helm dependencies update /tmp/${IOC_NAME}
# deploy the exported helm chart
helm upgrade --install ${IOC_NAME} /tmp/${IOC_NAME}
rm -r /tmp/${IOC_NAME}
)
}
# kubectl format strings
export podw="custom-columns=IOC:metadata.name,VERSION:metadata.labels.ioc_version,STATE:status.containerStatuses[0].state.*.reason,RESTARTS:status.containerStatuses[0].restartCount,STARTED:metadata.managedFields[0].time,IP:status.podIP,IMAGE:spec.containers[0].image"
export pods="custom-columns=IOC:metadata.labels.app,VERSION:metadata.labels.ioc_version,STATE:status.containerStatuses[0].state.*.reason,RESTARTS:status.containerStatuses[0].restartCount,STARTED:metadata.managedFields[0].time"
export deploys="custom-columns=DEPLOYMENT:metadata.labels.app,VERSION:metadata.labels.ioc_version,REPLICAS:spec.replicas,IMAGE:spec.template.spec.containers[0].image"
export services="custom-columns=SERVICE:metadata.labels.app,CLUSTER-IP:spec.clusterIP,EXTERNAL-IP:status.loadBalancer.ingress[0].ip,PORT:spec.ports[*].targetPort"
function beamline-k8s()
{
if [ -z ${1} ]
then
echo please specify a beamline
return
fi
kubectl get deployment -l beamline=${1} -o $deploys; echo
kubectl get pod -l beamline=${1} -o $pods; echo
echo configMaps
kubectl get configmap -l beamline=${1}; echo
echo Peristent Volume Claims
kubectl get pvc -l beamline=${1}; echo
}
function k8s-ioc()
{
action=${1}
shift
case ${action} in
a|attach)
ioc=${1:? "param 1 should be ioc e.g. bl45p-mo-ioc-01"}; shift
echo "connecting to ${ioc}. Detach with ^P^Q or stop with ^D"
kubectl attach -it deployment.apps/${ioc} ${*}
;;
del|delete)
ioc=${1:? "param 1 should be ioc e.g. bl45p-mo-ioc-01"}; shift
helm delete ${ioc}
;;
deploy)
ioc=${1:? "param 1 should be ioc e.g. bl45p-mo-ioc-01"}; shift
version=${1:? param 2 should be version e.g. 1.0b1.1}; shift
kube-ioc-deploy ${ioc} ${version} ${*}
;;
e|exec)
ioc=${1:? "param 1 should be ioc e.g. bl45p-mo-ioc-01"}; shift
echo "connecting to bash shell in ${ioc}. Exit with ^D"
kubectl exec -it deployment.apps/${ioc} ${*} -- bash
;;
g|graylog)
ioc=${1:? "param 1 should be an ioc e.g. bl45p-mo-ioc-01"}; shift
echo "${K8S_GRAYLOG_URL}/search?rangetype=relative&fields=message%2Csource&width=1489&highlightMessage=&relative=172800&q=pod_name%3A${ioc}*"
;;
h|history)
ioc=${1:? "param 1 should be ioc e.g. bl45p-mo-ioc-01"}; shift
helm history ${ioc}
;;
list)
ioc=${1:? "param 1 should be ioc e.g. bl45p-mo-ioc-01"}; shift
kubectl get all -l app=${ioc} ${*}; echo
# if there is no autosave then there may be no pvcs so ignore errors
kubectl get pvc -l app=${ioc} ${*} 2> /dev/null
;;
l|log)
ioc=${1:? "param 1 should be ioc e.g. bl45p-mo-ioc-01"}; shift
kubectl logs deployment.apps/${ioc} ${*}
;;
m|monitor)
bl=${1:? "param 1 should be a beamline e.g. bl45p"}; shift
watch -n0.5 -x -c bash -c "beamline-k8s ${bl} ${*}"
;;
ps)
if [ "${1}" = "-w" ] ; then
shift
format=${podw}
else
format=${pods}
fi
if [ -z ${1} ]; then
kubectl get pod -l is_ioc==True -o ${format}
else
kubectl get pod -l beamline==${1} -o ${format}
fi
echo
;;
purge)
# delete the helm local cache (helm prune is not yet implemented
# and even remove is hard to use if the resource names are too long
# to show in 'helm chart list')
rm -fr ~/.cache/helm/registry/cache/
;;
r|restart)
# just delete the pod - the deployment spins up a new one
ioc=${1:? "param 1 should be ioc e.g. bl45p-mo-ioc-01"}; shift
kubectl delete $(kubectl get pod -l app=${ioc} -o name)
;;
rollback)
ioc=${1:? "param 1 should be ioc e.g. bl45p-mo-ioc-01"}; shift
rev=${1:? "param 2 helm revision no. from k8s-ioc history"}; shift
helm rollback ${ioc} ${rev}
;;
start)
ioc=${1:? "param 1 should be ioc e.g. bl45p-mo-ioc-01"}; shift
kubectl scale deployment --replicas=1 ${ioc} ${*}
;;
stop)
ioc=${1:? param 1 should be ioc e.g. bl45p-mo-ioc-01 }; shift
kubectl scale deployment --replicas=0 ${ioc} ${*}
;;
*)
echo "
usage:
k8s-ioc <command> <options>
commands:
attach <ioc-name>
attach to a running ioc shell
delete <ioc-name>
delete all ioc resources except storage
deploy <ioc-name> <ioc-version>
deploy an ioc manifest from the beamline helm registry
exec <ioc-name>
execute bash in the ioc's container
history <ioc-name>
list the history of installed versions of an ioc
graylog <ioc-name>
print a URL to get to greylog historical logging for an ioc
list <ioc-name> [options]
list k8s resources associtated with ioc-name
-o output formatting e.g. -o name
log <ioc-name> [options]
display log of ioc output
-p for previous instance
-f to attach to output stream
monitor <beamline>
monitor the status of running IOCs on a beamline
ps [<beamline>]
list all running iocs [on beamline]
purge
clear the helm local cache
restart <ioc-name>
restart a running ioc
rollback <ioc-name> <revision>
rollback to a previous revision
(see history command for revision numbers)
start <ioc-name>
start a stopped ioc
stop <ioc-name>
stop a deployed ioc
"
;;
esac
}
# run most recently built image in the cache - including part build failures
function run_last()
{
docker run ${@} -it --user root $(docker images | awk '{print $3}' | awk 'NR==2')
}
export -f run_last
export -f kube-ioc-deploy
export -f beamline-k8s
export -f k8s-ioc