-
Notifications
You must be signed in to change notification settings - Fork 622
/
Copy pathdriver.go
208 lines (164 loc) · 5.83 KB
/
driver.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
/*
Copyright 2017 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 cinder
import (
"fmt"
"github.com/container-storage-interface/spec/lib/go/csi"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/client-go/listers/core/v1"
"k8s.io/cloud-provider-openstack/pkg/csi/cinder/openstack"
"k8s.io/cloud-provider-openstack/pkg/util/metadata"
"k8s.io/cloud-provider-openstack/pkg/util/mount"
"k8s.io/cloud-provider-openstack/pkg/version"
"k8s.io/klog/v2"
)
const (
driverName = "cinder.csi.openstack.org"
topologyKey = "topology." + driverName + "/zone"
// maxVolumesPerNode is the maximum number of volumes that can be attached to a node
maxVolumesPerNode = 256
// ResizeRequired parameter, if set to true, will trigger a resize on mount operation
ResizeRequired = driverName + "/resizeRequired"
)
var (
// CSI spec version
specVersion = "1.8.0"
// Driver version
// Version history:
// * 1.3.0: Up to version 1.3.0 driver version was the same as CSI spec version
// * 1.3.1: Bump for 1.21 release
// * 1.3.2: Allow --cloud-config to be given multiple times
// * 1.3.3: Bump for 1.22 release
// * 2.0.0: Bump for 1.23 release
Version = "2.0.0"
)
// Deprecated: use Driver instead
//
//revive:disable:exported
type CinderDriver = Driver
//revive:enable:exported
type Driver struct {
name string
fqVersion string //Fully qualified version in format {Version}@{CPO version}
endpoint string
clusterID string
withTopology bool
ids *identityServer
cs *controllerServer
ns *nodeServer
vcap []*csi.VolumeCapability_AccessMode
cscap []*csi.ControllerServiceCapability
nscap []*csi.NodeServiceCapability
pvcLister v1.PersistentVolumeClaimLister
}
type DriverOpts struct {
ClusterID string
Endpoint string
WithTopology bool
PVCLister v1.PersistentVolumeClaimLister
}
func NewDriver(o *DriverOpts) *Driver {
d := &Driver{
name: driverName,
fqVersion: fmt.Sprintf("%s@%s", Version, version.Version),
endpoint: o.Endpoint,
clusterID: o.ClusterID,
withTopology: o.WithTopology,
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)
d.AddControllerServiceCapabilities(
[]csi.ControllerServiceCapability_RPC_Type{
csi.ControllerServiceCapability_RPC_LIST_VOLUMES,
csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME,
csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME,
csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT,
csi.ControllerServiceCapability_RPC_LIST_SNAPSHOTS,
csi.ControllerServiceCapability_RPC_EXPAND_VOLUME,
csi.ControllerServiceCapability_RPC_CLONE_VOLUME,
csi.ControllerServiceCapability_RPC_LIST_VOLUMES_PUBLISHED_NODES,
csi.ControllerServiceCapability_RPC_GET_VOLUME,
})
d.AddVolumeCapabilityAccessModes(
[]csi.VolumeCapability_AccessMode_Mode{
csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
})
// ignoring error, because AddNodeServiceCapabilities is public
// and so potentially used somewhere else.
_ = d.AddNodeServiceCapabilities(
[]csi.NodeServiceCapability_RPC_Type{
csi.NodeServiceCapability_RPC_STAGE_UNSTAGE_VOLUME,
csi.NodeServiceCapability_RPC_EXPAND_VOLUME,
csi.NodeServiceCapability_RPC_GET_VOLUME_STATS,
})
d.ids = NewIdentityServer(d)
return d
}
func (d *Driver) AddControllerServiceCapabilities(cl []csi.ControllerServiceCapability_RPC_Type) {
csc := make([]*csi.ControllerServiceCapability, 0, len(cl))
for _, c := range cl {
klog.Infof("Enabling controller service capability: %v", c.String())
csc = append(csc, NewControllerServiceCapability(c))
}
d.cscap = csc
}
func (d *Driver) AddVolumeCapabilityAccessModes(vc []csi.VolumeCapability_AccessMode_Mode) []*csi.VolumeCapability_AccessMode {
vca := make([]*csi.VolumeCapability_AccessMode, 0, len(vc))
for _, c := range vc {
klog.Infof("Enabling volume access mode: %v", c.String())
vca = append(vca, NewVolumeCapabilityAccessMode(c))
}
d.vcap = vca
return vca
}
func (d *Driver) AddNodeServiceCapabilities(nl []csi.NodeServiceCapability_RPC_Type) error {
nsc := make([]*csi.NodeServiceCapability, 0, len(nl))
for _, n := range nl {
klog.Infof("Enabling node service capability: %v", n.String())
nsc = append(nsc, NewNodeServiceCapability(n))
}
d.nscap = nsc
return nil
}
func (d *Driver) ValidateControllerServiceRequest(c csi.ControllerServiceCapability_RPC_Type) error {
if c == csi.ControllerServiceCapability_RPC_UNKNOWN {
return nil
}
for _, cap := range d.cscap {
if c == cap.GetRpc().GetType() {
return nil
}
}
return status.Error(codes.InvalidArgument, c.String())
}
func (d *Driver) GetVolumeCapabilityAccessModes() []*csi.VolumeCapability_AccessMode {
return d.vcap
}
func (d *Driver) SetupControllerService(clouds map[string]openstack.IOpenStack) {
klog.Info("Providing controller service")
d.cs = NewControllerServer(d, clouds)
}
func (d *Driver) SetupNodeService(mount mount.IMount, metadata metadata.IMetadata, opts openstack.BlockStorageOpts, topologies map[string]string) {
klog.Info("Providing node service")
d.ns = NewNodeServer(d, mount, metadata, opts, topologies)
}
func (d *Driver) Run() {
if nil == d.cs && nil == d.ns {
klog.Fatal("No CSI services initialized")
}
RunServicesInitialized(d.endpoint, d.ids, d.cs, d.ns)
}