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

     1  #!/bin/bash
     2  
     3  # Copyright 2015 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  # ============================ Example ================================
    18  # apiVersion: v1
    19  # kind: Pod
    20  # metadata:
    21  #   name: busybox
    22  #   namespace: default
    23  # spec:
    24  #   containers:
    25  #   - name: busybox
    26  #     image: busybox
    27  #     command:
    28  #       - sleep
    29  #       - "3600"
    30  #     imagePullPolicy: IfNotPresent
    31  #     volumeMounts:
    32  #     - name: test
    33  #       mountPath: /data
    34  #   volumes:
    35  #   - name: test
    36  #     flexVolume:
    37  #       driver: "k8s/nfs"
    38  #       fsType: "nfs"
    39  #       options:
    40  #         server: nfs.example.k8s.io
    41  #         share: "/share/test1"
    42  # =====================================================================
    43  
    44  
    45  # Notes:
    46  #  - Please install "jq" package before using this driver.
    47  usage() {
    48  	err "Invalid usage. Usage: "
    49  	err "\t$0 init"
    50  	err "\t$0 mount <mount dir> <json params>"
    51  	err "\t$0 unmount <mount dir>"
    52  	exit 1
    53  }
    54  
    55  err() {
    56  	echo -ne $* 1>&2
    57  }
    58  
    59  log() {
    60  	echo -ne $* >&1
    61  }
    62  
    63  ismounted() {
    64  	MOUNT=`findmnt -n ${MNTPATH} 2>/dev/null | cut -d' ' -f1`
    65  	if [ "${MOUNT}" == "${MNTPATH}" ]; then
    66  		echo "1"
    67  	else
    68  		echo "0"
    69  	fi
    70  }
    71  
    72  domount() {
    73  	MNTPATH=$1
    74  
    75  	local NFS_SERVER=$(echo $2 | jq -r '.server')
    76  	local SHARE=$(echo $2 | jq -r '.share')
    77  	local PROTOCOL=$(echo $2 | jq -r '.protocol')
    78  	local ATIME=$(echo $2 | jq -r '.atime')
    79  	local READONLY=$(echo $2 | jq -r '.readonly')
    80  
    81  	if [ -n "${PROTOCOL}" ]; then
    82  		PROTOCOL="tcp"
    83  	fi
    84  
    85  	if [ -n "${ATIME}" ]; then
    86  		ATIME="0"
    87  	fi
    88  
    89  	if [ -n "${READONLY}" ]; then
    90  		READONLY="0"
    91  	fi
    92  
    93  	if [ "${PROTOCOL}" != "tcp" ] && [ "${PROTOCOL}" != "udp" ] ; then
    94  		err "{ \"status\": \"Failure\", \"message\": \"Invalid protocol ${PROTOCOL}\"}"
    95  		exit 1
    96  	fi
    97  
    98  	if [ $(ismounted) -eq 1 ] ; then
    99  		log '{"status": "Success"}'
   100  		exit 0
   101  	fi
   102  
   103  	mkdir -p ${MNTPATH} &> /dev/null
   104  
   105  	local NFSOPTS="${PROTOCOL},_netdev,soft,timeo=10,intr"
   106  	if [ "${ATIME}" == "0" ]; then
   107  		NFSOPTS="${NFSOPTS},noatime"
   108  	fi
   109  
   110  	if [ "${READONLY}" != "0" ]; then
   111  		NFSOPTS="${NFSOPTS},ro"
   112  	fi
   113  
   114  	mount -t nfs -o${NFSOPTS} ${NFS_SERVER}:/${SHARE} ${MNTPATH} &> /dev/null
   115  	if [ $? -ne 0 ]; then
   116  		err "{ \"status\": \"Failure\", \"message\": \"Failed to mount ${NFS_SERVER}:${SHARE} at ${MNTPATH}\"}"
   117  		exit 1
   118  	fi
   119  	log '{"status": "Success"}'
   120  	exit 0
   121  }
   122  
   123  unmount() {
   124  	MNTPATH=$1
   125  	if [ $(ismounted) -eq 0 ] ; then
   126  		log '{"status": "Success"}'
   127  		exit 0
   128  	fi
   129  
   130  	umount ${MNTPATH} &> /dev/null
   131  	if [ $? -ne 0 ]; then
   132  		err "{ \"status\": \"Failed\", \"message\": \"Failed to unmount volume at ${MNTPATH}\"}"
   133  		exit 1
   134  	fi
   135  
   136  	log '{"status": "Success"}'
   137  	exit 0
   138  }
   139  
   140  op=$1
   141  
   142  if ! command -v jq >/dev/null 2>&1; then
   143  	err "{ \"status\": \"Failure\", \"message\": \"'jq' binary not found. Please install jq package before using this driver\"}"
   144  	exit 1
   145  fi
   146  
   147  if [ "$op" = "init" ]; then
   148  	log '{"status": "Success", "capabilities": {"attach": false}}'
   149  	exit 0
   150  fi
   151  
   152  if [ $# -lt 2 ]; then
   153  	usage
   154  fi
   155  
   156  shift
   157  
   158  case "$op" in
   159  	mount)
   160  		domount $*
   161  		;;
   162  	unmount)
   163  		unmount $*
   164  		;;
   165  	*)
   166  		log '{"status": "Not supported"}'
   167  		exit 0
   168  esac
   169  
   170  exit 1