|
| 1 | +#!/bin/bash |
| 2 | +# Copyright 2018, Oracle Corporation and/or its affiliates. All rights reserved. |
| 3 | +# Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl. |
| 4 | + |
| 5 | +# This script does a best-effort k8s delete of run.sh integration test k8s artifacts. |
| 6 | +# It double checks whether its deletes are succeeding, and if not reports an "Error" and continue regardless. |
| 7 | +# |
| 8 | +# Note that it deletes elk and WL domain PV directories (as root). |
| 9 | + |
| 10 | +DOMAINS=(domain1 domain2 domain3 domain4) |
| 11 | +DOMAIN_NAMESPACES=(default default test test2) |
| 12 | +DCOUNT=${#DOMAINS[@]} |
| 13 | + |
| 14 | +OPER_NAMESPACES=(weblogic-operator weblogic-operator-2) |
| 15 | +OCOUNT=${#OPER_NAMESPACES[@]} |
| 16 | + |
| 17 | +echo @@ Cleaning up $OCOUNT operators |
| 18 | + |
| 19 | +export HOST_PATH=${HOST_PATH:-/scratch} |
| 20 | + |
| 21 | +SCRIPTPATH="$( cd "$(dirname "$0")" > /dev/null 2>&1 ; pwd -P )" |
| 22 | +export PROJECT_ROOT="$SCRIPTPATH/../../.." |
| 23 | +export RESULT_ROOT=${RESULT_ROOT:-/scratch/k8s_dir} |
| 24 | +export RESULT_DIR="$RESULT_ROOT/acceptance_test_tmp" |
| 25 | +export HOST_DIR="/scratch/k8s_dir/acceptance_test_tmp" |
| 26 | +mkdir -m 777 -p $RESULT_DIR |
| 27 | + |
| 28 | +function waitForDelete { |
| 29 | + maxwaitsecs=120 |
| 30 | + echo "@@ Waiting up to $maxwaitsecs seconds for ${1:?} that contain string ${2:?} to delete." |
| 31 | + |
| 32 | + artcount=1 |
| 33 | + mstart=`date +%s` |
| 34 | + while : ; do |
| 35 | + kubectl get $1 --show-labels=true --all-namespaces=true > $RESULT_DIR/kinv.out.tmp 2>&1 |
| 36 | + egrep -e "($2)" $RESULT_DIR/kinv.out.tmp > $RESULT_DIR/kinv.out2.tmp |
| 37 | + artcount="`cat $RESULT_DIR/kinv.out2.tmp | wc -l`" |
| 38 | + mnow=`date +%s` |
| 39 | + echo |
| 40 | + echo "@@ Waiting for $artcount artifacts to delete. Wait time $((mnow - mstart)) seconds (max=$maxwaitsecs). Waiting for:" |
| 41 | + cat $RESULT_DIR/kinv.out2.tmp |
| 42 | + if [ $((artcount)) -eq 0 ]; then |
| 43 | + echo "@@ No artifacts found." |
| 44 | + break |
| 45 | + fi |
| 46 | + if [ $((mnow - mstart)) -gt $((maxwaitsecs)) ]; then |
| 47 | + echo "@@ Error: $maxwaitsecs seconds reached. Giving up." |
| 48 | + break |
| 49 | + fi |
| 50 | + sleep 5 |
| 51 | + done |
| 52 | +} |
| 53 | + |
| 54 | +for ((i=0;i<DCOUNT;i++)); do |
| 55 | + curdomain=${DOMAINS[i]} |
| 56 | + curns=${DOMAIN_NAMESPACES[i]} |
| 57 | + |
| 58 | + echo @@ Deleting domain ${curdomain} in namespace $curn |
| 59 | + kubectl -n $curns delete domain $curdomain --ignore-not-found=true |
| 60 | + |
| 61 | + # Give operator some time to digest the domain deletion |
| 62 | + sleep 3 |
| 63 | + |
| 64 | + echo @@ Deleting create domain ${curdomain} job in namespace $curns |
| 65 | + kubectl -n $curns delete job domain-${curdomain}-job --ignore-not-found=true |
| 66 | + |
| 67 | + echo @@ Deleting domain pv and pvc for domain ${curdomain} in namespace $curns |
| 68 | + kubectl delete pv ${curdomain}-pv --ignore-not-found=true |
| 69 | + kubectl -n $curns delete pvc ${curdomain}-pv-claim --ignore-not-found=true |
| 70 | + |
| 71 | + echo @@ Deleting ${curdomain}-weblogic-credentials secret in namespace $curns |
| 72 | + kubectl -n $curns delete secret ${curdomain}-weblogic-credentials --ignore-not-found=true |
| 73 | + |
| 74 | + #echo @@ Deleting ${curdomain} traefik in namespace $curns |
| 75 | + #kubectl delete -f $RESULT_DIR/${curns}-${curdomain}/traefik-deployment.yaml --ignore-not-found=true |
| 76 | + #kubectl delete -f $RESULT_DIR/${curns}-${curdomain}/traefik-rbac.yaml --ignore-not-found=true |
| 77 | + kubectl -n $curns delete deploy ${curdomain}-cluster-1-traefik --ignore-not-found=true |
| 78 | + kubectl -n $curns delete service ${curdomain}-cluster-1-traefik --ignore-not-found=true |
| 79 | + kubectl -n $curns delete service ${curdomain}-cluster-1-traefik-dashboard --ignore-not-found=true |
| 80 | + kubectl -n $curns delete cm ${curdomain}-cluster-1-traefik --ignore-not-found=true |
| 81 | + kubectl -n $curns delete serviceaccount ${curdomain}-cluster-1-traefik --ignore-not-found=true |
| 82 | + kubectl -n $curns delete clusterrole ${curdomain}-cluster-1-traefik --ignore-not-found=true |
| 83 | + kubectl -n $curns delete clusterrolebinding ${curdomain}-cluster-1-traefik --ignore-not-found=true |
| 84 | + |
| 85 | +done |
| 86 | + |
| 87 | +for ((i=0;i<OCOUNT;i++)); do |
| 88 | + opns=${OPER_NAMESPACES[i]} |
| 89 | + echo @@ Deleting operator in namespace $opns |
| 90 | + #kubectl delete -f $RESULT_DIR/${opns}/weblogic-operator.yaml |
| 91 | + kubectl delete deploy weblogic-operator -n ${opns} --ignore-not-found=true |
| 92 | + sleep 10 |
| 93 | +done |
| 94 | + |
| 95 | +for ((i=0;i<DCOUNT;i++)); do |
| 96 | + curdomain=${DOMAINS[i]} |
| 97 | + curns=${DOMAIN_NAMESPACES[i]} |
| 98 | + kubectl -n $curns delete rolebinding weblogic-operator-rolebinding --ignore-not-found=true |
| 99 | + kubectl -n $curns delete rolebinding weblogic-operator-operator-rolebinding --ignore-not-found=true |
| 100 | +done |
| 101 | + |
| 102 | +kubectl delete clusterrole \ |
| 103 | + weblogic-operator-cluster-role-nonresource \ |
| 104 | + weblogic-operator-namespace-role \ |
| 105 | + weblogic-operator-cluster-role --ignore-not-found=true |
| 106 | + |
| 107 | +for ((i=0;i<OCOUNT;i++)); do |
| 108 | + opns=${OPER_NAMESPACES[i]} |
| 109 | + echo @@ Deleting clusterrolebindings for operator in $opns |
| 110 | + kubectl -n $opns delete clusterrolebinding \ |
| 111 | + ${opns}-operator-rolebinding-auth-delegator \ |
| 112 | + ${opns}-operator-rolebinding-discovery \ |
| 113 | + ${opns}-operator-rolebinding-nonresource \ |
| 114 | + ${opns}-operator-rolebinding --ignore-not-found=true |
| 115 | +done |
| 116 | + |
| 117 | + |
| 118 | +echo @@ Deleting crd |
| 119 | +kubectl delete crd domains.weblogic.oracle --ignore-not-found=true |
| 120 | + |
| 121 | +#echo @@ Deleting security |
| 122 | +#kubectl delete -f $PROJECT_ROOT/kubernetes/rbac.yaml --ignore-not-found=true |
| 123 | + |
| 124 | +#echo @@ Deleting kibani, logstash, and elasticsearch artifacts. |
| 125 | +kubectl delete -f $PROJECT_ROOT/src/integration-tests/kubernetes/kibana.yaml --ignore-not-found=true |
| 126 | +kubectl delete -f $PROJECT_ROOT/src/integration-tests/kubernetes/logstash.yaml --ignore-not-found=true |
| 127 | +kubectl delete -f $PROJECT_ROOT/src/integration-tests/kubernetes/elasticsearch.yaml --ignore-not-found=true |
| 128 | + |
| 129 | +echo @@ Deleting elk pv and pvc |
| 130 | +# TBD The next line can be deleted once the fix |
| 131 | +# to have uniquely scoped pv/pvc per operator is in place |
| 132 | +kubectl delete pv elk-pv --ignore-not-found=true |
| 133 | +for ((i=0;i<OCOUNT;i++)); do |
| 134 | + curns=${OPER_NAMESPACES[i]} |
| 135 | + kubectl delete pv elk-pv-${curns} --ignore-not-found=true |
| 136 | + kubectl -n ${curns} delete pvc elk-pvc --ignore-not-found=true |
| 137 | +done |
| 138 | + |
| 139 | +for ((i=0;i<OCOUNT;i++)); do |
| 140 | + opns=${OPER_NAMESPACES[i]} |
| 141 | + echo @@ Deleting weblogic-operator namespace |
| 142 | + kubectl delete namespace $opns --ignore-not-found=true |
| 143 | + sleep 1 # wait in case above needs below |
| 144 | +done |
| 145 | + |
| 146 | +echo @@ Deleting test namespace |
| 147 | +kubectl delete namespace test --ignore-not-found=true |
| 148 | +kubectl delete namespace test2 --ignore-not-found=true |
| 149 | + |
| 150 | +for ((i=0;i<DCOUNT;i++)); do |
| 151 | + curdomain=${DOMAINS[i]} |
| 152 | + curns=${DOMAIN_NAMESPACES[i]} |
| 153 | + echo @@ Deleting configmap domain-domain1-scripts in namespace $curns |
| 154 | + kubectl -n $curns delete cm domain-${curdomain}-scripts --ignore-not-found=true |
| 155 | +done |
| 156 | + |
| 157 | +JOB_NAME="weblogic-job" |
| 158 | +kubectl delete job $JOB_NAME --ignore-not-found=true |
| 159 | + |
| 160 | +waitForDelete "all,crd,cm,pv,pvc,ns,roles,rolebindings,clusterroles,clusterrolebindings,secrets" "logstash|kibana|elastisearch|weblogic|elk|domain" |
| 161 | + |
| 162 | +# clean-up volumes |
| 163 | +cp $PROJECT_ROOT/build/weblogic-job.yaml $RESULT_ROOT/domain-cleanup-job.yaml |
| 164 | +sed -i -e "s:%HOST_PATH%:$HOST_PATH:g" $RESULT_ROOT/domain-cleanup-job.yaml |
| 165 | +sed -i -e "s:%ARGS%:[\"-c\", \"rm -rf $HOST_DIR ; mkdir -m 777 -p $HOST_DIR\"]:g" $RESULT_ROOT/domain-cleanup-job.yaml |
| 166 | + |
| 167 | +kubectl create -f $RESULT_ROOT/domain-cleanup-job.yaml |
| 168 | +echo "Waiting for the job to complete..." |
| 169 | +JOB_STATUS="0" |
| 170 | +max=10 |
| 171 | +count=0 |
| 172 | +while [ "$JOB_STATUS" != "Completed" -a $count -lt $max ] ; do |
| 173 | + sleep 30 |
| 174 | + count=`expr $count + 1` |
| 175 | + JOB_STATUS=`kubectl get pods --show-all | grep "$JOB_NAME" | awk ' { print $3; } '` |
| 176 | + JOB_INFO=`kubectl get pods --show-all | grep "$JOB_NAME" | awk ' { print "pod", $1, "status is", $3; } '` |
| 177 | + echo "status on iteration $count of $max" |
| 178 | + echo "$JOB_INFO" |
| 179 | + |
| 180 | +done |
| 181 | + |
| 182 | +# Confirm the job pod is status completed |
| 183 | +JOB_POD=`kubectl get pods --show-all | grep "$JOB_NAME" | awk ' { print $1; } '` |
| 184 | +if [ "$JOB_STATUS" != "Completed" ]; then |
| 185 | + echo The cleanup job is not showing status completed after waiting 300 seconds |
| 186 | + echo Check the log output for errors |
| 187 | + kubectl logs jobs/$JOB_NAME |
| 188 | + fail "Exiting due to failure" |
| 189 | +fi |
| 190 | + |
| 191 | +kubectl delete job $JOB_NAME --ignore-not-found=true |
| 192 | + |
0 commit comments