This repository has been archived by the owner on Sep 24, 2021. It is now read-only.
/
flexvolume.go
232 lines (188 loc) · 6.77 KB
/
flexvolume.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
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cinder
import (
"encoding/json"
"fmt"
"os"
"github.com/golang/glog"
"k8s.io/frakti/pkg/flexvolume"
utilmetadata "k8s.io/frakti/pkg/util/metadata"
)
type FlexVolumeDriver struct {
uuid string
name string
volId string
fsType string
cinderConfig string
readOnly bool
manager *FlexManager
// metadata provides meta of the volume
metadata map[string]interface{}
}
// NewFlexVolumeDriver returns a flex volume driver
func NewFlexVolumeDriver(uuid string, name string) *FlexVolumeDriver {
return &FlexVolumeDriver{
uuid: uuid,
name: name,
}
}
// Invocation: <driver executable> init
func (d *FlexVolumeDriver) Init() (map[string]interface{}, error) {
// "{\"status\": \"Success\", \"capabilities\": {\"attach\": false}}"
return map[string]interface{}{
"capabilities": map[string]bool{
"attach": false,
},
}, nil
}
// initFlexVolumeDriverForMount parse user provided jsonOptions to initialize FlexVolumeDriver
func (d *FlexVolumeDriver) initFlexVolumeDriverForMount(jsonOptions string) error {
var volOptions map[string]interface{}
json.Unmarshal([]byte(jsonOptions), &volOptions)
if len(volOptions[flexvolume.VolIdKey].(string)) == 0 {
return fmt.Errorf("jsonOptions is not set by user properly: %#v", jsonOptions)
}
// cinder configure file is optional in jsonOptions
if userConfig, ok := volOptions[flexvolume.CinderConfigKey]; ok {
d.cinderConfig = userConfig.(string)
} else {
// use default configure if not provided
d.cinderConfig = flexvolume.CinderConfigFile
}
d.volId = volOptions[flexvolume.VolIdKey].(string)
// this is a system option
d.fsType = volOptions[flexvolume.SystemFsTypeKey].(string)
manager, err := NewFlexManager(d.cinderConfig)
if err != nil {
return err
}
d.manager = manager
return nil
}
// initFlexVolumeDriverForUnMount use targetMountDir to initialize FlexVolumeDriver from magic file
func (d *FlexVolumeDriver) initFlexVolumeDriverForUnMount(targetMountDir string) error {
// use the magic file to store volId since flexvolume will execute fresh new binary every time
var optsData flexvolume.FlexVolumeOptsData
err := flexvolume.ReadJsonOptsFile(targetMountDir, &optsData)
if err != nil {
return err
}
d.cinderConfig = optsData.CinderData.ConfigKey
d.volId = optsData.CinderData.VolumeID
manager, err := NewFlexManager(d.cinderConfig)
if err != nil {
return err
}
d.manager = manager
return nil
}
// Invocation: <driver executable> attach <json options> <node name>
func (d *FlexVolumeDriver) Attach(jsonOptions, nodeName string) (map[string]interface{}, error) {
return nil, nil
}
// Invocation: <driver executable> detach <mount device> <node name>
func (d *FlexVolumeDriver) Detach(mountDev, nodeName string) (map[string]interface{}, error) {
return nil, nil
}
// Invocation: <driver executable> waitforattach <mount device> <json options>
func (d *FlexVolumeDriver) WaitForAttach(mountDev, jsonOptions string) (map[string]interface{}, error) {
return map[string]interface{}{"device": mountDev}, nil
}
// Invocation: <driver executable> isattached <json options> <node name>
func (d *FlexVolumeDriver) IsAttached(jsonOptions, nodeName string) (map[string]interface{}, error) {
return map[string]interface{}{"attached": true}, nil
}
// Invocation: <driver executable> mount <mount dir> <json options>
// mount will:
// 1. attach Cinder volume to target dir by AttachDisk
// 2. store meta data generated by AttachDisk into a json file in target dir
func (d *FlexVolumeDriver) Mount(targetMountDir, jsonOptions string) (map[string]interface{}, error) {
glog.V(5).Infof("Cinder flexvolume mount %s to %s", d.volId, targetMountDir)
// initialize cinder driver from user provided jsonOptions
if err := d.initFlexVolumeDriverForMount(jsonOptions); err != nil {
return nil, err
}
// attach cinder disk to host machine
if err := d.manager.AttachDisk(d, targetMountDir); err != nil {
glog.V(4).Infof("AttachDisk failed: %v", err)
return nil, err
}
glog.V(3).Infof("Cinder volume %s attached", d.volId)
// append VolumeOptions with metadata
optsData := &flexvolume.FlexVolumeOptsData{
CinderData: d.generateOptionsData(d.metadata),
}
// create a file and write metadata into the it
if err := flexvolume.WriteJsonOptsFile(targetMountDir, optsData); err != nil {
os.Remove(targetMountDir)
detachDiskLogError(d)
return nil, err
}
return nil, nil
}
func (d *FlexVolumeDriver) generateOptionsData(metadata map[string]interface{}) *flexvolume.CinderVolumeOptsData {
var result *flexvolume.CinderVolumeOptsData
result.ConfigKey = d.cinderConfig
result.VolumeID = d.volId
result.FsType = d.fsType
if data, ok := metadata["volume_type"]; ok {
result.VolumeType = data.(string)
}
if data, ok := metadata["name"]; ok {
result.Name = data.(string)
}
if data, ok := metadata["hosts"]; ok {
if hosts, err := utilmetadata.ExtractStringSlice(data); err != nil {
glog.V(4).Infof("cannot parse metadata hosts: %v", err)
} else {
result.Hosts = hosts
}
}
if data, ok := metadata["ports"]; ok {
if ports, err := utilmetadata.ExtractStringSlice(data); err != nil {
glog.V(4).Infof("cannot parse metadata ports: %v", err)
} else {
result.Ports = ports
}
}
return result
}
// detachDiskLogError is a wrapper to detach first before log error
func detachDiskLogError(d *FlexVolumeDriver) {
err := d.manager.DetachDisk(d)
if err != nil {
glog.Warningf("Failed to detach disk: %v (%v)", d, err)
}
}
// Invocation: <driver executable> unmount <mount dir>
func (d *FlexVolumeDriver) Unmount(targetMountDir string) (map[string]interface{}, error) {
glog.V(5).Infof("Cinder flexvolume unmount of %s", targetMountDir)
// check the target directory
if _, err := os.Stat(targetMountDir); os.IsNotExist(err) {
return nil, fmt.Errorf("volume directory: %v does not exist", targetMountDir)
}
// initialize FlexVolumeDriver manager by reading cinderConfig from metadata file
if err := d.initFlexVolumeDriverForUnMount(targetMountDir); err != nil {
return nil, err
}
if err := d.manager.DetachDisk(d); err != nil {
return nil, err
}
// NOTE: the targetDir will be cleaned by flexvolume,
// we just need to clean up the metadata file.
if err := flexvolume.CleanUpMetadataFile(targetMountDir); err != nil {
return nil, err
}
return nil, nil
}