This repository has been archived by the owner on Jun 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
volume.go
147 lines (116 loc) · 3.37 KB
/
volume.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
package secrets
import (
"errors"
"os"
"path"
"github.com/Sirupsen/logrus"
"github.com/docker/docker/pkg/mount"
)
const (
volRoot = "/var/lib/rancher/volumes/rancher-secrets"
hostKeyPath = "/var/lib/rancher/etc/ssl/host.key"
)
// FlexVolume is a struct to implement the Rancher Volume interface
type FlexVolume struct {
secretWriter SecretWriter
}
// Init implements the flex volume interface and is a no-op at this time
func (sv *FlexVolume) Init() error {
return nil
}
// Create is implemented for Docker volume plugin API
func (sv *FlexVolume) Create(options map[string]interface{}) (map[string]interface{}, error) {
resp := map[string]interface{}{}
if name, ok := options["name"].(string); ok {
volPath := path.Join(volRoot, "staging", name)
if err := createTmpfs(volPath, options); err != nil {
logrus.Error(err)
return resp, err
}
resp["device"] = volPath
resp["name"] = name
return resp, nil
}
logrus.Error("Name not given as a string")
return resp, errors.New("Name not given")
}
// Delete is implemented for Docker volume plugin API it detaches the
// volume and removes its content.
func (sv *FlexVolume) Delete(options map[string]interface{}) error {
if device, ok := options["device"].(string); ok {
return sv.Detach(device)
}
return nil
}
// Attach is implemeneted as a no-op for the flexvolume API
func (sv *FlexVolume) Attach(params map[string]interface{}) (string, error) {
options, err := newOptions(params)
// func (sv *FlexVolume) Mount(params map[string]interface{}) (string, error) {
if options.Name == "" {
return "", errors.New("Volume Name not given")
}
volumeDevice := path.Join(volRoot, "staging", options.Name)
if err := createTmpfs(volumeDevice, params); err != nil {
logrus.Error(err)
return "", err
}
secretGetter, err := NewRancherSecretGetter(options)
if err != nil {
logrus.Error(err)
return "", err
}
secrets, err := secretGetter.GetSecrets(options)
if err != nil {
logrus.Error(err)
return "", err
}
decryptor, err := NewRSADecryptor(hostKeyPath)
if err != nil {
logrus.Error(err)
return "", err
}
secretWriter, err := NewRSASecretFileWriter(decryptor)
if err != nil {
logrus.Error(err)
return "", err
}
return volumeDevice, secretWriter.Write(secrets, volumeDevice)
}
// Detach effectively erases the volume.
func (sv *FlexVolume) Detach(device string) error {
if err := mount.Unmount(device); err != nil {
return err
}
return os.RemoveAll(device)
}
// Mount implements does a bind mount of the volume to the target directory
func (sv *FlexVolume) Mount(dir, device string, params map[string]interface{}) error {
//Default volume mode
return mount.Mount(device, dir, "none", "bind,rw")
}
// Unmount undoes the bind mount, and removes the target directory
func (sv *FlexVolume) Unmount(dir string) error {
// This will be a bind mount
if err := mount.Unmount(dir); err != nil {
return err
}
return os.RemoveAll(dir)
}
func createTmpfs(dir string, options map[string]interface{}) error {
mounted, err := mount.Mounted(dir)
if mounted || err != nil {
return err
}
mode := int(0755)
mountOpts := "size=10m"
if uMode, ok := options["mode"].(int); ok {
mode = int(uMode)
}
if mOpts, ok := options["mountOpts"].(string); ok {
mountOpts = mOpts
}
if err := os.MkdirAll(dir, os.FileMode(mode)); err != nil {
return err
}
return mount.Mount("tmpfs", dir, "tmpfs", mountOpts)
}