-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathuninstall-operator.sh
executable file
·170 lines (137 loc) · 4.39 KB
/
uninstall-operator.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
#!/usr/bin/env bash
set -eu -o pipefail
PROJECT_ROOT="$(git rev-parse --show-toplevel)"
declare -r PROJECT_ROOT
# config
declare DELETE_RESOURCES=false
declare OPERATOR_NS=""
declare OPERATOR_VERSION=""
declare OPERATOR=""
declare OPERATOR_CSV=""
declare SHOW_HELP=false
source "$PROJECT_ROOT/hack/utils.bash"
get_subscription_details() {
local name="$1"
# NOTE: check for the spec.name instead of the metadata.name since the
# subscription name can be different to the operator name
run kubectl get subscriptions -A -o jsonpath='{range .items[?(@.spec.name=="'"$name"'")]}{.metadata.namespace}/{.spec.name}{end}'
local op=""
op=$(kubectl get subscriptions -A -o jsonpath='{range .items[?(@.spec.name=="'"$name"'")]}{.metadata.namespace}/{.spec.name}{end}')
[[ "$op" == "" ]] && return 1
return 0
}
init() {
local subs=""
subs=$(
get_subscription_details "power-monitoring-operator" ||
get_subscription_details "kepler-operator"
)
[[ "$subs" == "" ]] && {
fail "failed to find any kepler/power-monitoring-operator subscription"
return 1
}
OPERATOR="${subs##*/}"
OPERATOR_NS="${subs%%/*}"
ok "found $OPERATOR installed in $OPERATOR_NS namespace"
OPERATOR_CSV=$(kubectl get csv -n "$OPERATOR_NS" -o name | grep -E "$OPERATOR\.v") || {
warn "No csv found for $OPERATOR! Is it installed?"
return 1
}
ok "found $OPERATOR csv: $OPERATOR_CSV"
local version=""
version=$(kubectl get -n "$OPERATOR_NS" "$OPERATOR_CSV" -o jsonpath="{.spec.version}")
[[ -z "$version" ]] && {
fail "Failed to find version of $OPERATOR - $OPERATOR_CSV"
return 1
}
OPERATOR_VERSION="v$version"
ok "found $OPERATOR version: $OPERATOR_VERSION"
return 0
}
parse_args() {
### while there are args parse them
while [[ -n "${1+xxx}" ]]; do
case $1 in
--help | -h)
shift
SHOW_HELP=true
return 0
;;
--delete)
DELETE_RESOURCES=true
shift
;; # exit the loop
*) return 1 ;; # show usage on everything else
esac
done
return 0
}
print_usage() {
local scr
scr="$(basename "$0")"
read -r -d '' help <<-EOF_HELP || true
Usage:
$scr
$scr --delete
─────────────────────────────────────────────────────────────────
Options:
--delete deletes the resources listed
EOF_HELP
echo -e "$help"
return 0
}
list_olm_resources() {
header "Listing Resources of $OPERATOR"
info_run kubectl get csv -n "$OPERATOR_NS"
kubectl get csv -n "$OPERATOR_NS" | grep -E "$OPERATOR|NAME" || true
run kubectl get olm -n "$OPERATOR_NS" -o wide || true
}
main() {
parse_args "$@" || {
print_usage
fail "failed to parse args"
return 1
}
$SHOW_HELP && {
print_usage
return 0
}
init || {
err "cannot gather operator details"
return 1
}
header "Resources of $OPERATOR - $OPERATOR_VERSION"
kubectl get csv "${OPERATOR}.$OPERATOR_VERSION" -n "$OPERATOR_NS" || {
kubectl get csv -n "$OPERATOR_NS" | grep -E "$OPERATOR|NAME" || true
line 50
info "$OPERATOR version found are ☝️"
fail "failed to find version $OPERATOR_VERSION of $OPERATOR."
list_olm_resources "$OPERATOR"
return 1
}
local label="operators.coreos.com/${OPERATOR}.$OPERATOR_NS="
header "Going to delete the following"
run kubectl get ns kepler || true
run kubectl get kepler -A
run kubectl get -n "$OPERATOR_NS" olm -l "$label"
run kubectl get crd,clusterrole,clusterrolebinding -l "$label" -A
run kubectl get operators "$OPERATOR.$OPERATOR_NS"
run kubectl get leases 0d9cbc82.sustainable.computing.io -n "$OPERATOR_NS" || true
run kubectl get catalogsource kepler-operator-catalog -n "$OPERATOR_NS" || true
line 50 heavy
! $DELETE_RESOURCES && {
info "To delete all resources listed above, rerun with --delete option added. \n"
info_run " $0 $* --delete"
return 0
}
header "Deleting $OPERATOR Version $OPERATOR_VERSION"
run kubectl delete kepler -A --all || true
run kubectl delete ns kepler || true
run kubectl delete -n "$OPERATOR_NS" olm -l "$label"
run kubectl delete operators,crd,clusterrole,clusterrolebinding -l "$label" -A || true
run kubectl delete operators "$OPERATOR.$OPERATOR_NS" || true
run kubectl delete leases 0d9cbc82.sustainable.computing.io -n "$OPERATOR_NS" || true
run kubectl delete catalogsource kepler-operator-catalog -n "$OPERATOR_NS" || true
ok "$OPERATOR version has been successfully uninstalled.\n"
}
main "$@"