Skip to content

Commit 0dba587

Browse files
committed
e2e prereqs - ensure volumesnapshotclass default
- Seems to be needed for OCP 4.7 Signed-off-by: Tesshu Flower <[email protected]>
1 parent 975ac65 commit 0dba587

File tree

1 file changed

+73
-19
lines changed

1 file changed

+73
-19
lines changed

hack/ensure-default-csi.sh

Lines changed: 73 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,56 @@
22

33
set -e -o pipefail
44

5-
# Makes sure the default storagedriver is a csi one
65

6+
updateDefaultStorageClass() {
7+
# Try to find a csi driver to use as default
8+
STORAGE_CLASSES=$(kubectl get storageclasses -o=jsonpath='{range .items[*]}{@.metadata.name}{"\t"}{@.provisioner}{"\n"}{end}')
9+
10+
IFS=$'\n'
11+
for sc in ${STORAGE_CLASSES}; do
12+
echo "sc is ${sc}"
13+
SC_NAME=$(echo "${sc}" | awk '{print $1}')
14+
PROVISIONER=$(echo "${sc}" | awk '{print $2}')
15+
16+
if [[ "${PROVISIONER}" =~ .*"csi".* ]]; then
17+
echo "Updating storage class ${SC_NAME} as the default ..."
18+
kubectl annotate sc/"${SC_NAME}" storageclass.kubernetes.io/is-default-class="true"
19+
20+
# Update DEFAULT_PROVISIONER
21+
DEFAULT_PROVISIONER=${PROVISIONER}
22+
23+
return 0
24+
fi
25+
done
26+
}
27+
28+
updateDefaultVolumeSnapshotClass() {
29+
DRIVER_TO_USE=$1
30+
31+
VSCS=$(kubectl get volumesnapshotclasses -o=jsonpath='{range .items[*]}{@.metadata.name}{"\t"}{@.driver}{"\n"}{end}')
32+
IFS=$'\n'
33+
for vsc in ${VSCS}; do
34+
echo "vsc is ${vsc}"
35+
VSC_NAME=$(echo "${vsc}" | awk '{print $1}')
36+
DRIVER=$(echo "${vsc}" | awk '{print $2}')
37+
38+
if [[ "${DRIVER}" == "${DRIVER_TO_USE}" ]]; then
39+
echo "Updating volume snapshot class ${VSC_NAME} as the default ..."
40+
kubectl annotate volumesnapshotclass/"${VSC_NAME}" snapshot.storage.kubernetes.io/is-default-class="true"
41+
42+
return 0
43+
fi
44+
done
45+
}
46+
47+
# Log storageclasses and volumesnapshotclasses (for debug purposes)
48+
echo "### Current storageclasses ###"
49+
kubectl get storageclasses
50+
echo "### Current volumesnapshotclasses ###"
51+
kubectl get volumesnapshotclasses
52+
echo ""
53+
54+
# First make sure the default storagedriver is a csi one
755
DEFAULT_STORAGE_CLASS=$(kubectl get storageclasses -o=jsonpath='{range .items[?(.metadata.annotations.storageclass\.kubernetes\.io/is-default-class=="true")]}{.metadata.name}{" "}{.provisioner}')
856

957
DEFAULT_STORAGE_CLASS_NAME=$(echo "${DEFAULT_STORAGE_CLASS}" | awk '{print $1}')
@@ -12,28 +60,34 @@ DEFAULT_PROVISIONER=$(echo "${DEFAULT_STORAGE_CLASS}" | awk '{print $2}')
1260
if [[ -n "${DEFAULT_STORAGE_CLASS_NAME}" ]]; then
1361
# Found a default storage class
1462
if [[ "${DEFAULT_PROVISIONER}" =~ .*"csi".* ]]; then
15-
echo "Default storage class ${DEFAULT_STORAGE_CLASS_NAME} is csi, no updates required."
16-
exit 0
17-
fi
63+
echo "Default storage class ${DEFAULT_STORAGE_CLASS_NAME} is csi, no update required."
64+
else
65+
echo "Removing default annotation on storageclass ${DEFAULT_STORAGE_CLASS_NAME} ..."
66+
# We need to remove the default annotation
67+
kubectl annotate sc/"${DEFAULT_STORAGE_CLASS_NAME}" storageclass.kubernetes.io/is-default-class-
1868

19-
# We need to remove the default annotation
20-
kubectl annotate sc/"${DEFAULT_STORAGE_CLASS_NAME}" storageclass.kubernetes.io/is-default-class-
69+
updateDefaultStorageClass
70+
fi
71+
else
72+
updateDefaultStorageClass
2173
fi
2274

23-
# No default set at this point, try to find a csi driver to use as default
24-
STORAGE_CLASSES=$(kubectl get storageclasses -o=jsonpath='{range .items[*]}{@.metadata.name}{"\t"}{@.provisioner}{"\n"}{end}')
75+
# Make sure the volumesnapshotclass has a default and uses the proper provisioner
76+
DEFAULT_VSC=$(kubectl get volumesnapshotclasses -o=jsonpath='{range .items[?(.metadata.annotations.snapshot\.storage\.kubernetes\.io/is-default-class=="true")]}{.metadata.name}{" "}{.driver}')
2577

26-
IFS=$'\n'
27-
for sc in ${STORAGE_CLASSES}; do
28-
echo "sc is ${sc}"
29-
SC_NAME=$(echo "${sc}" | awk '{print $1}')
30-
PROVISIONER=$(echo "${sc}" | awk '{print $2}')
78+
DEFAULT_VSC_NAME=$(echo "${DEFAULT_VSC}" | awk '{print $1}')
79+
DEFAULT_VSC_DRIVER=$(echo "${DEFAULT_VSC}" | awk '{print $2}')
3180

32-
if [[ "${PROVISIONER}" =~ .*"csi".* ]]; then
33-
echo "Updating storage class ${SC_NAME} as the default ..."
34-
kubectl annotate sc/"${SC_NAME}" storageclass.kubernetes.io/is-default-class="true"
81+
if [[ -n "${DEFAULT_VSC_NAME}" ]]; then
82+
# Found a default volume snapshot class
83+
if [[ "${DEFAULT_VSC_DRIVER}" == "${DEFAULT_PROVISIONER}" ]]; then
84+
echo "Default volume snapshot class ${DEFAULT_VSC_NAME} uses driver ${DEFAULT_PROVISIONER}, no update required."
85+
else
86+
echo "Removing default annotation on volume snapshot class ${DEFAULT_VSC_NAME} ..."
87+
kubectl annotate volumesnapshotclass/"${DEFAULT_VSC_NAME}" snapshot.storage.kubernetes.io/is-default-class-
3588

36-
exit 0
89+
updateDefaultVolumeSnapshotClass "${DEFAULT_PROVISIONER}"
3790
fi
38-
done
39-
91+
else
92+
updateDefaultVolumeSnapshotClass "${DEFAULT_PROVISIONER}"
93+
fi

0 commit comments

Comments
 (0)