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"
33 __kube_scale_all_resources() {
35 echo " Scaling down in 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
54 __kube_wait_for_zero_count() {
55 echo " Wait for scaling to zero in 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
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
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"
86 __kube_delete_all_resources() {
87 echo " Delete all in 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
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
115 __kube_wait_for_delete() {
116 echo " Wait for delete in 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
128 while [ ! -z "$result" ]; do
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"
144 __kube_wait_for_delete_pv() {
145 echo " Wait for delete 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
156 while [ ! -z "$result" ]; do
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"
173 echo "Will remove all kube resources marked with label 'autotest'"
176 echo "Usage: clean_kube.sh [--kubeconfig <kube-config-file>] | [--kubecontext <context name>]"
179 if [ $# -eq 0 ]; then
181 elif [ $# -eq 1 ]; then
184 elif [ $# -eq 2 ]; then
185 if [ $1 == "--kubeconfig" ]; then
187 echo "File $2 for --kubeconfig is not found"
191 KUBECONF="--kubeconfig $2"
192 elif [ $1 == "--kubecontext" ]; then
194 echo "No context found for --kubecontext"
198 KUBECONF="--context $2"
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
212 __kube_scale_all_resources $ns
215 __kube_wait_for_zero_count $ns
218 __kube_delete_all_resources $ns
222 __kube_wait_for_delete $ns
224 __kube_wait_for_delete_pv