This repository has been archived by the owner on Aug 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdriver.go
176 lines (145 loc) · 4.01 KB
/
driver.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"sync"
"time"
"github.com/docker/go-plugins-helpers/volume"
"github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs"
"github.com/square/keywhiz-fs"
klog "github.com/square/keywhiz-fs/log"
)
type keywhizConfig struct {
ServerURL string
CertFile string
KeyFile string
CaFile string
User string
Group string
Ping bool
Debug bool
TimeoutSeconds time.Duration
}
type keywhizServer struct {
*fuse.Server
connections int
}
type keywhizDriver struct {
root string
config keywhizConfig
servers map[string]*keywhizServer
m *sync.Mutex
}
func newKeywhizDriver(root string, config keywhizConfig) keywhizDriver {
return keywhizDriver{
root: root,
config: config,
servers: map[string]*keywhizServer{},
m: &sync.Mutex{},
}
}
func (d keywhizDriver) Create(r volume.Request) volume.Response {
return volume.Response{}
}
func (d keywhizDriver) Remove(r volume.Request) volume.Response {
d.m.Lock()
defer d.m.Unlock()
m := d.mountpoint(r.Name)
if s, ok := d.servers[m]; ok {
if s.connections <= 1 {
delete(d.servers, m)
}
}
return volume.Response{}
}
func (d keywhizDriver) Path(r volume.Request) volume.Response {
return volume.Response{Mountpoint: d.mountpoint(r.Name)}
}
func (d keywhizDriver) Mount(r volume.Request) volume.Response {
d.m.Lock()
defer d.m.Unlock()
m := d.mountpoint(r.Name)
log.Printf("Mounting volume %s on %s\n", r.Name, m)
s, ok := d.servers[m]
if ok && s.connections > 0 {
s.connections++
return volume.Response{Mountpoint: m}
}
fi, err := os.Lstat(m)
if os.IsNotExist(err) {
if err := os.MkdirAll(m, 0755); err != nil {
return volume.Response{Err: err.Error()}
}
} else if err != nil {
return volume.Response{Err: err.Error()}
}
if fi != nil && !fi.IsDir() {
return volume.Response{Err: fmt.Sprintf("%v already exist and it's not a directory", m)}
}
server, err := d.mountServer(m)
if err != nil {
return volume.Response{Err: err.Error()}
}
d.servers[m] = &keywhizServer{Server: server, connections: 1}
return volume.Response{Mountpoint: m}
}
func (d keywhizDriver) Unmount(r volume.Request) volume.Response {
d.m.Lock()
defer d.m.Unlock()
m := d.mountpoint(r.Name)
log.Printf("Unmounting volume %s from %s\n", r.Name, m)
if s, ok := d.servers[m]; ok {
if s.connections == 1 {
s.Unmount()
}
s.connections--
} else {
return volume.Response{Err: fmt.Sprintf("Unable to find volume mounted on %s", m)}
}
return volume.Response{}
}
func (d *keywhizDriver) mountpoint(name string) string {
return filepath.Join(d.root, name)
}
func (d *keywhizDriver) mountServer(mountpoint string) (*fuse.Server, error) {
logConfig := klog.Config{
Debug: d.config.Debug,
Mountpoint: mountpoint,
}
if err := os.MkdirAll(filepath.Dir(mountpoint), 0755); err != nil {
return nil, err
}
freshThreshold := 200 * time.Millisecond
backendDeadline := 500 * time.Millisecond
maxWait := d.config.TimeoutSeconds + backendDeadline
timeouts := keywhizfs.Timeouts{
Fresh: freshThreshold,
BackendDeadline: backendDeadline,
MaxWait: maxWait,
}
client := keywhizfs.NewClient(d.config.CertFile, d.config.KeyFile, d.config.CaFile,
d.config.ServerURL, d.config.TimeoutSeconds, logConfig, d.config.Ping)
ownership := keywhizfs.NewOwnership(d.config.User, d.config.Group)
kwfs, root, err := keywhizfs.NewKeywhizFs(&client, ownership, timeouts, logConfig)
if err != nil {
client.Errorf("Mount fail: %v\n", err)
return nil, err
}
mountOptions := &fuse.MountOptions{
AllowOther: true,
Name: kwfs.String(),
Options: []string{"default_permissions"},
}
// Empty Options struct avoids setting a global uid/gid override.
conn := nodefs.NewFileSystemConnector(root, &nodefs.Options{})
server, err := fuse.NewServer(conn.RawFS(), mountpoint, mountOptions)
if err != nil {
client.Errorf("Mount fail: %v\n", err)
return nil, err
}
go server.Serve()
return server, nil
}