BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 1 | #!/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 | |
| 21 | BOLD="\033[1m" |
| 22 | EBOLD="\033[0m" |
| 23 | RED="\033[31m\033[1m" |
| 24 | ERED="\033[0m" |
| 25 | GREEN="\033[32m\033[1m" |
| 26 | EGREEN="\033[0m" |
| 27 | YELLOW="\033[33m\033[1m" |
| 28 | EYELLOW="\033[0m" |
| 29 | SAMELINE="\033[0K\r" |
| 30 | |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 31 | __kube_scale_all_resources() { |
| 32 | |
| 33 | echo " Scaling down in namespace $1 ..." |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 34 | namespace=$1 |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 35 | resources="deployment replicaset statefulset" |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 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 |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 39 | for resid in $result; do |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 40 | 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 |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 46 | fi |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 47 | done |
| 48 | fi |
| 49 | done |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 50 | } |
| 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" |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 78 | fi |
| 79 | done |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 80 | fi |
| 81 | done |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 82 | } |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 83 | |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 84 | __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 " |
BjornMagnussonXA | 79e3700 | 2021-11-22 13:36:04 +0100 | [diff] [blame] | 93 | kubectl delete --grace-period=1 $restype $resid -n $namespace 1> /dev/null 2> /dev/null |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 94 | 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 " |
BjornMagnussonXA | 79e3700 | 2021-11-22 13:36:04 +0100 | [diff] [blame] | 107 | kubectl delete --grace-period=1 $restype $resid 1> /dev/null 2> /dev/null |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 108 | 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 " |
BjornMagnussonXA | 79e3700 | 2021-11-22 13:36:04 +0100 | [diff] [blame] | 122 | kubectl delete --grace-period=1 $restype $resid -n $namespace #1> /dev/null 2> /dev/null |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 123 | 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 " |
BjornMagnussonXA | 79e3700 | 2021-11-22 13:36:04 +0100 | [diff] [blame] | 150 | kubectl delete --grace-period=1 $restype $resid -n $namespace #1> /dev/null 2> /dev/null |
BjornMagnussonXA | 663566c | 2021-11-08 10:25:07 +0100 | [diff] [blame] | 151 | 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 | |
| 171 | echo "Will remove all kube resources marked with label 'autotest'" |
| 172 | |
| 173 | # List all namespace and scale/delete per namespace |
| 174 | nss=$(kubectl get ns -o jsonpath='{.items[*].metadata.name}') |
| 175 | if [ ! -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 |
| 190 | fi |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 191 | echo "Done" |