Skip to content

Commit bcc6636

Browse files
incorporate review comments
Signed-off-by: Rohit Yadav <[email protected]>
1 parent b15606f commit bcc6636

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

hack/cleanup-ag.sh

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,38 +29,43 @@ set -o pipefail
2929

3030
export DRY_RUN=false
3131
export VERBOSE=false
32+
export NAMESPACE=default
3233
export KUBECONFIG=$HOME/.kube/config
3334

3435
debug() {
3536
if [[ "$VERBOSE" == "true" ]]; then
36-
echo $@
37+
echo "[debug] $@"
3738
fi
3839
}
3940

41+
_kubectl() {
42+
kubectl -n $NAMESPACE -o json $@
43+
}
44+
45+
_cmk() {
46+
cmk -u $CS_URL -k $CS_APIKEY -s $CS_SECRETKEY -o json $@
47+
}
48+
4049
get_affinity_groups() {
41-
kubectl get cloudstackaffinitygroups -o json -A | jq -r '.items[].metadata.name'
50+
_kubectl get cloudstackaffinitygroups | jq -r '.items[].metadata.name'
4251
}
4352

4453
get_cluster() {
4554
affinityGroup=$1
46-
kubectl get cloudstackaffinitygroup $affinityGroup -o json | jq -r '.metadata.labels."cluster.x-k8s.io/cluster-name"'
55+
_kubectl get cloudstackaffinitygroup $affinityGroup | jq -r '.metadata.labels."cluster.x-k8s.io/cluster-name"'
4756
}
4857

4958
get_cluster_credentials() {
5059
cluster=$1
51-
kubectl get cloudstackcluster $cluster -o json | jq -r '.spec.failureDomains[].acsEndpoint.name' | uniq
60+
_kubectl get cloudstackcluster $cluster | jq -r '.spec.failureDomains[].acsEndpoint.name' | uniq
5261
}
5362

5463
setup_acs_credentials() {
5564
credential=$1
56-
export CS_URL=$(kubectl get secret $credential -o json | jq -r '.data."api-url"' | base64 -D)
57-
export CS_APIKEY=$(kubectl get secret $credential -o json | jq -r '.data."api-key"' | base64 -D)
58-
export CS_SECRETKEY=$(kubectl get secret $credential -o json | jq -r '.data."secret-key"' | base64 -D)
59-
debug "Using CloudStack Control Plane URL: $CS_URL and CloudStack Account: $(run_cmk list users | jq -r '.user[] | .account + " and User: " + .username')"
60-
}
61-
62-
run_cmk() {
63-
cmk -u $CS_URL -k $CS_APIKEY -s $CS_SECRETKEY -o json $@
65+
export CS_URL=$(_kubectl get secret $credential | jq -r '.data."api-url"' | base64 -D)
66+
export CS_APIKEY=$(_kubectl get secret $credential | jq -r '.data."api-key"' | base64 -D)
67+
export CS_SECRETKEY=$(_kubectl get secret $credential | jq -r '.data."secret-key"' | base64 -D)
68+
debug "Using CloudStack Control Plane URL: $CS_URL and CloudStack Account: $(_cmk list users | jq -r '.user[] | .account + " and User: " + .username')"
6469
}
6570

6671
main() {
@@ -69,15 +74,15 @@ main() {
6974
cluster=$(get_cluster $ag)
7075
for credential in $(get_cluster_credentials $cluster); do
7176
setup_acs_credentials $credential
72-
CS_AG_ID=$(kubectl get cloudstackaffinitygroup $ag -o json | jq -r '.spec.id')
73-
CS_AG_VMS=$(run_cmk list affinitygroups id=$CS_AG_ID | jq -r '.affinitygroup[0].virtualmachineIds')
77+
CS_AG_ID=$(_kubectl get cloudstackaffinitygroup $ag | jq -r '.spec.id')
78+
CS_AG_VMS=$(_cmk list affinitygroups id=$CS_AG_ID | jq -r '.affinitygroup[0].virtualmachineIds')
7479
if [[ "$CS_AG_VMS" == "null" ]]; then
7580
echo "Found Affinity Group ($CS_AG_ID) with no instances assigned:" $ag
7681
if [[ "$DRY_RUN" == "false" ]]; then
77-
kubectl delete cloudstackaffinitygroup $ag
78-
echo "Affinity Group ($CS_AG_ID) $ag has been removed"
82+
_kubectl delete cloudstackaffinitygroup $ag
83+
echo "[info] Affinity Group ($CS_AG_ID) $ag has been removed"
7984
else
80-
echo "[DRY RUN] Affinity Group ($CS_AG_ID) $ag has been removed"
85+
echo "[dryrun] Affinity Group ($CS_AG_ID) $ag has been removed"
8186
fi
8287
fi
8388
done
@@ -98,6 +103,7 @@ help() {
98103
echo "Options:"
99104
echo "-d Runs the tools in dry-run mode"
100105
echo "-k Pass custom kube config, default: \$HOME/.kube/config"
106+
echo "-n Kubernetes namespace, default: default"
101107
echo "-h Print this help"
102108
echo "-v Verbose mode"
103109
echo
@@ -111,6 +117,8 @@ while getopts ":dkvh" option; do
111117
export KUBECONFIG=$OPTARG;;
112118
v)
113119
export VERBOSE=true;;
120+
n)
121+
export NAMESPACE=$OPTARG;;
114122
h)
115123
help
116124
exit;;
@@ -121,4 +129,26 @@ while getopts ":dkvh" option; do
121129
esac
122130
done
123131

132+
if ! command -v jq &> /dev/null
133+
then
134+
echo "[error] jq could not be found, please install first"
135+
exit 1
136+
fi
137+
138+
if ! command -v kubectl &> /dev/null
139+
then
140+
echo "[error] kubectl could not be found, please install first"
141+
exit 1
142+
fi
143+
144+
if ! command -v cmkd &> /dev/null
145+
then
146+
echo "[error] cmk could not be found, please install https://github.com/apache/cloudstack-cloudmonkey/releases/tag/6.4.0-rc1 or newer"
147+
exit 1
148+
fi
149+
150+
debug "[options] DRY_RUN=$DRY_RUN"
151+
debug "[options] VERBOSE=$VERBOSE"
152+
debug "[options] NAMESPACE=$NAMESPACE"
153+
debug "[options] KUBECONFIG=$KUBECONFIG"
124154
main

0 commit comments

Comments
 (0)