This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscaleio
executable file
·829 lines (741 loc) · 24.3 KB
/
scaleio
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
#!/bin/sh
#
# ScaleIO FlexVolume driver
#
# Do not modify this script, configuration settings can be found at
# /opt/emc/scaleio/flexvolume/cfg/config
#########################################################
# err
# Arguments:
# $1: Name of calling function + error msg for stderr
# Returns
# Nothing
#########################################################
err() {
debug $*
shift
printf "%b" "$*" 1>&2
}
#########################################################
# log
# Arguments:
# $1: Name of calling function + msg for stdout
# Returns
# Nothing
#########################################################
log() {
debug $*
shift
printf "%b" "$*" >&1
}
#########################################################
# debug
# Arguments:
# $1: Name of calling function + msg for debug log
# Returns
# Nothing
#########################################################
debug() {
if [ "$SCALEIO_DEBUG" = "1" ]; then
logtofile $*
fi
}
#########################################################
# logtofile
# Arguments:
# $1: Name of calling function + msg for debug log
# Returns
# Nothing
#########################################################
logtofile() {
function=$1
shift
printf "%s pid=%s func=%s msg=\"%s\"\n" "$(date +"%Y-%m-%d %H:%M:%S")" "$$" "${function}" "$*" >> ${LOGFILE}
}
#########################################################
# success
# Arguments:
# $1: Name of calling function
# Returns
# Nothing
#########################################################
success() {
log $1 "{\"status\": \"Success\"}"
exit 0
}
#########################################################
# usage
# Arguments:
# Nothing
# Returns
# Nothing
#########################################################
usage() {
err ${FUNCNAME} "error: invalid usage\n"
err ${FUNCNAME} "\t$0 init\n"
err ${FUNCNAME} "\t$0 attach <json params> <nodename>\n"
err ${FUNCNAME} "\t$0 detach <mount device> <nodename>\n"
err ${FUNCNAME} "\t$0 waitforattach <mount device> <json params>\n"
err ${FUNCNAME} "\t$0 mountdevice <mount dir> <mount device> <json params>\n"
err ${FUNCNAME} "\t$0 mount <mount dir> <json params>\n"
err ${FUNCNAME} "\t$0 unmountdevice <mount dir>\n"
err ${FUNCNAME} "\t$0 unmount <mount dir>\n"
err ${FUNCNAME} "\t$0 isattached <json params> <nodename>\n"
exit 1
}
#########################################################
# getlabelfromvolume
# Arguments:
# $1: Volume ID
# $2: Label being requested
# Returns:
# value of the label or "<no value>" if not found
#########################################################
getlabelfromvolume() {
if [ "$USELABELS" != "1" ]; then
return
fi
local volid=$1
local label=$2
local value=$(${CLI_CMD} ${CONFIG_OPTION}=${KUBECONFIG} --token=${TOKEN} get pv ${volid} --template="{{.metadata.labels.${label}}}" | sed -e 's/<no value>//g')
debug $FUNCNAME "Retrieved label from volume ${volid} ${label}=${value}"
echo "${value}"
}
#########################################################
# setlabelonvolume
# Arguments:
# $1: Volume ID
# $2: Label being set
# $3: Value to set
# Returns:
# 0 if success
# 1 if failure
#########################################################
setlabelonvolume() {
if [ "$USELABELS" != "1" ]; then
echo 0
return
fi
local volid="${1}"
local label="${2}"
local newvalue="${3}"
local value=$(${CLI_CMD} ${CONFIG_OPTION}=${KUBECONFIG} --token=${TOKEN} label pv ${volid} --overwrite=false ${label}=${newvalue})
local ret=$(echo $?)
if [ "${ret}" = "0" ]; then
debug $FUNCNAME "Set label on volume ${volid} ${label}=${newvalue} succeeded: ${value}"
else
debug $FUNCNAME "Set label on volume ${volid} ${label}=${newvalue} failed: ${value}"
fi
echo ${ret}
}
#########################################################
# clearlabelonvolume
# Arguments:
# $1: Volume ID
# $2: Label to clear
# Returns
# Nothing
#########################################################
clearlabelonvolume() {
if [ "$USELABELS" != "1" ]; then
return
fi
local volid=$1
local label=$2
local value=$(${CLI_CMD} ${CONFIG_OPTION}=${KUBECONFIG} --token=${TOKEN} label pv ${volid} --overwrite ${label}-)
debug $FUNCNAME "Cleared label on volume ${volid} ${label}"
}
#########################################################
# ismounted
# Arguments:
# $1: Mount path
# Returns
# 1 if specified path mounted; else 0
#########################################################
ismounted() {
local mntpath=$1
local mount=$(findmnt -n "${mntpath}" 2>/dev/null | cut -d' ' -f1)
if [ "${mount}" = "${mntpath}" ]; then echo 1; else echo 0; fi
}
#########################################################
# volidfromjson
# Arguments:
# $1: JSON
# Returns
# Volume ID
#########################################################
volidfromjson() {
local json_params=$1
local volumeid=$(echo "${json_params}" | grep -Po '"volumeID":".*"?[^\\]"' | cut -d: -f 2 | cut -d\" -f 2)
if [ -z "${volumeid}" ] || [ "${volumeid}" = null ]; then
err ${FUNCNAME} "{\"status\": \"Failure\", \"message\": \"Unable to extract volumeID\"}"
exit 1
fi
echo ${volumeid}
}
#########################################################
# podnamefromjson
# Arguments:
# $1: JSON
# Returns
# Pod name
#########################################################
podnamefromjson() {
local json_params=$1
local podname=$(echo "${json_params}" | grep -Po '"kubernetes.io/pod.name":".*"?[^\\]"' | cut -d: -f 2 | cut -d\" -f 2)
if [ -z "${podname}" ] || [ "${podname}" = null ]; then
err ${FUNCNAME} "{\"status\": \"Failure\", \"message\": \"Unable to extract pod name\"}"
exit 1
fi
echo ${podname}
}
#########################################################
# getpodswithspecifiedstatus
# Arguments:
# $1: Pod status
# Returns
# List of pods (namespace/pod) with specified status
#########################################################
getpodswithspecifiedstatus() {
local status=$1
local pods=$(${CLI_CMD} ${CONFIG_OPTION}=${KUBECONFIG} --token=${TOKEN} get pods --all-namespaces=true | grep ${status} | awk '{printf "%s/%s\n", $1, $2}')
debug ${FUNCNAME} "Pods with status of ${status}:" "${pods}"
echo "${pods}"
}
#########################################################
# getflexvolsforpod
# Arguments:
# $1: Namespace
# $2: Pod name
# Returns
# List of FlexVolumes bound to specified pod
#########################################################
getflexvolsforpod() {
local ns=$1
local pod=$2
local flex_volume_ids=$(${CLI_CMD} ${CONFIG_OPTION}=${KUBECONFIG} --token=${TOKEN} get pod ${pod} --namespace=${ns} --template='{{range .spec.volumes}}{{.flexVolume.options.volumeID}} {{end}}' | sed -e 's/<no value>//g')
debug ${FUNCNAME} "FlexVolumes attached to pod ${ns}/${pod}:" "${flex_volume_ids}"
echo "${flex_volume_ids}"
}
#########################################################
# getpvcsforpod
# Arguments:
# $1: Namespace
# $2: Pod name
# Returns
# List of PVCs bound to specified pod (namespace is implicit)
#########################################################
getpvcsforpod() {
local ns=$1
local pod=$2
local pvcs=$(${CLI_CMD} ${CONFIG_OPTION}=${KUBECONFIG} --token=${TOKEN} get pod ${pod} --namespace=${ns} --template='{{range .spec.volumes}}{{.persistentVolumeClaim.claimName}} {{end}}' | sed -e 's/<no value>//g')
debug ${FUNCNAME} "PVCs attached to pod ${ns}/${pod}:" "${pvcs}"
echo "${pvcs}"
}
#########################################################
# getvolsforpvc
# Arguments:
# $1: Namespace
# $2: PVC name
# Returns
# List of volumes bound to specified PVC
#########################################################
getvolsforpvc() {
local ns=$1
local pvc=$2
local pvc_vol_names=$(${CLI_CMD} ${CONFIG_OPTION}=${KUBECONFIG} --token=${TOKEN} get pvc ${pvc} --namespace=${ns} --template='{{.spec.volumeName}}' | sed -e 's/<no value>//g')
debug ${FUNCNAME} "Volumes attached to PVC ${ns}/${pvc}:" "${pvc_vol_names}"
echo "${pvc_vol_names}"
}
#########################################################
# getpvcforvol
# Arguments:
# $1: Volume ID
# Returns
# PVC (namespace/pvc) bound to specified volume (if applicable)
#########################################################
getpvcforvol() {
local volumeid=$1
local ns
local pvc
local nspvc
local status=$(${CLI_CMD} ${CONFIG_OPTION}=${KUBECONFIG} --token=${TOKEN} get pv ${volumeid} --template='{{.status.phase}}' | sed -e 's/<no value>//g')
if [ ${status} = "Bound" ]; then
ns=$(${CLI_CMD} ${CONFIG_OPTION}=${KUBECONFIG} --token=${TOKEN} get pv ${volumeid} --template='{{.spec.claimRef.namespace}}' | sed -e 's/<no value>//g')
pvc=$(${CLI_CMD} ${CONFIG_OPTION}=${KUBECONFIG} --token=${TOKEN} get pv ${volumeid} --template='{{.spec.claimRef.name}}' | sed -e 's/<no value>//g')
nspvc="${ns}/${pvc}"
debug ${FUNCNAME} "PVC attached to PV ${volumeid}:" "${nspvc}"
echo "${nspvc}"
fi
}
#########################################################
# getaccessmodesforpvc
# Arguments:
# $1: Namespace
# $2: PVC name
# Returns
# Access modes set for specified PVC (if applicable)
#########################################################
getaccessmodesforpvc() {
local ns=$1
local pvc=$2
local modes=$(${CLI_CMD} ${CONFIG_OPTION}=${KUBECONFIG} --token=${TOKEN} get pvc ${pvc} --namespace=${ns} --template='{{.status.accessModes}}' | sed -e 's/<no value>//g')
debug ${FUNCNAME} "Access modes for PVC ${ns}/${pvc}:" "${modes}"
echo "${modes}"
}
#########################################################
# getaccessmodesforvol
# Arguments:
# $1: Volume ID
# Returns
# Access modes set for specified volume
#########################################################
getaccessmodesforvol() {
local volumeid=$1
local modes=$(${CLI_CMD} ${CONFIG_OPTION}=${KUBECONFIG} --token=${TOKEN} get pv ${volumeid} --template='{{.spec.accessModes}}' | sed -e 's/<no value>//g')
debug ${FUNCNAME} "Access modes for PV ${volumeid}:" "${modes}"
echo "${modes}"
}
#########################################################
# getpodlinkedtovol
# Arguments:
# $1: Volume ID
# $2: List of pods to examine (to see whether specified vol is linked to any)
# Returns
# Name of pod (namespace/pod) linked to specified volume
# If more than one pod in the list is linked to the specified volume, return the first found
#########################################################
getpodlinkedtovol() {
local volumeid=$1
local pods=$2
local i
for i in ${pods}
do
local ns=$(echo $i | awk -F'/' '{print $1}')
local pod=$(echo $i | awk -F'/' '{print $2}')
local flex_volume_ids=$(getflexvolsforpod ${ns} ${pod})
local j
for j in ${flex_volume_ids}
do
if [ "${j}" = "${volumeid}" ]; then
debug ${FUNCNAME} "Volume ${volumeid} is in use by pod ${i}"
echo ${i}
return
fi
done
local pvcs=$(getpvcsforpod ${ns} ${pod})
local k
for k in ${pvcs}
do
if [ "${k}" != "<no value>" ]; then
local pvc_vol_names=$(getvolsforpvc ${ns} ${k})
local m
for m in ${pvc_vol_names}
do
if [ "${m}" = "${volumeid}" ]; then
debug ${FUNCNAME} "Volume ${volumeid} claimed by PVC ${k} is in use by pod ${i}"
echo ${i}
return
fi
done
fi
done
done
return
}
#########################################################
# getpodstatus
# Arguments:
# $1: Namespace
# $2: Pod name
# Returns
# Status of specified pod
#########################################################
getpodstatus() {
local ns=$1
local pod=$2
local status=$(${CLI_CMD} ${CONFIG_OPTION}=${KUBECONFIG} --token=${TOKEN} get pod ${pod} --namespace=${ns} --template='{{.status.phase}}' | sed -e 's/<no value>//g')
if [ "$?" = 0 ]; then
debug ${FUNCNAME} "Status of pod ${ns}/${pod}:" "${status}"
echo ${status}
else
debug ${FUNCNAME} "Pod ${ns}/${pod} not found"
fi
}
#########################################################
# refreshattachedtopod
# Arguments:
# $1: Volume ID
# Returns
# Nothing
#########################################################
refreshattachedtopod() {
local volumeid=$1
if [ "${ENABLE_ONE_POD_RESTRICTION}" = "1" ]; then
local volume_attached_pod=$(getlabelfromvolume ${volumeid} "attachedPod")
if [ ! -z "${volume_attached_pod}" ]; then
local ns_running_pods=$(getpodswithspecifiedstatus "Running")
local ns_unknown_pods=$(getpodswithspecifiedstatus "Unknown")
local ns_running_or_unknown_pods="${ns_running_pods} ${ns_unknown_pods}"
local nspod=$(getpodlinkedtovol ${volumeid} ${ns_running_or_unknown_pods})
if [ -z ${nspod} ]; then
# proactively clear attachedPod label only if the following two criteria are met:
# - there are no Running pods bound to the specified volume
# - there are no Unknown pods potential bound to the specified volume (we don't want to assume)
clearlabelonvolume ${volumeid} "attachedPod"
fi
fi
fi
}
#########################################################
# accessmodefromjson
# Arguments:
# $1: JSON
# Returns
# Access mode
#########################################################
accessmodefromjson() {
local json_params=$1
local mode=$(echo "${json_params}" | grep -Po 'readwrite":".*"?[^\\]"' | cut -d: -f 2 | cut -d\" -f 2)
if [ -z "${mode}" ] || [ "${mode}" = null ]; then
debug ${FUNCNAME} "Unable to extract access mode"
exit 1
fi
echo ${mode}
}
#########################################################
# ismountmodeprohibited
# Arguments:
# $1: Mount access mode
# $1: Volume ID
# Returns
# 1 if mount mode prohibited (based on accessMode of volume)
# 0 otherwise
#########################################################
ismountmodeprohibited() {
local mode_requested=$1
local volumeid=$2
local pvcforvol=$(getpvcforvol ${volumeid})
local pvpvc_mode
if [ ! -z "${nspvc}" ]; then
local ns=$(echo ${nspvc} | awk -F'/' '{print $1}')
local pvc=$(echo ${nspvc} | awk -F'/' '{print $2}')
pvpvc_mode=$(getaccessmodesforpvc ${ns} ${pvc})
else
pvpvc_mode=$(getaccessmodesforvol ${volumeid})
fi
debug ${FUNCNAME} "mount access mode requested:" "${mode_requested}"
debug ${FUNCNAME} "PV/PVC access mode:" "${pvpvc_mode}"
if [ "${pvpvc_mode}" != "[ReadWriteOnce]" ] && [ "${mode_requested}" = "rw" ]; then
debug ${FUNCNAME} "Attempting to mount ReadOnly volume ${volumeid} as ReadWrite"
echo 1
else
echo 0
fi
}
#########################################################
# isnodeready
# Arguments:
# $1: Node
# Returns
# 1 if node is in Ready state (and Scheduling is enabled)
# 0 otherwise
#########################################################
isnodeready() {
local node_name=$1
local status=$(${CLI_CMD} ${CONFIG_OPTION}=${KUBECONFIG} --token=${TOKEN} get node | grep ${node_name} | awk '{print $2}')
debug ${FUNCNAME} "${node_name} status:" "${status}"
if [ "${status}" = "Ready" ]; then
echo 1
else
echo 0
fi
}
getvolumename() {
local json_params=$1
local volumeid=$(echo "${json_params}" | grep -Po '"volumeID":".*"?[^\\]"' | cut -d: -f 2 | cut -d\" -f 2)
if [ -z "${volumeid}" ] || [ "${volumeid}" = null ]; then
err ${FUNCNAME} "{\"status\": \"Failure\", \"message\": \"Unable to extract volumeID\"}"
exit 1
fi
log ${FUNCNAME} "{\"status\": \"Success\", \"volumeName\": \"${volumeid}\"}"
exit 0
}
attach() {
local json_params=$1
local node_name=$2
local volumeid=$(volidfromjson ${json_params})
if [ -z "${volumeid}" ]; then
err ${FUNCNAME} '{"status": "Failure", "message": "Unable to extract volumeID"}'
exit 1
fi
# if we are not using labels, return success as we assume all volumes
# are attached to all nodes
if [ "${USELABELS}" != "1" ]; then
log $FUNCNAME "{\"status\": \"Success\", \"device\":\"${volumeid}\"}"
exit 0
fi
local volume_attached_node=$(getlabelfromvolume ${volumeid} "attachedNode")
if [ ! -z "${volume_attached_node}" ]; then
local nspvc=$(getpvcforvol ${volumeid})
local mode
if [ -z "${nspvc}" ]; then
mode=$(getaccessmodesforvol ${volumeid})
else
local ns=$(echo ${nspvc} | awk -F'/' '{print $1}')
local pvc=$(echo ${nspvc} | awk -F'/' '{print $2}')
mode=$(getaccessmodesforpvc ${ns} ${pvc})
fi
# if accessMode is ReadWriteOnce and volume is already attached to a different node, fail this attach
if [ "${mode}" = "[ReadWriteOnce]" ] && [ "${volume_attached_node}" != "${node_name}" ]; then
err ${FUNCNAME} "{\"status\": \"Failure\", \"message\": \"Volume is in use by another node\"}"
exit 1
fi
log ${FUNCNAME} "{\"status\": \"Success\", \"device\":\"${volumeid}\"}"
exit 0
fi
# Only set label if specified node is "Ready"; avoid labeling/attaching to NotReady nodes
if [ "$(isnodeready ${node_name})" = "1" ]; then
if [ "$(setlabelonvolume ${volumeid} "attachedNode" "${node_name}")" = "1" ]; then
err ${FUNCNAME} "{\"status\": \"Failure\", \"message\": \"Failed to set attachedNode label\"}"
exit 1
fi
else
err ${FUNCNAME} "{\"status\": \"Failure\", \"message\": \"Specified node is not Ready\"}"
exit 1
fi
log ${FUNCNAME} "{\"status\": \"Success\", \"device\":\"${volumeid}\"}"
exit 0
}
detach() {
local volumeid=$1
clearlabelonvolume ${volumeid} "attachedNode"
if [ "${ENABLE_ONE_POD_RESTRICTION}" = "1" ]; then
clearlabelonvolume ${volumeid} "attachedPod"
fi
success ${FUNCNAME}
}
mountdevice() {
local mntpath=$1
local dev=$2
local json_params=$3
local fstype=$(echo "${json_params}" | grep -Po '"kubernetes.io/fsType":".*"?[^\\]"' | cut -d: -f 2 | cut -d\" -f 2)
if [ ! -b "${dev}" ]; then
err ${FUNCNAME} "{\"status\": \"Failure\", \"message\": \"${dev} does not exist\"}"
exit 1
fi
if [ "$(ismounted ${mntpath})" = "1" ] ; then
success ${FUNCNAME}
fi
local volumeid=$(volidfromjson ${json_params})
if [ -z "${volumeid}" ]; then
err ${FUNCNAME} "{\"status\": \"Failure\", \"message\": \"Unable to extract volumeID\"}"
exit 1
fi
local mode_requested=$(accessmodefromjson ${json_params})
if [ -z "${mode_requested}" ]; then
err ${FUNCNAME} "{\"status\": \"Failure\", \"message\": \"Unable to extract access mode\"}"
exit 1
fi
local isprohibited=$(ismountmodeprohibited ${mode_requested} ${volumeid})
if [ "${isprohibited}" = "1" ]; then
err ${FUNCNAME} "{\"status\": \"Failure\", \"message\": \"Attempting to mount ReadOnly volume ${volumeid} as ReadWrite\"}"
exit 1
fi
local volfstype=$(blkid -o udev "${dev}" 2>/dev/null | grep "ID_FS_TYPE"| cut -d"=" -f2)
if [ "${volfstype}" = "" ]; then
local cmd="mkfs -t ${fstype}"
if [ "${fstype}" = "ext4" ]; then
cmd="${cmd} -F"
elif [ "$FSTYPE" = "xfs" ]; then
cmd="${cmd} -f"
fi
if ! ${cmd} "${dev}" > /dev/null 2>&1; then
err ${FUNCNAME} "{\"status\": \"Failure\", \"message\": \"Failed to create fs ${fstype} on device ${dev}\"}"
exit 1
fi
fi
mkdir -p "${mntpath}" > /dev/null 2>&1
local selinux_status=$(sestatus |grep 'SELinux status' | grep disabled)
local selinux_enabled=$(echo $?)
if ! command mount --make-shared -o "${mode_requested}" "${dev}" "${mntpath}" > /dev/null 2>&1; then
debug ${FUNCNAME} "Failed to mount device ${dev} at ${mntpath}"
err ${FUNCNAME} "{\"status\": \"Failure\", \"message\": \"Failed to mount device ${dev} at ${mntpath}\"}"
exit 1
fi
if [ "${selinux_enabled}" = "1" ]; then
if ! chcon -t svirt_sandbox_file_t "${mntpath}" > /dev/null 2>&1; then
debug ${FUNCNAME} "Failed to modify SELINUX properties for ${mntpath}"
err ${FUNCNAME} "{\"status\": \"Failure\", \"message\": \"Failed to modify SELINUX properties for ${mntpath}\"}"
exit 1
fi
fi
success ${FUNCNAME}
}
mount() {
local mntpath=$1
local json_params=$2
if [ "$(ismounted ${mntpath})" = "1" ] ; then
success ${FUNCNAME}
fi
local volumeid=$(volidfromjson ${json_params})
if [ -z "${volumeid}" ]; then
err ${FUNCNAME} "{\"status\": \"Failure\", \"message\": \"Unable to extract volumeID\"}"
exit 1
fi
if [ "${ENABLE_ONE_POD_RESTRICTION}" = "1" ]; then
if [ "${ENABLE_SAME_NODE_AUTO_FAILOVER_FOR_ONE_POD_RESTRICTION}" = "1" ]; then
# proactively clear attachedPod label if deemed safe to do so
refreshattachedtopod ${volumeid}
fi
local volume_attached_pod=$(getlabelfromvolume ${volumeid} "attachedPod")
local pod=$(podnamefromjson ${json_params})
if [ ! -z "${volume_attached_pod}" ]; then
if [ "${pod}" != "${volume_attached_pod}" ]; then
err ${FUNCNAME} "{\"status\": \"Failure\", \"message\": \"Volume ${volumeid} is in use by another pod\"}"
exit 1
fi
else
if [ "$(setlabelonvolume ${volumeid} "attachedPod" "${pod}")" = "1" ]; then
err ${FUNCNAME} "{\"status\": \"Failure\", \"message\": \"Failed to set attachedPod label\"}"
exit 1
fi
fi
fi
local mode_requested=$(accessmodefromjson ${json_params})
if [ -z "${mode_requested}" ]; then
err ${FUNCNAME} "{\"status\": \"Failure\", \"message\": \"Unable to extract access mode\"}"
exit 1
fi
# ismountmodeprohibited check is not really needed here because...
# 1. mount does not get called unless mountdevice succeeds
# 2. mountdevice already performs this check
# 3. k8s currently ignores accessModes for .spec.containers.volumeMounts.readOnly, and hard-codes
# kubernetes.io/readwrite to be consistent the mode used in the preceding/successful mountdevice call
# ...but, we do it just in case
local isprohibited=$(ismountmodeprohibited ${mode_requested} ${volumeid})
if [ "${isprohibited}" = "1" ]; then
err ${FUNCNAME} "{\"status\": \"Failure\", \"message\": \"Attempting to mount ReadOnly volume ${volumeid} as ReadWrite\"}"
exit 1
fi
local dev="/dev/disk/by-id/$(ls -1 /dev/disk/by-id | grep "\-${volumeid}$")"
if [ -z "${dev}" ]; then
debug ${FUNCNAME} "${volumeid}" "not found or mapped"
err ${FUNCNAME} "{\"status\": \"Failure\", \"message\": \"Volume not found ${volumeid}\"}"
exit 1
fi
mkdir -p "${mntpath}" > /dev/null 2>&1
if ! command mount --make-shared -o "${mode_requested}" "${dev}" "${mntpath}" > /dev/null 2>&1; then
debug ${FUNCNAME} "Failed to mount device ${dev} at ${mntpath}"
err ${FUNCNAME} "{\"status\": \"Failure\", \"message\": \"Failed to mount device ${dev} at ${mntpath}\"}"
exit 1
fi
success ${FUNCNAME}
}
unmountdevice() {
local mntpath=$1
if [ "$(ismounted ${mntpath})" = "0" ] ; then success ${FUNCNAME}; fi
if ! umount "${mntpath}" > /dev/null 2>&1; then
err ${FUNCNAME} "{\"status\": \"Failed\", \"message\": \"Failed to unmount volume at ${mntpath}\"}"
exit 1
fi
sleep 2
success ${FUNCNAME}
}
isattached() {
local json_params=$1
local node_name=$2
local volumeid=$(volidfromjson ${json_params})
# if we are not using labels, return success as we assume all volumes
# are attached to all nodes
if [ "${USELABELS}" != "1" ]; then
log $FUNCNAME "{\"status\": \"Success\", \"attached\":true}"
exit 0
fi
local volume_attached_node=$(getlabelfromvolume ${volumeid} "attachedNode")
if [ ! -z "${volume_attached_node}" ]; then
if [ "${volume_attached_node}" = "${node_name}" ]; then
log ${FUNCNAME} "{\"status\": \"Success\", \"attached\":true}"
exit 0
fi
fi
log ${FUNCNAME} "{\"status\": \"Success\", \"attached\":false}"
exit 0
}
waitforattach() {
local expected_dev=$1
local json_params=$2
local volumeid=$(volidfromjson ${json_params})
local dev="/dev/disk/by-id/$(ls -1 /dev/disk/by-id | grep "\-${volumeid}$")"
if [ -z "${dev}" ]; then
debug ${FUNCNAME} "${volumeid}" "not found or mapped"
err ${FUNCNAME} "{\"status\": \"Failure\", \"message\": \"Volume not attached\"}"
exit 1
fi
log ${FUNCNAME} "{\"status\": \"Success\", \"device\":\"${dev}\"}"
exit 0
}
# MAIN
CONFIGFILE="/opt/emc/scaleio/flexvolume/cfg/config"
if [ -f "${CONFIGFILE}" ]; then
# source the config file
source "${CONFIGFILE}"
fi
# in case some values were not specific in the config file, set their values
SCALEIO_DEBUG="${SCALEIO_DEBUG:-1}"
KUBECONFIG="${KUBECONFIG:-/root/.kube/config}"
TOKEN_SCRIPT="${TOKEN_SCRIPT:-/opt/emc/scaleio/flexvolume/bin/get-token.sh}"
LOGFILE="${LOGFILE:-/var/log/scaleio-flexvol.log}"
ENABLE_ONE_POD_RESTRICTION="${ENABLE_ONE_POD_RESTRICTION:-0}"
USELABELS="${USELABELS:-1}"
op=$1
if [ "$op" = "init" ]; then
log "main" "{\"status\":\"Success\",\"capabilities\":{\"attach\":true}}"
exit 0
fi
if [ "$#" -lt "2" ]; then usage; fi
shift
debug "main" "$op" "$*"
TOKEN="$(${TOKEN_SCRIPT})"
WHICH_OC=$(which oc > /dev/null 2>&1)
WHICH_OC_RETURN=$(echo $?)
if [ "${WHICH_OC_RETURN}" = "0" ]; then
CLI_CMD="oc"
CONFIG_OPTION="--config"
else
CLI_CMD="kubectl"
CONFIG_OPTION="--kubeconfig"
fi
case "$op" in
attach)
attach "$@"
;;
detach)
detach "$@"
;;
waitforattach)
waitforattach "$@"
;;
mountdevice)
mountdevice "$@"
;;
mount)
mount "$@"
;;
unmountdevice)
unmountdevice "$@"
;;
unmount)
unmountdevice "$@"
;;
isattached)
isattached "$@"
;;
getvolumename)
getvolumename "$@"
;;
*)
err "main" "{\"status\": \"Not supported\"}"
exit 1
esac
exit 1