Skip to content

Commit 70c34a2

Browse files
authored
Merge pull request #173 from xing-yang/release-1.2
Cherry-pick: Verify PV/PVC binding and driver
2 parents 34fa9aa + d51c3c5 commit 70c34a2

File tree

4 files changed

+144
-35
lines changed

4 files changed

+144
-35
lines changed

pkg/controller/framework_test.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,15 @@ func newClaimArrayFinalizer(name, claimUID, capacity, boundToVolume string, phas
939939
}
940940

941941
// newVolume returns a new volume with given attributes
942-
func newVolume(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundToClaimName string, phase v1.PersistentVolumePhase, reclaimPolicy v1.PersistentVolumeReclaimPolicy, class string, annotations ...string) *v1.PersistentVolume {
942+
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 {
943+
inDriverName := mockDriverName
944+
if driver != "" {
945+
inDriverName = driver
946+
}
947+
inNamespace := testNamespace
948+
if namespace != "" {
949+
inNamespace = namespace
950+
}
943951
volume := v1.PersistentVolume{
944952
ObjectMeta: metav1.ObjectMeta{
945953
Name: name,
@@ -953,7 +961,7 @@ func newVolume(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundTo
953961
},
954962
PersistentVolumeSource: v1.PersistentVolumeSource{
955963
CSI: &v1.CSIPersistentVolumeSource{
956-
Driver: mockDriverName,
964+
Driver: inDriverName,
957965
VolumeHandle: volumeHandle,
958966
},
959967
},
@@ -971,7 +979,7 @@ func newVolume(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundTo
971979
Kind: "PersistentVolumeClaim",
972980
APIVersion: "v1",
973981
UID: types.UID(boundToClaimUID),
974-
Namespace: testNamespace,
982+
Namespace: inNamespace,
975983
Name: boundToClaimName,
976984
}
977985
}
@@ -981,9 +989,9 @@ func newVolume(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundTo
981989

982990
// newVolumeArray returns array with a single volume that would be returned by
983991
// newVolume() with the same parameters.
984-
func newVolumeArray(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundToClaimName string, phase v1.PersistentVolumePhase, reclaimPolicy v1.PersistentVolumeReclaimPolicy, class string) []*v1.PersistentVolume {
992+
func newVolumeArray(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundToClaimName string, phase v1.PersistentVolumePhase, reclaimPolicy v1.PersistentVolumeReclaimPolicy, class string, driver string, namespace string) []*v1.PersistentVolume {
985993
return []*v1.PersistentVolume{
986-
newVolume(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundToClaimName, phase, reclaimPolicy, class),
994+
newVolume(name, volumeUID, volumeHandle, capacity, boundToClaimUID, boundToClaimName, phase, reclaimPolicy, class, driver, namespace),
987995
}
988996
}
989997

pkg/controller/snapshot_controller.go

+29
Original file line numberDiff line numberDiff line change
@@ -875,11 +875,40 @@ func (ctrl *csiSnapshotController) getVolumeFromVolumeSnapshot(snapshot *crdv1.V
875875
return nil, fmt.Errorf("failed to retrieve PV %s from the API server: %q", pvName, err)
876876
}
877877

878+
// Verify binding between PV/PVC is still valid
879+
bound := ctrl.IsVolumeBoundToClaim(pv, pvc)
880+
if bound == false {
881+
klog.Warningf("binding between PV %s and PVC %s is broken", pvName, pvc.Name)
882+
return nil, fmt.Errorf("claim in dataSource not bound or invalid")
883+
}
884+
885+
// Verify driver for PVC is the same as driver for VolumeSnapshot
886+
if pv.Spec.PersistentVolumeSource.CSI == nil || pv.Spec.PersistentVolumeSource.CSI.Driver != ctrl.snapshotterName {
887+
klog.Warningf("driver for PV %s is different from driver %s for snapshot %s", pvName, ctrl.snapshotterName, snapshot.Name)
888+
return nil, fmt.Errorf("claim in dataSource not bound or invalid")
889+
}
890+
878891
klog.V(5).Infof("getVolumeFromVolumeSnapshot: snapshot [%s] PV name [%s]", snapshot.Name, pvName)
879892

880893
return pv, nil
881894
}
882895

896+
// IsVolumeBoundToClaim returns true, if given volume is pre-bound or bound
897+
// to specific claim. Both claim.Name and claim.Namespace must be equal.
898+
// If claim.UID is present in volume.Spec.ClaimRef, it must be equal too.
899+
func (ctrl *csiSnapshotController) IsVolumeBoundToClaim(volume *v1.PersistentVolume, claim *v1.PersistentVolumeClaim) bool {
900+
if volume.Spec.ClaimRef == nil {
901+
return false
902+
}
903+
if claim.Name != volume.Spec.ClaimRef.Name || claim.Namespace != volume.Spec.ClaimRef.Namespace {
904+
return false
905+
}
906+
if volume.Spec.ClaimRef.UID != "" && claim.UID != volume.Spec.ClaimRef.UID {
907+
return false
908+
}
909+
return true
910+
}
911+
883912
func (ctrl *csiSnapshotController) getStorageClassFromVolumeSnapshot(snapshot *crdv1.VolumeSnapshot) (*storagev1.StorageClass, error) {
884913
// Get storage class from PVC or PV
885914
pvc, err := ctrl.getClaimFromVolumeSnapshot(snapshot)

0 commit comments

Comments
 (0)