-
Notifications
You must be signed in to change notification settings - Fork 74
/
controller.go
196 lines (154 loc) · 6.39 KB
/
controller.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
package controller
import (
"context"
"fmt"
volume "github.com/hwameistor/hwameistor/pkg/local-disk-manager/member/controller/volume"
"github.com/container-storage-interface/spec/lib/go/csi"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/hwameistor/hwameistor/pkg/local-disk-manager/utils"
)
type Server struct {
// supportControllerCapability
supportControllerCapability []*csi.ControllerServiceCapability
// vm manager volume create,delete,query
vm volume.Manager
}
func NewServer() *Server {
server := &Server{}
server.vm = volume.New()
server.initControllerCapability()
return server
}
func (s *Server) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
log.Infof("Calling %s ...", utils.FuncName())
// validate request
if err := s.validateCreateVolumeRequest(req); err != nil {
log.WithError(err).Error("CreateVolumeRequest is invalid")
return nil, err
}
volume, err := s.vm.GetVolumeInfo(req.GetName())
if err != nil {
log.WithError(err).Error("Failed to get volume info")
return nil, err
}
// The difference between Update and Create is the logic of select disk
// In Create: we assume that the data volume is not bound to a disk,
// so we will comprehensively consider the nodes filtered by the scheduler and
// the internal parameters of the data volume request, and finally filter a qualified devPath
//
// In Update: we will not modify the topology info and the devPath of the volume
if volume.Exist {
if volume, err = s.vm.UpdateVolume(req.GetName(), req); err != nil {
log.WithError(err).Error("Failed to UpdateVolume")
return nil, err
}
log.Infof("Volume %s update success", req.GetName())
} else {
if volume, err = s.vm.CreateVolume(req.GetName(), req); err != nil {
log.WithError(err).Error("Failed to CreateVolume")
return nil, err
}
log.Infof("Volume %s created success", req.GetName())
}
return &csi.CreateVolumeResponse{
Volume: &csi.Volume{
CapacityBytes: volume.Capacity,
VolumeId: volume.Name,
VolumeContext: volume.VolumeContext,
},
}, nil
}
func (s *Server) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest) (*csi.DeleteVolumeResponse, error) {
log.Infof("Calling %s ...", utils.FuncName())
if req.GetVolumeId() == "" {
return nil, fmt.Errorf("volumeid is empty")
}
return &csi.DeleteVolumeResponse{}, s.vm.DeleteVolume(ctx, req.GetVolumeId())
}
func (s *Server) ControllerPublishVolume(context.Context, *csi.ControllerPublishVolumeRequest) (*csi.ControllerPublishVolumeResponse, error) {
log.Infof("Calling %s ...", utils.FuncName())
// just return success here
return &csi.ControllerPublishVolumeResponse{}, nil
}
func (s *Server) ControllerUnpublishVolume(context.Context, *csi.ControllerUnpublishVolumeRequest) (*csi.ControllerUnpublishVolumeResponse, error) {
log.Infof("Calling %s ...", utils.FuncName())
// just return success here
return &csi.ControllerUnpublishVolumeResponse{}, nil
}
func (s *Server) ValidateVolumeCapabilities(context.Context, *csi.ValidateVolumeCapabilitiesRequest) (*csi.ValidateVolumeCapabilitiesResponse, error) {
log.Infof("Calling %s ...", utils.FuncName())
return nil, fmt.Errorf("not implemented")
}
func (s *Server) ListVolumes(context.Context, *csi.ListVolumesRequest) (*csi.ListVolumesResponse, error) {
log.Infof("Calling %s ...", utils.FuncName())
return nil, fmt.Errorf("not implemented")
}
func (s *Server) GetCapacity(context.Context, *csi.GetCapacityRequest) (*csi.GetCapacityResponse, error) {
log.Infof("Calling %s ...", utils.FuncName())
return nil, fmt.Errorf("not implemented")
}
func (s *Server) ControllerGetCapabilities(context.Context, *csi.ControllerGetCapabilitiesRequest) (*csi.ControllerGetCapabilitiesResponse, error) {
log.Infof("Calling %s ...", utils.FuncName())
return &csi.ControllerGetCapabilitiesResponse{Capabilities: s.supportControllerCapability}, nil
}
func (s *Server) CreateSnapshot(context.Context, *csi.CreateSnapshotRequest) (*csi.CreateSnapshotResponse, error) {
log.Infof("Calling %s ...", utils.FuncName())
return nil, fmt.Errorf("not implemented")
}
func (s *Server) DeleteSnapshot(context.Context, *csi.DeleteSnapshotRequest) (*csi.DeleteSnapshotResponse, error) {
log.Infof("Calling %s ...", utils.FuncName())
return nil, fmt.Errorf("not implemented")
}
func (s *Server) ListSnapshots(context.Context, *csi.ListSnapshotsRequest) (*csi.ListSnapshotsResponse, error) {
log.Infof("Calling %s ...", utils.FuncName())
return nil, fmt.Errorf("not implemented")
}
func (s *Server) ControllerExpandVolume(context.Context, *csi.ControllerExpandVolumeRequest) (*csi.ControllerExpandVolumeResponse, error) {
log.Infof("Calling %s ...", utils.FuncName())
return nil, fmt.Errorf("not implemented")
}
func (s *Server) ControllerGetVolume(context.Context, *csi.ControllerGetVolumeRequest) (*csi.ControllerGetVolumeResponse, error) {
log.Infof("Calling %s ...", utils.FuncName())
return nil, fmt.Errorf("not implemented")
}
func (s *Server) getControllerCapability() []*csi.ControllerServiceCapability {
return s.supportControllerCapability
}
func (s *Server) verifyControllerCapability(needCap csi.ControllerServiceCapability_RPC_Type) error {
for _, existCap := range s.getControllerCapability() {
if existCap.GetRpc().Type == needCap {
return nil
}
}
return status.Errorf(codes.InvalidArgument, "unsupported controller capability %s", needCap)
}
func (s *Server) validateCreateVolumeRequest(req *csi.CreateVolumeRequest) error {
// verify Capability
if err := s.verifyControllerCapability(csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME); err != nil {
return err
}
return nil
}
func (s *Server) initControllerCapability() {
caps := []csi.ControllerServiceCapability_RPC_Type{
// for volume
csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME,
//ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME,
csi.ControllerServiceCapability_RPC_LIST_VOLUMES,
csi.ControllerServiceCapability_RPC_GET_VOLUME,
}
for _, c := range caps {
s.supportControllerCapability = append(s.supportControllerCapability, newControllerServiceCapability(c))
}
}
func newControllerServiceCapability(cap csi.ControllerServiceCapability_RPC_Type) *csi.ControllerServiceCapability {
return &csi.ControllerServiceCapability{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: cap,
},
},
}
}