2020-04-16 06:40:14 +00:00
|
|
|
#!/bin/bash -e
|
|
|
|
|
2022-04-11 08:43:21 +00:00
|
|
|
# This script can be used to install/delete snapshotcontroller and snapshot CRD
|
2020-04-16 06:40:14 +00:00
|
|
|
|
|
|
|
SCRIPT_DIR="$(dirname "${0}")"
|
|
|
|
|
2020-10-05 10:31:34 +00:00
|
|
|
# shellcheck source=build.env
|
|
|
|
source "${SCRIPT_DIR}/../build.env"
|
|
|
|
|
2024-02-15 12:32:51 +00:00
|
|
|
# shellcheck disable=SC1091
|
|
|
|
[ ! -e "${SCRIPT_DIR}"/utils.sh ] || source "${SCRIPT_DIR}"/utils.sh
|
|
|
|
|
2022-01-17 12:58:38 +00:00
|
|
|
SNAPSHOT_VERSION=${SNAPSHOT_VERSION:-"v5.0.1"}
|
2020-10-05 10:31:34 +00:00
|
|
|
|
2020-04-16 06:40:14 +00:00
|
|
|
TEMP_DIR="$(mktemp -d)"
|
|
|
|
SNAPSHOTTER_URL="https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/${SNAPSHOT_VERSION}"
|
|
|
|
|
|
|
|
# controller
|
|
|
|
SNAPSHOT_RBAC="${SNAPSHOTTER_URL}/deploy/kubernetes/snapshot-controller/rbac-snapshot-controller.yaml"
|
|
|
|
SNAPSHOT_CONTROLLER="${SNAPSHOTTER_URL}/deploy/kubernetes/snapshot-controller/setup-snapshot-controller.yaml"
|
|
|
|
|
|
|
|
# snapshot CRD
|
2020-09-29 07:55:26 +00:00
|
|
|
SNAPSHOTCLASS="${SNAPSHOTTER_URL}/client/config/crd/snapshot.storage.k8s.io_volumesnapshotclasses.yaml"
|
|
|
|
VOLUME_SNAPSHOT_CONTENT="${SNAPSHOTTER_URL}/client/config/crd/snapshot.storage.k8s.io_volumesnapshotcontents.yaml"
|
|
|
|
VOLUME_SNAPSHOT="${SNAPSHOTTER_URL}/client/config/crd/snapshot.storage.k8s.io_volumesnapshots.yaml"
|
2020-04-16 06:40:14 +00:00
|
|
|
|
2024-02-15 12:17:45 +00:00
|
|
|
# volumegroupsnapshot CRD
|
|
|
|
VOLUME_GROUP_SNAPSHOTCLASS="${SNAPSHOTTER_URL}/client/config/crd/groupsnapshot.storage.k8s.io_volumegroupsnapshotclasses.yaml"
|
|
|
|
VOLUME_GROUP_SNAPSHOT_CONTENT="${SNAPSHOTTER_URL}/client/config/crd/groupsnapshot.storage.k8s.io_volumegroupsnapshotcontents.yaml"
|
|
|
|
VOLUME_GROUP_SNAPSHOT="${SNAPSHOTTER_URL}/client/config/crd/groupsnapshot.storage.k8s.io_volumegroupsnapshots.yaml"
|
|
|
|
|
2020-04-16 06:40:14 +00:00
|
|
|
function install_snapshot_controller() {
|
|
|
|
local namespace=$1
|
|
|
|
if [ -z "${namespace}" ]; then
|
2022-01-17 12:58:38 +00:00
|
|
|
namespace="kube-system"
|
2020-04-16 06:40:14 +00:00
|
|
|
fi
|
|
|
|
|
2023-04-20 09:51:43 +00:00
|
|
|
create_or_delete_resource "create" "${namespace}"
|
2020-04-16 06:40:14 +00:00
|
|
|
|
2024-02-15 12:51:12 +00:00
|
|
|
pod_ready=$(kubectl_retry get pods -l app.kubernetes.io/name=snapshot-controller -n "${namespace}" -o jsonpath='{.items[0].status.containerStatuses[0].ready}')
|
2020-04-16 06:40:14 +00:00
|
|
|
INC=0
|
|
|
|
until [[ "${pod_ready}" == "true" || $INC -gt 20 ]]; do
|
|
|
|
sleep 10
|
|
|
|
((++INC))
|
2024-02-15 12:51:12 +00:00
|
|
|
pod_ready=$(kubectl_retry get pods -l app.kubernetes.io/name=snapshot-controller -n "${namespace}" -o jsonpath='{.items[0].status.containerStatuses[0].ready}')
|
2020-04-16 06:40:14 +00:00
|
|
|
echo "snapshotter pod status: ${pod_ready}"
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ "${pod_ready}" != "true" ]; then
|
|
|
|
echo "snapshotter controller creation failed"
|
2024-02-15 12:51:12 +00:00
|
|
|
kubectl_retry get pods -l app.kubernetes.io/name=snapshot-controller -n "${namespace}"
|
|
|
|
kubectl_retry describe po -l app.kubernetes.io/name=snapshot-controller -n "${namespace}"
|
2020-04-16 06:40:14 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "snapshot controller creation successful"
|
|
|
|
}
|
|
|
|
|
|
|
|
function cleanup_snapshot_controller() {
|
|
|
|
local namespace=$1
|
|
|
|
if [ -z "${namespace}" ]; then
|
2022-01-17 12:58:38 +00:00
|
|
|
namespace="kube-system"
|
2020-04-16 06:40:14 +00:00
|
|
|
fi
|
2023-04-20 09:51:43 +00:00
|
|
|
create_or_delete_resource "delete" "${namespace}"
|
2020-04-16 06:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function create_or_delete_resource() {
|
|
|
|
local operation=$1
|
|
|
|
local namespace=$2
|
|
|
|
temp_rbac=${TEMP_DIR}/snapshot-rbac.yaml
|
2020-12-16 05:52:09 +00:00
|
|
|
temp_snap_controller=${TEMP_DIR}/snapshot-controller.yaml
|
2020-04-16 06:40:14 +00:00
|
|
|
mkdir -p "${TEMP_DIR}"
|
|
|
|
curl -o "${temp_rbac}" "${SNAPSHOT_RBAC}"
|
2020-12-16 05:52:09 +00:00
|
|
|
curl -o "${temp_snap_controller}" "${SNAPSHOT_CONTROLLER}"
|
2022-01-17 12:58:38 +00:00
|
|
|
sed -i "s/namespace: kube-system/namespace: ${namespace}/g" "${temp_rbac}"
|
|
|
|
sed -i "s/namespace: kube-system/namespace: ${namespace}/g" "${temp_snap_controller}"
|
2024-02-15 12:27:37 +00:00
|
|
|
sed -i -E "s/(image: registry\.k8s\.io\/sig-storage\/snapshot-controller:).*$/\1$SNAPSHOT_VERSION/g" "${temp_snap_controller}"
|
2020-04-16 06:40:14 +00:00
|
|
|
|
2024-02-15 12:17:45 +00:00
|
|
|
if [ "${operation}" == "create" ]; then
|
|
|
|
# Argument to add/update
|
2024-12-12 12:32:14 +00:00
|
|
|
ARGUMENT="--feature-gates=CSIVolumeGroupSnapshot=true"
|
2024-02-15 12:17:45 +00:00
|
|
|
# Check if the argument is already present and set to false
|
2024-12-12 12:32:14 +00:00
|
|
|
if grep -q -E "^\s+-\s+--feature-gates=CSIVolumeGroupSnapshot=false" "${temp_snap_controller}"; then
|
|
|
|
sed -i -E "s/^\s+-\s+----feature-gates=CSIVolumeGroupSnapshot=false$/ - $ARGUMENT/" "${temp_snap_controller}"
|
2024-02-15 12:17:45 +00:00
|
|
|
# Check if the argument is already present and set to true
|
2024-12-12 12:32:14 +00:00
|
|
|
elif grep -q -E "^\s+-\s+--feature-gates=CSIVolumeGroupSnapshot=true" "${temp_snap_controller}"; then
|
2024-02-15 12:17:45 +00:00
|
|
|
echo "Argument already present and matching."
|
|
|
|
else
|
|
|
|
# Add the argument if it's not present
|
|
|
|
sed -i -E "/^(\s+)args:/a\ \ - $ARGUMENT" "${temp_snap_controller}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2024-02-15 12:32:51 +00:00
|
|
|
kubectl_retry "${operation}" -f "${VOLUME_GROUP_SNAPSHOTCLASS}"
|
|
|
|
kubectl_retry "${operation}" -f "${VOLUME_GROUP_SNAPSHOT_CONTENT}"
|
|
|
|
kubectl_retry "${operation}" -f "${VOLUME_GROUP_SNAPSHOT}"
|
|
|
|
kubectl_retry "${operation}" -f "${temp_rbac}"
|
|
|
|
kubectl_retry "${operation}" -f "${temp_snap_controller}" -n "${namespace}"
|
|
|
|
kubectl_retry "${operation}" -f "${SNAPSHOTCLASS}"
|
|
|
|
kubectl_retry "${operation}" -f "${VOLUME_SNAPSHOT_CONTENT}"
|
|
|
|
kubectl_retry "${operation}" -f "${VOLUME_SNAPSHOT}"
|
2020-04-16 06:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case "${1:-}" in
|
|
|
|
install)
|
|
|
|
install_snapshot_controller "$2"
|
|
|
|
;;
|
|
|
|
cleanup)
|
|
|
|
cleanup_snapshot_controller "$2"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "usage:" >&2
|
|
|
|
echo " $0 install" >&2
|
|
|
|
echo " $0 cleanup" >&2
|
|
|
|
;;
|
|
|
|
esac
|