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_delete_all_resources() {
32 echo "Deleting all from namespace: "$1
34 resources="deployments replicaset statefulset services pods configmaps pvc pv"
35 deleted_resourcetypes=""
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 deleted_resourcetypes=$deleted_resourcetypes" "$restype
40 for resid in $result; do
41 if [ $restype == "replicaset" ] || [ $restype == "statefulset" ]; then
42 kubectl scale $restype $resid -n $namespace --replicas=0 1> /dev/null 2> /dev/null
45 while [ $count -ne 0 ]; do
46 count=$(kubectl get $restype $resid -n $namespace -o jsonpath='{.status.replicas}' 2> /dev/null)
47 echo -ne " Scaling $restype $resid from namespace $namespace with label autotest to 0,count=$count....$(($SECONDS-$T_START)) seconds"$SAMELINE
48 if [ $? -eq 0 ] && [ ! -z "$count" ]; then
54 echo -e " Scaled $restype $resid from namespace $namespace with label $labelname=$labelid to 0,count=$count....$(($SECONDS-$T_START)) seconds$GREEN OK $EGREEN"
56 echo -ne " Deleting $restype $resid from namespace $namespace with label autotest "$SAMELINE
57 kubectl delete $restype $resid -n $namespace 1> /dev/null 2> /dev/null
59 echo -e " Deleted $restype $resid from namespace $namespace with label autotest $GREEN OK $EGREEN"
61 echo -e " Deleted $restype $resid from namespace $namespace with label autotest $GREEN Does not exist - OK $EGREEN"
67 if [ ! -z "$deleted_resourcetypes" ]; then
68 for restype in $deleted_resources; do
69 echo -ne " Waiting for $restype in namespace $namespace with label autotest to be deleted..."$SAMELINE
72 while [ ! -z "$result" ]; do
74 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
75 echo -ne " Waiting for $restype in namespace $namespace with label autotest to be deleted...$(($SECONDS-$T_START)) seconds "$SAMELINE
76 if [ -z "$result" ]; then
77 echo -e " Waiting for $restype in namespace $namespace with label autotest to be deleted...$(($SECONDS-$T_START)) seconds $GREEN OK $EGREEN"
78 elif [ $(($SECONDS-$T_START)) -gt 300 ]; then
79 echo -e " Waiting for $restype in namespace $namespace with label autotest to be deleted...$(($SECONDS-$T_START)) seconds $RED Failed $ERED"
86 echo "Will remove all kube resources marked with label 'autotest'"
87 __kube_delete_all_resources nonrtric
88 __kube_delete_all_resources nonrtric-ft
89 __kube_delete_all_resources onap