|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -u |
| 4 | + |
| 5 | +# ==================================================================== |
| 6 | +# Example configuration: |
| 7 | +# ==================================================================== |
| 8 | +# -------------------------------------------------------------------- |
| 9 | +# secret.yml: |
| 10 | +# -------------------------------------------------------------------- |
| 11 | +# apiVersion: v1 |
| 12 | +# kind: Secret |
| 13 | +# metadata: |
| 14 | +# name: cifs-secret |
| 15 | +# namespace: default |
| 16 | +# type: fstab/cifs |
| 17 | +# data: |
| 18 | +# username: 'ZXhhbXBsZQo=' |
| 19 | +# password: 'c2VjcmV0Cg==' |
| 20 | +# |
| 21 | +# -------------------------------------------------------------------- |
| 22 | +# pod.yml: |
| 23 | +# -------------------------------------------------------------------- |
| 24 | +# apiVersion: v1 |
| 25 | +# kind: Pod |
| 26 | +# metadata: |
| 27 | +# name: busybox |
| 28 | +# namespace: default |
| 29 | +# spec: |
| 30 | +# containers: |
| 31 | +# - name: busybox |
| 32 | +# image: busybox |
| 33 | +# command: |
| 34 | +# - sleep |
| 35 | +# - "3600" |
| 36 | +# imagePullPolicy: IfNotPresent |
| 37 | +# volumeMounts: |
| 38 | +# - name: test |
| 39 | +# mountPath: /data |
| 40 | +# volumes: |
| 41 | +# - name: test |
| 42 | +# flexVolume: |
| 43 | +# driver: "fstab/cifs" |
| 44 | +# fsType: "cifs" |
| 45 | +# secretRef: |
| 46 | +# name: "cifs-secret" |
| 47 | +# options: |
| 48 | +# networkPath: "//example-server/backup" |
| 49 | +# mountOptions: "dir_mode=0755,file_mode=0644,noperm" |
| 50 | +# -------------------------------------------------------------------- |
| 51 | + |
| 52 | +# Uncomment the following lines to see how this plugin is called: |
| 53 | +# echo >> /tmp/cifs.log |
| 54 | +# date >> /tmp/cifs.log |
| 55 | +# echo "$@" >> /tmp/cifs.log |
| 56 | + |
| 57 | +init() { |
| 58 | + assertBinaryInstalled mount.cifs cifs-utils |
| 59 | + assertBinaryInstalled jq jq |
| 60 | + assertBinaryInstalled mountpoint util-linux |
| 61 | + assertBinaryInstalled base64 coreutils |
| 62 | + echo '{ "status": "Success", "message": "The fstab/cifs flexvolume plugin was initialized successfully", "capabilities": { "attach": false } }' |
| 63 | + exit 0 |
| 64 | +} |
| 65 | + |
| 66 | +assertBinaryInstalled() { |
| 67 | + binary="$1" |
| 68 | + package="$2" |
| 69 | + if ! which "$binary" > /dev/null ; then |
| 70 | + errorExit "Failed to initialize the fstab/cifs flexvolume plugin. $binary command not found. Please install the $package package." |
| 71 | + fi |
| 72 | +} |
| 73 | + |
| 74 | +errorExit() { |
| 75 | + if [[ $# -ne 1 ]] ; then |
| 76 | + echo '{ "status": "Failure", "message": "Unknown error in the fstab/cifs flexvolume plugin." }' |
| 77 | + else |
| 78 | + jq -Mcn --arg message "$1" '{ "status": "Failure", "message": $message }' |
| 79 | + fi |
| 80 | + exit 1 |
| 81 | +} |
| 82 | + |
| 83 | +doMount() { |
| 84 | + if [[ -z ${1:-} || -z ${2:-} ]] ; then |
| 85 | + errorExit "cifs mount: syntax error. usage: cifs mount <mount dir> <json options>" |
| 86 | + fi |
| 87 | + mountPoint="$1" |
| 88 | + shift |
| 89 | + json=$(printf '%s ' "${@}") |
| 90 | + if ! jq -e . > /dev/null 2>&1 <<< "$json" ; then |
| 91 | + errorExit "cifs mount: syntax error. invalid json: '$json'" |
| 92 | + fi |
| 93 | + networkPath="$(jq --raw-output -e '.networkPath' <<< "$json" 2>/dev/null)" |
| 94 | + if [[ $? -ne 0 ]] ; then |
| 95 | + errorExit "cifs mount: option networkPath missing in flexvolume configuration." |
| 96 | + fi |
| 97 | + mountOptions="$(jq --raw-output -e '.mountOptions' <<< "$json" 2>/dev/null)" |
| 98 | + if [[ $? -ne 0 ]] ; then |
| 99 | + errorExit "cifs mount: option mountOptions missing in flexvolume configuration." |
| 100 | + fi |
| 101 | + cifsUsernameBase64="$(jq --raw-output -e '.["kubernetes.io/secret/username"]' <<< "$json" 2>/dev/null)" |
| 102 | + if [[ $? -ne 0 ]] ; then |
| 103 | + errorExit "cifs mount: username not found. the flexVolume definition must contain a secretRef to a secret with username and password." |
| 104 | + fi |
| 105 | + cifsPasswordBase64="$(jq --raw-output -e '.["kubernetes.io/secret/password"]' <<< "$json" 2>/dev/null)" |
| 106 | + if [[ $? -ne 0 ]] ; then |
| 107 | + errorExit "cifs mount: password not found. the flexVolume definition must contain a secretRef to a secret with username and password." |
| 108 | + fi |
| 109 | + cifsUsername="$(base64 --decode <<< "$cifsUsernameBase64" 2>/dev/null)" |
| 110 | + if [[ $? -ne 0 ]] ; then |
| 111 | + errorExit "cifs mount: username secret is not base64 encoded." |
| 112 | + fi |
| 113 | + cifsPassword="$(base64 --decode <<< "$cifsPasswordBase64" 2>/dev/null)" |
| 114 | + if [[ $? -ne 0 ]] ; then |
| 115 | + errorExit "cifs mount: password secret is not base64 encoded." |
| 116 | + fi |
| 117 | + if ! mkdir -p "$mountPoint" > /dev/null 2>&1 ; then |
| 118 | + errorExit "cifs mount: failed to create mount directory: '$mountPoint'" |
| 119 | + fi |
| 120 | + if [[ $(mountpoint "$mountPoint") = *"is a mountpoint"* ]] ; then |
| 121 | + errorExit "cifs mount: there is already a filesystem mounted under the mount directory: '$mountPoint'" |
| 122 | + fi |
| 123 | + if [[ ! -z $(ls -A "$mountPoint" 2>/dev/null) ]] ; then |
| 124 | + errorExit "cifs mount: mount directory is not an empty directory: '$mountPoint'" |
| 125 | + fi |
| 126 | + |
| 127 | + result=$(mount -t cifs "$networkPath" "$mountPoint" -o "username=$cifsUsername,password=$cifsPassword,$mountOptions" 2>&1) |
| 128 | + if [[ $? -ne 0 ]] ; then |
| 129 | + errorExit "cifs mount: failed to mount the network path: $result" |
| 130 | + fi |
| 131 | + echo '{ "status": "Success" }' |
| 132 | + exit 0 |
| 133 | +} |
| 134 | + |
| 135 | +doUnmount() { |
| 136 | + if [[ -z ${1:-} ]] ; then |
| 137 | + errorExit "cifs unmount: syntax error. usage: cifs unmount <mount dir>" |
| 138 | + fi |
| 139 | + mountPoint="$1" |
| 140 | + if [[ $(mountpoint "$mountPoint") != *"is a mountpoint"* ]] ; then |
| 141 | + errorExit "cifs unmount: no filesystem mounted under directory: '$mountPoint'" |
| 142 | + fi |
| 143 | + result=$(umount "$mountPoint" 2>&1) |
| 144 | + if [[ $? -ne 0 ]] ; then |
| 145 | + errorExit "cifs unmount: failed to unmount the network path: $result" |
| 146 | + fi |
| 147 | + echo '{ "status": "Success" }' |
| 148 | + exit 0 |
| 149 | +} |
| 150 | + |
| 151 | +not_supported() { |
| 152 | + echo '{ "status": "Not supported" }' |
| 153 | + exit 1 |
| 154 | +} |
| 155 | + |
| 156 | +command=${1:-} |
| 157 | +if [[ -n $command ]]; then |
| 158 | + shift |
| 159 | +fi |
| 160 | + |
| 161 | +case "$command" in |
| 162 | + init) |
| 163 | + init "$@" |
| 164 | + ;; |
| 165 | + mount) |
| 166 | + doMount "$@" |
| 167 | + ;; |
| 168 | + unmount) |
| 169 | + doUnmount "$@" |
| 170 | + ;; |
| 171 | + *) |
| 172 | + not_supported "$@" |
| 173 | + ;; |
| 174 | +esac |
0 commit comments