-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.go
More file actions
142 lines (120 loc) · 5.1 KB
/
controller.go
File metadata and controls
142 lines (120 loc) · 5.1 KB
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
package main
import (
"context"
"errors"
"github.com/container-storage-interface/spec/lib/go/csi"
"io/fs"
"k8s.io/klog/v2"
"os"
)
type Controller struct {
csi.ControllerServer
nfs *Nfs
}
var Volume = make(map[string]string)
// create disk then return volume info
func (c *Controller) CreateVolume(ctx context.Context, request *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
klog.Infof("CreateVolume: called with args %#v", request)
// 远程创建一个目录 create directory on remote
dt := c.nfs.provisionalPath(request.Name)
if dt.err != nil { // 错误创建
klog.Infoln("CreateVolume provisionalPath", dt.err)
return nil, dt.err
}
dt.createPath("/" + request.Name)
// umount nfs and delete local dir
err := c.nfs.umount(dt.localPath)
if err != nil {
klog.Infoln("CreateVolume umount", err)
return nil, err
}
return &csi.CreateVolumeResponse{
Volume: &csi.Volume{
VolumeId: dt.name,
CapacityBytes: 0,
VolumeContext: request.GetParameters(),
},
}, nil
}
func (c *Controller) DeleteVolume(ctx context.Context, request *csi.DeleteVolumeRequest) (*csi.DeleteVolumeResponse, error) {
klog.Infof("DeleteVolume: called with args %#v", request)
dt := c.nfs.provisionalPath(request.VolumeId)
if dt.err != nil { // 错误创建
klog.Infoln("provisionalPath", dt.err)
return nil, dt.err
}
// 挂载远程目录
//c.nfs.mount("", dt.localPath)
err := os.RemoveAll(dt.localPath + "/" + request.VolumeId)
klog.Infoln("DeleteVolume Remove", err)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
klog.Infoln("DeleteVolume Remove", err)
return nil, err
}
err = c.nfs.umount(dt.localPath)
// 还需要额外移除node上临时创建的目录
err = dt.removeProvisionalPath()
return &csi.DeleteVolumeResponse{}, nil
}
// ControllerPublishVolume 附加卷 在nfs可以不做任何操作
func (Controller) ControllerPublishVolume(ctx context.Context, request *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error) {
klog.Infof("ControllerPublishVolume: called with args %#v", request)
return &csi.ControllerPublishVolumeResponse{}, nil
}
func (Controller) ControllerUnpublishVolume(ctx context.Context, request *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
klog.Infof("ControllerUnpublishVolume: called with args %#v", request)
return &csi.ControllerUnpublishVolumeResponse{}, nil
}
func (Controller) ValidateVolumeCapabilities(ctx context.Context, request *csi.ValidateVolumeCapabilitiesRequest) (*csi.ValidateVolumeCapabilitiesResponse, error) {
klog.Infof("ValidateVolumeCapabilities: called with args %#v", request)
return &csi.ValidateVolumeCapabilitiesResponse{}, nil
}
func (Controller) ListVolumes(ctx context.Context, request *csi.ListVolumesRequest) (*csi.ListVolumesResponse, error) {
klog.Infof("ListVolumes: called with args %#v", request)
return &csi.ListVolumesResponse{}, nil
}
func (Controller) GetCapacity(ctx context.Context, request *csi.GetCapacityRequest) (*csi.GetCapacityResponse, error) {
klog.Infof("GetCapacity: called with args %#v", request)
return &csi.GetCapacityResponse{}, nil
}
var (
controllerCaps = []csi.ControllerServiceCapability_RPC_Type{
csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME,
csi.ControllerServiceCapability_RPC_SINGLE_NODE_MULTI_WRITER,
}
)
func (Controller) ControllerGetCapabilities(ctx context.Context, request *csi.ControllerGetCapabilitiesRequest) (*csi.ControllerGetCapabilitiesResponse, error) {
klog.Infof("ControllerGetCapabilities: called with args %#v", request)
var caps []*csi.ControllerServiceCapability
for _, controllerCap := range controllerCaps {
c := &csi.ControllerServiceCapability{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: controllerCap,
},
},
}
caps = append(caps, c)
}
return &csi.ControllerGetCapabilitiesResponse{Capabilities: caps}, nil
}
func (Controller) CreateSnapshot(ctx context.Context, request *csi.CreateSnapshotRequest) (*csi.CreateSnapshotResponse, error) {
klog.Infof("CreateSnapshot: called with args %#v", request)
return &csi.CreateSnapshotResponse{}, nil
}
func (Controller) DeleteSnapshot(ctx context.Context, request *csi.DeleteSnapshotRequest) (*csi.DeleteSnapshotResponse, error) {
klog.Infof("DeleteSnapshot: called with args %#v", request)
return &csi.DeleteSnapshotResponse{}, nil
}
func (Controller) ListSnapshots(ctx context.Context, request *csi.ListSnapshotsRequest) (*csi.ListSnapshotsResponse, error) {
klog.Infof("ListSnapshots: called with args %#v", request)
return &csi.ListSnapshotsResponse{}, nil
}
func (Controller) ControllerExpandVolume(ctx context.Context, request *csi.ControllerExpandVolumeRequest) (*csi.ControllerExpandVolumeResponse, error) {
klog.Infof("ControllerExpandVolume: called with args %#v", request)
return &csi.ControllerExpandVolumeResponse{}, nil
}
func (Controller) ControllerGetVolume(ctx context.Context, request *csi.ControllerGetVolumeRequest) (*csi.ControllerGetVolumeResponse, error) {
klog.Infof("ControllerGetVolume: called with args %#v", request)
return &csi.ControllerGetVolumeResponse{}, nil
}