20
20
# management cluster which are not assigned to any instances.
21
21
#
22
22
# Usage and help:
23
- # chmod +x cleanup-ag .sh
24
- # ./cleanup-ag .sh -h
23
+ # chmod +x cleanup-affinity-groups .sh
24
+ # ./cleanup-affinity-groups .sh -h
25
25
26
26
set -o errexit
27
27
set -o nounset
@@ -30,17 +30,19 @@ set -o pipefail
30
30
# script params
31
31
DRY_RUN=false
32
32
VERBOSE=false
33
+
33
34
# k8s params
34
35
NAMESPACE=default
35
36
KUBECONFIG=$HOME /.kube/config
37
+
36
38
# cmk params
37
39
CS_URL=
38
40
CS_APIKEY=
39
41
CS_SECRETKEY=
40
42
41
43
debug () {
42
44
if [[ " $VERBOSE " == " true" ]]; then
43
- echo " [debug] $@ "
45
+ echo -e " [debug] $@ "
44
46
fi
45
47
}
46
48
@@ -57,8 +59,8 @@ get_affinity_groups() {
57
59
}
58
60
59
61
get_cluster () {
60
- local affinityGroup =$1
61
- _kubectl get cloudstackaffinitygroup $affinityGroup | jq -r ' .metadata.labels."cluster.x-k8s.io/cluster-name"'
62
+ local affinitygroup =$1
63
+ _kubectl get cloudstackaffinitygroup $affinitygroup | jq -r ' .metadata.labels."cluster.x-k8s.io/cluster-name"'
62
64
}
63
65
64
66
get_cluster_credentials () {
@@ -75,20 +77,22 @@ setup_acs_credentials() {
75
77
}
76
78
77
79
main () {
78
- for ag in $( get_affinity_groups) ; do
79
- echo " [info] Checking CloudStack Affinity Group: $ag "
80
+ local ags=$( get_affinity_groups)
81
+ debug " Affinity groups in the namespace $NAMESPACE :\n$ags "
82
+ for ag in $ags ; do
83
+ echo -e " \033[0;32m[info]\033[0m Checking CloudStack Affinity Group: $ag "
80
84
local cluster=$( get_cluster $ag )
81
85
for credential in $( get_cluster_credentials $cluster ) ; do
82
86
setup_acs_credentials $credential
83
- CS_AG_ID =$( _kubectl get cloudstackaffinitygroup $ag | jq -r ' .spec.id' )
84
- CS_AG_VMS =$( _cmk list affinitygroups id=$CS_AG_ID | jq -r ' .affinitygroup[0].virtualmachineIds' )
85
- if [[ " $CS_AG_VMS " == " null" ]]; then
86
- echo " [ info] Found Affinity Group ($CS_AG_ID ) with no instances assigned:" $ag
87
+ local ag_uuid =$( _kubectl get cloudstackaffinitygroup $ag | jq -r ' .spec.id' )
88
+ local ag_instances =$( _cmk list affinitygroups id=$ag_uuid | jq -r ' .affinitygroup[0].virtualmachineIds' )
89
+ if [[ " $ag_instances " == " null" ]]; then
90
+ echo -e " \033[0;35m[ info]\033[0m Found Affinity Group ($ag_uuid ) with no instances assigned: $ag "
87
91
if [[ " $DRY_RUN " == " false" ]]; then
88
92
kubectl -n $NAMESPACE delete cloudstackaffinitygroup $ag
89
- echo " [ info] Affinity Group ($CS_AG_ID ) $ag has been removed"
93
+ echo -e " \033[0;31m[ info]\033[0m Affinity Group ($ag_uuid ) $ag has been removed"
90
94
else
91
- echo " [ dryrun] Affinity Group ($CS_AG_ID ) $ag has been removed"
95
+ echo -e " \033[0;35m[info]\033[0m [ dryrun] Affinity Group ($ag_uuid ) $ag has been removed"
92
96
fi
93
97
fi
94
98
done
@@ -115,7 +119,7 @@ help() {
115
119
echo
116
120
}
117
121
118
- while getopts " :dkvh " option; do
122
+ while getopts " :dk:vn:h " option; do
119
123
case $option in
120
124
d)
121
125
DRY_RUN=true;;
0 commit comments