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
10 # http://www.apache.org/licenses/LICENSE-2.0
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=================================================
19 # Script to clean all resources from kubernetes having the label 'autotest', i.e started by autotest
25 GREEN="\033[32m\033[1m"
27 YELLOW="\033[33m\033[1m"
31 __kube_scale_all_resources() {
33 echo " Scaling down in namespace $1 ..."
35 resources="deployment replicaset statefulset"
36 for restype in $resources; do
37 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
38 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
39 for resid in $result; do
40 count=$(kubectl get $restype $resid -n $namespace -o jsonpath='{.status.replicas}' 2> /dev/null)
41 if [ $? -eq 0 ] && [ ! -z "$count" ]; then
42 if [ $count -ne 0 ]; then
43 echo " Scaling $restype $resid in namespace $namespace with label autotest to 0, current count=$count."
44 kubectl scale $restype $resid -n $namespace --replicas=0 1> /dev/null 2> /dev/null
52 __kube_wait_for_zero_count() {
53 echo " Wait for scaling to zero in namespace $1 ..."
55 resources="deployment replicaset statefulset"
56 for restype in $resources; do
57 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
58 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
59 for resid in $result; do
63 while [ $count -gt 0 ]; do
64 count=$(kubectl get $restype $resid -n $namespace -o jsonpath='{.status.replicas}' 2> /dev/null)
65 if [ $? -eq 0 ] && [ ! -z "$count" ]; then
66 if [ $count -ne 0 ]; then
67 echo -ne " Scaling $restype $resid in namespace $namespace with label autotest to 0, current count=$count....$(($SECONDS-$T_START)) seconds"$SAMELINE
76 if [ $scaled -eq 1 ]; then
77 echo -e " Scaling $restype $resid in namespace $namespace with label autotest to 0, current count=$count....$(($SECONDS-$T_START)) seconds $GREEN OK $EGREEN"
84 __kube_delete_all_resources() {
85 echo " Delete all in namespace $1 ..."
87 resources="deployments replicaset statefulset services pods configmaps pvc "
88 for restype in $resources; do
89 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
90 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
91 for resid in $result; do
92 echo " Deleting $restype $resid in namespace $namespace with label autotest "
93 kubectl delete $restype $resid -n $namespace 1> /dev/null 2> /dev/null
99 __kube_delete_all_pv() {
100 echo " Delete pv ..."
102 for restype in $resources; do
103 result=$(kubectl get $restype -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
104 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
105 for resid in $result; do
106 echo " Deleting $restype $resid with label autotest "
107 kubectl delete $restype $resid 1> /dev/null 2> /dev/null
113 __kube_wait_for_delete() {
114 echo " Wait for delete in namespace $1 ..."
116 resources="deployments replicaset statefulset services pods configmaps pvc "
117 for restype in $resources; do
118 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
119 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
120 for resid in $result; do
121 echo " Deleting $restype $resid in namespace $namespace with label autotest "
122 kubectl delete $restype $resid -n $namespace #1> /dev/null 2> /dev/null
123 echo -ne " Waiting for $restype $resid in namespace $namespace with label autotest to be deleted..."$SAMELINE
126 while [ ! -z "$result" ]; do
128 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
129 echo -ne " Waiting for $restype $resid in namespace $namespace with label autotest to be deleted...$(($SECONDS-$T_START)) seconds "$SAMELINE
130 if [ -z "$result" ]; then
131 echo -e " Waiting for $restype $resid in namespace $namespace with label autotest to be deleted...$(($SECONDS-$T_START)) seconds $GREEN OK $EGREEN"
132 elif [ $(($SECONDS-$T_START)) -gt 300 ]; then
133 echo -e " Waiting for $restype $resid in namespace $namespace with label autotest to be deleted...$(($SECONDS-$T_START)) seconds $RED Failed $ERED"
142 __kube_wait_for_delete_pv() {
143 echo " Wait for delete pv ..."
145 for restype in $resources; do
146 result=$(kubectl get $restype -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
147 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
148 for resid in $result; do
149 echo " Deleting $restype $resid with label autotest "
150 kubectl delete $restype $resid -n $namespace #1> /dev/null 2> /dev/null
151 echo -ne " Waiting for $restype $resid with label autotest to be deleted..."$SAMELINE
154 while [ ! -z "$result" ]; do
156 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
157 echo -ne " Waiting for $restype $resid with label autotest to be deleted...$(($SECONDS-$T_START)) seconds "$SAMELINE
158 if [ -z "$result" ]; then
159 echo -e " Waiting for $restype $resid with label autotest to be deleted...$(($SECONDS-$T_START)) seconds $GREEN OK $EGREEN"
160 elif [ $(($SECONDS-$T_START)) -gt 300 ]; then
161 echo -e " Waiting for $restype $resid with label autotest to be deleted...$(($SECONDS-$T_START)) seconds $RED Failed $ERED"
171 echo "Will remove all kube resources marked with label 'autotest'"
173 # List all namespace and scale/delete per namespace
174 nss=$(kubectl get ns -o jsonpath='{.items[*].metadata.name}')
175 if [ ! -z "$nss" ]; then
177 __kube_scale_all_resources $ns
180 __kube_wait_for_zero_count $ns
183 __kube_delete_all_resources $ns
187 __kube_wait_for_delete $ns
189 __kube_wait_for_delete_pv