2
2
3
3
set -e -o pipefail
4
4
5
- # Makes sure the default storagedriver is a csi one
6
5
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
7
55
DEFAULT_STORAGE_CLASS=$( kubectl get storageclasses -o=jsonpath=' {range .items[?(.metadata.annotations.storageclass\.kubernetes\.io/is-default-class=="true")]}{.metadata.name}{" "}{.provisioner}' )
8
56
9
57
DEFAULT_STORAGE_CLASS_NAME=$( echo " ${DEFAULT_STORAGE_CLASS} " | awk ' {print $1}' )
@@ -12,28 +60,34 @@ DEFAULT_PROVISIONER=$(echo "${DEFAULT_STORAGE_CLASS}" | awk '{print $2}')
12
60
if [[ -n " ${DEFAULT_STORAGE_CLASS_NAME} " ]]; then
13
61
# Found a default storage class
14
62
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-
18
68
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
21
73
fi
22
74
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 }' )
25
77
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}' )
31
80
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-
35
88
36
- exit 0
89
+ updateDefaultVolumeSnapshotClass " ${DEFAULT_PROVISIONER} "
37
90
fi
38
- done
39
-
91
+ else
92
+ updateDefaultVolumeSnapshotClass " ${DEFAULT_PROVISIONER} "
93
+ fi
0 commit comments