Test case updates
[nonrtric.git] / test / common / clean_kube.sh
1 #!/bin/bash
2
3 #  ============LICENSE_START===============================================
4 #  Copyright (C) 2021 Nordix Foundation. All rights reserved.
5 #  ========================================================================
6 #  Licensed under the Apache License, Version 2.0 (the "License");
7 #  you may not use this file except in compliance with the License.
8 #  You may obtain a copy of the License at
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #  Unless required by applicable law or agreed to in writing, software
13 #  distributed under the License is distributed on an "AS IS" BASIS,
14 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #  See the License for the specific language governing permissions and
16 #  limitations under the License.
17 #  ============LICENSE_END=================================================
18
19 # Script to clean all resources from kubernetes having the label 'autotest', i.e started by autotest
20
21 BOLD="\033[1m"
22 EBOLD="\033[0m"
23 RED="\033[31m\033[1m"
24 ERED="\033[0m"
25 GREEN="\033[32m\033[1m"
26 EGREEN="\033[0m"
27 YELLOW="\033[33m\033[1m"
28 EYELLOW="\033[0m"
29 SAMELINE="\033[0K\r"
30
31 KUBECONF=""
32
33 __kube_scale_all_resources() {
34
35         echo " Scaling down in namespace $1 ..."
36         namespace=$1
37         resources="deployment replicaset statefulset"
38         for restype in $resources; do
39                 result=$(kubectl $KUBECONF get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
40                 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
41                         for resid in $result; do
42                                 count=$(kubectl $KUBECONF get $restype $resid  -n $namespace -o jsonpath='{.status.replicas}' 2> /dev/null)
43                                 if [ $? -eq 0 ] && [ ! -z "$count" ]; then
44                                         if [ $count -ne 0 ]; then
45                                                 echo "  Scaling $restype $resid in namespace $namespace with label autotest to 0, current count=$count."
46                                                 kubectl $KUBECONF scale  $restype $resid -n $namespace --replicas=0 1> /dev/null 2> /dev/null
47                                         fi
48                                 fi
49                         done
50                 fi
51         done
52 }
53
54 __kube_wait_for_zero_count() {
55         echo " Wait for scaling to zero in namespace $1 ..."
56         namespace=$1
57         resources="deployment replicaset statefulset"
58         for restype in $resources; do
59                 result=$(kubectl $KUBECONF get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
60                 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
61                         for resid in $result; do
62                                 T_START=$SECONDS
63                                 count=1
64                                 scaled=0
65                                 while [ $count -gt 0 ]; do
66                                         count=$(kubectl $KUBECONF get $restype $resid  -n $namespace -o jsonpath='{.status.replicas}' 2> /dev/null)
67                                         if [ $? -eq 0 ] && [ ! -z "$count" ]; then
68                                                 if [ $count -ne 0 ]; then
69                                                         echo -ne "  Scaling $restype $resid in namespace $namespace with label autotest to 0, current count=$count....$(($SECONDS-$T_START)) seconds"$SAMELINE
70                                                         scaled=1
71                                                 else
72                                                         sleep 0.5
73                                                 fi
74                                         else
75                                                 count=0
76                                         fi
77                                 done
78                                 if [ $scaled -eq 1 ]; then
79                                         echo -e "  Scaling $restype $resid in namespace $namespace with label autotest to 0, current count=$count....$(($SECONDS-$T_START)) seconds $GREEN OK $EGREEN"
80                                 fi
81                         done
82                 fi
83         done
84 }
85
86 __kube_delete_all_resources() {
87         echo " Delete all in namespace $1 ..."
88         namespace=$1
89         resources="deployments replicaset statefulset services pods configmaps pvc serviceaccounts secrets authorizationpolicies requestauthentications"
90         for restype in $resources; do
91                 result=$(kubectl $KUBECONF get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}' 2> /dev/null)
92                 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
93                         for resid in $result; do
94                                 echo  "  Deleting $restype $resid in namespace $namespace with label autotest "
95                                 kubectl $KUBECONF delete --grace-period=1 $restype $resid -n $namespace 1> /dev/null 2> /dev/null
96                         done
97                 fi
98         done
99 }
100
101 __kube_delete_all_pv() {
102         echo " Delete all non-namespaced resources ..."
103         resources="pv clusterrolebindings"
104         for restype in $resources; do
105                 result=$(kubectl $KUBECONF get $restype -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
106                 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
107                         for resid in $result; do
108                                 echo  "  Deleting $restype $resid with label autotest "
109                                 kubectl $KUBECONF delete --grace-period=1 $restype $resid 1> /dev/null 2> /dev/null
110                         done
111                 fi
112         done
113 }
114
115 __kube_wait_for_delete() {
116         echo " Wait for delete in namespace $1 ..."
117         namespace=$1
118         resources="deployments replicaset statefulset services pods configmaps pvc secrets"
119         for restype in $resources; do
120                 result=$(kubectl $KUBECONF get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
121                 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
122                         for resid in $result; do
123                                 echo  "  Deleting $restype $resid in namespace $namespace with label autotest "
124                                 kubectl $KUBECONF delete --grace-period=1 $restype $resid -n $namespace #1> /dev/null 2> /dev/null
125                                 echo -ne "  Waiting for $restype $resid in namespace $namespace with label autotest to be deleted..."$SAMELINE
126                                 T_START=$SECONDS
127                                 result="dummy"
128                                 while [ ! -z "$result" ]; do
129                                         sleep 0.5
130                                         result=$(kubectl $KUBECONF get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
131                                         echo -ne "  Waiting for $restype $resid in namespace $namespace with label autotest to be deleted...$(($SECONDS-$T_START)) seconds "$SAMELINE
132                                         if [ -z "$result" ]; then
133                                                 echo -e " Waiting for $restype $resid in namespace $namespace with label autotest to be deleted...$(($SECONDS-$T_START)) seconds $GREEN OK $EGREEN"
134                                         elif [ $(($SECONDS-$T_START)) -gt 300 ]; then
135                                                 echo -e " Waiting for $restype $resid in namespace $namespace with label autotest to be deleted...$(($SECONDS-$T_START)) seconds $RED Failed $ERED"
136                                                 result=""
137                                         fi
138                                 done
139                         done
140                 fi
141         done
142 }
143
144 __kube_wait_for_delete_pv() {
145         echo " Wait for delete pv ..."
146         resources="pv "
147         for restype in $resources; do
148                 result=$(kubectl $KUBECONF get $restype -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
149                 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
150                         for resid in $result; do
151                                 echo  "  Deleting $restype $resid with label autotest "
152                                 kubectl $KUBECONF delete --grace-period=1 $restype $resid -n $namespace #1> /dev/null 2> /dev/null
153                                 echo -ne "  Waiting for $restype $resid with label autotest to be deleted..."$SAMELINE
154                                 T_START=$SECONDS
155                                 result="dummy"
156                                 while [ ! -z "$result" ]; do
157                                         sleep 0.5
158                                         result=$(kubectl $KUBECONF get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
159                                         echo -ne "  Waiting for $restype $resid with label autotest to be deleted...$(($SECONDS-$T_START)) seconds "$SAMELINE
160                                         if [ -z "$result" ]; then
161                                                 echo -e " Waiting for $restype $resid with label autotest to be deleted...$(($SECONDS-$T_START)) seconds $GREEN OK $EGREEN"
162                                         elif [ $(($SECONDS-$T_START)) -gt 300 ]; then
163                                                 echo -e " Waiting for $restype $resid with label autotest to be deleted...$(($SECONDS-$T_START)) seconds $RED Failed $ERED"
164                                                 result=""
165                                         fi
166                                 done
167                         done
168                 fi
169         done
170 }
171
172
173 echo "Will remove all kube resources marked with label 'autotest'"
174
175 print_usage() {
176     echo "Usage: clean_kube.sh [--kubeconfig <kube-config-file>] | [--kubecontext <context name>]"
177 }
178
179 if [ $# -eq 0 ]; then
180 :
181 elif [ $# -eq 1 ]; then
182     print_usage
183     exit
184 elif [ $# -eq 2 ]; then
185     if [ $1 == "--kubeconfig" ]; then
186         if [ ! -f $2 ]; then
187             echo "File $2 for --kubeconfig is not found"
188             print_usage
189             exit
190         fi
191         KUBECONF="--kubeconfig $2"
192     elif [ $1 == "--kubecontext" ]; then
193         if [ -z $2 ]; then
194             echo "No context found for --kubecontext"
195             print_usage
196             exit
197         fi
198         KUBECONF="--context $2"
199     else
200         print_usage
201         exit
202     fi
203 else
204     print_usage
205     exit
206 fi
207
208 # List all namespace and scale/delete per namespace
209 nss=$(kubectl $KUBECONF get ns  -o jsonpath='{.items[*].metadata.name}')
210 if [ ! -z "$nss" ]; then
211         for ns in $nss; do
212                 __kube_scale_all_resources $ns
213         done
214         for ns in $nss; do
215                 __kube_wait_for_zero_count $ns
216         done
217         for ns in $nss; do
218                 __kube_delete_all_resources $ns
219         done
220         __kube_delete_all_pv
221         for ns in $nss; do
222                 __kube_wait_for_delete $ns
223         done
224         __kube_wait_for_delete_pv
225 fi
226 echo "Done"