# Copyright © 2020 The OpenEBS Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#!/bin/bash

set -e

if [ -z $1 ]; then
	# default namespace is openebs when all the custom resources are created
	ZFSPV_NAMESPACE="openebs"
else
	ZFSPV_NAMESPACE=$1
fi

echo "Fetching ZFS Volumes"
numVol=`kubectl get zfsvolumes.openebs.io --no-headers -n $ZFSPV_NAMESPACE | wc -l`

if [ $numVol -gt 0 ]; then
	echo "Cleaning the ZFS Volumes($numVol)"
	kubectl get zfsvolumes.openebs.io -n $ZFSPV_NAMESPACE -oyaml > volumes.yaml

	# remove the finalizer from the old CR
	sed -i "/zfs.openebs.io\/finalizer/d" volumes.yaml
	kubectl apply -f volumes.yaml

	# delete the old CR
	kubectl delete -f volumes.yaml
fi

# delete the ZFSVolume CRD definition
kubectl delete crd zfsvolumes.openebs.io

numAttach=`kubectl get volumeattachment --no-headers | grep zfs.csi.openebs.io | wc -l`

if [ $numAttach -gt 0 ]; then
	echo "Cleaning the volumeattachment($numAttach)"
	# delete the volumeattachment object
	kubectl delete volumeattachment --all
fi

echo "Fetching ZFS Snapshots"
numSnap=`kubectl get zfssnapshots.openebs.io --no-headers -n $ZFSPV_NAMESPACE | wc -l`

if [ $numSnap -gt 0 ]; then
	echo "Cleaning the ZFS Snapshot($numSnap)"
	kubectl get zfssnapshots.openebs.io -n $ZFSPV_NAMESPACE -oyaml > snapshots.yaml

	# remove the finalizer from the old CR
	sed -i "/zfs.openebs.io\/finalizer/d" snapshots.yaml
	kubectl apply -f snapshots.yaml

	# delete the old CR
	kubectl delete -f snapshots.yaml
fi

# delete the ZFSSnapshot CRD definition
kubectl delete crd zfssnapshots.openebs.io
