-
Notifications
You must be signed in to change notification settings - Fork 685
Expand file tree
/
Copy pathdriver.go
More file actions
370 lines (295 loc) · 9.96 KB
/
Copy pathdriver.go
File metadata and controls
370 lines (295 loc) · 9.96 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package manila
import (
"context"
"fmt"
"net"
"os"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
v1 "k8s.io/client-go/listers/core/v1"
"k8s.io/cloud-provider-openstack/pkg/csi/manila/csiclient"
"k8s.io/cloud-provider-openstack/pkg/csi/manila/manilaclient"
"k8s.io/cloud-provider-openstack/pkg/util/metadata"
"k8s.io/cloud-provider-openstack/pkg/version"
"k8s.io/klog/v2"
)
type Driver struct {
name string
fqVersion string // Fully qualified version in format {driverVersion}@{CPO version}
shareProto string
clusterID string
withTopology bool
serverEndpoint string
fwdEndpoint string
ids *identityServer
cs *controllerServer
ns *nodeServer
vcaps []*csi.VolumeCapability_AccessMode
cscaps []*csi.ControllerServiceCapability
nscaps []*csi.NodeServiceCapability
manilaClientBuilder manilaclient.Builder
csiClientBuilder csiclient.Builder
pvcLister v1.PersistentVolumeClaimLister
}
type DriverOpts struct {
DriverName string
ShareProto string
ClusterID string
WithTopology bool
ServerCSIEndpoint string
FwdCSIEndpoint string
ManilaClientBuilder manilaclient.Builder
CSIClientBuilder csiclient.Builder
PVCLister v1.PersistentVolumeClaimLister
}
type nonBlockingGRPCServer struct {
wg sync.WaitGroup
server *grpc.Server
}
const (
specVersion = "1.8.0"
driverVersion = "0.9.0"
topologyKey = "topology.manila.csi.openstack.org/zone"
)
var (
serverGRPCEndpointCallCounter uint64
)
func argNotEmpty(val, name string) error {
if val == "" {
return fmt.Errorf("%s is missing", name)
}
return nil
}
func NewDriver(o *DriverOpts) (*Driver, error) {
m := map[string]string{
"driver name": o.DriverName,
"driver endpoint": o.ServerCSIEndpoint,
"FWD endpoint": o.FwdCSIEndpoint,
"share protocol selector": o.ShareProto,
}
for k, v := range m {
if err := argNotEmpty(v, k); err != nil {
return nil, err
}
}
d := &Driver{
fqVersion: fmt.Sprintf("%s@%s", driverVersion, version.Version),
withTopology: o.WithTopology,
name: o.DriverName,
serverEndpoint: o.ServerCSIEndpoint,
fwdEndpoint: o.FwdCSIEndpoint,
shareProto: strings.ToUpper(o.ShareProto),
manilaClientBuilder: o.ManilaClientBuilder,
csiClientBuilder: o.CSIClientBuilder,
clusterID: o.ClusterID,
pvcLister: o.PVCLister,
}
klog.Info("Driver: ", d.name)
klog.Info("Driver version: ", d.fqVersion)
klog.Info("CSI spec version: ", specVersion)
klog.Infof("Topology awareness: %t", d.withTopology)
getShareAdapter(d.shareProto) // The program will terminate with a non-zero exit code if the share protocol selector is wrong
klog.Infof("Operating on %s shares", d.shareProto)
serverProto, serverAddr, err := parseGRPCEndpoint(o.ServerCSIEndpoint)
if err != nil {
return nil, fmt.Errorf("failed to parse server endpoint address %s: %v", o.ServerCSIEndpoint, err)
}
fwdProto, fwdAddr, err := parseGRPCEndpoint(o.FwdCSIEndpoint)
if err != nil {
return nil, fmt.Errorf("failed to parse proxy client address %s: %v", o.FwdCSIEndpoint, err)
}
d.serverEndpoint = endpointAddress(serverProto, serverAddr)
d.fwdEndpoint = endpointAddress(fwdProto, fwdAddr)
d.ids = &identityServer{d: d}
return d, nil
}
func (d *Driver) SetupControllerService() error {
klog.Info("Providing controller service")
d.addControllerServiceCapabilities([]csi.ControllerServiceCapability_RPC_Type{
csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME,
csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT,
csi.ControllerServiceCapability_RPC_EXPAND_VOLUME,
})
d.addVolumeCapabilityAccessModes([]csi.VolumeCapability_AccessMode_Mode{
csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER,
csi.VolumeCapability_AccessMode_MULTI_NODE_SINGLE_WRITER,
csi.VolumeCapability_AccessMode_MULTI_NODE_READER_ONLY,
csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
csi.VolumeCapability_AccessMode_SINGLE_NODE_READER_ONLY,
})
d.cs = &controllerServer{d: d}
return nil
}
func (d *Driver) SetupNodeService(metadata metadata.IMetadata) error {
klog.Info("Providing node service")
var supportsNodeStage bool
nodeCapsMap, err := d.initProxiedDriver()
if err != nil {
return fmt.Errorf("failed to initialize proxied CSI driver: %v", err)
}
nscaps := make([]csi.NodeServiceCapability_RPC_Type, 0, len(nodeCapsMap))
for c := range nodeCapsMap {
nscaps = append(nscaps, c)
if c == csi.NodeServiceCapability_RPC_STAGE_UNSTAGE_VOLUME {
supportsNodeStage = true
}
}
d.addNodeServiceCapabilities(nscaps)
d.ns = &nodeServer{
d: d,
metadata: metadata,
supportsNodeStage: supportsNodeStage,
nodeStageCache: make(map[volumeID]stageCacheEntry),
}
return nil
}
func (d *Driver) Run() {
if nil == d.cs && nil == d.ns {
klog.Fatal("No CSI services initialized")
}
s := nonBlockingGRPCServer{}
s.start(d.serverEndpoint, d.ids, d.cs, d.ns)
s.wait()
}
func (d *Driver) addControllerServiceCapabilities(cs []csi.ControllerServiceCapability_RPC_Type) {
caps := make([]*csi.ControllerServiceCapability, 0, len(cs))
for _, c := range cs {
klog.Infof("Enabling controller service capability: %v", c.String())
csc := &csi.ControllerServiceCapability{
Type: &csi.ControllerServiceCapability_Rpc{
Rpc: &csi.ControllerServiceCapability_RPC{
Type: c,
},
},
}
caps = append(caps, csc)
}
d.cscaps = caps
}
func (d *Driver) addVolumeCapabilityAccessModes(vs []csi.VolumeCapability_AccessMode_Mode) {
caps := make([]*csi.VolumeCapability_AccessMode, 0, len(vs))
for _, c := range vs {
klog.Infof("Enabling volume access mode: %v", c.String())
caps = append(caps, &csi.VolumeCapability_AccessMode{Mode: c})
}
d.vcaps = caps
}
func (d *Driver) addNodeServiceCapabilities(ns []csi.NodeServiceCapability_RPC_Type) {
caps := make([]*csi.NodeServiceCapability, 0, len(ns))
for _, c := range ns {
klog.Infof("Enabling node service capability: %v", c.String())
nsc := &csi.NodeServiceCapability{
Type: &csi.NodeServiceCapability_Rpc{
Rpc: &csi.NodeServiceCapability_RPC{
Type: c,
},
},
}
caps = append(caps, nsc)
}
d.nscaps = caps
}
func (d *Driver) initProxiedDriver() (csiNodeCapabilitySet, error) {
conn, err := d.csiClientBuilder.NewConnection(d.fwdEndpoint)
if err != nil {
return nil, fmt.Errorf("connecting to %s endpoint failed: %v", d.fwdEndpoint, err)
}
defer conn.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()
identityClient := d.csiClientBuilder.NewIdentityServiceClient(conn)
for {
if err = identityClient.ProbeForever(ctx, conn, time.Second*5); err == nil {
break
}
if status.Code(err) != codes.Unavailable {
return nil, fmt.Errorf("probe failed: %v", err)
}
klog.Warningf("proxied CSI driver probe returned Unavailable for %s, retrying: %v", d.fwdEndpoint, err)
select {
case <-ctx.Done():
case <-time.After(time.Second):
}
if ctx.Err() != nil {
return nil, fmt.Errorf("timed out probing proxied CSI driver %s: %v", d.fwdEndpoint, err)
}
}
pluginInfo, err := identityClient.GetPluginInfo(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get plugin info of the proxied driver: %v", err)
}
klog.Infof("proxying CSI driver %s version %s", pluginInfo.GetName(), pluginInfo.GetVendorVersion())
nodeCaps, err := csiNodeGetCapabilities(ctx, d.csiClientBuilder.NewNodeServiceClient(conn))
if err != nil {
return nil, fmt.Errorf("failed to get node capabilities: %v", err)
}
return nodeCaps, nil
}
func (s *nonBlockingGRPCServer) start(endpoint string, ids *identityServer, cs *controllerServer, ns *nodeServer) {
s.wg.Add(1)
go s.serve(endpoint, ids, cs, ns)
}
func (s *nonBlockingGRPCServer) wait() {
s.wg.Wait()
}
func (s *nonBlockingGRPCServer) serve(endpoint string, ids *identityServer, cs *controllerServer, ns *nodeServer) {
defer s.wg.Done()
proto, addr, err := parseGRPCEndpoint(endpoint)
if err != nil {
klog.Fatalf("couldn't parse GRPC server endpoint address %s: %v", endpoint, err)
}
if proto == "unix" {
if err = os.Remove(addr); err != nil && !os.IsNotExist(err) {
klog.Fatalf("failed to remove an existing socket file %s: %v", addr, err)
}
}
listener, err := net.Listen(proto, addr)
if err != nil {
klog.Fatalf("listen failed for GRPC server: %v", err)
}
server := grpc.NewServer(grpc.UnaryInterceptor(func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
callID := atomic.AddUint64(&serverGRPCEndpointCallCounter, 1)
klog.V(3).Infof("[ID:%d] GRPC call: %s", callID, info.FullMethod)
klog.V(5).Infof("[ID:%d] GRPC request: %s", callID, protosanitizer.StripSecrets(req))
resp, err := handler(ctx, req)
if err != nil {
klog.Errorf("[ID:%d] GRPC error: %v", callID, err)
} else {
klog.V(5).Infof("[ID:%d] GRPC response: %s", callID, protosanitizer.StripSecrets(resp))
}
return resp, err
}))
s.server = server
if ids != nil {
csi.RegisterIdentityServer(server, ids)
}
if cs != nil {
csi.RegisterControllerServer(server, cs)
}
if ns != nil {
csi.RegisterNodeServer(server, ns)
}
klog.Infof("listening for connections on %#v", listener.Addr())
if err := server.Serve(listener); err != nil {
klog.Fatalf("GRPC server failure: %v", err)
}
}