Skip to content

Commit 26c3eff

Browse files
authored
Merge pull request #172 from xing-yang/binding_bug
Verify PV/PVC binding and driver
2 parents 1a2decd + 712a334 commit 26c3eff

File tree

4 files changed

+133
-36
lines changed

4 files changed

+133
-36
lines changed

pkg/controller/framework_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,15 @@ func newClaimArrayFinalizer(name, claimUID, capacity, boundToVolume string, phas
940940
}
941941

942942
// newVolume returns a new volume with given attributes
943-
func newVolume(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundToClaimName string, phase v1.PersistentVolumePhase, reclaimPolicy v1.PersistentVolumeReclaimPolicy, class string, annotations ...string) *v1.PersistentVolume {
943+
func newVolume(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundToClaimName string, phase v1.PersistentVolumePhase, reclaimPolicy v1.PersistentVolumeReclaimPolicy, class string, driver string, namespace string, annotations ...string) *v1.PersistentVolume {
944+
inDriverName := mockDriverName
945+
if driver != "" {
946+
inDriverName = driver
947+
}
948+
inNamespace := testNamespace
949+
if namespace != "" {
950+
inNamespace = namespace
951+
}
944952
volume := v1.PersistentVolume{
945953
ObjectMeta: metav1.ObjectMeta{
946954
Name: name,
@@ -954,7 +962,7 @@ func newVolume(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundTo
954962
},
955963
PersistentVolumeSource: v1.PersistentVolumeSource{
956964
CSI: &v1.CSIPersistentVolumeSource{
957-
Driver: mockDriverName,
965+
Driver: inDriverName,
958966
VolumeHandle: volumeHandle,
959967
},
960968
},
@@ -972,7 +980,7 @@ func newVolume(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundTo
972980
Kind: "PersistentVolumeClaim",
973981
APIVersion: "v1",
974982
UID: types.UID(boundToClaimUID),
975-
Namespace: testNamespace,
983+
Namespace: inNamespace,
976984
Name: boundToClaimName,
977985
}
978986
}
@@ -982,9 +990,9 @@ func newVolume(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundTo
982990

983991
// newVolumeArray returns array with a single volume that would be returned by
984992
// newVolume() with the same parameters.
985-
func newVolumeArray(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundToClaimName string, phase v1.PersistentVolumePhase, reclaimPolicy v1.PersistentVolumeReclaimPolicy, class string) []*v1.PersistentVolume {
993+
func newVolumeArray(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundToClaimName string, phase v1.PersistentVolumePhase, reclaimPolicy v1.PersistentVolumeReclaimPolicy, class string, driver string, namespace string) []*v1.PersistentVolume {
986994
return []*v1.PersistentVolume{
987-
newVolume(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundToClaimName, phase, reclaimPolicy, class),
995+
newVolume(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundToClaimName, phase, reclaimPolicy, class, driver, namespace),
988996
}
989997
}
990998

pkg/controller/snapshot_controller.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,11 +897,40 @@ func (ctrl *csiSnapshotController) getVolumeFromVolumeSnapshot(snapshot *crdv1.V
897897
return nil, fmt.Errorf("failed to retrieve PV %s from the API server: %q", pvName, err)
898898
}
899899

900+
// Verify binding between PV/PVC is still valid
901+
bound := ctrl.IsVolumeBoundToClaim(pv, pvc)
902+
if bound == false {
903+
klog.Warningf("binding between PV %s and PVC %s is broken", pvName, pvc.Name)
904+
return nil, fmt.Errorf("claim in dataSource not bound or invalid")
905+
}
906+
907+
// Verify driver for PVC is the same as driver for VolumeSnapshot
908+
if pv.Spec.PersistentVolumeSource.CSI == nil || pv.Spec.PersistentVolumeSource.CSI.Driver != ctrl.snapshotterName {
909+
klog.Warningf("driver for PV %s is different from driver %s for snapshot %s", pvName, ctrl.snapshotterName, snapshot.Name)
910+
return nil, fmt.Errorf("claim in dataSource not bound or invalid")
911+
}
912+
900913
klog.V(5).Infof("getVolumeFromVolumeSnapshot: snapshot [%s] PV name [%s]", snapshot.Name, pvName)
901914

902915
return pv, nil
903916
}
904917

918+
// IsVolumeBoundToClaim returns true, if given volume is pre-bound or bound
919+
// to specific claim. Both claim.Name and claim.Namespace must be equal.
920+
// If claim.UID is present in volume.Spec.ClaimRef, it must be equal too.
921+
func (ctrl *csiSnapshotController) IsVolumeBoundToClaim(volume *v1.PersistentVolume, claim *v1.PersistentVolumeClaim) bool {
922+
if volume.Spec.ClaimRef == nil {
923+
return false
924+
}
925+
if claim.Name != volume.Spec.ClaimRef.Name || claim.Namespace != volume.Spec.ClaimRef.Namespace {
926+
return false
927+
}
928+
if volume.Spec.ClaimRef.UID != "" && claim.UID != volume.Spec.ClaimRef.UID {
929+
return false
930+
}
931+
return true
932+
}
933+
905934
func (ctrl *csiSnapshotController) getStorageClassFromVolumeSnapshot(snapshot *crdv1.VolumeSnapshot) (*storagev1.StorageClass, error) {
906935
// Get storage class from PVC or PV
907936
pvc, err := ctrl.getClaimFromVolumeSnapshot(snapshot)

0 commit comments

Comments
 (0)