-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
251 lines (218 loc) · 9.04 KB
/
node.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package services
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/choffmeister/csi-driver-truenas/internal/backends"
"github.com/choffmeister/csi-driver-truenas/internal/utils"
proto "github.com/container-storage-interface/spec/lib/go/csi"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var _ proto.NodeServer = (*NodeService)(nil)
type NodeService struct {
NodeId string
mountUtils *utils.MountUtils
iscsiUtils *utils.ISCSIUtils
}
func NewNodeService(nodeId string) *NodeService {
return &NodeService{
NodeId: nodeId,
mountUtils: utils.NewMountUtils(),
iscsiUtils: utils.NewISCSIUtils(),
}
}
func (s *NodeService) NodeStageVolume(ctx context.Context, req *proto.NodeStageVolumeRequest) (*proto.NodeStageVolumeResponse, error) {
return nil, status.Error(codes.Unimplemented, "not supported: NodeStageVolume")
}
func (s *NodeService) NodeUnstageVolume(ctx context.Context, req *proto.NodeUnstageVolumeRequest) (*proto.NodeUnstageVolumeResponse, error) {
return nil, status.Error(codes.Unimplemented, "not supported: NodeUnstageVolume")
}
func (s *NodeService) NodePublishVolume(ctx context.Context, req *proto.NodePublishVolumeRequest) (*proto.NodePublishVolumeResponse, error) {
if ephemeral := req.VolumeContext["csi.storage.k8s.io/ephemeral"]; ephemeral == "true" {
cifsIP := req.Secrets["cifs-ip"]
if cifsIP == "" {
return nil, status.Error(codes.InvalidArgument, "secret value cifs-ip is missing")
}
cifsUsername := req.Secrets["cifs-username"]
if cifsUsername == "" {
return nil, status.Error(codes.InvalidArgument, "secret value cifs-username is missing")
}
cifsPassword := req.Secrets["cifs-password"]
if cifsPassword == "" {
return nil, status.Error(codes.InvalidArgument, "secret value cifs-password is missing")
}
cifsShare := req.VolumeContext["cifs-share"]
if cifsShare == "" {
return nil, status.Error(codes.InvalidArgument, "volume context value cifs-share is missing")
}
cifs := fmt.Sprintf("//%s/%s", cifsIP, cifsShare)
cifsUID := req.VolumeContext["cifs-uid"]
cifsGID := req.VolumeContext["cifs-gid"]
options := []string{}
options = append(options, "username="+cifsUsername)
options = append(options, "password="+cifsPassword)
if cifsUID != "" {
options = append(options, "uid="+cifsUID)
}
if cifsGID != "" {
options = append(options, "gid="+cifsGID)
}
utils.Info.Printf("Mounting cifs %s to %s\n", cifs, req.TargetPath)
if err := os.MkdirAll(req.TargetPath, 0o775); err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("unable to create mount target path: %v", err))
}
_, _, err := utils.Command("mount", "-t", "cifs", "-o", strings.Join(options, ","), cifs, req.TargetPath)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("unable to cifs mount: %v", err))
}
return &proto.NodePublishVolumeResponse{}, nil
}
iscsiTarget := req.VolumeContext["iscsi-iqn"]
if iscsiTarget == "" {
return nil, status.Error(codes.InvalidArgument, "secret value iscsi-iqn is missing")
}
backend, err := NewBackendForNodePublish(req.PublishContext, req.Secrets)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("unable to create backend: %v", err))
}
iscsi, err := backends.LoadISCSISecrets(req.Secrets)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("unable to load iscsi secrets: %v", err))
}
podNamespace := req.VolumeContext["csi.storage.k8s.io/pod.namespace"]
podName := req.VolumeContext["csi.storage.k8s.io/pod.name"]
if podNamespace != "" && podName != "" {
err := backend.CommentVolume(ctx, req.VolumeId, fmt.Sprintf("%s/%s", podNamespace, podName))
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("unable to set volume comment: %v", err))
}
}
err = s.iscsiUtils.Login(iscsi.PortalIP, iscsi.PortalPort, iscsiTarget)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("unable to log into iscsi session: %v", err))
}
// TODO instead of hardcoding a wait time here it should be watched for the device to be available
time.Sleep(1 * time.Second)
devicePath, err := s.iscsiUtils.GenerateDeviceName(iscsi.PortalIP, iscsi.PortalPort, iscsiTarget)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("unable to generate iscsi device path: %v", err))
}
// TODO get desired file system from request
if err := s.mountUtils.FormatAndMountDevice(devicePath, req.TargetPath, "ext4"); err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("unable to mount device: %v", err))
}
utils.Info.Printf("Published volume %s\n", req.VolumeId)
return &proto.NodePublishVolumeResponse{}, nil
}
func (s *NodeService) NodeUnpublishVolume(ctx context.Context, req *proto.NodeUnpublishVolumeRequest) (*proto.NodeUnpublishVolumeResponse, error) {
_, err := NewBackendForNodeUnpublish()
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("unable to create backend: %v", err))
}
devicePath, _, err := s.mountUtils.GetDeviceNameFromMount(req.TargetPath)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("unable to detect device path from mountpoint: %v", err))
}
if err := s.mountUtils.UnmountDevice(req.TargetPath); err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("unable to unmount device: %v", err))
}
if strings.HasPrefix(devicePath, "//") {
// looks like cifs
return &proto.NodeUnpublishVolumeResponse{}, nil
}
portalIP, portalPort, iscsiTarget, err := s.iscsiUtils.ParseDeviceName(devicePath)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("unable to detect iscsi information from device path: %v", err))
}
if err := s.iscsiUtils.Logout(portalIP, portalPort, iscsiTarget); err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("unable to log out of iscsi session: %v", err))
}
utils.Info.Printf("Unpublished volume %s\n", req.VolumeId)
return &proto.NodeUnpublishVolumeResponse{}, nil
}
func (s *NodeService) NodeGetVolumeStats(ctx context.Context, req *proto.NodeGetVolumeStatsRequest) (*proto.NodeGetVolumeStatsResponse, error) {
if req.VolumePath == "" {
return nil, status.Error(codes.InvalidArgument, "missing volume path")
}
totalBytes, usedBytes, availableBytes, err := s.mountUtils.ByteFilesystemStats(req.VolumePath)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("failed to get volume byte stats: %s", err))
}
totalINodes, usedINodes, freeINodes, err := s.mountUtils.INodeFilesystemStats(req.VolumePath)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("failed to get volume inode stats: %s", err))
}
resp := &proto.NodeGetVolumeStatsResponse{
Usage: []*proto.VolumeUsage{
{
Unit: proto.VolumeUsage_BYTES,
Available: availableBytes,
Total: totalBytes,
Used: usedBytes,
},
{
Unit: proto.VolumeUsage_INODES,
Available: freeINodes,
Total: totalINodes,
Used: usedINodes,
},
},
}
return resp, nil
}
func (s *NodeService) NodeGetCapabilities(ctx context.Context, req *proto.NodeGetCapabilitiesRequest) (*proto.NodeGetCapabilitiesResponse, error) {
resp := &proto.NodeGetCapabilitiesResponse{
Capabilities: []*proto.NodeServiceCapability{
{
Type: &proto.NodeServiceCapability_Rpc{
Rpc: &proto.NodeServiceCapability_RPC{
Type: proto.NodeServiceCapability_RPC_EXPAND_VOLUME,
},
},
},
{
Type: &proto.NodeServiceCapability_Rpc{
Rpc: &proto.NodeServiceCapability_RPC{
Type: proto.NodeServiceCapability_RPC_GET_VOLUME_STATS,
},
},
},
},
}
return resp, nil
}
func (s *NodeService) NodeGetInfo(ctx context.Context, req *proto.NodeGetInfoRequest) (*proto.NodeGetInfoResponse, error) {
resp := &proto.NodeGetInfoResponse{
NodeId: s.NodeId,
}
return resp, nil
}
func (s *NodeService) NodeExpandVolume(ctx context.Context, req *proto.NodeExpandVolumeRequest) (*proto.NodeExpandVolumeResponse, error) {
size, _, ok := volumeSizeFromCapacityRange(req.GetCapacityRange())
if !ok {
return nil, status.Error(codes.OutOfRange, "invalid capacity range")
}
_, err := NewBackendForNodeExpandVolume()
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("unable to create backend: %v", err))
}
devicePath, _, err := s.mountUtils.GetDeviceNameFromMount(req.VolumePath)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("unable to detect device path from mountpoint: %v", err))
}
_, _, iscsiTarget, err := s.iscsiUtils.ParseDeviceName(devicePath)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("unable to detect iscsi information from device path: %v", err))
}
if err := s.iscsiUtils.Rescan(iscsiTarget); err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("unable to rescan iscsi target: %v", err))
}
if err := s.mountUtils.ResizeDevice(devicePath, req.VolumePath); err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("unable to resize device file system: %v", err))
}
utils.Info.Printf("Expanded volume %s\n", req.VolumeId)
return &proto.NodeExpandVolumeResponse{CapacityBytes: size}, nil
}