github.com/qsunny/k8s@v0.0.0-20220101153623-e6dca256d5bf/examples-master/staging/volumes/flexvolume/dummy-attachable (about)

     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 implements a tmpfs with a pre-populated file index.html.
    18  # Attach is required, but it is a no-op that always returns success.
    19  
    20  FLEX_DUMMY_LOG=${FLEX_DUMMY_LOG:-"/tmp/flex-dummy.log"}
    21  
    22  VALID_MNTDEVICE=foo
    23  
    24  # attach always returns one valid mount device so a different device
    25  # showing up in a subsequent driver call implies a bug
    26  validateMountDeviceOrDie() {
    27  	MNTDEVICE=$1
    28  	CALL=$2
    29  	if [ "$MNTDEVICE" != "$VALID_MNTDEVICE" ]; then
    30  		log "{\"status\":\"Failure\",\"message\":\"call "${CALL}" expected device "${VALID_MNTDEVICE}", got device "${MNTDEVICE}"\"}"
    31  		exit 0
    32  	fi
    33  }
    34  
    35  log() {
    36  	printf "$*" >&1
    37  }
    38  
    39  debug() {
    40  	echo "$(date) $*" >> "${FLEX_DUMMY_LOG}"
    41  }
    42  
    43  attach() {
    44  	debug "attach $@"
    45  	log "{\"status\":\"Success\",\"device\":\""${VALID_MNTDEVICE}"\"}"
    46  	exit 0
    47  }
    48  
    49  detach() {
    50  	debug "detach $@"
    51  	log "{\"status\":\"Success\"}"
    52  	exit 0
    53  }
    54  
    55  waitforattach() {
    56  	debug "waitforattach $@"
    57  	MNTDEVICE=$1
    58  	validateMountDeviceOrDie "$MNTDEVICE" "waitforattach"
    59  	log "{\"status\":\"Success\",\"device\":\""${MNTDEVICE}"\"}"
    60  	exit 0
    61  }
    62  
    63  isattached() {
    64  	debug "isattached $@"
    65  	log "{\"status\":\"Success\",\"attached\":true}"
    66  	exit 0
    67  }
    68  
    69  domountdevice() {
    70  	debug "domountdevice $@"
    71  	MNTDEVICE=$2
    72  	validateMountDeviceOrDie "$MNTDEVICE" "domountdevice"
    73  	MNTPATH=$1
    74  	mkdir -p ${MNTPATH} >/dev/null 2>&1
    75  	mount -t tmpfs none ${MNTPATH} >/dev/null 2>&1
    76  	echo "Hello from flexvolume!" >> "${MNTPATH}/index.html"
    77  	log "{\"status\":\"Success\"}"
    78  	exit 0
    79  }
    80  
    81  unmountdevice() {
    82  	debug "unmountdevice $@"
    83  	MNTDEVICE=$2
    84  	validateMountDeviceOrDie "$MNTDEVICE" "unmountdevice"
    85  	MNTPATH=$1
    86  	rm "${MNTPATH}/index.html" >/dev/null 2>&1
    87  	umount ${MNTPATH} >/dev/null 2>&1
    88  	log "{\"status\":\"Success\"}"
    89  	exit 0
    90  }
    91  
    92  op=$1
    93  
    94  if [ "$op" = "init" ]; then
    95  	debug "init $@"
    96  	log "{\"status\":\"Success\",\"capabilities\":{\"attach\":true}}"
    97  	exit 0
    98  fi
    99  
   100  shift
   101  
   102  case "$op" in
   103  	attach)
   104  		attach $*
   105  		;;
   106  	detach)
   107  		detach $*
   108  		;;
   109  	waitforattach)
   110  		waitforattach $*
   111  		;;
   112  	isattached)
   113  		isattached $*
   114  		;;
   115  	mountdevice)
   116  		domountdevice $*
   117  		;;
   118  	unmountdevice)
   119  		unmountdevice $*
   120  		;;
   121  	*)
   122  		log "{\"status\":\"Not supported\"}"
   123  		exit 0
   124  esac
   125  
   126  exit 1