forked from juliohm1978/kubernetes-cifs-volumedriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
234 lines (210 loc) · 6.34 KB
/
main.go
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
package main
import (
"github.com/pkg/errors"
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"syscall"
)
const (
retStatSuccess = "Success"
retStatFailure = "Failure"
retStatNotSupported = "Not supported"
retMsgInsufficientArgs = "Insufficient arguments"
retMsgUnsupportedOperation = "Unsupported operation"
retMsgInvalidMounterArgs = "Invalid mounter arguments"
)
const logFileName = "/var/log/kubernetes-cifs-volumedriver.log"
// returnMsg is the response given back to k8s
type returnMsg struct {
Status string
Message string
Capabilities capabilities
}
// Part of the repsonse that informs the driver's capabilities
type capabilities struct {
Attach bool
Detach bool
}
// arguments passed by k8 to this driver
type mounterArgs struct {
FsGroup string `json:"kubernetes.io/mounterArgs.FsGroup"`
FsGroupLegacy string `json:"kubernetes.io/fsGroup"` // k8s prior to 1.15
FsType string `json:"kubernetes.io/fsType"`
PodName string `json:"kubernetes.io/pod.name"`
PodNamespace string `json:"kubernetes.io/pod.namespace"`
PodUID string `json:"kubernetes.io/pod.uid"`
PvName string `json:"kubernetes.io/pvOrVolumeName"`
ReadWrite string `json:"kubernetes.io/readwrite"`
ServiceAccount string `json:"kubernetes.io/serviceAccount.name"`
Opts string `json:"opts"`
Server string `json:"server"`
Share string `json:"share"`
CredentialDomain string `json:"kubernetes.io/secret/domain"`
CredentialUser string `json:"kubernetes.io/secret/username"`
CredentialPass string `json:"kubernetes.io/secret/password"`
}
func unmarshalMounterArgs(s string) (ma mounterArgs) {
ma = mounterArgs{}
err := json.Unmarshal([]byte(s), &ma)
if err != nil {
panic(fmt.Sprintf("Error interpreting mounter args: %s", err))
}
if ma.CredentialDomain != "" {
decoded, err := base64.StdEncoding.DecodeString(ma.CredentialDomain)
if err != nil {
panic(fmt.Sprintf("Error decoding credential domain: %s", err))
}
ma.CredentialDomain = string(decoded)
}
if ma.CredentialUser != "" {
decoded, err := base64.StdEncoding.DecodeString(ma.CredentialUser)
if err != nil {
panic(fmt.Sprintf("Error decoding credential user: %s", err))
}
ma.CredentialUser = string(decoded)
}
if ma.CredentialPass != "" {
decoded, err := base64.StdEncoding.DecodeString(ma.CredentialPass)
if err != nil {
panic(fmt.Sprintf("Error decoding credential password: %s", err))
}
ma.CredentialPass = string(decoded)
}
// If we got fsGroup from the legacy json field, assume k8s prior to 1.15
if ma.FsGroupLegacy != "" {
ma.FsGroup = ma.FsGroupLegacy
}
return
}
func runCommand(cmd *exec.Cmd) error {
var b bytes.Buffer
cmd.Stdout = &b
cmd.Stderr = &b
if err := cmd.Start(); err != nil {
return errors.Wrapf(err, "Error start cmd [cmd=%s]", cmd)
}
if err := cmd.Wait(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
if _, ok := exiterr.Sys().(syscall.WaitStatus); ok {
// The program has exited with an exit code != 0
return errors.Wrapf(err, "Error running cmd [cmd=%s] [response=%s]", cmd, string(b.Bytes()))
}
} else {
return errors.Wrapf(err, "Error waiting for cmd to finish [cmd=%s]", cmd)
}
}
return nil
}
func createMountCmd(cmdLineArgs []string) (cmd *exec.Cmd) {
if len(cmdLineArgs) < 4 {
panic(retMsgInsufficientArgs)
}
var mArgs mounterArgs = unmarshalMounterArgs(cmdLineArgs[3])
var optsFinal []string
cmd = exec.Command("mount")
cmd.Args = append(cmd.Args, "-t")
cmd.Args = append(cmd.Args, "cifs")
if mArgs.FsGroup != "" {
optsFinal = append(optsFinal, fmt.Sprintf("uid=%s,gid=%s", mArgs.FsGroup, mArgs.FsGroup))
}
if mArgs.ReadWrite != "" {
optsFinal = append(optsFinal, mArgs.ReadWrite)
}
if mArgs.CredentialDomain != "" {
optsFinal = append(optsFinal, fmt.Sprintf("domain=%s", strings.Trim(mArgs.CredentialDomain, "\n\r")))
}
if mArgs.CredentialUser != "" {
optsFinal = append(optsFinal, fmt.Sprintf("username=%s", strings.Trim(mArgs.CredentialUser, "\n\r")))
}
if mArgs.CredentialPass != "" {
optsFinal = append(optsFinal, fmt.Sprintf("password=%s", strings.Trim(mArgs.CredentialPass, "\n\r")))
}
if mArgs.Opts != "" {
optsFinal = append(optsFinal, strings.Split(mArgs.Opts, ",")...)
}
if len(optsFinal) > 0 {
cmd.Args = append(cmd.Args, "-o", strings.Join(optsFinal, ","))
}
cmd.Args = append(cmd.Args, fmt.Sprintf("//%s%s", mArgs.Server, mArgs.Share))
cmd.Args = append(cmd.Args, cmdLineArgs[2])
return
}
func createUmountCmd(cmdLineArgs []string) (cmd *exec.Cmd) {
if len(cmdLineArgs) < 3 {
panic(retMsgInsufficientArgs)
}
cmd = exec.Command("umount")
cmd.Args = append(cmd.Args, cmdLineArgs[2])
return
}
// Dettach from main, allows tests to be written for this function
func driverMain(args []string) (ret returnMsg) {
ret.Status = retStatSuccess
defer func() {
err := recover()
if err != nil {
ret.Message = fmt.Sprintf("Unexpected executing volume driver: %s", err)
return
}
}()
if len(args) < 2 {
ret.Status = retStatFailure
ret.Message = retMsgInsufficientArgs
return
}
var operation = args[1]
var err error
switch operation {
case "init":
log.Println("Driver init")
ret.Status = retStatSuccess
ret.Capabilities.Attach = false
ret.Capabilities.Detach = false
case "mount":
cmd := createMountCmd(args)
log.Println(cmd.Args)
err = runCommand(cmd)
if err != nil {
ret.Status = retStatFailure
ret.Message = fmt.Sprintf("Error: %s", err)
}
case "unmount":
cmd := createUmountCmd(args)
log.Println(cmd.Args)
err = runCommand(cmd)
if err != nil {
ret.Status = retStatFailure
ret.Message = fmt.Sprintf("Error: %s", err)
}
default:
ret.Status = retStatNotSupported
ret.Message = retMsgUnsupportedOperation + ": " + operation
}
return
}
func main() {
// Log to file only if the log file already exists on disk.
if _, err := os.Stat(logFileName); err == nil {
logfile, err := os.OpenFile(logFileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Printf("WARNING: error opening file: %v", err)
}
log.SetOutput(logfile)
} else {
log.SetOutput(ioutil.Discard)
}
m := driverMain(os.Args)
jsonString, _ := json.Marshal(m)
fmt.Println(string(jsonString))
log.Println(string(jsonString))
if m.Status != retStatSuccess {
os.Exit(1)
}
}