@@ -37,7 +37,9 @@ import (
37
37
"k8s.io/kubernetes/pkg/features"
38
38
)
39
39
40
- var VersionConflictError = errors .New ("VersionError" )
40
+ // ErrVersionConflict is the error returned when resource version of requested
41
+ // object conflicts with the object in storage.
42
+ var ErrVersionConflict = errors .New ("VersionError" )
41
43
42
44
// VolumeReactor is a core.Reactor that simulates etcd and API server. It
43
45
// stores:
@@ -157,7 +159,7 @@ func (r *VolumeReactor) React(action core.Action) (handled bool, ret runtime.Obj
157
159
storedVer , _ := strconv .Atoi (storedVolume .ResourceVersion )
158
160
requestedVer , _ := strconv .Atoi (volume .ResourceVersion )
159
161
if storedVer != requestedVer {
160
- return true , obj , VersionConflictError
162
+ return true , obj , ErrVersionConflict
161
163
}
162
164
if reflect .DeepEqual (storedVolume , volume ) {
163
165
klog .V (4 ).Infof ("nothing updated volume %s" , volume .Name )
@@ -190,7 +192,7 @@ func (r *VolumeReactor) React(action core.Action) (handled bool, ret runtime.Obj
190
192
storedVer , _ := strconv .Atoi (storedClaim .ResourceVersion )
191
193
requestedVer , _ := strconv .Atoi (claim .ResourceVersion )
192
194
if storedVer != requestedVer {
193
- return true , obj , VersionConflictError
195
+ return true , obj , ErrVersionConflict
194
196
}
195
197
if reflect .DeepEqual (storedClaim , claim ) {
196
198
klog .V (4 ).Infof ("nothing updated claim %s" , claim .Name )
@@ -219,21 +221,19 @@ func (r *VolumeReactor) React(action core.Action) (handled bool, ret runtime.Obj
219
221
if found {
220
222
klog .V (4 ).Infof ("GetVolume: found %s" , volume .Name )
221
223
return true , volume .DeepCopy (), nil
222
- } else {
223
- klog .V (4 ).Infof ("GetVolume: volume %s not found" , name )
224
- return true , nil , fmt .Errorf ("Cannot find volume %s" , name )
225
224
}
225
+ klog .V (4 ).Infof ("GetVolume: volume %s not found" , name )
226
+ return true , nil , fmt .Errorf ("Cannot find volume %s" , name )
226
227
227
228
case action .Matches ("get" , "persistentvolumeclaims" ):
228
229
name := action .(core.GetAction ).GetName ()
229
230
claim , found := r .claims [name ]
230
231
if found {
231
232
klog .V (4 ).Infof ("GetClaim: found %s" , claim .Name )
232
233
return true , claim .DeepCopy (), nil
233
- } else {
234
- klog .V (4 ).Infof ("GetClaim: claim %s not found" , name )
235
- return true , nil , apierrs .NewNotFound (action .GetResource ().GroupResource (), name )
236
234
}
235
+ klog .V (4 ).Infof ("GetClaim: claim %s not found" , name )
236
+ return true , nil , apierrs .NewNotFound (action .GetResource ().GroupResource (), name )
237
237
238
238
case action .Matches ("delete" , "persistentvolumes" ):
239
239
name := action .(core.DeleteAction ).GetName ()
@@ -246,9 +246,8 @@ func (r *VolumeReactor) React(action core.Action) (handled bool, ret runtime.Obj
246
246
}
247
247
r .changedSinceLastSync ++
248
248
return true , nil , nil
249
- } else {
250
- return true , nil , fmt .Errorf ("Cannot delete volume %s: not found" , name )
251
249
}
250
+ return true , nil , fmt .Errorf ("Cannot delete volume %s: not found" , name )
252
251
253
252
case action .Matches ("delete" , "persistentvolumeclaims" ):
254
253
name := action .(core.DeleteAction ).GetName ()
@@ -261,9 +260,8 @@ func (r *VolumeReactor) React(action core.Action) (handled bool, ret runtime.Obj
261
260
}
262
261
r .changedSinceLastSync ++
263
262
return true , nil , nil
264
- } else {
265
- return true , nil , fmt .Errorf ("Cannot delete claim %s: not found" , name )
266
263
}
264
+ return true , nil , fmt .Errorf ("Cannot delete claim %s: not found" , name )
267
265
}
268
266
269
267
return false , nil , nil
@@ -299,12 +297,6 @@ func (r *VolumeReactor) getWatches(gvr schema.GroupVersionResource, ns string) [
299
297
return watches
300
298
}
301
299
302
- func (r * VolumeReactor ) ChangedSinceLastSync () int {
303
- r .lock .RLock ()
304
- defer r .lock .RUnlock ()
305
- return r .changedSinceLastSync
306
- }
307
-
308
300
// injectReactError returns an error when the test requested given action to
309
301
// fail. nil is returned otherwise.
310
302
func (r * VolumeReactor ) injectReactError (action core.Action ) error {
@@ -435,6 +427,7 @@ func (r *VolumeReactor) SyncAll() {
435
427
r .changedSinceLastSync = 0
436
428
}
437
429
430
+ // GetChangeCount returns changes since last sync.
438
431
func (r * VolumeReactor ) GetChangeCount () int {
439
432
r .lock .Lock ()
440
433
defer r .lock .Unlock ()
@@ -515,6 +508,7 @@ func (r *VolumeReactor) AddClaimEvent(claim *v1.PersistentVolumeClaim) {
515
508
}
516
509
}
517
510
511
+ // AddClaims adds PVCs into VolumeReactor.
518
512
func (r * VolumeReactor ) AddClaims (claims []* v1.PersistentVolumeClaim ) {
519
513
r .lock .Lock ()
520
514
defer r .lock .Unlock ()
@@ -523,6 +517,7 @@ func (r *VolumeReactor) AddClaims(claims []*v1.PersistentVolumeClaim) {
523
517
}
524
518
}
525
519
520
+ // AddVolumes adds PVs into VolumeReactor.
526
521
func (r * VolumeReactor ) AddVolumes (volumes []* v1.PersistentVolume ) {
527
522
r .lock .Lock ()
528
523
defer r .lock .Unlock ()
@@ -531,24 +526,28 @@ func (r *VolumeReactor) AddVolumes(volumes []*v1.PersistentVolume) {
531
526
}
532
527
}
533
528
529
+ // AddClaim adds a PVC into VolumeReactor.
534
530
func (r * VolumeReactor ) AddClaim (claim * v1.PersistentVolumeClaim ) {
535
531
r .lock .Lock ()
536
532
defer r .lock .Unlock ()
537
533
r .claims [claim .Name ] = claim
538
534
}
539
535
536
+ // AddVolume adds a PV into VolumeReactor.
540
537
func (r * VolumeReactor ) AddVolume (volume * v1.PersistentVolume ) {
541
538
r .lock .Lock ()
542
539
defer r .lock .Unlock ()
543
540
r .volumes [volume .Name ] = volume
544
541
}
545
542
543
+ // DeleteVolume deletes a PV by name.
546
544
func (r * VolumeReactor ) DeleteVolume (name string ) {
547
545
r .lock .Lock ()
548
546
defer r .lock .Unlock ()
549
547
delete (r .volumes , name )
550
548
}
551
549
550
+ // AddClaimBoundToVolume adds a PVC and binds it to corresponding PV.
552
551
func (r * VolumeReactor ) AddClaimBoundToVolume (claim * v1.PersistentVolumeClaim ) {
553
552
r .lock .Lock ()
554
553
defer r .lock .Unlock ()
@@ -558,6 +557,7 @@ func (r *VolumeReactor) AddClaimBoundToVolume(claim *v1.PersistentVolumeClaim) {
558
557
}
559
558
}
560
559
560
+ // MarkVolumeAvaiable marks a PV available by name.
561
561
func (r * VolumeReactor ) MarkVolumeAvaiable (name string ) {
562
562
r .lock .Lock ()
563
563
defer r .lock .Unlock ()
@@ -568,6 +568,7 @@ func (r *VolumeReactor) MarkVolumeAvaiable(name string) {
568
568
}
569
569
}
570
570
571
+ // NewVolumeReactor creates a volume reactor.
571
572
func NewVolumeReactor (client * fake.Clientset , fakeVolumeWatch , fakeClaimWatch * watch.FakeWatcher , errors []ReactorError ) * VolumeReactor {
572
573
reactor := & VolumeReactor {
573
574
volumes : make (map [string ]* v1.PersistentVolume ),
0 commit comments