-
Notifications
You must be signed in to change notification settings - Fork 34
/
osc-cleanup-project
executable file
·67 lines (59 loc) · 1.84 KB
/
osc-cleanup-project
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
#!/bin/bash
if [ -f ~/.kube/.kubeconfig ]; then
OSC_CONFIG=~/.kube/.kubeconfig
elif [ -f ~/.config/openshift/config ]; then
OSC_CONFIG=~/.config/openshift/config
else
echo "Error: No login config. Please log in using osc login first."
exit 1
fi
# Functions
confirm () {
# call with a prompt string or use a default
read -r -p "${1:-Are you sure? [y/N]} " response
case $response in
[yY][eE][sS]|[yY])
false
;;
*)
true
;;
esac
}
usage() {
echo "Usage: $0 [Options]"
echo ""
echo " Cleans up osc resources within the current project namespace. Assumes active kubernetes login."
echo ""
echo "Options:"
echo " -h : Help info"
echo " -y : Yes. Autoconfirm that you want to do this dangerous thing."
}
# Process options
while [[ $# -gt 0 ]] && [[ ."$1" = .-* ]] ;
do
opt=$1
shift
case "$opt" in
"--" ) break 2;;
"-y" )
confirmed=true; shift;;
"-h" )
usage; exit 0;;
*) echo >&2 "Invalid option: $@"; usage; exit 1;;
esac
done
# Setup
resource_types="build buildconfig images imagestreams imagerepository deploymentconfig route replicationcontroller service pod"
# Get current project namespace
context=$(awk '/current-context/ {print $2}' $OSC_CONFIG)
namespace=$(grep -B 4 "name: $context" $OSC_CONFIG | awk '/namespace/ {print $2}')
# Confirm we want to delete resources for this project
[ ! $confirmed ] && confirm "Are you sure you want to delete all resources in project '${namespace}'? [y/N]" && echo "Exiting..." && exit 1;
for resource_type in $resource_types; do
resource_list=$(osc get ${resource_type} | grep -v '^NAME\|POD\|CONTROLLER' | awk '{print $1}')
for name in $resource_list; do
echo -n "deleting:- $resource_type -> $name... "
[ "$(osc delete $resource_type $name)" == "$name" ] && echo "Done"
done
done