#!/bin/bash
#
#################################################################
# Licensed Materials - Property of IBM
# (C) Copyright IBM Corp. 2018.  All Rights Reserved.
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with
# IBM Corp.
#################################################################
#
# You need to run this once per cluster
#
# Example:
#     ./Cleanup.sh [ -n  <namespace> ] [--all] [ --force ]
#

FORCE=""
ALL=""

usage() {
  echo "Usage: $0 [-n <NAMESPACE>] [ --all ] [ --force ]"
  exit 1
}
# Delete pull secret

remove_crfin() {
  crd="$1"
  crdexists=$(kubectl get crd $crd 2>/dev/null)
  echo "Checking CRD: $crd"
  if [ "X$crdexists" == "X" ]; then
    return
  fi
  echo "Check for remaining custom resources of type $crd"
  for cr in $(kubectl get -n $NAMESPACE $crd -o name)
  do
    echo "Removing finalizers from $cr"
    kubectl patch -n $NAMESPACE $cr --type json -p='[{"op": "remove", "path": "/metadata/finalizers"}]' 2>/dev/null
    echo "Removing $cr"
    kubectl delete -n $NAMESPACE $cr 2>&1 | grep -v 'NotFound'
  done
  echo "Deleting $crd"
  if [[ $crd ==  "iscsequences.isc.ibm.com" ]]; then
     kubectl delete pod -lname=sequences --force --grace-period=0 --ignore-not-found=true
  fi
  kubectl patch crd $crd --type json -p='[{"op": "remove", "path": "/metadata/finalizers"}]' 2>/dev/null
  kubectl delete crd $crd 2>&1 | grep -v NotFound
}

remove_podfin() {
  type="$1"
  label="$2"
  for pod in $(kubectl get $type -l$label -o name)
  do
    kubectl patch -n $NAMESPACE $pod --type json -p='[{"op": "remove", "path": "/metadata/finalizers"}]'  2>/dev/null
    kubectl delete $pod 2>&1 | grep -v NotFound
  done

  echo "Check if some ${type}s with label $label were not deleted"
  pods=$(kubectl get $type -l$label -o name)
  if [ "X$pods" != "X" ]; then
    echo "ERROR: some ${type}s with label $label were not deleted"
    echo "$pods"
  fi   
}

kubermF() {
   type="$1"
   name="$2"

   obj=$(kubectl get $type $name -o name 2>/dev/null)
   if [ "X$obj" == "X" ]; then
     return
   fi

   checkF=$(kubectl get $obj -o yaml|grep finalizers)
   if [ "X$checkF" == "X" ]; then
     kubectl delete $obj
     return
   fi

   kubectl delete $obj --wait=false
   sleep 60

   obj=$(kubectl get $type $name -o name 2>/dev/null)
   if [ "X$obj" == "X" ]; then
     return
   fi

   kubectl patch $obj --type json -p='[{"op": "remove", "path": "/metadata/finalizers"}]' 
   kubectl delete $obj
}
 
set_namespace()
{
  NAMESPACE="$1"
  ns=$(kubectl get namespace $NAMESPACE -o name 2>/dev/null)
  if [ "X$ns" == "X" ]; then
    echo "ERROR: Invalid namespace $NAMESPACE"
    exit 1
  fi
}

if [ "X$WATCH_NAMESPACE" == "X" ]; then
  NAMESPACE=$(oc project | sed -e 's/^[^"]*"//' -e 's/".*$//')
else
  NAMESPACE="$WATCH_NAMESPACE"
fi

while true
do
  arg="$1"
  shift
  if [ "X$arg" == "X" ]; then
     break
  fi
  case $arg in
  -n)
    set_namespace "$1"
    shift
    ;;
  --force)
     FORCE="yes"
     ;;
  --all)
     ALL="yes"
     ;;
  *)
     echo "ERROR: invalid argument $arg"
     usage
     exit 1
  esac
done

remove_crfin iscsequences.isc.ibm.com
remove_crfin isccomponents.isc.ibm.com
remove_crfin redis.isc.ibm.com
remove_crfin couchdbs.isc.ibm.com
remove_crfin etcds.isc.ibm.com
remove_crfin minios.isc.ibm.com
remove_crfin oidcclient.isc.ibm.com
remove_crfin elastics.isc.ibm.com
remove_crfin iscsecrets.isc.ibm.com
remove_crfin isctrusts.isc.ibm.com
remove_crfin rabbitmq.isc.ibm.com
remove_crfin connectors.connector.isc.ibm.com
remove_crfin appentitlements.entitlements.extensions.platform.cp4s.ibm.com
remove_crfin arangobackuppolicies.backup.arangodb.com
remove_crfin arangobackups.backup.arangodb.com
remove_crfin arangodeploymentreplications.replication.database.arangodb.com
remove_crfin arangodeployments.database.arangodb.com
remove_crfin cases.isc.ibm.com
remove_crfin iscopenwhisks.isc.ibm.com
remove_crfin postgresqloperators.isc.ibm.com
remove_crfin cp4sfoundations.isc.ibm.com
remove_crfin iscguards.isc.ibm.com
remove_crfin iscinventories.isc.ibm.com

remove_podfin pod app=arangodb
remove_podfin pvc app=arangodb
kubectl delete sa arango-isc-service-account-default arango-isc-service-account-name --ignore-not-found=true

# generated by default by middleware operator
kubectl delete secret -n $NAMESPACE isc-jwt isc-helm-account --ignore-not-found=true

# Remove license
kubectl delete configmap  cp4s-extension-configmap --ignore-not-found=true
kubectl delete configmap -llicense=foundations

kubermF svc ambassador

for role in pgo-backrest-role \
            pgo-role
do
   kubectl delete role $role --ignore-not-found=true
done

for rolebinding in pgo-backrest-role-binding \
                   pgo-role-binding 
do
   kubectl delete rolebinding $rolebinding --ignore-not-found=true
done

for sa in ambassador \
   ibm-isc-application \
   ibm-isc-operators \
   pgo-backrest \
   postgres-operator
do
   kubectl delete serviceaccount $sa --ignore-not-found=true
done

for r in ibm-isc-operators ambassador arango-operator-rbac-deployment
do
  kubectl delete clusterrole $r --ignore-not-found=true
  kubectl delete clusterrolebinding $r --ignore-not-found=true
done


if [ "X$ALL" != "X" ]; then
# Remove openwhisk labels
  kubectl label nodes --all openwhisk-role-
  kubectl delete secret cp4s-extension-rootca --ignore-not-found=true
  kubectl delete serviceaccount ibm-dba-ek-isc-cases-elastic-bai-psp-sa --ignore-not-found=true
  kubectl delete secret ibm-isc-pull-secret --ignore-not-found=true
  kubectl delete  scc ibm-isc-scc ibm-isc-elastic --ignore-not-found=true
fi
