/
node_volume.go
158 lines (128 loc) · 4.06 KB
/
node_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
148
149
150
151
152
153
154
155
156
157
158
package volumeinfo
import (
"encoding/json"
"os"
"path"
"sync"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/klog"
)
type NodeVolumeStatus string
const (
nodeVolumeSaveFileName string = "node_volumes.json"
NodeVolumeStatusStage string = "stage"
)
// NodeVolume class, used by node to track created volumes
type NodeVolume struct {
ID string `yaml:"id" json:"id"`
StagingMountPath string `yaml:"staging_mount_path" json:"staging_mount_path"`
MountPath string `yaml:"mount_path" json:"mount_path"`
StagingMountOptions []string `yaml:"staging_mount_options" json:"staging_mount_options"`
MountOptions []string `yaml:"mount_options" json:"mount_options"`
ClientType string `yaml:"client_type" json:"client_type"`
ClientConfig map[string]string `yaml:"client_config" json:"client_config"`
DynamicVolumeProvisioning bool `yaml:"dynamic_volume_provisioning" json:"dynamic_volume_provisioning"`
StageVolume bool `yaml:"stage_volume" json:"stage_volume"`
}
// NodeVolumeManager manages node volumes
type NodeVolumeManager struct {
encryptKey string
savefilePath string
volumes map[string]*NodeVolume
mutex sync.Mutex
}
// NewNodeVolumeManager creates ControllerVolumeManager
func NewNodeVolumeManager(encryptKey string, saveDirPath string) (*NodeVolumeManager, error) {
if saveDirPath == "" {
saveDirPath = "/"
}
manager := &NodeVolumeManager{
encryptKey: encryptKey,
savefilePath: path.Join(saveDirPath, nodeVolumeSaveFileName),
volumes: map[string]*NodeVolume{},
mutex: sync.Mutex{},
}
err := manager.load()
if err != nil {
klog.Errorf("failed to access volume file %q, %s. ignoring...", manager.savefilePath, err)
return manager, nil
}
return manager, nil
}
func (manager *NodeVolumeManager) save() error {
jsonBytes, err := json.Marshal(manager.volumes)
if err != nil {
return status.Errorf(codes.Internal, err.Error())
}
// encrypt data
encryptedBytes, err := encrypt(jsonBytes, []byte(manager.encryptKey))
if err != nil {
return status.Errorf(codes.Internal, err.Error())
}
return os.WriteFile(manager.savefilePath, encryptedBytes, 0644)
}
func (manager *NodeVolumeManager) load() error {
_, err := os.Stat(manager.savefilePath)
if err != nil {
if os.IsNotExist(err) {
// file not exist
return nil
}
return status.Errorf(codes.Internal, err.Error())
}
jsonBytes, err := os.ReadFile(manager.savefilePath)
if err != nil {
return status.Errorf(codes.Internal, err.Error())
}
if len(jsonBytes) > 0 && json.Valid(jsonBytes) {
// plaintext json
return json.Unmarshal(jsonBytes, &manager.volumes)
}
// decrypt data
decryptedBytes, err := decrypt(jsonBytes, []byte(manager.encryptKey))
if err != nil {
return status.Errorf(codes.Internal, err.Error())
}
if len(decryptedBytes) > 0 && json.Valid(decryptedBytes) {
// plaintext json
return json.Unmarshal(decryptedBytes, &manager.volumes)
}
return json.Unmarshal(decryptedBytes, &manager.volumes)
}
// Get returns the volume with given id
func (manager *NodeVolumeManager) Get(id string) *NodeVolume {
manager.mutex.Lock()
defer manager.mutex.Unlock()
vol, ok := manager.volumes[id]
if !ok {
return nil
}
return vol
}
// Put puts a volume
func (manager *NodeVolumeManager) Put(volume *NodeVolume) error {
manager.mutex.Lock()
defer manager.mutex.Unlock()
manager.volumes[volume.ID] = volume
return manager.save()
}
// Pop returns NodeVolume with given id and delete
func (manager *NodeVolumeManager) Pop(id string) (*NodeVolume, error) {
manager.mutex.Lock()
defer manager.mutex.Unlock()
vol, ok := manager.volumes[id]
if ok {
delete(manager.volumes, id)
err := manager.save()
return vol, err
}
return nil, nil
}
// Check returns presence of NodeVolume with given id
func (manager *NodeVolumeManager) Check(id string) bool {
manager.mutex.Lock()
defer manager.mutex.Unlock()
_, ok := manager.volumes[id]
return ok
}