Adaptation of test env to helm chart
[nonrtric.git] / test / common / clean_kube.sh
index 8e2f676..405c194 100755 (executable)
@@ -28,64 +28,164 @@ YELLOW="\033[33m\033[1m"
 EYELLOW="\033[0m"
 SAMELINE="\033[0K\r"
 
-__kube_delete_all_resources() {
-       echo "Deleting all from namespace: "$1
+__kube_scale_all_resources() {
+
+       echo " Scaling down in namespace $1 ..."
        namespace=$1
-       resources="deployments replicaset statefulset services pods configmaps pvc pv"
-       deleted_resourcetypes=""
+       resources="deployment replicaset statefulset"
        for restype in $resources; do
                result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
                if [ $? -eq 0 ] && [ ! -z "$result" ]; then
-                       deleted_resourcetypes=$deleted_resourcetypes" "$restype
                        for resid in $result; do
-                               if [ $restype == "replicaset" ] || [ $restype == "statefulset" ]; then
-                                       kubectl scale  $restype $resid -n $namespace --replicas=0 1> /dev/null 2> /dev/null
-                                       T_START=$SECONDS
-                                       count=1
-                                       while [ $count -ne 0 ]; do
-                                               count=$(kubectl get $restype $resid  -n $namespace -o jsonpath='{.status.replicas}' 2> /dev/null)
-                                               echo -ne "  Scaling $restype $resid from namespace $namespace with label autotest to 0,count=$count....$(($SECONDS-$T_START)) seconds"$SAMELINE
-                                               if [ $? -eq 0 ] && [ ! -z "$count" ]; then
-                                                       sleep 0.5
+                               count=$(kubectl get $restype $resid  -n $namespace -o jsonpath='{.status.replicas}' 2> /dev/null)
+                               if [ $? -eq 0 ] && [ ! -z "$count" ]; then
+                                       if [ $count -ne 0 ]; then
+                                               echo "  Scaling $restype $resid in namespace $namespace with label autotest to 0, current count=$count."
+                                               kubectl scale  $restype $resid -n $namespace --replicas=0 1> /dev/null 2> /dev/null
+                                       fi
+                               fi
+                       done
+               fi
+       done
+}
+
+__kube_wait_for_zero_count() {
+       echo " Wait for scaling to zero in namespace $1 ..."
+       namespace=$1
+       resources="deployment replicaset statefulset"
+       for restype in $resources; do
+               result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
+               if [ $? -eq 0 ] && [ ! -z "$result" ]; then
+                       for resid in $result; do
+                               T_START=$SECONDS
+                               count=1
+                               scaled=0
+                               while [ $count -gt 0 ]; do
+                                       count=$(kubectl get $restype $resid  -n $namespace -o jsonpath='{.status.replicas}' 2> /dev/null)
+                                       if [ $? -eq 0 ] && [ ! -z "$count" ]; then
+                                               if [ $count -ne 0 ]; then
+                                                       echo -ne "  Scaling $restype $resid in namespace $namespace with label autotest to 0, current count=$count....$(($SECONDS-$T_START)) seconds"$SAMELINE
+                                                       scaled=1
                                                else
-                                                       count=0
+                                                       sleep 0.5
                                                fi
-                                       done
-                                       echo -e "  Scaled $restype $resid from namespace $namespace with label $labelname=$labelid to 0,count=$count....$(($SECONDS-$T_START)) seconds$GREEN OK $EGREEN"
+                                       else
+                                               count=0
+                                       fi
+                               done
+                               if [ $scaled -eq 1 ]; then
+                                       echo -e "  Scaling $restype $resid in namespace $namespace with label autotest to 0, current count=$count....$(($SECONDS-$T_START)) seconds $GREEN OK $EGREEN"
                                fi
-                               echo -ne "  Deleting $restype $resid from namespace $namespace with label autotest "$SAMELINE
+                       done
+               fi
+       done
+}
+
+__kube_delete_all_resources() {
+       echo " Delete all in namespace $1 ..."
+       namespace=$1
+       resources="deployments replicaset statefulset services pods configmaps pvc "
+       for restype in $resources; do
+               result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
+               if [ $? -eq 0 ] && [ ! -z "$result" ]; then
+                       for resid in $result; do
+                               echo  "  Deleting $restype $resid in namespace $namespace with label autotest "
                                kubectl delete $restype $resid -n $namespace 1> /dev/null 2> /dev/null
-                               if [ $? -eq 0 ]; then
-                                       echo -e "  Deleted $restype $resid from namespace $namespace with label autotest $GREEN OK $EGREEN"
-                               else
-                                       echo -e "  Deleted $restype $resid from namespace $namespace with label autotest $GREEN Does not exist - OK $EGREEN"
-                               fi
-                               #fi
                        done
                fi
        done
-       if [ ! -z "$deleted_resourcetypes" ]; then
-               for restype in $deleted_resources; do
-                       echo -ne "  Waiting for $restype in namespace $namespace with label autotest to be deleted..."$SAMELINE
-                       T_START=$SECONDS
-                       result="dummy"
-                       while [ ! -z "$result" ]; do
-                               sleep 0.5
-                               result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
-                               echo -ne "  Waiting for $restype in namespace $namespace with label autotest to be deleted...$(($SECONDS-$T_START)) seconds "$SAMELINE
-                               if [ -z "$result" ]; then
-                                       echo -e " Waiting for $restype in namespace $namespace with label autotest to be deleted...$(($SECONDS-$T_START)) seconds $GREEN OK $EGREEN"
-                               elif [ $(($SECONDS-$T_START)) -gt 300 ]; then
-                                       echo -e " Waiting for $restype in namespace $namespace with label autotest to be deleted...$(($SECONDS-$T_START)) seconds $RED Failed $ERED"
-                                       result=""
-                               fi
+}
+
+__kube_delete_all_pv() {
+       echo " Delete pv ..."
+       resources="pv"
+       for restype in $resources; do
+               result=$(kubectl get $restype -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
+               if [ $? -eq 0 ] && [ ! -z "$result" ]; then
+                       for resid in $result; do
+                               echo  "  Deleting $restype $resid with label autotest "
+                               kubectl delete $restype $resid 1> /dev/null 2> /dev/null
                        done
-               done
-       fi
+               fi
+       done
+}
+
+__kube_wait_for_delete() {
+       echo " Wait for delete in namespace $1 ..."
+       namespace=$1
+       resources="deployments replicaset statefulset services pods configmaps pvc "
+       for restype in $resources; do
+               result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
+               if [ $? -eq 0 ] && [ ! -z "$result" ]; then
+                       for resid in $result; do
+                               echo  "  Deleting $restype $resid in namespace $namespace with label autotest "
+                               kubectl delete $restype $resid -n $namespace #1> /dev/null 2> /dev/null
+                               echo -ne "  Waiting for $restype $resid in namespace $namespace with label autotest to be deleted..."$SAMELINE
+                               T_START=$SECONDS
+                               result="dummy"
+                               while [ ! -z "$result" ]; do
+                                       sleep 0.5
+                                       result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
+                                       echo -ne "  Waiting for $restype $resid in namespace $namespace with label autotest to be deleted...$(($SECONDS-$T_START)) seconds "$SAMELINE
+                                       if [ -z "$result" ]; then
+                                               echo -e " Waiting for $restype $resid in namespace $namespace with label autotest to be deleted...$(($SECONDS-$T_START)) seconds $GREEN OK $EGREEN"
+                                       elif [ $(($SECONDS-$T_START)) -gt 300 ]; then
+                                               echo -e " Waiting for $restype $resid in namespace $namespace with label autotest to be deleted...$(($SECONDS-$T_START)) seconds $RED Failed $ERED"
+                                               result=""
+                                       fi
+                               done
+                       done
+               fi
+       done
 }
+
+__kube_wait_for_delete_pv() {
+       echo " Wait for delete pv ..."
+       resources="pv "
+       for restype in $resources; do
+               result=$(kubectl get $restype -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
+               if [ $? -eq 0 ] && [ ! -z "$result" ]; then
+                       for resid in $result; do
+                               echo  "  Deleting $restype $resid with label autotest "
+                               kubectl delete $restype $resid -n $namespace #1> /dev/null 2> /dev/null
+                               echo -ne "  Waiting for $restype $resid with label autotest to be deleted..."$SAMELINE
+                               T_START=$SECONDS
+                               result="dummy"
+                               while [ ! -z "$result" ]; do
+                                       sleep 0.5
+                                       result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
+                                       echo -ne "  Waiting for $restype $resid with label autotest to be deleted...$(($SECONDS-$T_START)) seconds "$SAMELINE
+                                       if [ -z "$result" ]; then
+                                               echo -e " Waiting for $restype $resid with label autotest to be deleted...$(($SECONDS-$T_START)) seconds $GREEN OK $EGREEN"
+                                       elif [ $(($SECONDS-$T_START)) -gt 300 ]; then
+                                               echo -e " Waiting for $restype $resid with label autotest to be deleted...$(($SECONDS-$T_START)) seconds $RED Failed $ERED"
+                                               result=""
+                                       fi
+                               done
+                       done
+               fi
+       done
+}
+
+
 echo "Will remove all kube resources marked with label 'autotest'"
-__kube_delete_all_resources nonrtric
-__kube_delete_all_resources nonrtric-ft
-__kube_delete_all_resources onap
 
+# List all namespace and scale/delete per namespace
+nss=$(kubectl get ns  -o jsonpath='{.items[*].metadata.name}')
+if [ ! -z "$nss" ]; then
+       for ns in $nss; do
+               __kube_scale_all_resources $ns
+       done
+       for ns in $nss; do
+               __kube_wait_for_zero_count $ns
+       done
+       for ns in $nss; do
+               __kube_delete_all_resources $ns
+       done
+       __kube_delete_all_pv
+       for ns in $nss; do
+               __kube_wait_for_delete $ns
+       done
+       __kube_wait_for_delete_pv
+fi
 echo "Done"
\ No newline at end of file