This repository has been archived by the owner on Feb 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
node.go
203 lines (173 loc) · 5.9 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
package service
import (
"bufio"
"bytes"
"errors"
"os"
"os/exec"
"path/filepath"
"strings"
"k8s.io/utils/mount"
"github.com/container-storage-interface/spec/lib/go/csi"
ovirtsdk "github.com/ovirt/go-ovirt"
"golang.org/x/net/context"
"k8s.io/klog"
)
type NodeService struct {
nodeId string
ovirtClient *OvirtClient
}
var NodeCaps = []csi.NodeServiceCapability_RPC_Type{
csi.NodeServiceCapability_RPC_STAGE_UNSTAGE_VOLUME,
}
func devFromVolumeId(id string, diskInterface ovirtsdk.DiskInterface) (string, error) {
switch diskInterface {
case ovirtsdk.DISKINTERFACE_VIRTIO:
return "/dev/disk/by-id/virtio-" + id, nil
case ovirtsdk.DISKINTERFACE_VIRTIO_SCSI:
return "/dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_" + id, nil
}
return "", errors.New("device type is unsupported")
}
func (n *NodeService) NodeStageVolume(_ context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
klog.Infof("Staging volume %s with %+v", req.VolumeId, req)
device, err := getDeviceByAttachmentId(req.VolumeId, n.nodeId, n.ovirtClient.connection)
// is there a filesystem on this device?
filesystem, err := getDeviceInfo(device)
if err != nil {
return nil, err
}
fsType := req.VolumeCapability.GetMount().FsType
if filesystem == "" {
// no filesystem - create it
klog.Infof("Creating FS %s on device %s", fsType, device)
makeFSErr := makeFS(device, fsType)
if makeFSErr != nil {
return nil, makeFSErr
}
}
return &csi.NodeStageVolumeResponse{}, nil
}
func (n *NodeService) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstageVolumeRequest) (*csi.NodeUnstageVolumeResponse, error) {
return &csi.NodeUnstageVolumeResponse{}, nil
}
func (n *NodeService) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
device, err := getDeviceByAttachmentId(req.VolumeId, n.nodeId, n.ovirtClient.connection)
targetPath := req.GetTargetPath()
err = os.MkdirAll(targetPath, 0750)
if err != nil {
return nil, errors.New(err.Error())
}
fsType := req.VolumeCapability.GetMount().FsType
klog.Infof("Mounting devicePath %s, on targetPath: %s with FS type: %s",
device, targetPath, fsType)
mounter := mount.New("")
err = mounter.Mount(device, targetPath, fsType, []string{})
if err != nil {
klog.Errorf("Failed mounting %v", err)
return nil, err
}
return &csi.NodePublishVolumeResponse{}, nil
}
func (n *NodeService) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublishVolumeRequest) (*csi.NodeUnpublishVolumeResponse, error) {
mounter := mount.New("")
klog.Infof("Unmounting %s", req.GetTargetPath())
err := mounter.Unmount(req.GetTargetPath())
if err != nil {
klog.Infof("Failed to unmount")
return nil, err
}
return &csi.NodeUnpublishVolumeResponse{}, nil
}
func (n *NodeService) NodeGetVolumeStats(context.Context, *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) {
panic("implement me")
}
func (n *NodeService) NodeExpandVolume(context.Context, *csi.NodeExpandVolumeRequest) (*csi.NodeExpandVolumeResponse, error) {
panic("implement me")
}
func (n *NodeService) NodeGetInfo(context.Context, *csi.NodeGetInfoRequest) (*csi.NodeGetInfoResponse, error) {
return &csi.NodeGetInfoResponse{NodeId: n.nodeId}, nil
}
func (n *NodeService) NodeGetCapabilities(context.Context, *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) {
caps := make([]*csi.NodeServiceCapability, 0, len(NodeCaps))
for _, c := range NodeCaps {
caps = append(
caps,
&csi.NodeServiceCapability{
Type: &csi.NodeServiceCapability_Rpc{
Rpc: &csi.NodeServiceCapability_RPC{
Type: c,
},
},
},
)
}
return &csi.NodeGetCapabilitiesResponse{Capabilities: caps}, nil
}
func getDeviceByAttachmentId(volumeID, nodeID string, conn *ovirtsdk.Connection) (string, error) {
attachment, err := diskAttachmentByVmAndDisk(conn, nodeID, volumeID)
if err != nil {
return "", err
}
klog.Infof("Extracting pvc volume name %s", volumeID)
disk, _ := conn.FollowLink(attachment.MustDisk())
diskID := ""
if disk, ok := disk.(*ovirtsdk.Disk); !ok {
return "", errors.New("Couldn't retrieve disk from attachemnt")
} else {
diskID = disk.MustId()[:20]
klog.Infof("Extracted pvc volume name %s", diskID)
}
device, err := devFromVolumeId(diskID, attachment.MustInterface())
if err != nil {
return "", err
}
return device, nil
}
// getDeviceInfo will return the first Device which is a partition and its filesystem.
// if the given Device disk has no partition then an empty zero valued device will return
func getDeviceInfo(device string) (string, error) {
devicePath, err := filepath.EvalSymlinks(device)
if err != nil {
return "", errors.New(err.Error())
}
cmd := exec.Command("lsblk", "-nro", "FSTYPE", devicePath)
out, err := cmd.Output()
exitError, incompleteCmd := err.(*exec.ExitError)
if err != nil && incompleteCmd {
return "", errors.New(err.Error() + "lsblk failed with " + string(exitError.Stderr))
}
reader := bufio.NewReader(bytes.NewReader(out))
line, _, err := reader.ReadLine()
if err != nil {
return "", err
}
return string(line), nil
}
func makeFS(device string, fsType string) error {
// caution, use -F to force creating the filesystem if it doesn't exit. May not be portable for fs other
// than ext family
klog.Infof("Mounting device %s, with FS %s", device, fsType)
var force string
if strings.HasPrefix(fsType, "ext") {
force = "-F"
}
cmd := exec.Command("mkfs", force, "-t", fsType, device)
err := cmd.Run()
exitError, incompleteCmd := err.(*exec.ExitError)
if err != nil && incompleteCmd {
return errors.New(err.Error() + " mkfs failed with " + string(exitError.Error()))
}
return nil
}
// isMountpoint find out if a given directory is a real mount point
func isMountpoint(mountDir string) bool {
cmd := exec.Command("findmnt", mountDir)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return false
}
return true
}