blob: a3a3e5b0e8fc4fe01f0b1af82218c4f1d6a22a92 [file] [log] [blame]
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001#!/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
21BOLD="\033[1m"
22EBOLD="\033[0m"
23RED="\033[31m\033[1m"
24ERED="\033[0m"
25GREEN="\033[32m\033[1m"
26EGREEN="\033[0m"
27YELLOW="\033[33m\033[1m"
28EYELLOW="\033[0m"
29SAMELINE="\033[0K\r"
30
BjornMagnussonXA663566c2021-11-08 10:25:07 +010031__kube_scale_all_resources() {
32
33 echo " Scaling down in namespace $1 ..."
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010034 namespace=$1
BjornMagnussonXA663566c2021-11-08 10:25:07 +010035 resources="deployment replicaset statefulset"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010036 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
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010039 for resid in $result; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +010040 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
45 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010046 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010047 done
48 fi
49 done
BjornMagnussonXA663566c2021-11-08 10:25:07 +010050}
51
52__kube_wait_for_zero_count() {
53 echo " Wait for scaling to zero in namespace $1 ..."
54 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
60 T_START=$SECONDS
61 count=1
62 scaled=0
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
68 scaled=1
69 else
70 sleep 0.5
71 fi
72 else
73 count=0
74 fi
75 done
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"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010078 fi
79 done
BjornMagnussonXA663566c2021-11-08 10:25:07 +010080 fi
81 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010082}
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010083
BjornMagnussonXA663566c2021-11-08 10:25:07 +010084__kube_delete_all_resources() {
85 echo " Delete all in namespace $1 ..."
86 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 "
BjornMagnussonXA79e37002021-11-22 13:36:04 +010093 kubectl delete --grace-period=1 $restype $resid -n $namespace 1> /dev/null 2> /dev/null
BjornMagnussonXA663566c2021-11-08 10:25:07 +010094 done
95 fi
96 done
97}
98
99__kube_delete_all_pv() {
100 echo " Delete pv ..."
101 resources="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 "
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100107 kubectl delete --grace-period=1 $restype $resid 1> /dev/null 2> /dev/null
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100108 done
109 fi
110 done
111}
112
113__kube_wait_for_delete() {
114 echo " Wait for delete in namespace $1 ..."
115 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 "
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100122 kubectl delete --grace-period=1 $restype $resid -n $namespace #1> /dev/null 2> /dev/null
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100123 echo -ne " Waiting for $restype $resid in namespace $namespace with label autotest to be deleted..."$SAMELINE
124 T_START=$SECONDS
125 result="dummy"
126 while [ ! -z "$result" ]; do
127 sleep 0.5
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"
134 result=""
135 fi
136 done
137 done
138 fi
139 done
140}
141
142__kube_wait_for_delete_pv() {
143 echo " Wait for delete pv ..."
144 resources="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 "
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100150 kubectl delete --grace-period=1 $restype $resid -n $namespace #1> /dev/null 2> /dev/null
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100151 echo -ne " Waiting for $restype $resid with label autotest to be deleted..."$SAMELINE
152 T_START=$SECONDS
153 result="dummy"
154 while [ ! -z "$result" ]; do
155 sleep 0.5
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"
162 result=""
163 fi
164 done
165 done
166 fi
167 done
168}
169
170
171echo "Will remove all kube resources marked with label 'autotest'"
172
173# List all namespace and scale/delete per namespace
174nss=$(kubectl get ns -o jsonpath='{.items[*].metadata.name}')
175if [ ! -z "$nss" ]; then
176 for ns in $nss; do
177 __kube_scale_all_resources $ns
178 done
179 for ns in $nss; do
180 __kube_wait_for_zero_count $ns
181 done
182 for ns in $nss; do
183 __kube_delete_all_resources $ns
184 done
185 __kube_delete_all_pv
186 for ns in $nss; do
187 __kube_wait_for_delete $ns
188 done
189 __kube_wait_for_delete_pv
190fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100191echo "Done"