blob: 8e2f676a53467d1b3acc4920c07218f896b7ab46 [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
31__kube_delete_all_resources() {
32 echo "Deleting all from namespace: "$1
33 namespace=$1
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010034 resources="deployments replicaset statefulset services pods configmaps pvc pv"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010035 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
43 T_START=$SECONDS
44 count=1
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
49 sleep 0.5
50 else
51 count=0
52 fi
53 done
54 echo -e " Scaled $restype $resid from namespace $namespace with label $labelname=$labelid to 0,count=$count....$(($SECONDS-$T_START)) seconds$GREEN OK $EGREEN"
55 fi
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
58 if [ $? -eq 0 ]; then
59 echo -e " Deleted $restype $resid from namespace $namespace with label autotest $GREEN OK $EGREEN"
60 else
61 echo -e " Deleted $restype $resid from namespace $namespace with label autotest $GREEN Does not exist - OK $EGREEN"
62 fi
63 #fi
64 done
65 fi
66 done
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
70 T_START=$SECONDS
71 result="dummy"
72 while [ ! -z "$result" ]; do
73 sleep 0.5
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"
80 result=""
81 fi
82 done
83 done
84 fi
85}
86echo "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
90
91echo "Done"