/
mock.go
288 lines (255 loc) · 7.72 KB
/
mock.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
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
package mock
import (
"fmt"
"strconv"
"strings"
snapv1 "github.com/kubernetes-incubator/external-storage/snapshot/pkg/apis/crd/v1"
snapshotVolume "github.com/kubernetes-incubator/external-storage/snapshot/pkg/volume"
storkvolume "github.com/libopenstorage/stork/drivers/volume"
"github.com/libopenstorage/stork/pkg/errors"
"github.com/pborman/uuid"
"github.com/portworx/sched-ops/k8s/core"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
k8shelper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
)
const (
driverName = "MockDriver"
// mockStorageClassName is the storage class for mock driver PVCs
mockStorageClassName = "mockDriverStorageClass"
provisionerName = "kubernetes.io/mock-volume"
clusterID = "MockClusterID"
// RackLabel Label used for the mock driver to set rack information
RackLabel = "mock/rack"
// ZoneLabel Label used for the mock driver to set zone information
ZoneLabel = "mock/zone"
// RegionLabel Label used for the mock driver to set region information
RegionLabel = "mock/region"
)
// Driver Mock driver for tests
type Driver struct {
storkvolume.ClusterPairNotSupported
storkvolume.MigrationNotSupported
storkvolume.GroupSnapshotNotSupported
storkvolume.ClusterDomainsNotSupported
storkvolume.BackupRestoreNotSupported
storkvolume.CloneNotSupported
storkvolume.SnapshotRestoreNotSupported
nodes []*storkvolume.NodeInfo
volumes map[string]*storkvolume.Info
pvcs map[string]*v1.PersistentVolumeClaim
interfaceError error
clusterID string
}
// String Returns the name for the driver
func (m Driver) String() string {
return driverName
}
// Init Initialize the mock driver
func (m Driver) Init(_ interface{}) error {
return nil
}
// Stop Stops the mock driver
func (m Driver) Stop() error {
return nil
}
// CreateCluster Creates a cluster with specified number of nodes
func (m *Driver) CreateCluster(numNodes int, nodes *v1.NodeList) error {
if len(m.nodes) > 0 {
m.nodes = m.nodes[:0]
}
for i := 0; i < numNodes; i++ {
node := &storkvolume.NodeInfo{
StorageID: "node" + strconv.Itoa(i+1),
Hostname: "node" + strconv.Itoa(i+1),
SchedulerID: "node" + strconv.Itoa(i+1),
Status: storkvolume.NodeOnline,
}
node.IPs = append(node.IPs, "192.168.0."+strconv.Itoa(i+1))
for _, n := range nodes.Items {
found := false
if node.StorageID == n.Name {
found = true
} else {
for _, address := range n.Status.Addresses {
if address.Type == v1.NodeHostName {
if address.Address == node.Hostname ||
strings.HasPrefix(address.Address, node.Hostname+".") {
found = true
break
}
} else if address.Type == v1.NodeInternalIP && address.Address == node.IPs[0] {
found = true
break
}
}
}
if found {
node.Rack = n.Labels[RackLabel]
node.Zone = n.Labels[ZoneLabel]
node.Region = n.Labels[RegionLabel]
}
}
m.nodes = append(m.nodes, node)
}
m.volumes = make(map[string]*storkvolume.Info)
m.pvcs = make(map[string]*v1.PersistentVolumeClaim)
m.interfaceError = nil
m.clusterID = "stork-test-" + uuid.New()
return nil
}
// GetStorageClassName Returns the storageclass name to be used by tests
func (m *Driver) GetStorageClassName() string {
return mockStorageClassName
}
// GetClusterID returns the clusterID for the driver
func (m *Driver) GetClusterID() (string, error) {
return m.clusterID, nil
}
// NewPVC Create a new PVC reference
func (m *Driver) NewPVC(volumeName string) *v1.PersistentVolumeClaim {
pvc := &v1.PersistentVolumeClaim{}
pvc.Name = volumeName
pvc.Spec.VolumeName = volumeName
storageClassName := m.GetStorageClassName()
pvc.Spec.StorageClassName = &storageClassName
m.pvcs[volumeName] = pvc
return pvc
}
// ProvisionVolume Provision a volume in the mock driver
func (m *Driver) ProvisionVolume(
volumeName string,
replicaIndexes []int,
size uint64,
labels map[string]string,
) error {
if _, ok := m.volumes[volumeName]; ok {
return fmt.Errorf("volume %v already exists", volumeName)
}
volume := &storkvolume.Info{
VolumeID: volumeName,
VolumeName: volumeName,
Size: size,
Labels: labels,
}
for i := 0; i < len(replicaIndexes); i++ {
nodeIndex := replicaIndexes[i]
if len(m.nodes) <= nodeIndex {
return fmt.Errorf("node not found")
}
volume.DataNodes = append(volume.DataNodes, m.nodes[nodeIndex].StorageID)
}
m.volumes[volumeName] = volume
return nil
}
// UpdateNodeStatus Update status for a node
func (m *Driver) UpdateNodeStatus(
nodeIndex int,
nodeStatus storkvolume.NodeStatus,
) error {
if len(m.nodes) <= nodeIndex {
return fmt.Errorf("node %v not found", nodeIndex)
}
m.nodes[nodeIndex].Status = nodeStatus
return nil
}
// UpdateNodeIP Update IP for a node
func (m *Driver) UpdateNodeIP(
nodeIndex int,
ip string,
) error {
if len(m.nodes) <= nodeIndex {
return fmt.Errorf("node %v not found", nodeIndex)
}
m.nodes[nodeIndex].IPs[0] = ip
return nil
}
// SetInterfaceError to the specified error. Used for negative testing
func (m *Driver) SetInterfaceError(err error) {
m.interfaceError = err
}
// InspectVolume Return information for a given volume
func (m Driver) InspectVolume(volumeID string) (*storkvolume.Info, error) {
if m.interfaceError != nil {
return nil, m.interfaceError
}
volume, ok := m.volumes[volumeID]
if !ok {
return nil, &errors.ErrNotFound{
ID: volumeID,
Type: "volume",
}
}
return volume, nil
}
// GetNodes Get info about the nodes where the driver is running
func (m Driver) GetNodes() ([]*storkvolume.NodeInfo, error) {
if m.interfaceError != nil {
return nil, m.interfaceError
}
return m.nodes, nil
}
// InspectNode using ID
func (m Driver) InspectNode(id string) (*storkvolume.NodeInfo, error) {
return nil, &errors.ErrNotSupported{}
}
// GetPodVolumes Get the Volumes in the Pod that use the mock driver
func (m Driver) GetPodVolumes(podSpec *v1.PodSpec, namespace string) ([]*storkvolume.Info, error) {
if m.interfaceError != nil {
return nil, m.interfaceError
}
var volumes []*storkvolume.Info
for _, volume := range podSpec.Volumes {
if volume.PersistentVolumeClaim != nil {
pvc, ok := m.pvcs[volume.PersistentVolumeClaim.ClaimName]
if !ok {
logrus.Debugf("PVCs: %+v", m.pvcs)
return nil, &errors.ErrNotFound{
ID: volume.PersistentVolumeClaim.ClaimName,
Type: "PVC",
}
}
storageClassName := k8shelper.GetPersistentVolumeClaimClass(pvc)
if storageClassName == "" {
logrus.Debugf("Empty StorageClass in PVC %v , ignoring", pvc.Name)
continue
}
// Assume all mock volumes have the same storageclass
if storageClassName != mockStorageClassName {
continue
}
volumeInfo, err := m.InspectVolume(pvc.Spec.VolumeName)
if err != nil {
return nil, err
}
volumes = append(volumes, volumeInfo)
}
}
return volumes, nil
}
// OwnsPVC returns true because it owns all PVCs created by tests
func (m *Driver) OwnsPVC(coreOps core.Ops, pvc *v1.PersistentVolumeClaim) bool {
return true
}
// OwnsPV returns true because it owns all PVC created by tests
func (m *Driver) OwnsPV(pv *v1.PersistentVolume) bool {
return true
}
// GetSnapshotPlugin Returns nil since snapshot is not supported in the mock driver
func (m *Driver) GetSnapshotPlugin() snapshotVolume.Plugin {
return nil
}
// GetVolumeClaimTemplates Not implemented for mock driver
func (m *Driver) GetVolumeClaimTemplates([]v1.PersistentVolumeClaim) (
[]v1.PersistentVolumeClaim, error) {
return nil, &errors.ErrNotImplemented{}
}
// GetSnapshotType Not implemented for mock driver
func (m *Driver) GetSnapshotType(snap *snapv1.VolumeSnapshot) (string, error) {
return "", &errors.ErrNotImplemented{}
}
func init() {
if err := storkvolume.Register(driverName, &Driver{}); err != nil {
logrus.Panicf("Error registering mock volume driver: %v", err)
}
}