...
1#!/bin/sh
2
3# Copyright 2017 The Kubernetes Authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# This driver is especially designed to test a long mounting scenario
18# which can cause a volume to be detached while mount is in progress.
19
20
21FLEX_DUMMY_LOG=${FLEX_DUMMY_LOG:-"/tmp/flex-dummy.log"}
22
23VALID_MNTDEVICE=foo
24
25# attach always returns one valid mount device so a different device
26# showing up in a subsequent driver call implies a bug
27validateMountDeviceOrDie() {
28 MNTDEVICE=$1
29 CALL=$2
30 if [ "$MNTDEVICE" != "$VALID_MNTDEVICE" ]; then
31 log "{\"status\":\"Failure\",\"message\":\"call "${CALL}" expected device "${VALID_MNTDEVICE}", got device "${MNTDEVICE}"\"}"
32 exit 0
33 fi
34}
35
36log() {
37 printf "$*" >&1
38}
39
40debug() {
41 echo "$(date) $*" >> "${FLEX_DUMMY_LOG}"
42}
43
44attach() {
45 debug "attach $@"
46 log "{\"status\":\"Success\",\"device\":\""${VALID_MNTDEVICE}"\"}"
47 exit 0
48}
49
50detach() {
51 debug "detach $@"
52 # TODO issue 44737 detach is passed PV name, not mount device
53 log "{\"status\":\"Success\"}"
54 exit 0
55}
56
57waitforattach() {
58 debug "waitforattach $@"
59 MNTDEVICE=$1
60 validateMountDeviceOrDie "$MNTDEVICE" "waitforattach"
61 log "{\"status\":\"Success\",\"device\":\""${MNTDEVICE}"\"}"
62 exit 0
63}
64
65isattached() {
66 debug "isattached $@"
67 log "{\"status\":\"Success\",\"attached\":true}"
68 exit 0
69}
70
71domountdevice() {
72 debug "domountdevice $@"
73 MNTDEVICE=$2
74 validateMountDeviceOrDie "$MNTDEVICE" "domountdevice"
75 MNTPATH=$1
76 mkdir -p ${MNTPATH} >/dev/null 2>&1
77 mount -t tmpfs none ${MNTPATH} >/dev/null 2>&1
78 sleep 120
79 echo "Hello from flexvolume!" >> "${MNTPATH}/index.html"
80 log "{\"status\":\"Success\"}"
81 exit 0
82}
83
84unmountdevice() {
85 debug "unmountdevice $@"
86 MNTPATH=$1
87 rm "${MNTPATH}/index.html" >/dev/null 2>&1
88 umount ${MNTPATH} >/dev/null 2>&1
89 log "{\"status\":\"Success\"}"
90 exit 0
91}
92
93expandvolume() {
94 debug "expandvolume $@"
95 log "{\"status\":\"Success\"}"
96 exit 0
97}
98
99expandfs() {
100 debug "expandfs $@"
101 log "{\"status\":\"Success\"}"
102 exit 0
103}
104
105op=$1
106
107if [ "$op" = "init" ]; then
108 debug "init $@"
109 log "{\"status\":\"Success\",\"capabilities\":{\"attach\":true, \"requiresFSResize\":true}}"
110 exit 0
111fi
112
113shift
114
115case "$op" in
116 attach)
117 attach $*
118 ;;
119 detach)
120 detach $*
121 ;;
122 waitforattach)
123 waitforattach $*
124 ;;
125 isattached)
126 isattached $*
127 ;;
128 mountdevice)
129 domountdevice $*
130 ;;
131 unmountdevice)
132 unmountdevice $*
133 ;;
134 expandvolume)
135 expandvolume $*
136 ;;
137 expandfs)
138 expandfs $*
139 ;;
140 *)
141 log "{\"status\":\"Not supported\"}"
142 exit 0
143esac
144
145exit 1
View as plain text